-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtest_json_decoder.py
199 lines (182 loc) · 6.74 KB
/
test_json_decoder.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
#
# Copyright (c) 2023 Airbyte, Inc., all rights reserved.
#
import gzip
import json
import os
import pytest
import requests
from airbyte_cdk import YamlDeclarativeSource
from airbyte_cdk.models import SyncMode
from airbyte_cdk.sources.declarative.decoders.json_decoder import JsonDecoder, JsonlDecoder
from airbyte_cdk.sources.declarative.models import DeclarativeStream as DeclarativeStreamModel
from airbyte_cdk.sources.declarative.parsers.model_to_component_factory import (
ModelToComponentFactory,
)
from airbyte_cdk.sources.declarative.decoders import GzipJsonDecoder
@pytest.mark.parametrize(
"response_body, first_element",
[
("", {}),
("[]", {}),
('{"healthcheck": {"status": "ok"}}', {"healthcheck": {"status": "ok"}}),
],
)
def test_json_decoder(requests_mock, response_body, first_element):
requests_mock.register_uri("GET", "https://airbyte.io/", text=response_body)
response = requests.get("https://airbyte.io/")
assert next(JsonDecoder(parameters={}).decode(response)) == first_element
@pytest.mark.parametrize(
"response_body, expected_json",
[
("", []),
('{"id": 1, "name": "test1"}', [{"id": 1, "name": "test1"}]),
(
'{"id": 1, "name": "test1"}\n{"id": 2, "name": "test2"}',
[{"id": 1, "name": "test1"}, {"id": 2, "name": "test2"}],
),
],
ids=["empty_response", "one_line_json", "multi_line_json"],
)
def test_jsonl_decoder(requests_mock, response_body, expected_json):
requests_mock.register_uri("GET", "https://airbyte.io/", text=response_body)
response = requests.get("https://airbyte.io/")
assert list(JsonlDecoder(parameters={}).decode(response)) == expected_json
@pytest.fixture(name="large_events_response")
def large_event_response_fixture():
jsonl_string = f"{json.dumps(data)}\n"
lines_in_response = 2_000_000 # ≈ 58 MB of response
dir_path = os.path.dirname(os.path.realpath(__file__))
file_path = f"{dir_path}/test_response.txt"
with open(file_path, "w") as file:
for _ in range(lines_in_response):
file.write(jsonl_string)
yield (lines_in_response, file_path)
os.remove(file_path)
@pytest.mark.slow
@pytest.mark.limit_memory("20 MB")
def test_jsonl_decoder_memory_usage(requests_mock, large_events_response):
lines_in_response, file_path = large_events_response
content = """
name: users
type: DeclarativeStream
retriever:
type: SimpleRetriever
decoder:
type: JsonlDecoder
paginator:
type: "NoPagination"
requester:
path: "users/{{ stream_slice.slice }}"
type: HttpRequester
url_base: "https://for-all-mankind.nasa.com/api/v1"
http_method: GET
authenticator:
type: NoAuth
request_headers: {}
request_body_json: {}
record_selector:
type: RecordSelector
extractor:
type: DpathExtractor
field_path: []
partition_router:
type: ListPartitionRouter
cursor_field: "slice"
values:
- users1
- users2
- users3
- users4
primary_key: []
"""
factory = ModelToComponentFactory()
stream_manifest = YamlDeclarativeSource._parse(content)
stream = factory.create_component(
model_type=DeclarativeStreamModel, component_definition=stream_manifest, config={}
)
def get_body():
return open(file_path, "rb", buffering=30)
counter = 0
requests_mock.get("https://for-all-mankind.nasa.com/api/v1/users/users1", body=get_body())
requests_mock.get("https://for-all-mankind.nasa.com/api/v1/users/users2", body=get_body())
requests_mock.get("https://for-all-mankind.nasa.com/api/v1/users/users3", body=get_body())
requests_mock.get("https://for-all-mankind.nasa.com/api/v1/users/users4", body=get_body())
stream_slices = list(stream.stream_slices(sync_mode=SyncMode.full_refresh))
for stream_slice in stream_slices:
for _ in stream.retriever.read_records(records_schema={}, stream_slice=stream_slice):
counter += 1
assert counter == lines_in_response * len(stream_slices)
@pytest.mark.parametrize(
"encoding",
[
"utf-8",
"utf",
],
ids=["utf-8", "utf"],
)
def test_gzipjson_decoder(requests_mock, encoding):
response_to_compress = json.dumps(
[
{
"campaignId": 214078428,
"campaignName": "sample-campaign-name-214078428",
"adGroupId": "6490134",
"adId": "665320125",
"targetId": "791320341",
"asin": "G000PSH142",
"advertisedAsin": "G000PSH142",
"keywordBid": "511234974",
"keywordId": "965783021",
},
{
"campaignId": 44504582,
"campaignName": "sample-campaign-name-44504582",
"adGroupId": "6490134",
"adId": "665320125",
"targetId": "791320341",
"asin": "G000PSH142",
"advertisedAsin": "G000PSH142",
"keywordBid": "511234974",
"keywordId": "965783021",
},
{
"campaignId": 509144838,
"campaignName": "sample-campaign-name-509144838",
"adGroupId": "6490134",
"adId": "665320125",
"targetId": "791320341",
"asin": "G000PSH142",
"advertisedAsin": "G000PSH142",
"keywordBid": "511234974",
"keywordId": "965783021",
},
{
"campaignId": 231712082,
"campaignName": "sample-campaign-name-231712082",
"adGroupId": "6490134",
"adId": "665320125",
"targetId": "791320341",
"asin": "G000PSH142",
"advertisedAsin": "G000PSH142",
"keywordBid": "511234974",
"keywordId": "965783021",
},
{
"campaignId": 895306040,
"campaignName": "sample-campaign-name-895306040",
"adGroupId": "6490134",
"adId": "665320125",
"targetId": "791320341",
"asin": "G000PSH142",
"advertisedAsin": "G000PSH142",
"keywordBid": "511234974",
"keywordId": "965783021",
},
]
)
body = gzip.compress(response_to_compress.encode(encoding))
requests_mock.register_uri("GET", "https://airbyte.io/", content=body)
response = requests.get("https://airbyte.io/")
assert len(list(GzipJsonDecoder(parameters={}, encoding=encoding).decode(response))) == 5