|
| 1 | +# Copyright (c) 2023 Airbyte, Inc., all rights reserved. |
| 2 | + |
| 3 | +from typing import Any, Dict, Optional |
| 4 | +from unittest import TestCase |
| 5 | + |
| 6 | +from airbyte_cdk.test.catalog_builder import CatalogBuilder |
| 7 | +from airbyte_cdk.test.entrypoint_wrapper import EntrypointOutput, read |
| 8 | +from airbyte_cdk.test.mock_http import HttpMocker, HttpRequest |
| 9 | +from airbyte_cdk.test.mock_http.request import ANY_QUERY_PARAMS |
| 10 | +from airbyte_cdk.test.mock_http.response_builder import ( |
| 11 | + FieldPath, |
| 12 | + HttpResponseBuilder, |
| 13 | + NestedPath, |
| 14 | + RecordBuilder, |
| 15 | + create_record_builder, |
| 16 | + create_response_builder, |
| 17 | + find_template, |
| 18 | +) |
| 19 | +from airbyte_protocol.models import ConfiguredAirbyteCatalog, SyncMode |
| 20 | +from integration.config import KlaviyoConfigBuilder |
| 21 | +from source_klaviyo import SourceKlaviyo |
| 22 | + |
| 23 | +_ENDPOINT_TEMPLATE_NAME = "profiles" |
| 24 | +_STREAM_NAME = "profiles" |
| 25 | +_RECORDS_PATH = FieldPath("data") |
| 26 | + |
| 27 | + |
| 28 | +def _config() -> KlaviyoConfigBuilder: |
| 29 | + return KlaviyoConfigBuilder() |
| 30 | + |
| 31 | + |
| 32 | +def _catalog(sync_mode: SyncMode) -> ConfiguredAirbyteCatalog: |
| 33 | + return CatalogBuilder().with_stream(_STREAM_NAME, sync_mode).build() |
| 34 | + |
| 35 | + |
| 36 | +def _a_profile_request() -> HttpRequest: |
| 37 | + return HttpRequest( |
| 38 | + url=f"https://a.klaviyo.com/api/profiles", |
| 39 | + query_params=ANY_QUERY_PARAMS |
| 40 | + ) |
| 41 | + |
| 42 | + |
| 43 | +def _a_profile() -> RecordBuilder: |
| 44 | + return create_record_builder( |
| 45 | + find_template(_ENDPOINT_TEMPLATE_NAME, __file__), |
| 46 | + _RECORDS_PATH, |
| 47 | + record_id_path=FieldPath("id"), |
| 48 | + record_cursor_path=NestedPath(["attributes", "updated"]), |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +def _profiles_response() -> HttpResponseBuilder: |
| 53 | + return create_response_builder( |
| 54 | + find_template(_ENDPOINT_TEMPLATE_NAME, __file__), |
| 55 | + _RECORDS_PATH, |
| 56 | + ) |
| 57 | + |
| 58 | + |
| 59 | +def _read( |
| 60 | + config_builder: KlaviyoConfigBuilder, sync_mode: SyncMode, state: Optional[Dict[str, Any]] = None, expecting_exception: bool = False |
| 61 | +) -> EntrypointOutput: |
| 62 | + catalog = _catalog(sync_mode) |
| 63 | + config = config_builder.build() |
| 64 | + return read(SourceKlaviyo(), config, catalog, state, expecting_exception) |
| 65 | + |
| 66 | + |
| 67 | +class FullRefreshTest(TestCase): |
| 68 | + @HttpMocker() |
| 69 | + def test_when_read_then_extract_records(self, http_mocker: HttpMocker) -> None: |
| 70 | + http_mocker.get( |
| 71 | + _a_profile_request(), |
| 72 | + _profiles_response().with_record(_a_profile()).build(), |
| 73 | + ) |
| 74 | + |
| 75 | + output = _read(_config(), SyncMode.full_refresh) |
| 76 | + |
| 77 | + assert len(output.records) == 1 |
| 78 | + |
| 79 | + @HttpMocker() |
| 80 | + def test_given_region_is_number_when_read_then_cast_as_string(self, http_mocker: HttpMocker) -> None: |
| 81 | + http_mocker.get( |
| 82 | + _a_profile_request(), |
| 83 | + _profiles_response().with_record(_a_profile().with_field(NestedPath(["attributes", "location", "region"]), 10)).build(), |
| 84 | + ) |
| 85 | + |
| 86 | + output = _read(_config(), SyncMode.full_refresh) |
| 87 | + |
| 88 | + assert len(output.records) == 1 |
| 89 | + assert isinstance(output.records[0].record.data["attributes"]["location"]["region"], str) |
0 commit comments