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 LogentriesAPI, related to teledentistry data.""" 

2# pylint: skip-file 

3 

4import json 

5import logging 

6from unittest import TestCase, mock 

7 

8import pytest 

9from sdc_etl_libs.api_helpers.apis.Impact.ImpactAPI import Impact 

10from sdc_etl_libs.sdc_file_helpers.SDCFileHelpers import SDCFileHelpers 

11 

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

13 

14get_credentials_mock_obj = mock.MagicMock( 

15 return_value={ 

16 "account_id": "MOCKED_API_KEY", # pragma: allowlist secret 

17 "auth_token": "MOCKED_AUTH_TOKEN", # pragma: allowlist secret 

18 "base_url": "http://url/to/api/", 

19 }) 

20 

21endpoint_schema_ = { 

22 "type": "source", 

23 "tag": "main_source", 

24 "info": { 

25 "access": { 

26 "api_name": "impact", 

27 "base_url": "OVERWRITTEN_BASE_URL", 

28 "endpoint_name": "REPORT_ID" 

29 }, 

30 "opts": {} 

31 } 

32 } 

33 

34input_values = {'endpoint_schema_': endpoint_schema_, 'day_': '1970-01-01'} 

35 

36 

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

38def test_impact_init(): 

39 """ Test instantiating the class """ 

40 impact_api = Impact(schema_={'name': 'actions-by-partner'}, data_map_={}, **input_values) 

41 assert impact_api.credentials is not None 

42 assert impact_api.account_id == 'MOCKED_API_KEY' 

43 assert impact_api.auth_token == 'MOCKED_AUTH_TOKEN' 

44 assert impact_api.base_url == 'OVERWRITTEN_BASE_URL' 

45 assert impact_api.endpoint_name == 'REPORT_ID' 

46 assert impact_api.day == '1970-01-01' 

47 

48 

49def __build_response_text(payload): 

50 """ method that builds a mock object to serve as input""" 

51 obj = type('', (), {})() 

52 obj.json = lambda: json.loads(payload) 

53 return obj 

54 

55 

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

57@mock.patch("requests.request") 

58def test_by_day_report_success(mock_request): 

59 """ Test calling the public method fetching by_day report """ 

60 

61 by_day = input_values.copy() 

62 by_day['endpoint_schema_']['endpoint_name_'] = 'att_adv_performance_by_day_pm_only' 

63 

64 impact_api = Impact(schema_={'name': 'actions-by-day'}, **by_day) 

65 

66 response = '''{"Records": [{"date_display": "Jan 01, 1970", 

67 "media_count": "1", 

68 "Clicks": "2", 

69 "myactions__19377": "3", 

70 "myactions__19374": "4", 

71 "myactions__19376": "5", 

72 "myactions__19372": "6", 

73 "Revenue": "7.0", 

74 "TotalCost": "8.0" 

75 }]}''' 

76 

77 mock_request.side_effect = [__build_response_text(response)] 

78 

79 expected = [{ 

80 'DATE': 'Jan 01, 1970', 

81 'PARTNERS': '1', 

82 'CLICKS': '2', 

83 'ACTIONS_ALIGNER_PURCHASE': '3', 

84 'ACTIONS_INITIAL_LEAD': '4', 

85 'ACTIONS_ONLINE_SALE': '5', 

86 'ACTIONS_SMILE_ASSESSMENT': '6', 

87 'REVENUE': '7.0', 

88 'TOTAL_COST': '8.0' 

89 }] 

90 

91 ret = impact_api.get_response_data() 

92 assert ret == expected 

93 

94 

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

96@mock.patch("requests.request") 

97def test_by_partner_report_success(mock_request): 

98 """ Test calling the public method fetching by_partner report """ 

99 

100 by_partner = input_values.copy() 

101 by_partner['endpoint_schema_']['endpoint_name_'] = 'att_adv_performance_by_media_pm_only' 

102 

103 impact_api = Impact(schema_={'name': 'actions-by-partner'}, **by_partner) 

104 

105 response = '''{"Records": [{"Media": "1", 

106 "media_id": "2", 

107 "Clicks": "3", 

108 "myactions__19377": "4", 

109 "myactions__19374": "5", 

110 "myactions__19376": "6", 

111 "myactions__19372": "7", 

112 "Revenue": "8.0", 

113 "TotalCost": "9.0" 

114 }]}''' 

115 

116 mock_request.side_effect = [__build_response_text(response)] 

117 

118 expected = [{ 

119 'PARTNER': '1', 

120 'PARTNER_ID': '2', 

121 'CLICKS': '3', 

122 'ACTIONS_ALIGNER_PURCHASE': '4', 

123 'ACTIONS_INITIAL_LEAD': '5', 

124 'ACTIONS_ONLINE_SALE': '6', 

125 'ACTIONS_SMILE_ASSESSMENT': '7', 

126 'REVENUE': '8.0', 

127 'TOTAL_COST': '9.0', 

128 'DATE': '1970-01-01' 

129 }] 

130 

131 ret = impact_api.get_response_data() 

132 assert ret == expected 

133 

134 

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

136@mock.patch("requests.request") 

137def test_by_partner_report_exceptions(mock_request): 

138 """ Test calling the public method fetching first report """ 

139 

140 by_partner = input_values.copy() 

141 by_partner['endpoint_schema_']['endpoint_name_'] = 'att_adv_performance_by_media_pm_only' 

142 

143 impact_api = Impact(schema_={'name': 'actions-by-partner'}, **by_partner) 

144 

145 response = '''{"Records": [{"Media": "1", 

146 "media_id": "2", 

147 "Clicks": "3", 

148 "myactions__19377": "4", 

149 "myactions__19374": "5", 

150 "myactions__19376": "6", 

151 "myactions__1937X": "7", 

152 "Revenue": "8.0", 

153 "TotalCost": "9.0" 

154 }]}''' 

155 

156 mock_request.side_effect = [__build_response_text(response)] 

157 

158 with pytest.raises(KeyError, match=('myactions__19372')): 

159 impact_api.get_response_data()