-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
test_streams.py
292 lines (251 loc) · 10.3 KB
/
test_streams.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#
# Copyright (c) 2023 Airbyte, Inc., all rights reserved.
#
import json
from http import HTTPStatus
from urllib.parse import parse_qs, urlparse
import pytest
import requests
import responses
from airbyte_cdk.models import SyncMode
from jsonschema import validate
from source_amazon_ads import SourceAmazonAds
def setup_responses(
profiles_response=None,
portfolios_response=None,
campaigns_response=None,
adgroups_response=None,
targeting_response=None,
product_ads_response=None,
generic_response=None,
):
responses.add(
responses.POST,
"https://api.amazon.com/auth/o2/token",
json={"access_token": "alala", "expires_in": 10},
)
if profiles_response:
responses.add(
responses.GET,
"https://advertising-api.amazon.com/v2/profiles",
body=profiles_response,
)
if portfolios_response:
responses.add(
responses.GET,
"https://advertising-api.amazon.com/v2/portfolios/extended",
body=portfolios_response,
)
if campaigns_response:
responses.add(
responses.GET,
"https://advertising-api.amazon.com/sd/campaigns",
body=campaigns_response,
)
if adgroups_response:
responses.add(
responses.GET,
"https://advertising-api.amazon.com/sd/adGroups",
body=adgroups_response,
)
if targeting_response:
responses.add(
responses.GET,
"https://advertising-api.amazon.com/sd/targets",
body=targeting_response,
)
if product_ads_response:
responses.add(
responses.GET,
"https://advertising-api.amazon.com/sd/productAds",
body=product_ads_response,
)
if generic_response:
responses.add(
responses.GET,
f"https://advertising-api.amazon.com/{generic_response}",
json=[],
)
def get_all_stream_records(stream, stream_slice=None):
records = stream.read_records(SyncMode.full_refresh, stream_slice=stream_slice)
return [r for r in records]
def get_stream_by_name(streams, stream_name):
for stream in streams:
if stream.name == stream_name:
return stream
raise Exception(f"Expected stream {stream_name} not found")
@responses.activate
def test_streams_profile(config, profiles_response):
setup_responses(profiles_response=profiles_response)
source = SourceAmazonAds()
streams = source.streams(config)
profile_stream = get_stream_by_name(streams, "profiles")
schema = profile_stream.get_json_schema()
records = get_all_stream_records(profile_stream)
assert len(responses.calls) == 2
assert len(profile_stream._profiles) == 4
assert len(records) == 4
expected_records = json.loads(profiles_response)
for record, expected_record in zip(records, expected_records):
validate(schema=schema, instance=record)
assert record == expected_record
@responses.activate
def test_streams_portfolios(config, profiles_response, portfolios_response):
setup_responses(profiles_response=profiles_response, portfolios_response=portfolios_response)
source = SourceAmazonAds()
streams = source.streams(config)
portfolio_stream = get_stream_by_name(streams, "portfolios")
schema = portfolio_stream.get_json_schema()
records = get_all_stream_records(portfolio_stream)
assert len(responses.calls) == 6
assert len(records) == 8
expected_records = json.loads(portfolios_response)
for record, expected_record in zip(records, expected_records):
validate(schema=schema, instance=record)
assert record == expected_record
@responses.activate
def test_streams_campaigns_4_vendors(config, profiles_response, campaigns_response):
profiles_response = json.loads(profiles_response)
for profile in profiles_response:
profile["accountInfo"]["type"] = "vendor"
profiles_response = json.dumps(profiles_response)
setup_responses(profiles_response=profiles_response, campaigns_response=campaigns_response)
source = SourceAmazonAds()
streams = source.streams(config)
profile_stream = get_stream_by_name(streams, "profiles")
campaigns_stream = get_stream_by_name(streams, "sponsored_display_campaigns")
profile_records = get_all_stream_records(profile_stream)
campaigns_records = get_all_stream_records(campaigns_stream)
assert len(campaigns_records) == len(profile_records) * len(json.loads(campaigns_response))
@pytest.mark.parametrize(
("page_size"),
[1, 2, 5, 1000000],
)
@responses.activate
def test_streams_campaigns_pagination(mocker, config, profiles_response, campaigns_response, page_size):
mocker.patch("source_amazon_ads.streams.common.SubProfilesStream.page_size", page_size)
profiles_response = json.loads(profiles_response)
for profile in profiles_response:
profile["accountInfo"]["type"] = "vendor"
profiles_response = json.dumps(profiles_response)
setup_responses(profiles_response=profiles_response)
source = SourceAmazonAds()
streams = source.streams(config)
profile_stream = get_stream_by_name(streams, "profiles")
campaigns_stream = get_stream_by_name(streams, "sponsored_display_campaigns")
campaigns = json.loads(campaigns_response)
def campaigns_paginated_response_cb(request):
query = urlparse(request.url).query
query = parse_qs(query)
start_index, count = (int(query.get(f, [0])[0]) for f in ["startIndex", "count"])
response_body = campaigns[start_index : start_index + count]
return (200, {}, json.dumps(response_body))
responses.add_callback(
responses.GET,
"https://advertising-api.amazon.com/sd/campaigns",
content_type="application/json",
callback=campaigns_paginated_response_cb,
)
profile_records = get_all_stream_records(profile_stream)
campaigns_records = get_all_stream_records(campaigns_stream)
assert len(campaigns_records) == len(profile_records) * len(json.loads(campaigns_response))
@pytest.mark.parametrize(("status_code"), [HTTPStatus.FORBIDDEN, HTTPStatus.UNAUTHORIZED])
@responses.activate
def test_streams_campaigns_pagination_403_error(mocker, status_code, config, profiles_response, campaigns_response):
setup_responses(profiles_response=profiles_response)
responses.add(
responses.GET,
"https://advertising-api.amazon.com/sd/campaigns",
json={"message": "msg"},
status=status_code,
)
source = SourceAmazonAds()
streams = source.streams(config)
campaigns_stream = get_stream_by_name(streams, "sponsored_display_campaigns")
with pytest.raises(requests.exceptions.HTTPError):
get_all_stream_records(campaigns_stream)
@responses.activate
def test_streams_campaigns_pagination_403_error_expected(mocker, config, profiles_response, campaigns_response):
setup_responses(profiles_response=profiles_response)
responses.add(
responses.GET,
"https://advertising-api.amazon.com/sd/campaigns",
json={"code": "403", "details": "details", "requestId": "xxx"},
status=403,
)
source = SourceAmazonAds()
streams = source.streams(config)
campaigns_stream = get_stream_by_name(streams, "sponsored_display_campaigns")
campaigns_records = get_all_stream_records(campaigns_stream)
assert campaigns_records == []
@pytest.mark.parametrize(
("stream_name", "endpoint"),
[
("sponsored_display_ad_groups", "sd/adGroups"),
("sponsored_display_product_ads", "sd/productAds"),
("sponsored_display_targetings", "sd/targets"),
],
)
@responses.activate
def test_streams_displays(
config,
stream_name,
endpoint,
profiles_response,
adgroups_response,
targeting_response,
product_ads_response,
):
setup_responses(
profiles_response=profiles_response,
adgroups_response=adgroups_response,
targeting_response=targeting_response,
product_ads_response=product_ads_response,
)
source = SourceAmazonAds()
streams = source.streams(config)
test_stream = get_stream_by_name(streams, stream_name)
records = get_all_stream_records(test_stream)
assert len(records) == 4
schema = test_stream.get_json_schema()
for r in records:
validate(schema=schema, instance=r)
assert any([endpoint in call.request.url for call in responses.calls])
@pytest.mark.parametrize(
("stream_name", "endpoint"),
[
("sponsored_brands_campaigns", "sb/campaigns"),
("sponsored_brands_ad_groups", "sb/adGroups"),
("sponsored_brands_keywords", "sb/keywords"),
("sponsored_product_campaigns", "v2/sp/campaigns"),
("sponsored_product_ad_groups", "v2/sp/adGroups"),
("sponsored_product_keywords", "v2/sp/keywords"),
("sponsored_product_negative_keywords", "v2/sp/negativeKeywords"),
("sponsored_product_ads", "v2/sp/productAds"),
("sponsored_product_targetings", "v2/sp/targets"),
],
)
@responses.activate
def test_streams_brands_and_products(config, stream_name, endpoint, profiles_response):
setup_responses(profiles_response=profiles_response, generic_response=endpoint)
source = SourceAmazonAds()
streams = source.streams(config)
test_stream = get_stream_by_name(streams, stream_name)
records = get_all_stream_records(test_stream)
assert records == []
assert any([endpoint in call.request.url for call in responses.calls])
@responses.activate
def test_sponsored_product_ad_group_bid_recommendations_404_error(caplog, config, profiles_response):
setup_responses(profiles_response=profiles_response)
responses.add(
responses.GET,
"https://advertising-api.amazon.com/v2/sp/adGroups/xxx/bidRecommendations",
json={"code": "404", "details": "404 Either the specified ad group identifier was not found or the specified ad group was found but no associated bid was found."},
status=404,
)
source = SourceAmazonAds()
streams = source.streams(config)
test_stream = get_stream_by_name(streams, "sponsored_product_ad_group_bid_recommendations")
records = get_all_stream_records(test_stream, stream_slice={'profileId': '1231', 'adGroupId': 'xxx'})
assert records == []
assert "Skip current AdGroup because the specified ad group has no associated bid" in caplog.text