From 61a9ee36c5af265d01496a24c176bdfc72cafa38 Mon Sep 17 00:00:00 2001 From: Artem Inzhyyants <36314070+artem1205@users.noreply.github.com> Date: Mon, 3 Mar 2025 12:47:05 +0100 Subject: [PATCH] feat: add microseconds timestamp format (#373) Signed-off-by: Artem Inzhyyants --- .../declarative/interpolation/macros.py | 2 + .../declarative/interpolation/test_macros.py | 53 +++++++++++++------ 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/airbyte_cdk/sources/declarative/interpolation/macros.py b/airbyte_cdk/sources/declarative/interpolation/macros.py index 62b6904c8..02982584a 100644 --- a/airbyte_cdk/sources/declarative/interpolation/macros.py +++ b/airbyte_cdk/sources/declarative/interpolation/macros.py @@ -173,6 +173,8 @@ def format_datetime( ) if format == "%s": return str(int(dt_datetime.timestamp())) + elif format == "%ms": + return str(int(dt_datetime.timestamp() * 1_000_000)) return dt_datetime.strftime(format) diff --git a/unit_tests/sources/declarative/interpolation/test_macros.py b/unit_tests/sources/declarative/interpolation/test_macros.py index c531a9811..8543cd507 100644 --- a/unit_tests/sources/declarative/interpolation/test_macros.py +++ b/unit_tests/sources/declarative/interpolation/test_macros.py @@ -30,68 +30,91 @@ def test_macros_export(test_name, fn_name, found_in_macros): @pytest.mark.parametrize( - "test_name, input_value, format, input_format, expected_output", + "input_value, format, input_format, expected_output", [ - ("test_datetime_string_to_date", "2022-01-01T01:01:01Z", "%Y-%m-%d", None, "2022-01-01"), - ("test_date_string_to_date", "2022-01-01", "%Y-%m-%d", None, "2022-01-01"), - ("test_datetime_string_to_date", "2022-01-01T00:00:00Z", "%Y-%m-%d", None, "2022-01-01"), + ("2022-01-01T01:01:01Z", "%Y-%m-%d", None, "2022-01-01"), + ("2022-01-01", "%Y-%m-%d", None, "2022-01-01"), + ("2022-01-01T00:00:00Z", "%Y-%m-%d", None, "2022-01-01"), ( - "test_datetime_with_tz_string_to_date", "2022-01-01T00:00:00Z", "%Y-%m-%d", None, "2022-01-01", ), ( - "test_datetime_string_to_datetime", "2022-01-01T01:01:01Z", "%Y-%m-%dT%H:%M:%SZ", None, "2022-01-01T01:01:01Z", ), ( - "test_datetime_string_with_tz_to_datetime", "2022-01-01T01:01:01-0800", "%Y-%m-%dT%H:%M:%SZ", None, "2022-01-01T09:01:01Z", ), ( - "test_datetime_object_tz_to_date", datetime.datetime(2022, 1, 1, 1, 1, 1), "%Y-%m-%d", None, "2022-01-01", ), ( - "test_datetime_object_tz_to_datetime", datetime.datetime(2022, 1, 1, 1, 1, 1), "%Y-%m-%dT%H:%M:%SZ", None, "2022-01-01T01:01:01Z", ), ( - "test_datetime_string_to_rfc2822_date", "Sat, 01 Jan 2022 01:01:01 +0000", "%Y-%m-%d", "%a, %d %b %Y %H:%M:%S %z", "2022-01-01", ), + ( + "2022-01-01T01:01:01Z", + "%s", + "%Y-%m-%dT%H:%M:%SZ", + "1640998861", + ), + ( + "2022-01-01T01:01:01Z", + "%ms", + "%Y-%m-%dT%H:%M:%SZ", + "1640998861000000", + ), + ], + ids=[ + "test_datetime_string_to_date", + "test_date_string_to_date", + "test_datetime_string_to_date", + "test_datetime_with_tz_string_to_date", + "test_datetime_string_to_datetime", + "test_datetime_string_with_tz_to_datetime", + "test_datetime_object_tz_to_date", + "test_datetime_object_tz_to_datetime", + "test_datetime_string_to_rfc2822_date", + "test_datetime_string_to_timestamp_in_seconds", + "test_datetime_string_to_timestamp_in_microseconds", ], ) -def test_format_datetime(test_name, input_value, format, input_format, expected_output): +def test_format_datetime(input_value, format, input_format, expected_output): format_datetime = macros["format_datetime"] assert format_datetime(input_value, format, input_format) == expected_output @pytest.mark.parametrize( - "test_name, input_value, expected_output", + "input_value, expected_output", [ - ("test_one_day", "P1D", datetime.timedelta(days=1)), - ("test_6_days_23_hours", "P6DT23H", datetime.timedelta(days=6, hours=23)), + ("P1D", datetime.timedelta(days=1)), + ("P6DT23H", datetime.timedelta(days=6, hours=23)), + ], + ids=[ + "test_one_day", + "test_6_days_23_hours", ], ) -def test_duration(test_name, input_value, expected_output): +def test_duration(input_value: str, expected_output: datetime.timedelta): duration_fn = macros["duration"] assert duration_fn(input_value) == expected_output