diff options
Diffstat (limited to 'tests/time')
-rw-r--r-- | tests/time/__init__.py | 0 | ||||
-rw-r--r-- | tests/time/test_add.py | 78 | ||||
-rw-r--r-- | tests/time/test_behavior.py | 49 | ||||
-rw-r--r-- | tests/time/test_comparison.py | 185 | ||||
-rw-r--r-- | tests/time/test_construct.py | 22 | ||||
-rw-r--r-- | tests/time/test_diff.py | 350 | ||||
-rw-r--r-- | tests/time/test_fluent_setters.py | 12 | ||||
-rw-r--r-- | tests/time/test_strings.py | 39 | ||||
-rw-r--r-- | tests/time/test_sub.py | 112 |
9 files changed, 847 insertions, 0 deletions
diff --git a/tests/time/__init__.py b/tests/time/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/time/__init__.py diff --git a/tests/time/test_add.py b/tests/time/test_add.py new file mode 100644 index 0000000..7075ebe --- /dev/null +++ b/tests/time/test_add.py @@ -0,0 +1,78 @@ +from __future__ import annotations + +from datetime import timedelta + +import pytest + +import pendulum + + +def test_add_hours_positive(): + assert pendulum.time(12, 34, 56).add(hours=1).hour == 13 + + +def test_add_hours_zero(): + assert pendulum.time(12, 34, 56).add(hours=0).hour == 12 + + +def test_add_hours_negative(): + assert pendulum.time(12, 34, 56).add(hours=-1).hour == 11 + + +def test_add_minutes_positive(): + assert pendulum.time(12, 34, 56).add(minutes=1).minute == 35 + + +def test_add_minutes_zero(): + assert pendulum.time(12, 34, 56).add(minutes=0).minute == 34 + + +def test_add_minutes_negative(): + assert pendulum.time(12, 34, 56).add(minutes=-1).minute == 33 + + +def test_add_seconds_positive(): + assert pendulum.time(12, 34, 56).add(seconds=1).second == 57 + + +def test_add_seconds_zero(): + assert pendulum.time(12, 34, 56).add(seconds=0).second == 56 + + +def test_add_seconds_negative(): + assert pendulum.time(12, 34, 56).add(seconds=-1).second == 55 + + +def test_add_timedelta(): + delta = timedelta(seconds=45, microseconds=123456) + d = pendulum.time(3, 12, 15, 654321) + + d = d.add_timedelta(delta) + assert d.minute == 13 + assert d.second == 0 + assert d.microsecond == 777777 + + d = pendulum.time(3, 12, 15, 654321) + + d = d + delta + assert d.minute == 13 + assert d.second == 0 + assert d.microsecond == 777777 + + +def test_add_timedelta_with_days(): + delta = timedelta(days=3, seconds=45, microseconds=123456) + d = pendulum.time(3, 12, 15, 654321) + + with pytest.raises(TypeError): + d.add_timedelta(delta) + + +def test_addition_invalid_type(): + d = pendulum.time(3, 12, 15, 654321) + + with pytest.raises(TypeError): + d + 3 + + with pytest.raises(TypeError): + 3 + d diff --git a/tests/time/test_behavior.py b/tests/time/test_behavior.py new file mode 100644 index 0000000..0071c94 --- /dev/null +++ b/tests/time/test_behavior.py @@ -0,0 +1,49 @@ +from __future__ import annotations + +import pickle + +from datetime import time + +import pytest + +import pendulum + +from pendulum import Time + + +@pytest.fixture() +def p(): + return pendulum.Time(12, 34, 56, 123456, tzinfo=pendulum.timezone("Europe/Paris")) + + +@pytest.fixture() +def d(): + return time(12, 34, 56, 123456, tzinfo=pendulum.timezone("Europe/Paris")) + + +def test_hash(p, d): + assert hash(d) == hash(p) + dt1 = Time(12, 34, 57, 123456) + + assert hash(p) != hash(dt1) + + +def test_pickle(): + dt1 = Time(12, 34, 56, 123456) + s = pickle.dumps(dt1) + dt2 = pickle.loads(s) + + assert dt2 == dt1 + + +def test_utcoffset(p, d): + assert d.utcoffset() == p.utcoffset() + + +def test_dst(p, d): + assert d.dst() == p.dst() + + +def test_tzname(p, d): + assert d.tzname() == p.tzname() + assert Time(12, 34, 56, 123456).tzname() == time(12, 34, 56, 123456).tzname() diff --git a/tests/time/test_comparison.py b/tests/time/test_comparison.py new file mode 100644 index 0000000..f1ef275 --- /dev/null +++ b/tests/time/test_comparison.py @@ -0,0 +1,185 @@ +from __future__ import annotations + +from datetime import time + +import pendulum + +from tests.conftest import assert_time + + +def test_equal_to_true(): + t1 = pendulum.time(1, 2, 3) + t2 = pendulum.time(1, 2, 3) + t3 = time(1, 2, 3) + + assert t1 == t2 + assert t1 == t3 + + +def test_equal_to_false(): + t1 = pendulum.time(1, 2, 3) + t2 = pendulum.time(1, 2, 4) + t3 = time(1, 2, 4) + + assert t1 != t2 + assert t1 != t3 + + +def test_not_equal_to_none(): + t1 = pendulum.time(1, 2, 3) + + assert t1 != None # noqa + + +def test_greater_than_true(): + t1 = pendulum.time(1, 2, 3) + t2 = pendulum.time(1, 2, 2) + t3 = time(1, 2, 2) + + assert t1 > t2 + assert t1 > t3 + + +def test_greater_than_false(): + t1 = pendulum.time(1, 2, 2) + t2 = pendulum.time(1, 2, 3) + t3 = time(1, 2, 3) + + assert not t1 > t2 + assert not t1 > t3 + + +def test_greater_than_or_equal_true(): + t1 = pendulum.time(1, 2, 3) + t2 = pendulum.time(1, 2, 2) + t3 = time(1, 2, 2) + + assert t1 >= t2 + assert t1 >= t3 + + +def test_greater_than_or_equal_true_equal(): + t1 = pendulum.time(1, 2, 3) + t2 = pendulum.time(1, 2, 3) + t3 = time(1, 2, 3) + + assert t1 >= t2 + assert t1 >= t3 + + +def test_greater_than_or_equal_false(): + t1 = pendulum.time(1, 2, 2) + t2 = pendulum.time(1, 2, 3) + t3 = time(1, 2, 3) + + assert not t1 >= t2 + assert not t1 >= t3 + + +def test_less_than_true(): + t1 = pendulum.time(1, 2, 2) + t2 = pendulum.time(1, 2, 3) + t3 = time(1, 2, 3) + + assert t1 < t2 + assert t1 < t3 + + +def test_less_than_false(): + t1 = pendulum.time(1, 2, 3) + t2 = pendulum.time(1, 2, 2) + t3 = time(1, 2, 2) + + assert not t1 < t2 + assert not t1 < t3 + + +def test_less_than_or_equal_true(): + t1 = pendulum.time(1, 2, 2) + t2 = pendulum.time(1, 2, 3) + t3 = time(1, 2, 3) + + assert t1 <= t2 + assert t1 <= t3 + + +def test_less_than_or_equal_true_equal(): + t1 = pendulum.time(1, 2, 3) + t2 = pendulum.time(1, 2, 3) + t3 = time(1, 2, 3) + + assert t1 <= t2 + assert t1 <= t3 + + +def test_less_than_or_equal_false(): + t1 = pendulum.time(1, 2, 3) + t2 = pendulum.time(1, 2, 2) + t3 = time(1, 2, 2) + + assert not t1 <= t2 + assert not t1 <= t3 + + +def test_closest(): + instance = pendulum.time(12, 34, 56) + t1 = pendulum.time(12, 34, 54) + t2 = pendulum.time(12, 34, 59) + closest = instance.closest(t1, t2) + assert t1 == closest + + closest = instance.closest(t2, t1) + assert t1 == closest + + +def test_closest_with_time(): + instance = pendulum.time(12, 34, 56) + t1 = pendulum.time(12, 34, 54) + t2 = pendulum.time(12, 34, 59) + closest = instance.closest(t1, t2) + + assert_time(closest, 12, 34, 54) + + +def test_closest_with_equals(): + instance = pendulum.time(12, 34, 56) + t1 = pendulum.time(12, 34, 56) + t2 = pendulum.time(12, 34, 59) + closest = instance.closest(t1, t2) + assert t1 == closest + + +def test_farthest(): + instance = pendulum.time(12, 34, 56) + t1 = pendulum.time(12, 34, 54) + t2 = pendulum.time(12, 34, 59) + farthest = instance.farthest(t1, t2) + assert t2 == farthest + + farthest = instance.farthest(t2, t1) + assert t2 == farthest + + +def test_farthest_with_time(): + instance = pendulum.time(12, 34, 56) + t1 = pendulum.time(12, 34, 54) + t2 = pendulum.time(12, 34, 59) + farthest = instance.farthest(t1, t2) + + assert_time(farthest, 12, 34, 59) + + +def test_farthest_with_equals(): + instance = pendulum.time(12, 34, 56) + t1 = pendulum.time(12, 34, 56) + t2 = pendulum.time(12, 34, 59) + + farthest = instance.farthest(t1, t2) + assert t2 == farthest + + +def test_comparison_to_unsupported(): + t1 = pendulum.now().time() + + assert t1 != "test" + assert t1 not in ["test"] diff --git a/tests/time/test_construct.py b/tests/time/test_construct.py new file mode 100644 index 0000000..7d81b37 --- /dev/null +++ b/tests/time/test_construct.py @@ -0,0 +1,22 @@ +from __future__ import annotations + +import pendulum + +from tests.conftest import assert_time + + +def test_init(): + t = pendulum.time(12, 34, 56, 123456) + + assert_time(t, 12, 34, 56, 123456) + + +def test_init_with_missing_values(): + t = pendulum.time(12, 34, 56) + assert_time(t, 12, 34, 56, 0) + + t = pendulum.time(12, 34) + assert_time(t, 12, 34, 0, 0) + + t = pendulum.time(12) + assert_time(t, 12, 0, 0, 0) diff --git a/tests/time/test_diff.py b/tests/time/test_diff.py new file mode 100644 index 0000000..629a058 --- /dev/null +++ b/tests/time/test_diff.py @@ -0,0 +1,350 @@ +from __future__ import annotations + +import pendulum + +from pendulum import Time + + +def test_diff_in_hours_positive(): + dt = Time(12, 34, 56) + assert dt.diff(dt.add(hours=2).add(seconds=3672)).in_hours() == 3 + + +def test_diff_in_hours_negative_with_sign(): + dt = Time(12, 34, 56) + assert dt.diff(dt.subtract(hours=2).add(seconds=3600), False).in_hours() == -1 + + +def test_diff_in_hours_negative_no_sign(): + dt = Time(12, 34, 56) + assert dt.diff(dt.subtract(hours=2).add(seconds=3600)).in_hours() == 1 + + +def test_diff_in_hours_vs_default_now(): + with pendulum.travel_to(pendulum.today().at(12, 34, 56)): + now = pendulum.now().time() + + assert now.subtract(hours=2).diff().in_hours() == 2 + + +def test_diff_in_hours_ensure_is_truncated(): + dt = Time(12, 34, 56) + assert dt.diff(dt.add(hours=2).add(seconds=5401)).in_hours() == 3 + + +def test_diff_in_minutes_positive(): + dt = Time(12, 34, 56) + assert dt.diff(dt.add(hours=1).add(minutes=2)).in_minutes() == 62 + + +def test_diff_in_minutes_positive_big(): + dt = Time(12, 34, 56) + assert dt.diff(dt.add(hours=25).add(minutes=2)).in_minutes() == 62 + + +def test_diff_in_minutes_negative_with_sign(): + dt = Time(12, 34, 56) + assert dt.diff(dt.subtract(hours=1).add(minutes=2), False).in_minutes() == -58 + + +def test_diff_in_minutes_negative_no_sign(): + dt = Time(12, 34, 56) + assert dt.diff(dt.subtract(hours=1).add(minutes=2)).in_minutes() == 58 + + +def test_diff_in_minutes_vs_default_now(): + with pendulum.travel_to(pendulum.today().at(12, 34, 56)): + now = pendulum.now().time() + + assert now.subtract(hours=1).diff().in_minutes() == 60 + + +def test_diff_in_minutes_ensure_is_truncated(): + dt = Time(12, 34, 56) + assert dt.diff(dt.add(minutes=1).add(seconds=59)).in_minutes() == 1 + + +def test_diff_in_seconds_positive(): + dt = Time(12, 34, 56) + assert dt.diff(dt.add(minutes=1).add(seconds=2)).in_seconds() == 62 + + +def test_diff_in_seconds_positive_big(): + dt = Time(12, 34, 56) + assert dt.diff(dt.add(hours=2).add(seconds=2)).in_seconds() == 7202 + + +def test_diff_in_seconds_negative_with_sign(): + dt = Time(12, 34, 56) + assert dt.diff(dt.subtract(minutes=1).add(seconds=2), False).in_seconds() == -58 + + +def test_diff_in_seconds_negative_no_sign(): + dt = Time(12, 34, 56) + assert dt.diff(dt.subtract(minutes=1).add(seconds=2)).in_seconds() == 58 + + +def test_diff_in_seconds_vs_default_now(): + with pendulum.travel_to(pendulum.today().at(12, 34, 56)): + now = pendulum.now().time() + + assert now.subtract(hours=1).diff().in_seconds() == 3600 + + +def test_diff_in_seconds_ensure_is_truncated(): + dt = Time(12, 34, 56) + assert dt.diff(dt.add(seconds=1.9)).in_seconds() == 1 + + +def test_diff_for_humans_now_and_second(): + with pendulum.travel_to(pendulum.today().at(12, 34, 56)): + now = pendulum.now().time() + + assert now.diff_for_humans() == "a few seconds ago" + + +def test_diff_for_humans_now_and_seconds(): + with pendulum.travel_to(pendulum.today().at(12, 34, 56)): + now = pendulum.now().time() + + assert now.subtract(seconds=2).diff_for_humans() == "a few seconds ago" + + +def test_diff_for_humans_now_and_nearly_minute(): + with pendulum.travel_to(pendulum.today().at(12, 34, 56)): + now = pendulum.now().time() + + assert now.subtract(seconds=59).diff_for_humans() == "59 seconds ago" + + +def test_diff_for_humans_now_and_minute(): + with pendulum.travel_to(pendulum.today().at(12, 34, 56)): + now = pendulum.now().time() + + assert now.subtract(minutes=1).diff_for_humans() == "1 minute ago" + + +def test_diff_for_humans_now_and_minutes(): + with pendulum.travel_to(pendulum.today().at(12, 34, 56)): + now = pendulum.now().time() + + assert now.subtract(minutes=2).diff_for_humans() == "2 minutes ago" + + +def test_diff_for_humans_now_and_nearly_hour(): + with pendulum.travel_to(pendulum.today().at(12, 34, 56)): + now = pendulum.now().time() + + assert now.subtract(minutes=59).diff_for_humans() == "59 minutes ago" + + +def test_diff_for_humans_now_and_hour(): + with pendulum.travel_to(pendulum.today().at(12, 34, 56)): + now = pendulum.now().time() + + assert now.subtract(hours=1).diff_for_humans() == "1 hour ago" + + +def test_diff_for_humans_now_and_hours(): + with pendulum.travel_to(pendulum.today().at(12, 34, 56)): + now = pendulum.now().time() + + assert now.subtract(hours=2).diff_for_humans() == "2 hours ago" + + +def test_diff_for_humans_now_and_future_second(): + with pendulum.travel_to(pendulum.today().at(12, 34, 56)): + now = pendulum.now().time() + + assert now.add(seconds=1).diff_for_humans() == "in a few seconds" + + +def test_diff_for_humans_now_and_future_seconds(): + with pendulum.travel_to(pendulum.today().at(12, 34, 56)): + now = pendulum.now().time() + + assert now.add(seconds=2).diff_for_humans() == "in a few seconds" + + +def test_diff_for_humans_now_and_nearly_future_minute(): + with pendulum.travel_to(pendulum.today().at(12, 34, 56)): + now = pendulum.now().time() + + assert now.add(seconds=59).diff_for_humans() == "in 59 seconds" + + +def test_diff_for_humans_now_and_future_minute(): + with pendulum.travel_to(pendulum.today().at(12, 34, 56)): + now = pendulum.now().time() + + assert now.add(minutes=1).diff_for_humans() == "in 1 minute" + + +def test_diff_for_humans_now_and_future_minutes(): + with pendulum.travel_to(pendulum.today().at(12, 34, 56)): + now = pendulum.now().time() + + assert now.add(minutes=2).diff_for_humans() == "in 2 minutes" + + +def test_diff_for_humans_now_and_nearly_future_hour(): + with pendulum.travel_to(pendulum.today().at(12, 34, 56)): + now = pendulum.now().time() + + assert now.add(minutes=59).diff_for_humans() == "in 59 minutes" + + +def test_diff_for_humans_now_and_future_hour(): + with pendulum.travel_to(pendulum.today().at(12, 34, 56)): + now = pendulum.now().time() + + assert now.add(hours=1).diff_for_humans() == "in 1 hour" + + +def test_diff_for_humans_now_and_future_hours(): + with pendulum.travel_to(pendulum.today().at(12, 34, 56)): + now = pendulum.now().time() + + assert now.add(hours=2).diff_for_humans() == "in 2 hours" + + +def test_diff_for_humans_other_and_second(): + with pendulum.travel_to(pendulum.today().at(12, 34, 56)): + now = pendulum.now().time() + + assert now.diff_for_humans(now.add(seconds=1)) == "a few seconds before" + + +def test_diff_for_humans_other_and_seconds(): + with pendulum.travel_to(pendulum.today().at(12, 34, 56)): + now = pendulum.now().time() + + assert now.diff_for_humans(now.add(seconds=2)) == "a few seconds before" + + +def test_diff_for_humans_other_and_nearly_minute(): + with pendulum.travel_to(pendulum.today().at(12, 34, 56)): + now = pendulum.now().time() + + assert now.diff_for_humans(now.add(seconds=59)) == "59 seconds before" + + +def test_diff_for_humans_other_and_minute(): + with pendulum.travel_to(pendulum.today().at(12, 34, 56)): + now = pendulum.now().time() + + assert now.diff_for_humans(now.add(minutes=1)) == "1 minute before" + + +def test_diff_for_humans_other_and_minutes(): + with pendulum.travel_to(pendulum.today().at(12, 34, 56)): + now = pendulum.now().time() + + assert now.diff_for_humans(now.add(minutes=2)) == "2 minutes before" + + +def test_diff_for_humans_other_and_nearly_hour(): + with pendulum.travel_to(pendulum.today().at(12, 34, 56)): + now = pendulum.now().time() + + assert now.diff_for_humans(now.add(minutes=59)) == "59 minutes before" + + +def test_diff_for_humans_other_and_hour(): + with pendulum.travel_to(pendulum.today().at(12, 34, 56)): + now = pendulum.now().time() + + assert now.diff_for_humans(now.add(hours=1)) == "1 hour before" + + +def test_diff_for_humans_other_and_hours(): + with pendulum.travel_to(pendulum.today().at(12, 34, 56)): + now = pendulum.now().time() + + assert now.diff_for_humans(now.add(hours=2)) == "2 hours before" + + +def test_diff_for_humans_other_and_future_second(): + with pendulum.travel_to(pendulum.today().at(12, 34, 56)): + now = pendulum.now().time() + + assert now.diff_for_humans(now.subtract(seconds=1)) == "a few seconds after" + + +def test_diff_for_humans_other_and_future_seconds(): + with pendulum.travel_to(pendulum.today().at(12, 34, 56)): + now = pendulum.now().time() + + assert now.diff_for_humans(now.subtract(seconds=2)) == "a few seconds after" + + +def test_diff_for_humans_other_and_nearly_future_minute(): + with pendulum.travel_to(pendulum.today().at(12, 34, 56)): + now = pendulum.now().time() + + assert now.diff_for_humans(now.subtract(seconds=59)) == "59 seconds after" + + +def test_diff_for_humans_other_and_future_minute(): + with pendulum.travel_to(pendulum.today().at(12, 34, 56)): + now = pendulum.now().time() + + assert now.diff_for_humans(now.subtract(minutes=1)) == "1 minute after" + + +def test_diff_for_humans_other_and_future_minutes(): + with pendulum.travel_to(pendulum.today().at(12, 34, 56)): + now = pendulum.now().time() + + assert now.diff_for_humans(now.subtract(minutes=2)) == "2 minutes after" + + +def test_diff_for_humans_other_and_nearly_future_hour(): + with pendulum.travel_to(pendulum.today().at(12, 34, 56)): + now = pendulum.now().time() + + assert now.diff_for_humans(now.subtract(minutes=59)) == "59 minutes after" + + +def test_diff_for_humans_other_and_future_hour(): + with pendulum.travel_to(pendulum.today().at(12, 34, 56)): + now = pendulum.now().time() + + assert now.diff_for_humans(now.subtract(hours=1)) == "1 hour after" + + +def test_diff_for_humans_other_and_future_hours(): + with pendulum.travel_to(pendulum.today().at(12, 34, 56)): + now = pendulum.now().time() + + assert now.diff_for_humans(now.subtract(hours=2)) == "2 hours after" + + +def test_diff_for_humans_absolute_seconds(): + with pendulum.travel_to(pendulum.today().at(12, 34, 56)): + now = pendulum.now().time() + + assert now.diff_for_humans(now.subtract(seconds=59), True) == "59 seconds" + now = pendulum.now().time() + + assert now.diff_for_humans(now.add(seconds=59), True) == "59 seconds" + + +def test_diff_for_humans_absolute_minutes(): + with pendulum.travel_to(pendulum.today().at(12, 34, 56)): + now = pendulum.now().time() + + assert now.diff_for_humans(now.subtract(minutes=30), True) == "30 minutes" + now = pendulum.now().time() + + assert now.diff_for_humans(now.add(minutes=30), True) == "30 minutes" + + +def test_diff_for_humans_absolute_hours(): + with pendulum.travel_to(pendulum.today().at(12, 34, 56)): + now = pendulum.now().time() + + assert now.diff_for_humans(now.subtract(hours=3), True) == "3 hours" + now = pendulum.now().time() + + assert now.diff_for_humans(now.add(hours=3), True) == "3 hours" diff --git a/tests/time/test_fluent_setters.py b/tests/time/test_fluent_setters.py new file mode 100644 index 0000000..a678e56 --- /dev/null +++ b/tests/time/test_fluent_setters.py @@ -0,0 +1,12 @@ +from __future__ import annotations + +from pendulum import Time +from tests.conftest import assert_time + + +def test_replace(): + t = Time(12, 34, 56, 123456) + t = t.replace(1, 2, 3, 654321) + + assert isinstance(t, Time) + assert_time(t, 1, 2, 3, 654321) diff --git a/tests/time/test_strings.py b/tests/time/test_strings.py new file mode 100644 index 0000000..db3c9cd --- /dev/null +++ b/tests/time/test_strings.py @@ -0,0 +1,39 @@ +from __future__ import annotations + +from pendulum import Time + + +def test_to_string(): + d = Time(1, 2, 3) + assert str(d) == "01:02:03" + d = Time(1, 2, 3, 123456) + assert str(d) == "01:02:03.123456" + + +def test_repr(): + d = Time(1, 2, 3) + assert repr(d) == "Time(1, 2, 3)" + + d = Time(1, 2, 3, 123456) + assert repr(d) == "Time(1, 2, 3, 123456)" + + +def test_format_with_locale(): + d = Time(14, 15, 16) + assert d.format("hh:mm:ss A", locale="fr") == "02:15:16 PM" + + +def test_strftime(): + d = Time(14, 15, 16) + assert d.strftime("%H") == "14" + + +def test_for_json(): + d = Time(14, 15, 16) + assert d.for_json() == "14:15:16" + + +def test_format(): + d = Time(14, 15, 16) + assert f"{d}" == "14:15:16" + assert f"{d:mm}" == "15" diff --git a/tests/time/test_sub.py b/tests/time/test_sub.py new file mode 100644 index 0000000..1a957ad --- /dev/null +++ b/tests/time/test_sub.py @@ -0,0 +1,112 @@ +from __future__ import annotations + +from datetime import time +from datetime import timedelta + +import pytest +import pytz + +import pendulum + +from pendulum import Time +from tests.conftest import assert_duration + + +def test_sub_hours_positive(): + assert Time(0, 0, 0).subtract(hours=1).hour == 23 + + +def test_sub_hours_zero(): + assert Time(0, 0, 0).subtract(hours=0).hour == 0 + + +def test_sub_hours_negative(): + assert Time(0, 0, 0).subtract(hours=-1).hour == 1 + + +def test_sub_minutes_positive(): + assert Time(0, 0, 0).subtract(minutes=1).minute == 59 + + +def test_sub_minutes_zero(): + assert Time(0, 0, 0).subtract(minutes=0).minute == 0 + + +def test_sub_minutes_negative(): + assert Time(0, 0, 0).subtract(minutes=-1).minute == 1 + + +def test_sub_seconds_positive(): + assert Time(0, 0, 0).subtract(seconds=1).second == 59 + + +def test_sub_seconds_zero(): + assert Time(0, 0, 0).subtract(seconds=0).second == 0 + + +def test_sub_seconds_negative(): + assert Time(0, 0, 0).subtract(seconds=-1).second == 1 + + +def test_subtract_timedelta(): + delta = timedelta(seconds=16, microseconds=654321) + d = Time(3, 12, 15, 777777) + + d = d.subtract_timedelta(delta) + assert d.minute == 11 + assert d.second == 59 + assert d.microsecond == 123456 + + d = Time(3, 12, 15, 777777) + + d = d - delta + assert d.minute == 11 + assert d.second == 59 + assert d.microsecond == 123456 + + +def test_add_timedelta_with_days(): + delta = timedelta(days=3, seconds=45, microseconds=123456) + d = Time(3, 12, 15, 654321) + + with pytest.raises(TypeError): + d.subtract_timedelta(delta) + + +def test_subtract_invalid_type(): + d = Time(0, 0, 0) + + with pytest.raises(TypeError): + d - "ab" + + with pytest.raises(TypeError): + "ab" - d + + +def test_subtract_time(): + t = Time(12, 34, 56) + t1 = Time(1, 1, 1) + t2 = time(1, 1, 1) + t3 = time(1, 1, 1, tzinfo=pytz.timezone("Europe/Paris")) + + diff = t - t1 + assert isinstance(diff, pendulum.Duration) + assert_duration(diff, 0, hours=11, minutes=33, seconds=55) + + diff = t1 - t + assert isinstance(diff, pendulum.Duration) + assert_duration(diff, 0, hours=-11, minutes=-33, seconds=-55) + + diff = t - t2 + assert isinstance(diff, pendulum.Duration) + assert_duration(diff, 0, hours=11, minutes=33, seconds=55) + + diff = t2 - t + assert isinstance(diff, pendulum.Duration) + assert_duration(diff, 0, hours=-11, minutes=-33, seconds=-55) + + with pytest.raises(TypeError): + t - t3 + + with pytest.raises(TypeError): + t3 - t |