Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""" Testing module for the Sharepoint API data""" 

2 

3import json 

4import logging 

5import unittest 

6from unittest import mock 

7 

8from sdc_etl_libs.api_helpers.apis.Sharepoint.sharepoint import Sharepoint 

9 

10logging.info("EXECUTING: %s", __name__) 

11 

12get_credentials_mock_obj = mock.MagicMock( 

13 return_value={ 

14 "app_id": "MOCKED_APP_ID", 

15 "secret": "MOCKED_SECRET", 

16 "advertiser_id": "MOCKED_ADVERTISER", 

17 "access_token": "MOCKED_ACCESS_TOKEN", 

18 "day_": "DAY", 

19 "client_id": "CLIENT_ID", 

20 "client_secret": "CLIENT_SECRET" 

21 }) 

22 

23endpoint_schema_ = { 

24 "type": "api", 

25 "tag": "main_source", 

26 "info": { 

27 "access": { 

28 "base_url": "BASE_URL", 

29 "token_url": "TOKEN_URL_", 

30 "api_name": "API_NAME_", 

31 "scope": ["SCOPE_"], 

32 "endpoint_name": "ENDPOINT_NAME_", 

33 "secrets": { 

34 "type": "aws_secrets", 

35 "opts": { 

36 "name": "TikTok/API" 

37 } 

38 } 

39 }, 

40 "file_info": { 

41 "type": "sharepoint_list", 

42 "opts": { 

43 "format": "binary", 

44 "headers": True, 

45 "file_name": "Contact Center Roster" 

46 } 

47 }, 

48 "opts": { 

49 "datetime_format": None, 

50 "tz": None, 

51 "api_call_details": { 

52 "method": "get" 

53 } 

54 } 

55 } 

56} 

57 

58input_values = {'day_': '1970-01-01'} 

59 

60 

61def mocked_requests_post(*args, **kwargs): 

62 

63 class MockResponse: 

64 

65 def __init__(self, text_, status_code_): 

66 self.text = json.dumps(text_) 

67 self.status_code = status_code_ 

68 

69 return MockResponse({"access_token": "TOKEN_"}, 200) 

70 

71 

72def mocked_requests_get(url_, **kwargs): 

73 

74 class MockResponse: 

75 

76 def __init__(self, text_message_, status_code_): 

77 self.text_message = text_message_ 

78 self.status_code = status_code_ 

79 

80 def json(self): 

81 return self.text_message 

82 

83 text_message = kwargs['content'] if 'content' in kwargs else {"id": "ID1,ID2"} 

84 return MockResponse(text_message, 200) 

85 

86 

87class TestClass(unittest.TestCase): 

88 

89 @mock.patch("sdc_etl_libs.api_helpers.API.API.get_credentials", get_credentials_mock_obj) 

90 @mock.patch("requests.post", mocked_requests_post) 

91 @mock.patch("requests.get", mocked_requests_get) 

92 def test_sharepoint_init(self): 

93 """ Test instantiating the class """ 

94 sharepoint_api = Sharepoint(schema_={'schema': 'SCHEMA'}, endpoint_schema_=endpoint_schema_, **input_values) 

95 

96 assert sharepoint_api.schema == {'schema': 'SCHEMA'} 

97 assert sharepoint_api.file_name == 'Contact Center Roster' 

98 assert sharepoint_api.file_type == 'sharepoint_list' 

99 

100 @mock.patch("sdc_etl_libs.api_helpers.API.API.get_credentials", get_credentials_mock_obj) 

101 @mock.patch("requests.post", mocked_requests_post) 

102 @mock.patch("requests.get") 

103 def test_lists_pagination_one_page(self, mock_get): 

104 mock_get.side_effect = [ 

105 mocked_requests_get("url", content={"id": "ID_,ID2_"}), 

106 mocked_requests_get("url", content={"value": [{ 

107 "id": 1, 

108 "name": "Contact Center Roster" 

109 }]}), 

110 mocked_requests_get( 

111 "url", 

112 content={"columns": [{ 

113 "name": "COLUMN_NAME", 

114 "hidden": False, 

115 "readOnly": False, 

116 "columnGroup": "A", 

117 }]}), 

118 mocked_requests_get("url", content={"value": [{ 

119 "fields": { 

120 "COLUMN_NAME": "RECORD_VALUE" 

121 } 

122 }]}) 

123 ] 

124 schema = {'fields': [{"name": "COLUMN_NAME", "type": {"type": "string"}}]} 

125 sharepoint_api = Sharepoint(schema_=schema, endpoint_schema_=endpoint_schema_, **input_values) 

126 result = sharepoint_api.get_response_data() 

127 assert result.shape == (1, 1) 

128 assert len(result.df.columns.values) == 1 

129 assert result.df.columns.values[0] == 'COLUMN_NAME' 

130 assert result.df['COLUMN_NAME'].iloc[0] == 'RECORD_VALUE' 

131 

132 @mock.patch("sdc_etl_libs.api_helpers.API.API.get_credentials", get_credentials_mock_obj) 

133 @mock.patch("requests.post", mocked_requests_post) 

134 @mock.patch("requests.get") 

135 def test_lists_pagination_two_pages(self, mock_get): 

136 mock_get.side_effect = [ 

137 mocked_requests_get("url", content={"id": "ID_,ID2_"}), 

138 mocked_requests_get("url", content={"value": [{ 

139 "id": 1, 

140 "name": "Contact Center Roster" 

141 }]}), 

142 mocked_requests_get( 

143 "url", 

144 content={"columns": [{ 

145 "name": "COLUMN_NAME", 

146 "hidden": False, 

147 "readOnly": False, 

148 "columnGroup": "A", 

149 }]}), 

150 mocked_requests_get( 

151 "url", 

152 content={ 

153 "value": [{ 

154 "fields": { 

155 "COLUMN_NAME": "RECORD_VALUE_1" 

156 } 

157 }], 

158 "@odata.nextLink": "next_page" 

159 }), 

160 mocked_requests_get("url", content={"value": [{ 

161 "fields": { 

162 "COLUMN_NAME": "RECORD_VALUE_2" 

163 } 

164 }]}) 

165 ] 

166 schema = {'fields': [{"name": "COLUMN_NAME", "type": {"type": "string"}}]} 

167 sharepoint_api = Sharepoint(schema_=schema, endpoint_schema_=endpoint_schema_, **input_values) 

168 result = sharepoint_api.get_response_data() 

169 assert result.shape == (2, 1) 

170 assert len(result.df.columns.values) == 1 

171 assert result.df.columns.values[0] == 'COLUMN_NAME' 

172 assert result.df['COLUMN_NAME'].iloc[0] == 'RECORD_VALUE_1' 

173 assert result.df['COLUMN_NAME'].iloc[1] == 'RECORD_VALUE_2'