-
Notifications
You must be signed in to change notification settings - Fork 4.6k
move test to HttpMocker #35069
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
Closed
maxi297
wants to merge
19
commits into
master
from
maxi297/source-github-integration-tests-responses
Closed
move test to HttpMocker #35069
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
9157553
Source Github: add integration tests
artem1205 e8cbed2
Merge remote-tracking branch 'origin/master' into artem1205/source-gi…
artem1205 8f85394
Source Github: fix formatting
artem1205 78596ad
Source Github: fix formatting
artem1205 ee904f6
Airbyte CDK: add headers
artem1205 ce41a4f
Source Github: add test for pagination
artem1205 9f1dee8
Source Github: add test incremental + error
artem1205 19dca25
Source GitHub: rewrite using responses
artem1205 d3a75c4
Source GitHub: rewrite using responses
artem1205 1dd5d94
Revert "Airbyte CDK: add headers"
artem1205 3834666
Source GitHub: rewrite using responses
artem1205 c1ef906
Source GitHub: update test
artem1205 a034ba8
Source GitHub: add transformation check
artem1205 5707dd8
Source GitHub: Revert pyproject.toml
artem1205 e321c6d
Source GitHub: Add OrderedRegistry to strictly check order and amount…
artem1205 775c3a8
Merge remote-tracking branch 'origin/master' into artem1205/source-gi…
artem1205 eb6772c
Source GitHub: ref
artem1205 28984f9
Source GitHub: ref
artem1205 0627ea0
move test to HttpMocker
maxi297 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
35 changes: 35 additions & 0 deletions
35
airbyte-integrations/connectors/source-github/unit_tests/integration/config.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Copyright (c) 2023 Airbyte, Inc., all rights reserved. | ||
|
||
from datetime import datetime | ||
from typing import Any, Dict, List | ||
|
||
|
||
class ConfigBuilder: | ||
def __init__(self) -> None: | ||
self._config: Dict[str, Any] = { | ||
"credentials": {"option_title": "PAT Credentials", "personal_access_token": "GITHUB_TEST_TOKEN"}, | ||
"start_date": "2020-05-01T00:00:00Z", | ||
} | ||
|
||
def with_repositories(self, repositories: List[str]) -> "ConfigBuilder": | ||
self._config["repositories"] = repositories | ||
return self | ||
|
||
def with_client_secret(self, client_secret: str) -> "ConfigBuilder": | ||
self._config["client_secret"] = client_secret | ||
return self | ||
|
||
def with_start_date(self, start_datetime: datetime) -> "ConfigBuilder": | ||
self._config["start_date"] = start_datetime.isoformat()[:-13] + "Z" | ||
return self | ||
|
||
def with_branches(self, branches: List[str]) -> "ConfigBuilder": | ||
self._config["branches"] = branches | ||
return self | ||
|
||
def with_api_url(self, api_url: str) -> "ConfigBuilder": | ||
self._config["api_url"] = api_url | ||
return self | ||
|
||
def build(self) -> Dict[str, Any]: | ||
return self._config |
102 changes: 102 additions & 0 deletions
102
airbyte-integrations/connectors/source-github/unit_tests/integration/test_events.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
# Copyright (c) 2023 Airbyte, Inc., all rights reserved. | ||
|
||
from unittest import TestCase | ||
|
||
import json | ||
from airbyte_cdk.models import SyncMode | ||
from airbyte_cdk.test.catalog_builder import CatalogBuilder | ||
from airbyte_cdk.test.entrypoint_wrapper import read | ||
from airbyte_cdk.test.mock_http.mocker import HttpMocker | ||
from airbyte_cdk.test.mock_http import HttpRequest, HttpResponse | ||
from airbyte_cdk.test.mock_http.response_builder import find_template | ||
from source_github import SourceGithub | ||
|
||
from .config import ConfigBuilder | ||
|
||
_CONFIG = ConfigBuilder().with_repositories(["airbytehq/integration-test"]).build() | ||
|
||
|
||
def _create_catalog(sync_mode: SyncMode = SyncMode.full_refresh): | ||
return CatalogBuilder().with_stream(name="events", sync_mode=sync_mode).build() | ||
|
||
|
||
class EventsTest(TestCase): | ||
def setUp(self) -> None: | ||
"""Base setup for all tests. Add responses for: | ||
1. rate limit checker | ||
2. repositories | ||
3. branches | ||
""" | ||
|
||
self.r_mock = HttpMocker() | ||
self.r_mock.__enter__() | ||
self.r_mock.get( | ||
HttpRequest( | ||
url="https://api.github.com/rate_limit", | ||
query_params={}, | ||
headers={ | ||
"Accept": "application/vnd.github+json", | ||
"X-GitHub-Api-Version": "2022-11-28", | ||
"Authorization": "token GITHUB_TEST_TOKEN", | ||
}, | ||
), | ||
HttpResponse( | ||
json.dumps({ | ||
"resources": { | ||
"core": {"limit": 5000, "used": 0, "remaining": 5000, "reset": 5070908800}, | ||
"graphql": {"limit": 5000, "used": 0, "remaining": 5000, "reset": 5070908800}, | ||
} | ||
}), | ||
200 | ||
) | ||
) | ||
|
||
self.r_mock.get( | ||
HttpRequest( | ||
url=f"https://api.github.com/repos/{_CONFIG.get('repositories')[0]}", | ||
query_params={"per_page": 100}, | ||
), | ||
HttpResponse( | ||
json.dumps({"full_name": "airbytehq/integration-test", "default_branch": "master"}), | ||
200 | ||
) | ||
) | ||
|
||
self.r_mock.get( | ||
HttpRequest( | ||
url=f"https://api.github.com/repos/{_CONFIG.get('repositories')[0]}/branches", | ||
query_params={"per_page": 100}, | ||
), | ||
HttpResponse( | ||
json.dumps([{"repository": "airbytehq/integration-test", "name": "master"}]), | ||
200 | ||
) | ||
) | ||
|
||
def teardown(self): | ||
"""Stops and resets RequestsMock instance. | ||
|
||
If ``assert_all_requests_are_fired`` is set to ``True``, will raise an error | ||
if some requests were not processed. | ||
""" | ||
self.r_mock.__exit__() | ||
|
||
def test_full_refresh_no_pagination(self): | ||
"""Ensure http integration, record extraction and transformation""" | ||
|
||
self.r_mock.get( | ||
HttpRequest( | ||
url=f"https://api.github.com/repos/{_CONFIG.get('repositories')[0]}/events", | ||
query_params={"per_page": 100}, | ||
), | ||
HttpResponse( | ||
json.dumps(find_template("events", __file__)), | ||
200 | ||
) | ||
) | ||
|
||
source = SourceGithub() | ||
actual_messages = read(source, config=_CONFIG, catalog=_create_catalog()) | ||
|
||
assert len(actual_messages.records) == 2 | ||
assert all(("repository", "airbytehq/integration-test") in x.record.data.items() for x in actual_messages.records) |
63 changes: 63 additions & 0 deletions
63
airbyte-integrations/connectors/source-github/unit_tests/resource/http/response/events.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
[ | ||
{ | ||
"id": "22249084964", | ||
"type": "PushEvent", | ||
"actor": { | ||
"id": 583231, | ||
"login": "octocat", | ||
"display_login": "octocat", | ||
"gravatar_id": "", | ||
"url": "https://api.github.com/users/octocat", | ||
"avatar_url": "https://avatars.githubusercontent.com/u/583231?v=4" | ||
}, | ||
"repo": { | ||
"id": 1296269, | ||
"name": "octocat/Hello-World", | ||
"url": "https://api.github.com/repos/octocat/Hello-World" | ||
}, | ||
"payload": { | ||
"push_id": 10115855396, | ||
"size": 1, | ||
"distinct_size": 1, | ||
"ref": "refs/heads/master", | ||
"head": "7a8f3ac80e2ad2f6842cb86f576d4bfe2c03e300", | ||
"before": "883efe034920928c47fe18598c01249d1a9fdabd", | ||
"commits": [ | ||
{ | ||
"sha": "7a8f3ac80e2ad2f6842cb86f576d4bfe2c03e300", | ||
"author": { | ||
"email": "[email protected]", | ||
"name": "Monalisa Octocat" | ||
}, | ||
"message": "commit", | ||
"distinct": true, | ||
"url": "https://api.github.com/repos/octocat/Hello-World/commits/7a8f3ac80e2ad2f6842cb86f576d4bfe2c03e300" | ||
} | ||
] | ||
}, | ||
"public": true, | ||
"created_at": "2022-06-09T12:47:28Z" | ||
}, | ||
{ | ||
"id": "22237752260", | ||
"type": "WatchEvent", | ||
"actor": { | ||
"id": 583231, | ||
"login": "octocat", | ||
"display_login": "octocat", | ||
"gravatar_id": "", | ||
"url": "https://api.github.com/users/octocat", | ||
"avatar_url": "https://avatars.githubusercontent.com/u/583231?v=4" | ||
}, | ||
"repo": { | ||
"id": 1296269, | ||
"name": "octocat/Hello-World", | ||
"url": "https://api.github.com/repos/octocat/Hello-World" | ||
}, | ||
"payload": { | ||
"action": "started" | ||
}, | ||
"public": true, | ||
"created_at": "2022-06-08T23:29:25Z" | ||
} | ||
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1379,17 +1379,17 @@ def test_stream_contributor_activity_accepted_response(caplog, rate_limit_mock_r | |
status=200, | ||
) | ||
responses.add( | ||
responses.GET, | ||
"https://api.github.com/repos/airbytehq/test_airbyte?per_page=100", | ||
json={"full_name": "airbytehq/test_airbyte", "default_branch": "default_branch"}, | ||
status=200, | ||
) | ||
responses.GET, | ||
"https://api.github.com/repos/airbytehq/test_airbyte?per_page=100", | ||
json={"full_name": "airbytehq/test_airbyte", "default_branch": "default_branch"}, | ||
status=200, | ||
) | ||
responses.add( | ||
responses.GET, | ||
"https://api.github.com/repos/airbytehq/test_airbyte/branches?per_page=100", | ||
json={}, | ||
status=200, | ||
) | ||
responses.GET, | ||
"https://api.github.com/repos/airbytehq/test_airbyte/branches?per_page=100", | ||
json={}, | ||
status=200, | ||
) | ||
resp = responses.add( | ||
responses.GET, | ||
"https://api.github.com/repos/airbytehq/test_airbyte/stats/contributors?per_page=100", | ||
|
@@ -1401,9 +1401,14 @@ def test_stream_contributor_activity_accepted_response(caplog, rate_limit_mock_r | |
configured_catalog = { | ||
"streams": [ | ||
{ | ||
"stream": {"name": "contributor_activity", "json_schema": {}, "supported_sync_modes": ["full_refresh"],"source_defined_primary_key": [["id"]]}, | ||
"stream": { | ||
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. Should |
||
"name": "contributor_activity", | ||
"json_schema": {}, | ||
"supported_sync_modes": ["full_refresh"], | ||
"source_defined_primary_key": [["id"]], | ||
}, | ||
"sync_mode": "full_refresh", | ||
"destination_sync_mode": "overwrite" | ||
"destination_sync_mode": "overwrite", | ||
} | ||
] | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Could
start_date.strftime("%Y-%m-%dT%H:%M:%SZ")
be used instead ofstart_datetime.isoformat()[:-13] + "Z"
here?