|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +import pendulum |
| 8 | + |
| 9 | + |
| 10 | +if TYPE_CHECKING: |
| 11 | + from collections.abc import Callable |
| 12 | + |
| 13 | + |
| 14 | +@pytest.mark.parametrize("factory", [pendulum.date, pendulum.datetime]) |
| 15 | +@pytest.mark.parametrize("reverse", [False, True]) |
| 16 | +@pytest.mark.parametrize("absolute", [False, True]) |
| 17 | +@pytest.mark.parametrize("amount", [0, -1, -2]) |
| 18 | +def test_range_rejects_nonpositive_amount( |
| 19 | + factory: Callable[[int, int, int], pendulum.Date], |
| 20 | + reverse: bool, |
| 21 | + absolute: bool, |
| 22 | + amount: int, |
| 23 | +) -> None: |
| 24 | + start = factory(2024, 1, 10) |
| 25 | + end = factory(2024, 1, 14) |
| 26 | + if reverse: |
| 27 | + start, end = end, start |
| 28 | + interval = pendulum.Interval(start, end, absolute=absolute) |
| 29 | + with pytest.raises(ValueError, match="amount must be positive"): |
| 30 | + next(interval.range("days", amount)) |
| 31 | + |
| 32 | + |
| 33 | +@pytest.mark.parametrize("amount", [0, -1]) |
| 34 | +def test_single_point_range_rejects_nonpositive_amount(amount: int) -> None: |
| 35 | + point = pendulum.date(2024, 1, 10) |
| 36 | + interval = pendulum.Interval(point, point) |
| 37 | + with pytest.raises(ValueError, match="amount must be positive"): |
| 38 | + next(interval.range("days", amount)) |
| 39 | + |
| 40 | + |
| 41 | +@pytest.mark.parametrize("factory", [pendulum.date, pendulum.datetime]) |
| 42 | +@pytest.mark.parametrize("reverse", [False, True]) |
| 43 | +@pytest.mark.parametrize("amount", [1, 2, 3, 5]) |
| 44 | +def test_positive_range_amount_uses_interval_direction( |
| 45 | + factory: Callable[[int, int, int], pendulum.Date], reverse: bool, amount: int |
| 46 | +) -> None: |
| 47 | + start = factory(2024, 1, 10) |
| 48 | + end = factory(2024, 1, 14) |
| 49 | + if reverse: |
| 50 | + start, end = end, start |
| 51 | + interval = pendulum.Interval(start, end) |
| 52 | + direction = -1 if reverse else 1 |
| 53 | + expected = [ |
| 54 | + factory(2024, 1, day) |
| 55 | + for day in range(start.day, end.day + direction, direction * amount) |
| 56 | + ] |
| 57 | + assert list(interval.range("days", amount)) == expected |
0 commit comments