Skip to content

Commit

Permalink
fix: Change to reverse ordering of Overdraft instances (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
antonagestam authored Nov 6, 2023
1 parent 7e4cfaa commit cc48484
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/immoney/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -674,28 +674,28 @@ def __eq__(self, other: object) -> bool:

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
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
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
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
return self.subunits >= other.subunits
if isinstance(other, Money) and self.currency == other.currency:
return True
return NotImplemented
Expand Down
20 changes: 20 additions & 0 deletions tests/test_overdraft.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,26 @@ def test_total_ordering_within_currency(self, x: int, y: int) -> None:
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)

def test_ordering_of_non_equals(self) -> None:
a = SEK.overdraft(2)
b = SEK.overdraft(1)
assert a < b
assert a <= b
assert not (a > b)
assert not (a >= b)
assert b > a
assert b >= a
assert not (b < a)
assert not (b <= a)

def test_ordering_of_equals(self) -> None:
a = SEK.overdraft(1)
b = SEK.overdraft(1)
assert not a < b
assert not a > b
assert a <= b
assert a >= b

@given(a=overdrafts(), b=overdrafts() | monies())
@example(NOK.overdraft(1), SEK.overdraft(1))
@example(SEK.overdraft(1), NOK.overdraft(2))
Expand Down

0 comments on commit cc48484

Please sign in to comment.