|
| 1 | +# |
| 2 | +# Copyright (c) 2025 Airbyte, Inc., all rights reserved. |
| 3 | +# |
| 4 | + |
| 5 | +from unittest.mock import Mock |
| 6 | + |
| 7 | +from airbyte_cdk.sources.declarative.incremental import PerPartitionWithGlobalCursor |
| 8 | +from airbyte_cdk.sources.declarative.incremental.declarative_cursor import DeclarativeCursor |
| 9 | +from airbyte_cdk.sources.declarative.incremental.global_substream_cursor import ( |
| 10 | + GlobalSubstreamCursor, |
| 11 | +) |
| 12 | +from airbyte_cdk.sources.declarative.incremental.per_partition_cursor import ( |
| 13 | + StreamSlice, |
| 14 | +) |
| 15 | +from airbyte_cdk.sources.declarative.partition_routers import AsyncJobPartitionRouter |
| 16 | +from airbyte_cdk.sources.declarative.partition_routers.partition_router import PartitionRouter |
| 17 | +from airbyte_cdk.sources.declarative.stream_slicers import StreamSlicerTestReadDecorator |
| 18 | + |
| 19 | +CURSOR_SLICE_FIELD = "cursor slice field" |
| 20 | + |
| 21 | + |
| 22 | +class MockedCursorBuilder: |
| 23 | + def __init__(self): |
| 24 | + self._stream_slices = [] |
| 25 | + self._stream_state = {} |
| 26 | + |
| 27 | + def with_stream_slices(self, stream_slices): |
| 28 | + self._stream_slices = stream_slices |
| 29 | + return self |
| 30 | + |
| 31 | + def with_stream_state(self, stream_state): |
| 32 | + self._stream_state = stream_state |
| 33 | + return self |
| 34 | + |
| 35 | + def build(self): |
| 36 | + cursor = Mock(spec=DeclarativeCursor) |
| 37 | + cursor.get_stream_state.return_value = self._stream_state |
| 38 | + cursor.stream_slices.return_value = self._stream_slices |
| 39 | + return cursor |
| 40 | + |
| 41 | + |
| 42 | +def mocked_partition_router(): |
| 43 | + return Mock(spec=PartitionRouter) |
| 44 | + |
| 45 | + |
| 46 | +def test_show_as_wrapped_instance(): |
| 47 | + first_partition = {"first_partition_key": "first_partition_value"} |
| 48 | + mocked_partition_router().stream_slices.return_value = [ |
| 49 | + StreamSlice( |
| 50 | + partition=first_partition, cursor_slice={}, extra_fields={"extra_field": "extra_value"} |
| 51 | + ), |
| 52 | + ] |
| 53 | + cursor = ( |
| 54 | + MockedCursorBuilder() |
| 55 | + .with_stream_slices([{CURSOR_SLICE_FIELD: "first slice cursor value"}]) |
| 56 | + .build() |
| 57 | + ) |
| 58 | + |
| 59 | + global_cursor = GlobalSubstreamCursor(cursor, mocked_partition_router) |
| 60 | + wrapped_slicer = StreamSlicerTestReadDecorator( |
| 61 | + wrapped_slicer=global_cursor, |
| 62 | + maximum_number_of_slices=5, |
| 63 | + ) |
| 64 | + assert isinstance(wrapped_slicer, GlobalSubstreamCursor) |
| 65 | + assert not isinstance(wrapped_slicer, AsyncJobPartitionRouter) |
| 66 | + assert not isinstance(wrapped_slicer, PerPartitionWithGlobalCursor) |
| 67 | + |
| 68 | + assert isinstance(global_cursor, GlobalSubstreamCursor) |
| 69 | + assert not isinstance(global_cursor, AsyncJobPartitionRouter) |
| 70 | + assert not isinstance(global_cursor, PerPartitionWithGlobalCursor) |
0 commit comments