Skip to content
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

Merged
merged 7 commits into from
Apr 22, 2022

Conversation

lgomezm
Copy link
Contributor

@lgomezm lgomezm commented Mar 27, 2022

What

How

  • Adds correct handling of pagination for that kind of stream.
  • Overwrites the request_params stream method.

Recommended reading order

  1. streams.py
  2. test_streams.py

🚨 User Impact 🚨

Are there any breaking changes? What is the end result perceived by the user? If yes, please merge this PR with the 🚨🚨 emoji so changelog authors can further highlight this if needed.

Pre-merge Checklist

Expand the relevant checklist and delete the others.

New Connector

Community member or Airbyter

  • Community member? Grant edit access to maintainers (instructions)
  • Secrets in the connector's spec are annotated with airbyte_secret
  • Unit & integration tests added and passing. Community members, please provide proof of success locally e.g: screenshot or copy-paste unit, integration, and acceptance test output. To run acceptance tests for a Python connector, follow instructions in the README. For java connectors run ./gradlew :airbyte-integrations:connectors:<name>:integrationTest.
  • Code reviews completed
  • Documentation updated
    • Connector's README.md
    • Connector's bootstrap.md. See description and examples
    • docs/SUMMARY.md
    • docs/integrations/<source or destination>/<name>.md including changelog. See changelog example
    • docs/integrations/README.md
    • airbyte-integrations/builds.md
  • PR name follows PR naming conventions

Airbyter

If this is a community PR, the Airbyte engineer reviewing this PR is responsible for the below items.

  • Create a non-forked branch based on this PR and test the below items on it
  • Build is successful
  • If new credentials are required for use in CI, add them to GSM. Instructions.
  • /test connector=connectors/<name> command is passing
  • New Connector version released on Dockerhub by running the /publish command described here
  • After the connector is published, connector added to connector index as described here
  • Seed specs have been re-generated by building the platform and committing the changes to the seed spec files, as described here
Updating a connector

Community member or Airbyter

  • Grant edit access to maintainers (instructions)
  • Secrets in the connector's spec are annotated with airbyte_secret
  • Unit & integration tests added and passing. Community members, please provide proof of success locally e.g: screenshot or copy-paste unit, integration, and acceptance test output. To run acceptance tests for a Python connector, follow instructions in the README. For java connectors run ./gradlew :airbyte-integrations:connectors:<name>:integrationTest.
  • Code reviews completed
  • Documentation updated
    • Connector's README.md
    • Connector's bootstrap.md. See description and examples
    • Changelog updated in docs/integrations/<source or destination>/<name>.md including changelog. See changelog example
  • PR name follows PR naming conventions

Airbyter

If this is a community PR, the Airbyte engineer reviewing this PR is responsible for the below items.

  • Create a non-forked branch based on this PR and test the below items on it
  • Build is successful
  • If new credentials are required for use in CI, add them to GSM. Instructions.
  • /test connector=connectors/<name> command is passing
  • New Connector version released on Dockerhub by running the /publish command described here
  • After the new connector version is published, connector version bumped in the seed directory as described here
  • Seed specs have been re-generated by building the platform and committing the changes to the seed spec files, as described here
Connector Generator
  • Issue acceptance criteria met
  • PR name follows PR naming conventions
  • If adding a new generator, add it to the list of scaffold modules being tested
  • The generator test modules (all connectors with -scaffold in their name) have been updated with the latest scaffold by running ./gradlew :airbyte-integrations:connector-templates:generator:testScaffoldTemplates then checking in your changes
  • Documentation which references the generator is updated as needed

Tests

Unit

Put your unit tests output here.

Integration

Put your integration tests output here.

Acceptance

Put your acceptance tests output here.

@github-actions github-actions bot added the area/connectors Connector related issues label Mar 27, 2022

# 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}
Copy link
Contributor

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"

Copy link
Contributor Author

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.

"""

response_body = {
"object": "list",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the original response from Notion both object and type are part of the results array. In my opinion It would be better if the tests are kept consistent with the actual implementation
image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in fda250e.

"has_more": True,
"type": "user"
}
url = "https://api.notion.com/v1/users?page_size=100"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a need to declare the url variable? couldn't the url be passed directly to requests_mock.get()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to requests_mock.get() in fda250e.

stream = Users(config=MagicMock())

user_ids = set()
for record in stream.read_records(sync_mode=SyncMode.full_refresh):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a need to fill the user_ids set before asserting that the set is of length 220? wouldn't the following snippet do the job? Might also save you some memory from allocating an additional data structure.

records = stream.read_records(sync_mode=SyncMode.full_refresh):
records_length = sum(1 for record in records)
assert records_length == 220

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in fda250e.

@lgomezm
Copy link
Contributor Author

lgomezm commented Mar 28, 2022

Hi @itaseskii. I've addressed your comments. Please take a look again when you get a chance and let me know if there's anything else I should do. Thanks for reviewing!

@lgomezm
Copy link
Contributor Author

lgomezm commented Mar 28, 2022

This is the unit test execution:
Screen Shot 2022-03-28 at 9 26 52 AM

@itaseskii
Copy link
Contributor

Hey @lgomezm I have went through you changes and I have no further comments, great job! :)

@lgomezm
Copy link
Contributor Author

lgomezm commented Apr 18, 2022

@marcosmarxm @alafanechere Hi guys. This PR has been open for a while now. Could you take a look? Thanks in advance!

@aharelick aharelick linked an issue Apr 18, 2022 that may be closed by this pull request
@alafanechere alafanechere self-assigned this Apr 22, 2022
Copy link
Contributor

@alafanechere alafanechere left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @lgomezm,
Thank you for your contribution. I made minor suggestions around the tests. I'll bump the version myself as I can commit to your branch.

Comment on lines +83 to +94
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I suggest leverage parametrize wherever you can.

Suggested change
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
@pytest.mark.parametrize("next_page_token, expected_params", [(None, {"page_size": 100}), ({"next_cursor": "123"}, {"start_cursor": "123", "page_size": 100})])
def test_users_request_params(patch_base_class, next_page_token, expected_params):
stream = Users(config=MagicMock())
inputs = {"stream_slice": None, "stream_state": None, "next_page_token": next_page_token}
assert stream.request_params(**inputs) == expected_params

"""
Test shows that Users stream uses pagination as per Notion API docs.
"""

Copy link
Contributor

Choose a reason for hiding this comment

The 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

@github-actions github-actions bot added the area/documentation Improvements or additions to documentation label Apr 22, 2022
@alafanechere
Copy link
Contributor

alafanechere commented Apr 22, 2022

/publish connector=connectors/source-notion auto-bump-version=false

🕑 connectors/source-notion https://github.com/airbytehq/airbyte/actions/runs/2207187005
❌ Failed to publish connectors/source-notion

@alafanechere
Copy link
Contributor

alafanechere commented Apr 22, 2022

/publish connector=connectors/source-notion auto-bump-version=false

🕑 connectors/source-notion https://github.com/airbytehq/airbyte/actions/runs/2208150419
🚀 Successfully published connectors/source-notion
✅ connectors/source-notion https://github.com/airbytehq/airbyte/actions/runs/2208150419

Copy link
Contributor

@alafanechere alafanechere left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @lgomezm for this improvements. I had to update some schemas too as it looks like notion API evolved a bit.

@alafanechere alafanechere merged commit c16af25 into airbytehq:master Apr 22, 2022
@lgomezm lgomezm deleted the lgomez/notion_user_pagination branch April 22, 2022 14:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/connectors Connector related issues area/documentation Improvements or additions to documentation community
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Pagination
4 participants