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 TikTok API 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.TikTok.TikTokAPI import TikTok 

10 

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

12 

13get_credentials_mock_obj = mock.MagicMock( 

14 return_value={ 

15 "app_id": "MOCKED_APP_ID", # pragma: allowlist secret 

16 "secret": "MOCKED_SECRET", # pragma: allowlist secret 

17 "advertiser_id": "MOCKED_ADVERTISER", # pragma: allowlist secret 

18 "access_token": "MOCKED_ACCESS_TOKEN", # pragma: allowlist secret 

19 "day_": "DAY", 

20 }) 

21 

22endpoint_schema_ = { 

23 "type": "api", 

24 "tag": "main_source", 

25 "info": { 

26 "access": { 

27 "base_url": "OVERWRITTEN_BASE_URL", 

28 "endpoint_name": "ads_report", 

29 "credentials": { 

30 "type": "aws_secrets", 

31 "opts": { 

32 "name": "TikTok/API" 

33 } 

34 } 

35 } 

36 } 

37} 

38 

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

40 

41 

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

43def test_tiktok_init(): 

44 """ Test instantiating the class """ 

45 tiktok_api = TikTok(schema_={'name': 'actions_by_partner'}, data_map_={}, **input_values) 

46 assert tiktok_api.credentials is not None 

47 assert tiktok_api.app_id == 'MOCKED_APP_ID' 

48 assert tiktok_api.secret == 'MOCKED_SECRET' 

49 assert tiktok_api.advertiser_id == 'MOCKED_ADVERTISER' 

50 assert tiktok_api.access_token == 'MOCKED_ACCESS_TOKEN' 

51 assert tiktok_api.base_url == 'OVERWRITTEN_BASE_URL' 

52 assert tiktok_api.endpoint == 'ads_report' 

53 assert tiktok_api.day == '1970-01-01' 

54 

55 

56def __build_response_text(payload, status_code): 

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

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

59 obj.status_code = status_code 

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

61 return obj 

62 

63 

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

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

66def test_by_day_report_success(mock_request): 

67 """ Test calling the public method fetching ads_report """ 

68 

69 tiktok_api = TikTok(schema_={'name': 'actions_by_day'}, **input_values) 

70 

71 first_response = '''{"data": 

72 {"page_info": {"total_number": 2, "page": 1, "page_size": 1, "total_page": 2}, 

73 "list": [{"ad_id": 1, 

74 "ad_name": "ad_name_", 

75 "adgroup_id": 2, 

76 "adgroup_name": "adgroup_name_", 

77 "campaign_id": 3, 

78 "campaign_name": "campaign_name_" 

79 }]} 

80 }''' 

81 

82 second_response = '''{"data": 

83 {"page_info": {"total_number": 2, "page": 2, "page_size": 1, "total_page": 2}, 

84 "list": [{"ad_id": 11, 

85 "ad_name": "ad_name_", 

86 "adgroup_id": 22, 

87 "adgroup_name": "adgroup_name_", 

88 "campaign_id": 33, 

89 "campaign_name": "campaign_name_" 

90 }]} 

91 }''' 

92 third_response = '''{"data": 

93 {"list": [{"metrics": {"show_cnt": 111, 

94 "click_cnt": 222, 

95 "stat_cost": 333.3, 

96 "time_attr_convert_cnt": 444}, 

97 "dimensions": {"placement_id": "PLACEMENT_TIKTOK", 

98 "ad_id": 1}}, 

99 {"metrics": {"show_cnt": 1111, 

100 "click_cnt": 2222, 

101 "stat_cost": 3333.3, 

102 "time_attr_convert_cnt": 4444}, 

103 "dimensions": {"placement_id": "PLACEMENT_TIKTOK", 

104 "ad_id": 11}}]} 

105 }''' 

106 

107 mock_request.side_effect = [ 

108 __build_response_text(first_response, 200), 

109 __build_response_text(second_response, 200), 

110 __build_response_text(third_response, 200) 

111 ] 

112 

113 expected = [{ 

114 'AD_ID': 1, 

115 'AD_NAME': 'ad_name_', 

116 'ADGROUP_ID': 2, 

117 'ADGROUP_NAME': 'adgroup_name_', 

118 'CAMPAIGN_ID': 3, 

119 'CAMPAIGN_NAME': 'campaign_name_', 

120 'DATE': '1970-01-01', 

121 'STAT_COST': 333.3, 

122 'CLICK_CNT': 222, 

123 'SHOW_CNT': 111, 

124 'TIME_ATTR_CONVERT_CNT': 444 

125 }, { 

126 'AD_ID': 11, 

127 'AD_NAME': 'ad_name_', 

128 'ADGROUP_ID': 22, 

129 'ADGROUP_NAME': 'adgroup_name_', 

130 'CAMPAIGN_ID': 33, 

131 'CAMPAIGN_NAME': 'campaign_name_', 

132 'DATE': '1970-01-01', 

133 'STAT_COST': 3333.3, 

134 'CLICK_CNT': 2222, 

135 'SHOW_CNT': 1111, 

136 'TIME_ATTR_CONVERT_CNT': 4444 

137 }] 

138 

139 ret = tiktok_api.get_response_data() 

140 assert ret == expected 

141 

142 

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

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

145def test_by_day_report_fail(mock_request): 

146 """ Test calling the public method fetching ads_report """ 

147 

148 tiktok_api = TikTok(schema_={'name': 'actions_by_day'}, **input_values) 

149 

150 mock_request.side_effect = [__build_response_text('{}', 400)] 

151 

152 expected = [] 

153 

154 ret = tiktok_api.get_response_data() 

155 assert ret == expected