def test_floor_dividing_money_by_int_or_decimal_should_work_with_proper_rounding( to_type, dividends_amount, divisor, expected_amount, precision): # noqa: E501 dividend = Money(dividends_amount, 'EUR', precision) expected_quotient = Money(expected_amount, 'EUR', precision) assert dividend // to_type(divisor) == expected_quotient
def test_multiplying_with_factors_of_money_and_not_int_or_decimal_should_not_work( other): # noqa: E501 with pytest.raises(TypeError): Money(1, 'USD') * other with pytest.raises(TypeError): other * Money(1, 'USD')
def test_money_comparison_operators_should_fail_between_money_instances_of_different_currencies( operator): # noqa: E501 money1 = Money(1, 'EUR') money2 = Money(1, 'USD') with pytest.raises(IncompatibleCurrencyError): operator(money1, money2)
def test_multiplying_with_factors_of_money_and_int_or_decimal_should_work( to_type, money_amount, multiplier, expected_amount): # noqa: E501 money_factor = Money(money_amount, 'EUR') other_factor = to_type(multiplier) expected_money = Money(expected_amount, 'EUR') assert money_factor * other_factor == expected_money assert other_factor * money_factor == expected_money
def test_money_comparison_operators_should_not_fail_between_money_instances_of_the_same_currency( operator): # noqa: E501 money1 = Money(1, 'EUR') money2 = Money(1, 'EUR') try: operator(money1, money2) except IncompatibleCurrencyError as e: pytest.fail(str(e))
def test_money_hash__two_identical_instances_of_money_should_have_the_same_hash( amount, currency): # noqa: E501 instance1 = Money(amount=amount, currency=currency) instance2 = Money(amount=amount, currency=currency) assert hash(instance1) == hash(instance2) # checking hashing invariant assert instance1 == instance2
def test_money_subtract_should_not_work_with_instances_of_other_types( non_money_object): money = Money('10', 'USD') with pytest.raises(TypeError): money - non_money_object with pytest.raises(TypeError): non_money_object - money
def test_money_add_should_not_work_with_instances_of_other_types( non_money_object): money = Money('10', 'EUR') with pytest.raises(TypeError): money + non_money_object with pytest.raises(TypeError): non_money_object + money
def test_money_pos(): assert +Money(1, 'PLN').amount == Decimal('1') assert +Money(-1, 'PLN').amount == Decimal('-1') assert +Money(0, 'PLN').amount == Decimal('0')
def test_ge(amount1, amount2): assert amount1 >= amount2 assert Money(amount1, 'DKK') >= Money(amount2, 'DKK') # double-check the inverse relation with the same data assert not (Money(amount1, 'DKK') < Money(amount2, 'DKK'))
def test_gt(amount1, amount2, precision): assert amount1 > amount2 assert Money(amount1, 'DKK', precision) > Money(amount2, 'DKK', precision) # double-check the inverse relation with the same data assert not (Money(amount1, 'DKK', precision) <= Money( amount2, 'DKK', precision))
def test_lte(amount1, amount2): assert amount1 <= amount2 assert Money(amount1, 'CHF') <= Money(amount2, 'CHF') # double-check the inverse relation with the same data assert not (Money(amount1, 'CHF') > Money(amount2, 'CHF'))
def test_lt(amount1, amount2, precision): assert amount1 < amount2 assert Money(amount1, 'CHF', precision) < Money(amount2, 'CHF', precision) # double-check the inverse relation with the same data assert not (Money(amount1, 'CHF', precision) >= Money( amount2, 'CHF', precision))
def test_currency_code_validation_should_raise_error_on_non_string( non_string_object): with pytest.raises(TypeError): Money(amount=1, currency=non_string_object)
def test_money_amount_round(amount, precision, expected_amount): money = Money(amount, 'GBP', precision) assert money.amount == expected_amount
def test_money_add_should_work_between_money_instances_of_the_same_currency( amount1, amount2, expected): # noqa: E501 money1 = Money(amount1, 'EUR') money2 = Money(amount2, 'EUR') assert (money1 + money2).amount == expected
def test_money_true_and_floor_dividing_should_not_work_with_instances_of_other_types_than_int_and_decimal( operation, other): # noqa: E501 with pytest.raises(TypeError): operation(Money(1, 'USD'), other)
def test_money_bool(amount, expected): assert bool(Money(amount=amount, currency='XYZ')) == expected
def test_true_dividing_int_or_decimal_by_money_should_work_with_proper_rounding( to_type, dividend, divisors_amount, expected_amount): # noqa: E501 divisor = Money(divisors_amount, 'EUR') expected_quotient = Money(expected_amount, 'EUR') assert to_type(dividend) / divisor == expected_quotient
def test_multiplying_with_factors_of_money_and_decimal_should_work_with_proper_rounding( money_amount, multiplier, expected_amount, precision): # noqa: E501 assert (Money(money_amount, 'GBP', precision) * multiplier == Money( expected_amount, 'GBP', precision)) assert (multiplier * Money(money_amount, 'GBP', precision) == Money( expected_amount, 'GBP', precision))
def test_money_add_should_not_work_between_money_instances_of_different_currencies( ): money1 = Money('10', 'EUR') money2 = Money('20', 'USD') with pytest.raises(IncompatibleCurrencyError): money1 + money2
def test_money_comparison_operators_should_fail_with_instances_of_other_types( non_money_object, operator): # noqa: E501 money = Money('10', 'USD') with pytest.raises(TypeError): operator(money, non_money_object)
def test_money_properties(amount, currency, expected_amount): money = Money(amount, currency) assert money.amount == expected_amount assert money.currency == currency
def test_eq(amount): assert Money(amount, 'CHF') == Money(amount, 'CHF')
def test_money_str(amount, currency, expected): assert str(Money(amount, currency)) == expected
def test_ne(amount1, amount2, precision): assert amount1 != amount2 assert Money(amount1, 'CHF', precision) != Money(amount2, 'CHF', precision)
def test_currency_code_validation_should_raise_error_on_malformatted_code( bad_code): with pytest.raises(MalformattedCurrencyCodeError): Money(amount=1, currency=bad_code)
(999, 'USD'), (42, 'EUR'), ]) def test_money_hash__two_identical_instances_of_money_should_have_the_same_hash( amount, currency): # noqa: E501 instance1 = Money(amount=amount, currency=currency) instance2 = Money(amount=amount, currency=currency) assert hash(instance1) == hash(instance2) # checking hashing invariant assert instance1 == instance2 @pytest.mark.parametrize('money1, money2', [ (Money('10', 'ABC'), Money('10', 'ABX')), (Money('10.0000', 'GBP', '.0000'), Money('10.0001', 'GBP', '.0000')), (Money('10', 'GBP'), Money('-10', 'GBP')), ]) def test_money_hash__two_different_instances_of_money_should_have_different_hashes( money1, money2): # noqa: E501 assert (hash(money1) != hash(money2)) # ================================= TEST UNARY OPERATORS ================================= def test_money_neg(): assert -Money(1, 'PLN').amount == Decimal('-1') assert -Money(-1, 'PLN').amount == Decimal('1') assert -Money(0, 'PLN').amount == Decimal('0')