-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
test_streams.py
241 lines (197 loc) · 8.68 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
#
# Copyright (c) 2023 Airbyte, Inc., all rights reserved.
#
import logging
import random
from http import HTTPStatus
from unittest.mock import MagicMock
import pytest
import requests
from airbyte_cdk.models import SyncMode
from source_notion.streams import Blocks, NotionStream, Pages, Users
@pytest.fixture
def patch_base_class(mocker):
# Mock abstract methods to enable instantiating abstract class
mocker.patch.object(NotionStream, "path", "v0/example_endpoint")
mocker.patch.object(NotionStream, "primary_key", "test_primary_key")
mocker.patch.object(NotionStream, "__abstractmethods__", set())
def test_request_params(patch_base_class):
stream = NotionStream(config=MagicMock())
inputs = {"stream_slice": None, "stream_state": None, "next_page_token": None}
expected_params = {}
assert stream.request_params(**inputs) == expected_params
def test_next_page_token(patch_base_class, requests_mock):
stream = NotionStream(config=MagicMock())
requests_mock.get("https://dummy", json={"next_cursor": "aaa"})
inputs = {"response": requests.get("https://dummy")}
expected_token = {"next_cursor": "aaa"}
assert stream.next_page_token(**inputs) == expected_token
@pytest.mark.parametrize(
"response_json, expected_output",
[({"next_cursor": "some_cursor", "has_more": True}, {"next_cursor": "some_cursor"}), ({"has_more": False}, None), ({}, None)],
)
def test_next_page_token_with_no_cursor(patch_base_class, response_json, expected_output):
stream = NotionStream(config=MagicMock())
mock_response = MagicMock()
mock_response.json.return_value = response_json
result = stream.next_page_token(mock_response)
assert result == expected_output
def test_parse_response(patch_base_class, requests_mock):
stream = NotionStream(config=MagicMock())
requests_mock.get("https://dummy", json={"results": [{"a": 123}, {"b": "xx"}]})
resp = requests.get("https://dummy")
inputs = {"response": resp, "stream_state": MagicMock()}
expected_parsed_object = [{"a": 123}, {"b": "xx"}]
assert list(stream.parse_response(**inputs)) == expected_parsed_object
def test_request_headers(patch_base_class):
stream = NotionStream(config=MagicMock())
inputs = {"stream_slice": None, "stream_state": None, "next_page_token": None}
expected_headers = {"Notion-Version": "2022-06-28"}
assert stream.request_headers(**inputs) == expected_headers
def test_http_method(patch_base_class):
stream = NotionStream(config=MagicMock())
expected_method = "GET"
assert stream.http_method == expected_method
@pytest.mark.parametrize(
("http_status", "should_retry"),
[
(HTTPStatus.OK, False),
(HTTPStatus.BAD_REQUEST, True),
(HTTPStatus.TOO_MANY_REQUESTS, True),
(HTTPStatus.INTERNAL_SERVER_ERROR, True),
],
)
def test_should_retry(patch_base_class, http_status, should_retry):
response_mock = MagicMock()
response_mock.status_code = http_status
stream = NotionStream(config=MagicMock())
assert stream.should_retry(response_mock) == should_retry
def test_should_not_retry_with_ai_block(requests_mock):
stream = Blocks(parent=None, config=MagicMock())
json_response = {
"object": "error",
"status": 400,
"code": "validation_error",
"message": "Block type ai_block is not supported via the API.",
}
requests_mock.get("https://api.notion.com/v1/blocks/123", json=json_response, status_code=400)
test_response = requests.get("https://api.notion.com/v1/blocks/123")
assert not stream.should_retry(test_response)
def test_should_not_retry_with_not_found_block(requests_mock):
stream = Blocks(parent=None, config=MagicMock())
json_response = {
"object": "error",
"status": 404,
"message": "Not Found for url: https://api.notion.com/v1/blocks/123/children?page_size=100",
}
requests_mock.get("https://api.notion.com/v1/blocks/123", json=json_response, status_code=404)
test_response = requests.get("https://api.notion.com/v1/blocks/123")
assert not stream.should_retry(test_response)
def test_empty_blocks_results(requests_mock):
stream = Blocks(parent=None, config=MagicMock())
requests_mock.get(
"https://api.notion.com/v1/blocks/aaa/children",
json={
"next_cursor": None,
},
)
stream.block_id_stack = ["aaa"]
assert list(stream.read_records(sync_mode=SyncMode.incremental, stream_slice=[])) == []
def test_backoff_time(patch_base_class):
response_mock = MagicMock(headers={"retry-after": "10"})
stream = NotionStream(config=MagicMock())
assert stream.backoff_time(response_mock) == 10.0
def test_users_request_params(patch_base_class):
stream = Users(config=MagicMock())
# No next_page_token. First pull
inputs = {"stream_slice": None, "stream_state": None, "next_page_token": None}
expected_params = {"page_size": 100}
assert stream.request_params(**inputs) == expected_params
# When getting pages after the first pull.
inputs = {"stream_slice": None, "stream_state": None, "next_page_token": {"next_cursor": "123"}}
expected_params = {"start_cursor": "123", "page_size": 100}
assert stream.request_params(**inputs) == expected_params
def test_user_stream_handles_pagination_correctly(requests_mock):
"""
Test shows that Users stream uses pagination as per Notion API docs.
"""
response_body = {
"object": "list",
"results": [{"id": f"{x}", "object": "user", "type": ["person", "bot"][random.randint(0, 1)]} for x in range(100)],
"next_cursor": "bc48234b-77b2-41a6-95a3-6a8abb7887d5",
"has_more": True,
"type": "user",
}
requests_mock.get("https://api.notion.com/v1/users?page_size=100", json=response_body)
response_body = {
"object": "list",
"results": [{"id": f"{x}", "object": "user", "type": ["person", "bot"][random.randint(0, 1)]} for x in range(100, 200)],
"next_cursor": "67030467-b97b-4729-8fd6-2fb33d012da4",
"has_more": True,
"type": "user",
}
requests_mock.get("https://api.notion.com/v1/users?page_size=100&start_cursor=bc48234b-77b2-41a6-95a3-6a8abb7887d5", json=response_body)
response_body = {
"object": "list",
"results": [{"id": f"{x}", "object": "user", "type": ["person", "bot"][random.randint(0, 1)]} for x in range(200, 220)],
"next_cursor": None,
"has_more": False,
"type": "user",
}
requests_mock.get("https://api.notion.com/v1/users?page_size=100&start_cursor=67030467-b97b-4729-8fd6-2fb33d012da4", json=response_body)
stream = Users(config=MagicMock())
records = stream.read_records(sync_mode=SyncMode.full_refresh)
records_length = sum(1 for _ in records)
assert records_length == 220
@pytest.mark.parametrize(
"stream,parent,url,status_code,response_content,expected_availability,expected_reason_substring",
[
(
Users,
None,
"https://api.notion.com/v1/users",
403,
b'{"object": "error", "status": 403, "code": "restricted_resource"}',
False,
"This is likely due to insufficient permissions for your Notion integration.",
),
(
Blocks,
Pages,
"https://api.notion.com/v1/blocks/123/children",
403,
b'{"object": "error", "status": 403, "code": "restricted_resource"}',
False,
"This is likely due to insufficient permissions for your Notion integration.",
),
(
Users,
None,
"https://api.notion.com/v1/users",
200,
b'{"object": "list", "results": [{"id": "123", "object": "user", "type": "person"}]}',
True,
None,
),
],
)
def test_403_error_handling(
requests_mock, stream, parent, url, status_code, response_content, expected_availability, expected_reason_substring
):
"""
Test that availability strategy flags streams with 403 error as unavailable
and returns custom Notion integration message.
"""
requests_mock.get(url=url, status_code=status_code, content=response_content)
if parent:
stream = stream(parent=parent, config=MagicMock())
stream.parent.stream_slices = MagicMock(return_value=[{"id": "123"}])
stream.parent.read_records = MagicMock(return_value=[{"id": "123", "object": "page"}])
else:
stream = stream(config=MagicMock())
is_available, reason = stream.check_availability(logger=logging.Logger, source=MagicMock())
assert is_available is expected_availability
if expected_reason_substring:
assert expected_reason_substring in reason
else:
assert reason is None