-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathtest_datetime_parser.py
More file actions
127 lines (120 loc) · 3.92 KB
/
test_datetime_parser.py
File metadata and controls
127 lines (120 loc) · 3.92 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
#
# Copyright (c) 2023 Airbyte, Inc., all rights reserved.
#
import datetime
import pytest
from airbyte_cdk.sources.declarative.datetime.datetime_parser import 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