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