-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🎉 Source Notion: Adds pagination to Users stream #11452
Changes from all commits
2c6f0fd
11cfa83
fda250e
6d85961
1fbb529
ac5965c
e241d21
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -3,11 +3,13 @@ | |||||||||||||||||||||||||||||||||||
# | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
from http import HTTPStatus | ||||||||||||||||||||||||||||||||||||
import random | ||||||||||||||||||||||||||||||||||||
from unittest.mock import MagicMock | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
import pytest | ||||||||||||||||||||||||||||||||||||
import requests | ||||||||||||||||||||||||||||||||||||
from source_notion.streams import NotionStream | ||||||||||||||||||||||||||||||||||||
from airbyte_cdk.models import SyncMode | ||||||||||||||||||||||||||||||||||||
from source_notion.streams import NotionStream, Users | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
@pytest.fixture | ||||||||||||||||||||||||||||||||||||
|
@@ -76,3 +78,56 @@ def test_backoff_time(patch_base_class): | |||||||||||||||||||||||||||||||||||
stream = NotionStream(config=MagicMock()) | ||||||||||||||||||||||||||||||||||||
expected_backoff_time = None | ||||||||||||||||||||||||||||||||||||
assert stream.backoff_time(response_mock) == expected_backoff_time | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
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 | ||||||||||||||||||||||||||||||||||||
Comment on lines
+83
to
+94
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I suggest leverage parametrize wherever you can.
Suggested change
|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
def test_user_stream_handles_pagination_correclty(requests_mock): | ||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||
Test shows that Users stream uses pagination as per Notion API docs. | ||||||||||||||||||||||||||||||||||||
""" | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I would suggest to use a function to generate your requests/responses sequence |
||||||||||||||||||||||||||||||||||||
response_body = { | ||||||||||||||||||||||||||||||||||||
"object": "list", | ||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in fda250e. |
||||||||||||||||||||||||||||||||||||
"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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of using a dummy start_cursor value maybe use a self documenting value that will be provided from Notion such as "next_cursor": "fe2cc560-036c-44cd-90e8-294d5a74cebc"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right. Updated in fda250e.