Skip to content

Commit

Permalink
CDK: offset_increment.page_size make as [int, InterpolatedString] (ai…
Browse files Browse the repository at this point in the history
…rbytehq#20190)

Signed-off-by: Sergey Chvalyuk <[email protected]>
  • Loading branch information
grubberr authored Dec 7, 2022
1 parent fd8b83d commit 3bab0da
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
3 changes: 3 additions & 0 deletions airbyte-cdk/python/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 0.12.3
Low-code: Finally, make `OffsetIncrement.page_size` interpolated string or int

## 0.12.2
Revert breaking change on `read_config` while keeping the improvement on the error message

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,12 @@ class OffsetIncrement(PaginationStrategy, JsonSchemaMixin):
"""

config: Config
page_size: Union[InterpolatedString, str, int]
page_size: Union[str, int]
options: InitVar[Mapping[str, Any]]

def __post_init__(self, options: Mapping[str, Any]):
self._offset = 0
# InterpolatedString page_size may contain one of int/str types,
# so we need to ensure that its `.string` attribute is of *string* type
self.page_size.string = str(self.page_size.string)
self.page_size = InterpolatedString(self.page_size, options=options)

def next_page_token(self, response: requests.Response, last_records: List[Mapping[str, Any]]) -> Optional[Any]:
if len(last_records) < self.page_size.eval(self.config):
Expand Down
2 changes: 1 addition & 1 deletion airbyte-cdk/python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

setup(
name="airbyte-cdk",
version="0.12.2",
version="0.12.3",
description="A framework for writing Airbyte Connectors.",
long_description=README,
long_description_content_type="text/markdown",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,23 @@

import pytest
import requests
from airbyte_cdk.sources.declarative.interpolation import InterpolatedString
from airbyte_cdk.sources.declarative.parsers.factory import DeclarativeComponentFactory
from airbyte_cdk.sources.declarative.requesters.paginators.strategies.offset_increment import OffsetIncrement


@pytest.mark.parametrize(
"test_name, page_size, expected_next_page_token, expected_offset",
"test_name, page_size, options, expected_next_page_token, expected_offset",
[
("test_same_page_size", InterpolatedString(string="2", options={}), 2, 2),
("test_same_page_size", InterpolatedString(string=2, options={}), 2, 2),
("test_larger_page_size", InterpolatedString(string="{{ options['page_size'] }}", options={"page_size": 3}), None, 0),
("test_same_page_size", "2", {}, 2, 2),
("test_same_page_size", 2, {}, 2, 2),
("test_larger_page_size", "{{ options['page_size'] }}", {"page_size": 3}, None, 0),
],
)
def test_offset_increment_paginator_strategy(test_name, page_size, expected_next_page_token, expected_offset):
paginator_strategy = OffsetIncrement(page_size=page_size, options={}, config={})
def test_offset_increment_paginator_strategy(test_name, page_size, options, expected_next_page_token, expected_offset):
expected_type = DeclarativeComponentFactory.get_default_type("page_size", OffsetIncrement)
page_size = expected_type(page_size)

paginator_strategy = OffsetIncrement(page_size=page_size, options=options, config={})
assert paginator_strategy._offset == 0

response = requests.Response()
Expand All @@ -39,8 +42,8 @@ def test_offset_increment_paginator_strategy(test_name, page_size, expected_next

def test_offset_increment_paginator_strategy_rises():
paginator_strategy = OffsetIncrement(
page_size=InterpolatedString(string="{{ options['page_size'] }}", options={"page_size": "invalid value"}),
options={},
page_size="{{ options['page_size'] }}",
options={"page_size": "invalid value"},
config={}
)
with pytest.raises(Exception) as exc:
Expand Down

0 comments on commit 3bab0da

Please sign in to comment.