Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Implement SubunitFraction ordering #84

Merged
merged 1 commit into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/immoney/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,42 @@ def __eq__(self, other: object) -> bool:
return self.value == -other.subunits
return NotImplemented

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

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

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

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

def __neg__(self) -> SubunitFraction[C_co]:
return SubunitFraction(-self.value, self.currency)

Expand Down
23 changes: 23 additions & 0 deletions tests/strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@

import random
from typing import Final
from typing import TypeAlias

from hypothesis.strategies import composite
from hypothesis.strategies import decimals
from hypothesis.strategies import fractions
from hypothesis.strategies import integers
from hypothesis.strategies import just
from hypothesis.strategies import text

from immoney import Currency
from immoney import Money
from immoney._base import Overdraft
from immoney._base import SubunitFraction
from immoney._base import valid_subunit
from immoney.currencies import SEK
from immoney.currencies import SEKType

valid_sek_decimals: Final = decimals(
min_value=0,
Expand Down Expand Up @@ -53,3 +59,20 @@ def overdrafts(
subunits=integers(min_value=1),
) -> Overdraft[Currency]:
return Overdraft.from_subunit(draw(subunits), draw(currencies))


@composite
def subunit_fractions(
draw,
currencies=currencies(),
subunits=fractions(),
) -> SubunitFraction[Currency]:
return SubunitFraction(draw(subunits), draw(currencies))


sek_monetaries: Final = (
subunit_fractions(currencies=just(SEK))
| monies(currencies=just(SEK))
| overdrafts(currencies=just(SEK))
)
SEKMonetary: TypeAlias = Money[SEKType] | Overdraft[SEKType] | SubunitFraction[SEKType]
117 changes: 117 additions & 0 deletions tests/test_subunit_fraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@
from immoney.currencies import SEKType
from immoney.errors import ParseError

from .strategies import SEKMonetary
from .strategies import currencies
from .strategies import monies
from .strategies import overdrafts
from .strategies import sek_monetaries
from .strategies import subunit_fractions


def test_init_normalizes_value() -> None:
Expand Down Expand Up @@ -124,6 +129,60 @@ def test_equality() -> None:
assert different_one != -overdraft_one


@given(monies())
def test_compares_equal_to_same_currency_money(money: Money[Currency]) -> None:
fraction = money.currency.fraction(money.subunits)
assert fraction == money
assert money == fraction
assert fraction >= money
assert fraction <= money
assert money >= fraction
assert money <= fraction


@given(overdrafts())
def test_compares_equal_to_same_currency_overdraft(
overdraft: Overdraft[Currency],
) -> None:
fraction = overdraft.currency.fraction(-overdraft.subunits)
assert fraction == overdraft
assert overdraft == fraction
assert fraction >= overdraft
assert fraction <= overdraft
assert overdraft >= fraction
assert overdraft <= fraction


@given(monies(), currencies())
def test_compares_unequal_to_differing_currency_money(
money: Money[Currency],
currency: Currency,
) -> None:
fraction = currency.fraction(money.subunits)
assert fraction != money
assert money != fraction


@given(monies(), currencies())
def test_compares_unequal_to_differing_currency_overdraft(
overdraft: Overdraft[Currency],
currency: Currency,
) -> None:
fraction = currency.fraction(overdraft.subunits)
assert fraction != overdraft
assert overdraft != fraction


def test_compares_unequal_across_values() -> None:
fraction = SEK.fraction(99, 100)
money = SEK(1)
assert fraction != money
assert money != fraction
overdraft = SEK.overdraft(1)
assert fraction != overdraft
assert overdraft != fraction


def test_from_money_returns_instance() -> None:
class FooType(Currency):
code = "foo"
Expand Down Expand Up @@ -434,3 +493,61 @@ def test_raises_type_error_for_invalid_other(
),
):
b / a # type: ignore[operator]


class TestOrdering:
@given(sek_monetaries, sek_monetaries)
def test_total_ordering_within_currency(
self, a: SEKMonetary, b: SEKMonetary
) -> 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)

@pytest.mark.parametrize(
("larger", "smaller"),
[
(SEK.fraction(0), SEK.overdraft_from_subunit(1)),
(SEK.overdraft_from_subunit(1), SEK.fraction(-2)),
(SEK.fraction(1), SEK.zero),
(SEK.zero, SEK.fraction(-1)),
(SEK(1), SEK.fraction(1, 2)),
],
)
def test_all_ordering_combinations(
self,
larger: SubunitFraction[Currency] | Overdraft[Currency] | Money[Currency],
smaller: SubunitFraction[Currency] | Overdraft[Currency] | Money[Currency],
) -> None:
assert larger > smaller
assert not larger < smaller
assert larger >= smaller
assert not larger <= smaller
assert smaller < larger
assert not smaller > larger
assert smaller <= larger
assert not smaller >= larger

@given(a=subunit_fractions(), b=overdrafts() | monies() | subunit_fractions())
@example(NOK.fraction(1), SEK.fraction(1))
@example(SEK.fraction(1), NOK.fraction(2))
def test_raises_type_error_for_ordering_across_currencies(
self,
a: SubunitFraction[Currency],
b: SubunitFraction[Currency] | 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