-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathtest_datetime_parser.py
More file actions
161 lines (148 loc) · 5.13 KB
/
Copy pathtest_datetime_parser.py
File metadata and controls
161 lines (148 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#
# Copyright (c) 2023 Airbyte, Inc., all rights reserved.
#
import datetime
import pytest
from airbyte_cdk.models import FailureType
from airbyte_cdk.sources.declarative.datetime.datetime_parser import (
DatetimeFormatMismatchError,
DatetimeParser,
)
@pytest.mark.parametrize(
"input_date, date_format, expected_output_date",
[
(
"2021-01-01T00:00:00.000000+0000",
"%Y-%m-%dT%H:%M:%S.%f%z",
datetime.datetime(2021, 1, 1, 0, 0, tzinfo=datetime.timezone.utc),
),
(
"2021-01-01T00:00:00.000000+0400",
"%Y-%m-%dT%H:%M:%S.%f%z",
datetime.datetime(
2021, 1, 1, 0, 0, tzinfo=datetime.timezone(datetime.timedelta(seconds=14400))
),
),
(
"1609459200",
"%s",
datetime.datetime(2021, 1, 1, 0, 0, tzinfo=datetime.timezone.utc),
),
(
"1675092508.873709",
"%s_as_float",
datetime.datetime(2023, 1, 30, 15, 28, 28, 873709, tzinfo=datetime.timezone.utc),
),
(
"1675092508873709",
"%epoch_microseconds",
datetime.datetime(2023, 1, 30, 15, 28, 28, 873709, tzinfo=datetime.timezone.utc),
),
(
"1609459200001",
"%ms",
datetime.datetime(2021, 1, 1, 0, 0, 0, 1000, tzinfo=datetime.timezone.utc),
),
(
"20210101",
"%Y%m%d",
datetime.datetime(2021, 1, 1, 0, 0, tzinfo=datetime.timezone.utc),
),
(
"2021-11-22T08:41:55.640Z",
"%Y-%m-%dT%H:%M:%S.%_msZ",
datetime.datetime(2021, 11, 22, 8, 41, 55, 640000, tzinfo=datetime.timezone.utc),
),
],
ids=[
"test_parse_date_iso",
"test_parse_date_iso_with_timezone_not_utc",
"test_parse_timestamp",
"test_parse_timestamp_as_float",
"test_parse_timestamp_microseconds",
"test_parse_ms_timestamp",
"test_parse_date_ms",
"test_parse_format_datetime_with__ms",
],
)
def test_parse_date(input_date: str, date_format: str, expected_output_date: datetime.datetime):
parser = DatetimeParser()
output_date = parser.parse(input_date, date_format)
assert output_date == expected_output_date
@pytest.mark.parametrize(
"input_dt, datetimeformat, expected_output",
[
(
datetime.datetime(2021, 1, 1, 0, 0, tzinfo=datetime.timezone.utc),
"%s",
"1609459200",
),
(
datetime.datetime(2021, 1, 1, 0, 0, 0, 1000, tzinfo=datetime.timezone.utc),
"%ms",
"1609459200001",
),
(
datetime.datetime(2023, 1, 30, 15, 28, 28, 873709, tzinfo=datetime.timezone.utc),
"%s_as_float",
"1675092508.873709",
),
(
datetime.datetime(2023, 1, 30, 15, 28, 28, 873709, tzinfo=datetime.timezone.utc),
"%epoch_microseconds",
"1675092508873709",
),
(
datetime.datetime(2021, 1, 1, 0, 0, tzinfo=datetime.timezone.utc),
"%Y-%m-%d",
"2021-01-01",
),
(
datetime.datetime(2021, 1, 1, 0, 0, tzinfo=datetime.timezone.utc),
"%Y%m%d",
"20210101",
),
(
datetime.datetime(2021, 11, 22, 8, 41, 55, 640000, tzinfo=datetime.timezone.utc),
"%Y-%m-%dT%H:%M:%S.%_msZ",
"2021-11-22T08:41:55.640Z",
),
],
ids=[
"test_format_timestamp",
"test_format_timestamp_ms",
"test_format_timestamp_as_float",
"test_format_timestamp_microseconds",
"test_format_string",
"test_format_to_number",
"test_parse_format_datetime_with__ms",
],
)
def test_format_datetime(input_dt: datetime.datetime, datetimeformat: str, expected_output: str):
parser = DatetimeParser()
output_date = parser.format(input_dt, datetimeformat)
assert output_date == expected_output
def test_given_value_not_matching_format_when_parse_then_raise_traced_error():
with pytest.raises(DatetimeFormatMismatchError) as exc_info:
DatetimeParser().parse("2025-06-28T18:35:16Z", "%Y-%m-%dT%H:%M:%S.%fZ")
assert isinstance(exc_info.value, ValueError)
assert (
exc_info.value.message == "Datetime value matches none of the configured datetime formats."
)
assert exc_info.value.failure_type == FailureType.system_error
assert (
exc_info.value.internal_message
== "No format in ['%Y-%m-%dT%H:%M:%S.%fZ'] matching 2025-06-28T18:35:16Z"
)
def test_given_context_when_with_context_then_message_names_stream_and_cursor_field():
error = DatetimeFormatMismatchError(
value="2025-06-28T18:35:16Z", formats=["%Y-%m-%dT%H:%M:%S.%fZ"]
).with_context(cursor_field="updated_at", stream_name="items")
assert error.message == (
'Cursor field "updated_at" of stream "items" matches none of the configured datetime '
"formats."
)
assert (
error.internal_message
== "No format in ['%Y-%m-%dT%H:%M:%S.%fZ'] matching 2025-06-28T18:35:16Z"
)