|
| 1 | +# Copyright (c) 2024 Airbyte, Inc., all rights reserved. |
| 2 | + |
| 3 | +import sys |
| 4 | +from pathlib import Path |
| 5 | +from typing import Any, Dict, Optional |
| 6 | +from unittest import TestCase |
| 7 | + |
| 8 | +from config_builder import ConfigBuilder |
| 9 | + |
| 10 | +from airbyte_cdk import ConfiguredAirbyteCatalog, SyncMode, TState, YamlDeclarativeSource |
| 11 | +from airbyte_cdk.test.catalog_builder import CatalogBuilder |
| 12 | +from airbyte_cdk.test.entrypoint_wrapper import EntrypointOutput |
| 13 | +from airbyte_cdk.test.entrypoint_wrapper import read as entrypoint_read |
| 14 | +from airbyte_cdk.test.mock_http import HttpMocker, HttpRequest |
| 15 | +from airbyte_cdk.test.mock_http.response_builder import ( |
| 16 | + FieldPath, |
| 17 | + HttpResponseBuilder, |
| 18 | + RecordBuilder, |
| 19 | + create_record_builder, |
| 20 | + create_response_builder, |
| 21 | + find_template, |
| 22 | +) |
| 23 | +from airbyte_cdk.test.state_builder import StateBuilder |
| 24 | + |
| 25 | + |
| 26 | +def _get_manifest_path() -> Path: |
| 27 | + source_declarative_manifest_path = Path("/airbyte/integration_code/source_declarative_manifest") |
| 28 | + if source_declarative_manifest_path.exists(): |
| 29 | + return source_declarative_manifest_path |
| 30 | + return Path(__file__).parent.parent.parent |
| 31 | + |
| 32 | + |
| 33 | +_SOURCE_FOLDER_PATH = _get_manifest_path() |
| 34 | +_YAML_FILE_PATH = _SOURCE_FOLDER_PATH / "manifest.yaml" |
| 35 | + |
| 36 | +sys.path.append(str(_SOURCE_FOLDER_PATH)) # to allow loading custom components |
| 37 | + |
| 38 | + |
| 39 | +def _catalog() -> ConfiguredAirbyteCatalog: |
| 40 | + return CatalogBuilder().with_stream("segments", SyncMode.full_refresh).build() |
| 41 | + |
| 42 | + |
| 43 | +def _source(catalog: ConfiguredAirbyteCatalog, config: Dict[str, Any], state: Optional[TState]) -> YamlDeclarativeSource: |
| 44 | + return YamlDeclarativeSource(path_to_yaml=str(_YAML_FILE_PATH), catalog=catalog, config=config, state=state) |
| 45 | + |
| 46 | + |
| 47 | +def _response_template() -> Dict[str, Any]: |
| 48 | + return find_template("segments", __file__) |
| 49 | + |
| 50 | + |
| 51 | +def _record() -> RecordBuilder: |
| 52 | + return create_record_builder( |
| 53 | + _response_template(), FieldPath("segments"), record_id_path=FieldPath("id"), record_cursor_path=FieldPath("updated_at") |
| 54 | + ) |
| 55 | + |
| 56 | + |
| 57 | +def _response() -> HttpResponseBuilder: |
| 58 | + return create_response_builder( |
| 59 | + response_template=_response_template(), |
| 60 | + records_path=FieldPath("segments"), |
| 61 | + ) |
| 62 | + |
| 63 | + |
| 64 | +def read( |
| 65 | + config_builder: Optional[ConfigBuilder] = None, |
| 66 | + state_builder: Optional[StateBuilder] = None, |
| 67 | + expecting_exception: bool = False, |
| 68 | +) -> EntrypointOutput: |
| 69 | + catalog = _catalog() |
| 70 | + config = config_builder.build() if config_builder else ConfigBuilder().build() |
| 71 | + state = state_builder.build() if state_builder else StateBuilder().build() |
| 72 | + return entrypoint_read(_source(catalog, config, state), config, catalog, state, expecting_exception) |
| 73 | + |
| 74 | + |
| 75 | +class SegmentsTest(TestCase): |
| 76 | + @HttpMocker() |
| 77 | + def test_when_read_then_extract_records(self, http_mocker: HttpMocker) -> None: |
| 78 | + http_mocker.get( |
| 79 | + HttpRequest("https://api.intercom.io/segments", query_params={"per_page": "150"}), |
| 80 | + _response().with_record(_record()).with_record(_record()).build(), |
| 81 | + ) |
| 82 | + output = read(ConfigBuilder(), StateBuilder()) |
| 83 | + assert len(output.records) == 2 |
0 commit comments