|
| 1 | +# Copyright (c) 2023 Airbyte, Inc., all rights reserved. |
| 2 | + |
| 3 | +import json |
| 4 | +from unittest import TestCase |
| 5 | + |
| 6 | +from airbyte_cdk.models import SyncMode |
| 7 | +from airbyte_cdk.test.catalog_builder import CatalogBuilder |
| 8 | +from airbyte_cdk.test.entrypoint_wrapper import read |
| 9 | +from airbyte_cdk.test.mock_http import HttpMocker, HttpRequest, HttpResponse |
| 10 | +from airbyte_cdk.test.mock_http.response_builder import find_template |
| 11 | +from airbyte_cdk.test.state_builder import StateBuilder |
| 12 | +from airbyte_protocol.models import AirbyteStreamStatus, Level, TraceType |
| 13 | +from source_github import SourceGithub |
| 14 | + |
| 15 | +from .config import ConfigBuilder |
| 16 | + |
| 17 | +_CONFIG = ConfigBuilder().with_repositories(["airbytehq/integration-test"]).build() |
| 18 | + |
| 19 | + |
| 20 | +def _create_catalog(sync_mode: SyncMode = SyncMode.full_refresh): |
| 21 | + return CatalogBuilder().with_stream(name="events", sync_mode=sync_mode).build() |
| 22 | + |
| 23 | + |
| 24 | +class EventsTest(TestCase): |
| 25 | + def setUp(self) -> None: |
| 26 | + """Base setup for all tests. Add responses for: |
| 27 | + 1. rate limit checker |
| 28 | + 2. repositories |
| 29 | + 3. branches |
| 30 | + """ |
| 31 | + |
| 32 | + self.r_mock = HttpMocker() |
| 33 | + self.r_mock.__enter__() |
| 34 | + self.r_mock.get( |
| 35 | + HttpRequest( |
| 36 | + url="https://api.github.com/rate_limit", |
| 37 | + query_params={}, |
| 38 | + headers={ |
| 39 | + "Accept": "application/vnd.github+json", |
| 40 | + "X-GitHub-Api-Version": "2022-11-28", |
| 41 | + "Authorization": "token GITHUB_TEST_TOKEN", |
| 42 | + }, |
| 43 | + ), |
| 44 | + HttpResponse( |
| 45 | + json.dumps( |
| 46 | + { |
| 47 | + "resources": { |
| 48 | + "core": {"limit": 5000, "used": 0, "remaining": 5000, "reset": 5070908800}, |
| 49 | + "graphql": {"limit": 5000, "used": 0, "remaining": 5000, "reset": 5070908800}, |
| 50 | + } |
| 51 | + } |
| 52 | + ), |
| 53 | + 200, |
| 54 | + ), |
| 55 | + ) |
| 56 | + |
| 57 | + self.r_mock.get( |
| 58 | + HttpRequest( |
| 59 | + url=f"https://api.github.com/repos/{_CONFIG.get('repositories')[0]}", |
| 60 | + query_params={"per_page": 100}, |
| 61 | + ), |
| 62 | + HttpResponse(json.dumps({"full_name": "airbytehq/integration-test", "default_branch": "master"}), 200), |
| 63 | + ) |
| 64 | + |
| 65 | + self.r_mock.get( |
| 66 | + HttpRequest( |
| 67 | + url=f"https://api.github.com/repos/{_CONFIG.get('repositories')[0]}/branches", |
| 68 | + query_params={"per_page": 100}, |
| 69 | + ), |
| 70 | + HttpResponse(json.dumps([{"repository": "airbytehq/integration-test", "name": "master"}]), 200), |
| 71 | + ) |
| 72 | + |
| 73 | + def teardown(self): |
| 74 | + """Stops and resets HttpMocker instance.""" |
| 75 | + self.r_mock.__exit__() |
| 76 | + |
| 77 | + def test_read_full_refresh_no_pagination(self): |
| 78 | + """Ensure http integration and record extraction""" |
| 79 | + self.r_mock.get( |
| 80 | + HttpRequest( |
| 81 | + url=f"https://api.github.com/repos/{_CONFIG.get('repositories')[0]}/events", |
| 82 | + query_params={"per_page": 100}, |
| 83 | + ), |
| 84 | + HttpResponse(json.dumps(find_template("events", __file__)), 200), |
| 85 | + ) |
| 86 | + |
| 87 | + source = SourceGithub() |
| 88 | + actual_messages = read(source, config=_CONFIG, catalog=_create_catalog()) |
| 89 | + |
| 90 | + assert len(actual_messages.records) == 2 |
| 91 | + |
| 92 | + def test_read_transformation(self): |
| 93 | + """Ensure transformation applied to all records""" |
| 94 | + |
| 95 | + self.r_mock.get( |
| 96 | + HttpRequest( |
| 97 | + url=f"https://api.github.com/repos/{_CONFIG.get('repositories')[0]}/events", |
| 98 | + query_params={"per_page": 100}, |
| 99 | + ), |
| 100 | + HttpResponse(json.dumps(find_template("events", __file__)), 200), |
| 101 | + ) |
| 102 | + |
| 103 | + source = SourceGithub() |
| 104 | + actual_messages = read(source, config=_CONFIG, catalog=_create_catalog()) |
| 105 | + |
| 106 | + assert len(actual_messages.records) == 2 |
| 107 | + assert all(("repository", "airbytehq/integration-test") in x.record.data.items() for x in actual_messages.records) |
| 108 | + |
| 109 | + def test_full_refresh_with_pagination(self): |
| 110 | + """Ensure pagination""" |
| 111 | + self.r_mock.get( |
| 112 | + HttpRequest( |
| 113 | + url=f"https://api.github.com/repos/{_CONFIG.get('repositories')[0]}/events", |
| 114 | + query_params={"per_page": 100}, |
| 115 | + ), |
| 116 | + HttpResponse( |
| 117 | + body=json.dumps(find_template("events", __file__)), |
| 118 | + status_code=200, |
| 119 | + headers={"Link": '<https://api.github.com/repos/{}/events?page=2>; rel="next"'.format(_CONFIG.get("repositories")[0])}, |
| 120 | + ), |
| 121 | + ) |
| 122 | + self.r_mock.get( |
| 123 | + HttpRequest( |
| 124 | + url=f"https://api.github.com/repos/{_CONFIG.get('repositories')[0]}/events", |
| 125 | + query_params={"per_page": 100, "page": 2}, |
| 126 | + ), |
| 127 | + HttpResponse( |
| 128 | + body=json.dumps(find_template("events", __file__)), |
| 129 | + status_code=200, |
| 130 | + ), |
| 131 | + ) |
| 132 | + source = SourceGithub() |
| 133 | + actual_messages = read(source, config=_CONFIG, catalog=_create_catalog()) |
| 134 | + |
| 135 | + assert len(actual_messages.records) == 4 |
| 136 | + |
| 137 | + def test_given_state_more_recent_than_some_records_when_read_incrementally_then_filter_records(self): |
| 138 | + """Ensure incremental sync. |
| 139 | + Stream `Events` is semi-incremental, so all requests will be performed and only new records will be extracted""" |
| 140 | + |
| 141 | + self.r_mock.get( |
| 142 | + HttpRequest( |
| 143 | + url=f"https://api.github.com/repos/{_CONFIG.get('repositories')[0]}/events", |
| 144 | + query_params={"per_page": 100}, |
| 145 | + ), |
| 146 | + HttpResponse(json.dumps(find_template("events", __file__)), 200), |
| 147 | + ) |
| 148 | + |
| 149 | + source = SourceGithub() |
| 150 | + actual_messages = read( |
| 151 | + source, |
| 152 | + config=_CONFIG, |
| 153 | + catalog=_create_catalog(sync_mode=SyncMode.incremental), |
| 154 | + state=StateBuilder() |
| 155 | + .with_stream_state("events", {"airbytehq/integration-test": {"created_at": "2022-06-09T10:00:00Z"}}) |
| 156 | + .build(), |
| 157 | + ) |
| 158 | + assert len(actual_messages.records) == 1 |
| 159 | + |
| 160 | + def test_when_read_incrementally_then_emit_state_message(self): |
| 161 | + """Ensure incremental sync emits correct stream state message""" |
| 162 | + |
| 163 | + self.r_mock.get( |
| 164 | + HttpRequest( |
| 165 | + url=f"https://api.github.com/repos/{_CONFIG.get('repositories')[0]}/events", |
| 166 | + query_params={"per_page": 100}, |
| 167 | + ), |
| 168 | + HttpResponse(json.dumps(find_template("events", __file__)), 200), |
| 169 | + ) |
| 170 | + |
| 171 | + source = SourceGithub() |
| 172 | + actual_messages = read( |
| 173 | + source, |
| 174 | + config=_CONFIG, |
| 175 | + catalog=_create_catalog(sync_mode=SyncMode.incremental), |
| 176 | + state=StateBuilder() |
| 177 | + .with_stream_state("events", {"airbytehq/integration-test": {"created_at": "2020-06-09T10:00:00Z"}}) |
| 178 | + .build(), |
| 179 | + ) |
| 180 | + assert actual_messages.state_messages[0].state.data == {'events': {'airbytehq/integration-test': {'created_at': '2022-06-09T12:47:28Z'}}} |
| 181 | + |
| 182 | + def test_read_handles_expected_error_correctly_and_exits_with_complete_status(self): |
| 183 | + """Ensure read() method does not raise an Exception and log message with error is in output""" |
| 184 | + self.r_mock.get( |
| 185 | + HttpRequest( |
| 186 | + url=f"https://api.github.com/repos/{_CONFIG.get('repositories')[0]}/events", |
| 187 | + query_params={"per_page": 100}, |
| 188 | + ), |
| 189 | + HttpResponse('{"message":"some_error_message"}', 403), |
| 190 | + ) |
| 191 | + source = SourceGithub() |
| 192 | + actual_messages = read(source, config=_CONFIG, catalog=_create_catalog()) |
| 193 | + |
| 194 | + assert Level.ERROR in [x.log.level for x in actual_messages.logs] |
| 195 | + events_stream_complete_message = [x for x in actual_messages.trace_messages if x.trace.type == TraceType.STREAM_STATUS][-1] |
| 196 | + assert events_stream_complete_message.trace.stream_status.stream_descriptor.name == 'events' |
| 197 | + assert events_stream_complete_message.trace.stream_status.status == AirbyteStreamStatus.COMPLETE |
0 commit comments