-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathtest_date.py
More file actions
72 lines (58 loc) · 2.05 KB
/
test_date.py
File metadata and controls
72 lines (58 loc) · 2.05 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
"""Unit tests for date transforms."""
from datetime import datetime
from airbyte_cdk.utils.transforms.date import (
try_parse_date,
extract_date_parts,
floor_to_month,
ceil_to_month,
)
def test_try_parse_date():
"""Test date parsing function."""
# Test with datetime object
dt = datetime(2023, 1, 15)
assert try_parse_date(dt) == dt
# Test with non-date object
assert try_parse_date("2023-01-15") is None
assert try_parse_date(123) is None
assert try_parse_date(None) is None
def test_extract_date_parts():
"""Test date parts extraction function."""
# Test with valid datetime
dt = datetime(2023, 1, 15) # Sunday
parts = extract_date_parts(dt)
assert parts["year"] == 2023
assert parts["month"] == 1
assert parts["day"] == 15
assert parts["dow"] == 6 # Sunday is 6
# Test with invalid input
parts = extract_date_parts(None)
assert all(v is None for v in parts.values())
parts = extract_date_parts("not a date")
assert all(v is None for v in parts.values())
def test_floor_to_month():
"""Test floor to month function."""
# Test normal cases
dt = datetime(2023, 1, 15)
assert floor_to_month(dt) == datetime(2023, 1, 1)
dt = datetime(2023, 12, 31)
assert floor_to_month(dt) == datetime(2023, 12, 1)
# Test first day of month
dt = datetime(2023, 1, 1)
assert floor_to_month(dt) == dt
# Test with invalid input
assert floor_to_month(None) is None
assert floor_to_month("not a date") is None
def test_ceil_to_month():
"""Test ceil to month function."""
# Test normal cases
dt = datetime(2023, 1, 15)
assert ceil_to_month(dt) == datetime(2023, 2, 1)
# Test end of year
dt = datetime(2023, 12, 15)
assert ceil_to_month(dt) == datetime(2024, 1, 1)
# Test first day of month
dt = datetime(2023, 1, 1)
assert ceil_to_month(dt) == datetime(2023, 2, 1)
# Test with invalid input
assert ceil_to_month(None) is None
assert ceil_to_month("not a date") is None