Skip to content

Commit

Permalink
feature: Implement total ordering for Overdraft (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
antonagestam authored Nov 6, 2023
1 parent f609ac5 commit 7e4cfaa
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/immoney/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,34 @@ def __eq__(self, other: object) -> bool:
return self.currency == other.currency and self.subunits == other.subunits
return NotImplemented

def __gt__(self: Overdraft[C_co], other: Overdraft[C_co] | Money[C_co]) -> bool:
if isinstance(other, Overdraft) and self.currency == other.currency:
return self.subunits > other.subunits
if isinstance(other, Money) and self.currency == other.currency:
return False
return NotImplemented

def __ge__(self: Overdraft[C_co], other: Overdraft[C_co] | Money[C_co]) -> bool:
if isinstance(other, Overdraft) and self.currency == other.currency:
return self.subunits >= other.subunits
if isinstance(other, Money) and self.currency == other.currency:
return False
return NotImplemented

def __lt__(self: Overdraft[C_co], other: Overdraft[C_co] | Money[C_co]) -> bool:
if isinstance(other, Overdraft) and self.currency == other.currency:
return self.subunits < other.subunits
if isinstance(other, Money) and self.currency == other.currency:
return True
return NotImplemented

def __le__(self: Overdraft[C_co], other: Overdraft[C_co] | Money[C_co]) -> bool:
if isinstance(other, Overdraft) and self.currency == other.currency:
return self.subunits <= other.subunits
if isinstance(other, Money) and self.currency == other.currency:
return True
return NotImplemented

@overload
def __add__(
self: Overdraft[C_co],
Expand Down
91 changes: 91 additions & 0 deletions tests/test_overdraft.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from hypothesis import example
from hypothesis import given
from hypothesis.strategies import integers
from hypothesis.strategies import just
from hypothesis.strategies import text
from typing_extensions import assert_type

Expand Down Expand Up @@ -122,6 +123,96 @@ def test_hash() -> None:
assert hash(SEK.overdraft(13)) == hash(SEK.overdraft(13))


class TestOrdering:
def test_can_check_equality_with_zero(self) -> None:
assert SEK.overdraft("0.01") != 0
assert 0 != SEK.overdraft("0.01")
assert NOK.overdraft("0.01") != 0
assert 0 != NOK.overdraft("0.01")

@given(value=overdrafts(), number=integers(min_value=1))
@example(SEK.overdraft(1), 1)
@example(SEK.overdraft("0.1"), 1)
@example(SEK.overdraft("0.01"), 1)
def test_cannot_check_equality_with_non_zero(
self,
value: Overdraft[Currency],
number: int,
) -> None:
assert value != number

@given(value=valid_overdraft_subunits)
def test_can_check_equality_with_instance(self, value: int) -> None:
instance = SEK.overdraft_from_subunit(value)
assert instance == SEK.overdraft_from_subunit(value)
next_plus = SEK.overdraft_from_subunit(instance.subunits + 1)
assert next_plus != value
assert value != next_plus

other_currency = NOK.overdraft_from_subunit(value)
assert other_currency != instance
assert instance != other_currency
assert other_currency != next_plus
assert next_plus != other_currency

@given(a=overdrafts(), b=overdrafts())
@example(NOK.overdraft(1), SEK.overdraft(1))
@example(SEK.overdraft(1), NOK.overdraft(1))
@example(SEK.overdraft(10), NOK.overdraft(10))
def test_never_equal_across_currencies(
self,
a: Overdraft[Currency],
b: Overdraft[Currency],
) -> None:
assert a != b

@given(valid_overdraft_subunits, valid_overdraft_subunits)
@example(1, 1)
@example(2, 1)
@example(1, 2)
def test_total_ordering_within_currency(self, x: int, y: int) -> None:
a = SEK.overdraft_from_subunit(x)
b = SEK.overdraft_from_subunit(y)
assert (a > b and b < a) or (a < b and b > a) or (a == b and b == a)
assert (a >= b and b <= a) or (a <= b and b >= a)

@given(a=overdrafts(), b=overdrafts() | monies())
@example(NOK.overdraft(1), SEK.overdraft(1))
@example(SEK.overdraft(1), NOK.overdraft(2))
def test_raises_type_error_for_ordering_across_currencies(
self,
a: Overdraft[Currency],
b: Overdraft[Currency] | Money[Currency],
) -> None:
with pytest.raises(TypeError):
a > b # noqa: B015
with pytest.raises(TypeError):
a >= b # noqa: B015
with pytest.raises(TypeError):
a < b # noqa: B015
with pytest.raises(TypeError):
a <= b # noqa: B015
with pytest.raises(TypeError):
b > a # noqa: B015
with pytest.raises(TypeError):
b >= a # noqa: B015
with pytest.raises(TypeError):
b < a # noqa: B015
with pytest.raises(TypeError):
b <= a # noqa: B015

@given(monies(currencies=just(SEK)), overdrafts(currencies=just(SEK)))
def test_always_strictly_less_than_money(
self,
money: Money[SEKType],
overdraft: Overdraft[SEKType],
) -> None:
assert money > overdraft
assert money >= overdraft
assert not (money < overdraft)
assert not (money <= overdraft)


@given(overdrafts())
def test_abs_returns_money(value: Overdraft[Any]) -> None:
assert abs(value) is value.currency.from_subunit(value.subunits)
Expand Down

0 comments on commit 7e4cfaa

Please sign in to comment.