Skip to content

Commit 53ab5e5

Browse files
committed
Duration to ISO8601 tests.
1 parent 2fde995 commit 53ab5e5

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

tests/duration/test_to_iso8601.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import pendulum
2+
3+
4+
def test_all():
5+
d = pendulum.duration(years=2, months=3, days=4, hours=5, minutes=6, seconds=7, microseconds=50)
6+
7+
expected = "P2Y3M4DT5H6M7.00005S"
8+
assert d.to_iso8601_string() == expected
9+
10+
11+
def test_basic():
12+
d = pendulum.Duration(years=2, months=3, days=4, hours=5, minutes=6, seconds=7)
13+
14+
expected = "P2Y3M4DT5H6M7S"
15+
assert d.to_iso8601_string() == expected
16+
17+
18+
def test_microsecond_alone():
19+
d = pendulum.duration(microseconds=5)
20+
21+
expected = "PT0.000005S"
22+
assert d.to_iso8601_string() == expected
23+
24+
25+
def test_microsecond_trailing_zeros():
26+
d = pendulum.duration(microseconds=500)
27+
28+
expected = "PT0.0005S"
29+
assert d.to_iso8601_string() == expected
30+
31+
32+
def test_second_and_microsecond():
33+
d = pendulum.duration(seconds=50, microseconds=5)
34+
35+
expected = "PT50.000005S"
36+
assert d.to_iso8601_string() == expected
37+
38+
39+
def test_lots_of_days():
40+
d = pendulum.duration(days=500)
41+
# should not be coverted to months, years as info is lost
42+
43+
expected = "P500D"
44+
assert d.to_iso8601_string() == expected
45+
46+
d = pendulum.duration(days=40)
47+
48+
expected = "P40D"
49+
assert d.to_iso8601_string() == expected
50+
51+
52+
def test_lots_of_hours():
53+
# NOTE: this will fail until total_seconds normalization
54+
# no longer occurs
55+
d = pendulum.duration(hours=36)
56+
# should not be coverted to days, as can be different
57+
# depending on daylight savings changes
58+
59+
expected = "PT36H"
60+
assert d.to_iso8601_string() == expected
61+
62+
63+
def test_days_and_months():
64+
d = pendulum.duration(months=1, days=40)
65+
# not equivalent to P2M10D
66+
67+
expected = "P1M40D"
68+
assert d.to_iso8601_string() == expected
69+
70+
71+
def test_weeks_alone():
72+
d = pendulum.duration(days=21)
73+
74+
# Could also validly be P21D
75+
expected = "P3W"
76+
assert d.to_iso8601_string() == expected
77+
78+
79+
def test_weeks_and_other():
80+
d = pendulum.duration(years=2, days=21)
81+
82+
expected = "P2Y21D"
83+
assert d.to_iso8601_string() == expected
84+
85+
86+
def test_weeks_and_time():
87+
d = pendulum.duration(days=21, minutes=7)
88+
89+
expected = "P21DT7M"
90+
assert d.to_iso8601_string() == expected
91+
92+
93+
def test_empty():
94+
# NOTE: can't validly test this in isolation,
95+
# as "P0D" and "PT0S" etc. are equally valid
96+
# ISO8601 representations
97+
d = pendulum.duration()
98+
s = d.to_iso8601_string()
99+
# should be something like "PT0S" or "P0D"
100+
parsed = pendulum.parse(s)
101+
102+
assert parsed.years == 0
103+
assert parsed.months == 0
104+
assert parsed.weeks == 0
105+
assert parsed.remaining_days == 0
106+
assert parsed.hours == 0
107+
assert parsed.minutes == 0
108+
assert parsed.remaining_seconds == 0
109+
assert parsed.microseconds == 0
110+

0 commit comments

Comments
 (0)