Skip to content

Commit

Permalink
chore: update method name
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristoGrab committed Feb 10, 2025
1 parent 5614a91 commit fdfd154
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
)
from airbyte_cdk.sources.message import MessageRepository
from airbyte_cdk.sources.types import Config, Record, StreamSlice, StreamState
from airbyte_cdk.utils.mapping_helpers import _validate_multiple_request_options
from airbyte_cdk.utils.mapping_helpers import _validate_component_request_option_paths


@dataclass
Expand Down Expand Up @@ -122,8 +122,10 @@ def __post_init__(self, parameters: Mapping[str, Any]) -> None:

if not self.cursor_datetime_formats:
self.cursor_datetime_formats = [self.datetime_format]

_validate_multiple_request_options(self.config, self.start_time_option, self.end_time_option)

_validate_component_request_option_paths(
self.config, self.start_time_option, self.end_time_option
)

def get_stream_state(self) -> StreamState:
return {self.cursor_field.eval(self.config): self._cursor} if self._cursor else {} # type: ignore # cursor_field is converted to an InterpolatedString in __post_init__
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
)
from airbyte_cdk.sources.declarative.requesters.request_path import RequestPath
from airbyte_cdk.sources.types import Config, Record, StreamSlice, StreamState
from airbyte_cdk.utils.mapping_helpers import combine_mappings, _validate_multiple_request_options
from airbyte_cdk.utils.mapping_helpers import (
combine_mappings,
_validate_component_request_option_paths,
)


@dataclass
Expand Down Expand Up @@ -113,9 +116,9 @@ def __post_init__(self, parameters: Mapping[str, Any]) -> None:
)
if isinstance(self.url_base, str):
self.url_base = InterpolatedString(string=self.url_base, parameters=parameters)

if self.page_token_option and not isinstance(self.page_token_option, RequestPath):
_validate_multiple_request_options(
_validate_component_request_option_paths(
self.config,
self.page_size_option,
self.page_token_option,
Expand Down
75 changes: 60 additions & 15 deletions unit_tests/utils/test_mapping_helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import pytest

from airbyte_cdk.utils.mapping_helpers import combine_mappings, _validate_multiple_request_options, RequestOption, RequestOptionType
from airbyte_cdk.utils.mapping_helpers import (
combine_mappings,
_validate_component_request_option_paths,
RequestOption,
RequestOptionType,
)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -119,13 +124,18 @@ def test_body_json_requests(test_name, mappings, expected_result, expected_error
def mock_config() -> dict[str, str]:
return {"test": "config"}


@pytest.mark.parametrize(
"test_name, option1, option2, should_raise",
[
(
"different_fields",
RequestOption(field_name="field1", inject_into=RequestOptionType.body_json, parameters={}),
RequestOption(field_name="field2", inject_into=RequestOptionType.body_json, parameters={}),
RequestOption(
field_name="field1", inject_into=RequestOptionType.body_json, parameters={}
),
RequestOption(
field_name="field2", inject_into=RequestOptionType.body_json, parameters={}
),
False,
),
(
Expand All @@ -136,41 +146,76 @@ def mock_config() -> dict[str, str]:
),
(
"different_nested_paths",
RequestOption(field_path=["data", "query1", "limit"], inject_into=RequestOptionType.body_json, parameters={}),
RequestOption(field_path=["data", "query2", "limit"], inject_into=RequestOptionType.body_json, parameters={}),
RequestOption(
field_path=["data", "query1", "limit"],
inject_into=RequestOptionType.body_json,
parameters={},
),
RequestOption(
field_path=["data", "query2", "limit"],
inject_into=RequestOptionType.body_json,
parameters={},
),
False,
),
(
"same_nested_paths",
RequestOption(field_path=["data", "query", "limit"], inject_into=RequestOptionType.body_json, parameters={}),
RequestOption(field_path=["data", "query", "limit"], inject_into=RequestOptionType.body_json, parameters={}),
RequestOption(
field_path=["data", "query", "limit"],
inject_into=RequestOptionType.body_json,
parameters={},
),
RequestOption(
field_path=["data", "query", "limit"],
inject_into=RequestOptionType.body_json,
parameters={},
),
True,
),
(
"different_inject_types",
RequestOption(field_name="field", inject_into=RequestOptionType.header, parameters={}),
RequestOption(field_name="field", inject_into=RequestOptionType.body_json, parameters={}),
RequestOption(
field_name="field", inject_into=RequestOptionType.body_json, parameters={}
),
False,
),
]
],
)
def test_request_option_validation(test_name, option1, option2, should_raise, mock_config):
"""Test various combinations of request option validation"""
if should_raise:
with pytest.raises(ValueError, match="duplicate keys detected"):
_validate_multiple_request_options(mock_config, option1, option2)
_validate_component_request_option_paths(mock_config, option1, option2)
else:
_validate_multiple_request_options(mock_config, option1, option2)
_validate_component_request_option_paths(mock_config, option1, option2)


@pytest.mark.parametrize(
"test_name, options",
[
("none_options", [None, RequestOption(field_name="field", inject_into=RequestOptionType.header, parameters={}), None]),
("single_option", [RequestOption(field_name="field", inject_into=RequestOptionType.header, parameters={})]),
(
"none_options",
[
None,
RequestOption(
field_name="field", inject_into=RequestOptionType.header, parameters={}
),
None,
],
),
(
"single_option",
[
RequestOption(
field_name="field", inject_into=RequestOptionType.header, parameters={}
)
],
),
("all_none", [None, None, None]),
("empty_list", []),
]
],
)
def test_edge_cases(test_name, options, mock_config):
"""Test edge cases like None values and single options"""
_validate_multiple_request_options(mock_config, *options)
_validate_component_request_option_paths(mock_config, *options)

0 comments on commit fdfd154

Please sign in to comment.