Skip to content

Commit 7a693a2

Browse files
committed
Added unit tests
1 parent d997cf0 commit 7a693a2

File tree

1 file changed

+142
-0
lines changed

1 file changed

+142
-0
lines changed

unit_tests/sources/declarative/test_manifest_declarative_source.py

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1901,3 +1901,145 @@ def validate_refs(yaml_file: str) -> List[str]:
19011901
/ "airbyte_cdk/sources/declarative/declarative_component_schema.yaml"
19021902
)
19031903
assert not validate_refs(yaml_file_path)
1904+
1905+
1906+
@pytest.mark.parametrize(
1907+
"test_name, manifest, pages, expected_states_qty",
1908+
[
1909+
(
1910+
"test_with_pagination_and_partition_router",
1911+
{
1912+
"version": "0.34.2",
1913+
"type": "DeclarativeSource",
1914+
"check": {"type": "CheckStream", "stream_names": ["Rates"]},
1915+
"streams": [
1916+
{
1917+
"type": "DeclarativeStream",
1918+
"name": "Rates",
1919+
"primary_key": [],
1920+
"schema_loader": {
1921+
"type": "InlineSchemaLoader",
1922+
"schema": {
1923+
"$schema": "http://json-schema.org/schema#",
1924+
"properties": {
1925+
"ABC": {"type": "number"},
1926+
"AED": {"type": "number"},
1927+
"partition": {"type": "number"},
1928+
},
1929+
"type": "object",
1930+
},
1931+
},
1932+
"retriever": {
1933+
"type": "SimpleRetriever",
1934+
"requester": {
1935+
"type": "HttpRequester",
1936+
"url_base": "https://api.apilayer.com",
1937+
"path": "/exchangerates_data/latest",
1938+
"http_method": "GET",
1939+
"request_parameters": {},
1940+
"request_headers": {},
1941+
"request_body_json": {},
1942+
"authenticator": {
1943+
"type": "ApiKeyAuthenticator",
1944+
"header": "apikey",
1945+
"api_token": "{{ config['api_key'] }}",
1946+
},
1947+
},
1948+
"partition_router": {
1949+
"type": "ListPartitionRouter",
1950+
"values": ["0", "1"],
1951+
"cursor_field": "partition",
1952+
},
1953+
"record_selector": {
1954+
"type": "RecordSelector",
1955+
"extractor": {"type": "DpathExtractor", "field_path": ["rates"]},
1956+
},
1957+
"paginator": {
1958+
"type": "DefaultPaginator",
1959+
"page_size": 2,
1960+
"page_size_option": {
1961+
"inject_into": "request_parameter",
1962+
"field_name": "page_size",
1963+
},
1964+
"page_token_option": {"inject_into": "path", "type": "RequestPath"},
1965+
"pagination_strategy": {
1966+
"type": "CursorPagination",
1967+
"cursor_value": "{{ response._metadata.next }}",
1968+
"page_size": 2,
1969+
},
1970+
},
1971+
},
1972+
"incremental_sync": {
1973+
"type": "DatetimeBasedCursor",
1974+
"cursor_datetime_formats": ["%Y-%m-%dT%H:%M:%S.%fZ"],
1975+
"datetime_format": "%Y-%m-%dT%H:%M:%S.%fZ",
1976+
"cursor_field": "updated_at",
1977+
"start_datetime": {
1978+
"datetime": "{{ config.get('start_date', '2020-10-16T00:00:00.000Z') }}"
1979+
},
1980+
},
1981+
}
1982+
],
1983+
"spec": {
1984+
"connection_specification": {
1985+
"$schema": "http://json-schema.org/draft-07/schema#",
1986+
"type": "object",
1987+
"required": ["api_key"],
1988+
"properties": {
1989+
"api_key": {
1990+
"type": "string",
1991+
"title": "API Key",
1992+
"airbyte_secret": True,
1993+
},
1994+
"start_date": {
1995+
"title": "Start Date",
1996+
"description": "UTC date and time in the format YYYY-MM-DDTHH:MM:SS.000Z. During incremental sync, any data generated before this date will not be replicated. If left blank, the start date will be set to 2 years before the present date.",
1997+
"pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$",
1998+
"pattern_descriptor": "YYYY-MM-DDTHH:MM:SS.000Z",
1999+
"examples": ["2020-11-16T00:00:00.000Z"],
2000+
"type": "string",
2001+
"format": "date-time",
2002+
},
2003+
},
2004+
"additionalProperties": True,
2005+
},
2006+
"documentation_url": "https://example.org",
2007+
"type": "Spec",
2008+
},
2009+
},
2010+
(
2011+
_create_page(
2012+
{
2013+
"rates": [
2014+
{"ABC": 0, "partition": 0, "updated_at": "2020-11-16T00:00:00.000Z"},
2015+
{"AED": 1, "partition": 0, "updated_at": "2020-11-16T00:00:00.000Z"},
2016+
],
2017+
"_metadata": {"next": "next"},
2018+
}
2019+
),
2020+
_create_page(
2021+
{
2022+
"rates": [
2023+
{"USD": 3, "partition": 0, "updated_at": "2020-11-16T00:00:00.000Z"}
2024+
],
2025+
"_metadata": {},
2026+
}
2027+
),
2028+
_create_page(
2029+
{
2030+
"rates": [
2031+
{"ABC": 2, "partition": 1, "updated_at": "2020-11-16T00:00:00.000Z"}
2032+
],
2033+
"_metadata": {},
2034+
}
2035+
),
2036+
),
2037+
2,
2038+
),
2039+
],
2040+
)
2041+
def test_slice_checkpoint(test_name, manifest, pages, expected_states_qty):
2042+
_stream_name = "Rates"
2043+
with patch.object(SimpleRetriever, "_fetch_next_page", side_effect=pages):
2044+
states = [message.state for message in _run_read(manifest, _stream_name) if message.state]
2045+
assert len(states) == expected_states_qty

0 commit comments

Comments
 (0)