def __eq__(self, other) -> bool: if not isinstance(other, Money): return False return ( self.currency == other.currency and decimals_equal(self.quantity, other.quantity) )
def validate(self, movement_specs: List[MovementSpec]) -> None: currency_set = set(x.money.currency for x in movement_specs) account_set = set(x.account for x in movement_specs) account_currency_pair_set = set( (x.money.currency, x.account) for x in movement_specs) if len(movement_specs) < 2: self.fail("TWO_OR_MORE_MOVEMENTS") if len(account_set) == 1: self.fail("SINGLE_ACCOUNT") if len(currency_set) == 1: value = sum(x.money.quantity for x in movement_specs) if not decimals_equal(value, Decimal(0)): self.fail("UNBALANCED_SINGLE_CURRENCY") # Each pair (account, currency) should only appear once if len(account_currency_pair_set) < len(movement_specs): self.fail('REPEATED_CURRENCY_ACCOUNT_PAIR')
def test_decimals_equal_not_equal(self): one = Decimal('1.128') two = one - Decimal('0.003') assert decimals_equal(one, two) is False
def test_decimals_equal_equal(self): one = Decimal('123.4567') two = one + Decimal('0.0001') assert decimals_equal(one, two) is True
def test_decimals_equal_not_equal_limit(self): one = Decimal('123.005') two = one + Decimal('0.001') assert decimals_equal(one, two) is False