def test_quantize(): price1 = CurrencyWithTax(Currency(10, "EUR"), Currency(15, "EUR")) price2 = CurrencyWithTax(Currency(30, "EUR"), Currency(45, "EUR")) price_range = CurrencyRangeTax(price1, price2) result = price_range.quantize() assert str(result.start.net.amount) == "10.00" assert str(result.stop.net.amount) == "30.00"
def test_repr(): price1 = CurrencyWithTax(Currency(10, "EUR"), Currency(15, "EUR")) price2 = CurrencyWithTax(Currency(30, "EUR"), Currency(45, "EUR")) price_range = CurrencyRangeTax(price1, price2) assert str(price_range) == ( "CurrencyRangeTax(CurrencyWithTax(net=10 EUR, gross=15 EUR), CurrencyWithTax(net=30 EUR, gross=45 EUR))" )
def test_init(): currency = CurrencyWithTax(net=Currency(1, "USD"), gross=Currency(1, "USD")) assert (currency.net == Currency(1, "USD")) and ( currency.gross == Currency(1, "USD") ) with pytest.raises(ValueError): CurrencyWithTax(net=Currency(1, "USD"), gross=Currency(1, "GBP")) with pytest.raises(TypeError): CurrencyWithTax(1, 1)
def test_membership(): price1 = CurrencyWithTax(Currency(10, "EUR"), Currency(15, "EUR")) price2 = CurrencyWithTax(Currency(30, "EUR"), Currency(45, "EUR")) price_range = CurrencyRangeTax(price1, price2) assert price1 in price_range assert price2 in price_range assert (price1 + price2) / 2 in price_range assert price1 + price2 not in price_range with pytest.raises(TypeError): 15 in price_range
def test_subtraction_with_money(): price1 = CurrencyWithTax(Currency(40, "EUR"), Currency(60, "EUR")) price2 = CurrencyWithTax(Currency(80, "EUR"), Currency(120, "EUR")) price_range = CurrencyRangeTax(price1, price2) price3 = Currency(10, "EUR") result = price_range - price3 assert result.start == price1 - price3 assert result.stop == price2 - price3 with pytest.raises(ValueError): price_range - Currency(1, "BTC")
def test_addition_with_taxed_money(): price1 = CurrencyWithTax(Currency(10, "EUR"), Currency(15, "EUR")) price2 = CurrencyWithTax(Currency(30, "EUR"), Currency(45, "EUR")) price_range = CurrencyRangeTax(price1, price2) price3 = CurrencyWithTax(Currency(40, "EUR"), Currency(60, "EUR")) result = price_range + price3 assert result.start == price1 + price3 assert result.stop == price2 + price3 with pytest.raises(ValueError): price_range + CurrencyWithTax(Currency(1, "BTC"), Currency(1, "BTC"))
def test_replace(): price1 = CurrencyWithTax(Currency(10, "EUR"), Currency(15, "EUR")) price2 = CurrencyWithTax(Currency(30, "EUR"), Currency(45, "EUR")) price3 = CurrencyWithTax(Currency(20, "EUR"), Currency(30, "EUR")) price_range = CurrencyRangeTax(price1, price2) result = price_range.replace(stop=price3) assert result.start == price1 assert result.stop == price3 result = price_range.replace(start=price3) assert result.start == price3 assert result.stop == price2
def test_construction(): price1 = CurrencyWithTax(Currency(10, "EUR"), Currency(15, "EUR")) price2 = CurrencyWithTax(Currency(30, "EUR"), Currency(45, "EUR")) price_range = CurrencyRangeTax(price1, price2) assert price_range.start == price1 assert price_range.stop == price2 with pytest.raises(ValueError): CurrencyRangeTax( price1, CurrencyWithTax(Currency(20, "PLN"), Currency(20, "PLN"))) with pytest.raises(ValueError): CurrencyRangeTax(price2, price1)
def test_addition_with_money_range(): price1 = CurrencyWithTax(Currency(10, "EUR"), Currency(15, "EUR")) price2 = CurrencyWithTax(Currency(30, "EUR"), Currency(45, "EUR")) price_range1 = CurrencyRangeTax(price1, price2) price3 = Currency(40, "EUR") price4 = Currency(80, "EUR") price_range2 = CurrencyRange(price3, price4) result = price_range1 + price_range2 assert result.start == price1 + price3 assert result.stop == price2 + price4 with pytest.raises(ValueError): price_range1 + CurrencyRange(Currency(1, "BTC"), Currency(2, "BTC"))
def test_percentage_discount(): price = CurrencyWithTax(Currency(100, "BTC"), Currency(100, "BTC")) discount = partial(percentage_discount, percentage=10) result = discount(price) assert result.net == Currency(90, "BTC") assert result.gross == Currency(90, "BTC") price_range = CurrencyRangeTax(price, price) result = discount(price_range) assert result.start == CurrencyWithTax(Currency(90, "BTC"), Currency(90, "BTC")) assert result.stop == CurrencyWithTax(Currency(90, "BTC"), Currency(90, "BTC"))
def test_subtraction_with_money_range(): price1 = Currency(10, "EUR") price2 = Currency(30, "EUR") price_range1 = CurrencyRange(price1, price2) price3 = CurrencyWithTax(Currency(40, "EUR"), Currency(60, "EUR")) price4 = CurrencyWithTax(Currency(80, "EUR"), Currency(120, "EUR")) price_range2 = CurrencyRangeTax(price3, price4) result = price_range2 - price_range1 assert result.start == price3 - price1 assert result.stop == price4 - price2 with pytest.raises(ValueError): price_range2 - CurrencyRange(Currency(1, "BTC"), Currency(2, "BTC"))
def test_sub(): currency1 = CurrencyWithTax(Currency(10, "USD"), Currency(15, "USD")) currency2 = CurrencyWithTax(Currency(30, "USD"), Currency(45, "USD")) assert currency2 - currency1 == CurrencyWithTax( Currency(20, "USD"), Currency(30, "USD") ) currency3 = currency1 - Currency(5, "USD") assert currency3.net == Currency(5, "USD") assert currency3.gross == Currency(10, "USD") with pytest.raises(ValueError): currency1 - CurrencyWithTax(Currency(10, "GBP"), Currency(10, "GBP")) with pytest.raises(TypeError): currency1 - 1
def test_discount(): price = CurrencyWithTax(Currency(100, "BTC"), Currency(100, "BTC")) discount = partial(fractional_discount, fraction=Decimal("0.25")) result = discount(price) assert result.net == Currency(75, "BTC") assert result.gross == Currency(75, "BTC") price_range = CurrencyRangeTax(price, price) result = discount(price_range) assert result.start == CurrencyWithTax(Currency(75, "BTC"), Currency(75, "BTC")) assert result.stop == CurrencyWithTax(Currency(75, "BTC"), Currency(75, "BTC")) result = discount(Currency(100, "BTC")) assert result == Currency(75, "BTC") with pytest.raises(TypeError): discount(100)
def test_application(): result = flat_tax(CurrencyWithTax(Currency(10, "BTC"), Currency(10, "BTC")), 1) assert result.net == Currency(10, "BTC") assert result.gross == Currency(20, "BTC") result = flat_tax(Currency(100, "BTC"), Decimal("0.5")) assert result.net == Currency(100, "BTC") assert result.gross == Currency(150, "BTC") with pytest.raises(TypeError): flat_tax(1, 1)
def test_get_tax_for_rate(set_country_codes, set_vat_country_code_default1): country_code = [*set_vat_country_code_default1][0] rates_ref = set_vat_country_code_default1.get(country_code, None).get( 'standard_rate', None) rates = utils.get_tax_rates_for_country(country_code) # test standard tax tax_func = utils.get_tax_for_rate(rates) assert tax_func(Currency(100, 'USD')) == CurrencyWithTax( net=Currency(100, 'USD'), gross=Currency(120, 'USD'))
def test_application(): price = CurrencyWithTax(Currency(30, "BTC"), Currency(30, "BTC")) discount = partial(fixed_discount, discount=Currency(10, "BTC")) result = discount(price) assert result.net == Currency(20, "BTC") assert result.gross == Currency(20, "BTC") price_range = CurrencyRange(price.net, price.net) result = discount(price_range) assert result.start == Currency(20, "BTC") assert result.stop == Currency(20, "BTC") price_range = CurrencyRangeTax(price, price) result = discount(price_range) assert result.start == CurrencyWithTax(Currency(20, "BTC"), Currency(20, "BTC")) assert result.stop == CurrencyWithTax(Currency(20, "BTC"), Currency(20, "BTC")) with pytest.raises(TypeError): discount(1)
def test_tax_from_gross(): result = flat_tax( CurrencyWithTax(Currency(120, "USD"), Currency(120, "USD")), Decimal("0.2"), keep_gross=True, ) assert result.net == Currency(100, "USD") assert result.gross == Currency(120, "USD") result = flat_tax(Currency(150, "BTC"), Decimal("0.5"), keep_gross=True) assert result.net == Currency(100, "BTC") assert result.gross == Currency(150, "BTC")
def test_range(): price_range = CurrencyRange(Currency(10, "BTC"), Currency(20, "BTC")) result = flat_tax(price_range, 1) assert result.start == CurrencyWithTax(Currency(10, "BTC"), Currency(20, "BTC")) assert result.stop == CurrencyWithTax(Currency(20, "BTC"), Currency(40, "BTC")) price_range = CurrencyRangeTax( CurrencyWithTax(Currency(10, "BTC"), Currency(10, "BTC")), CurrencyWithTax(Currency(20, "BTC"), Currency(20, "BTC")), ) result = flat_tax(price_range, 1) assert result.start == CurrencyWithTax(Currency(10, "BTC"), Currency(20, "BTC")) assert result.stop == CurrencyWithTax(Currency(20, "BTC"), Currency(40, "BTC"))
def fixed_discount(base: T, discount: Currency) -> T: """Apply a fixed discount to any currency type.""" if isinstance(base, CurrencyRange): return CurrencyRange( fixed_discount(base.start, discount), fixed_discount(base.stop, discount) ) if isinstance(base, CurrencyRangeTax): return CurrencyRangeTax( fixed_discount(base.start, discount), fixed_discount(base.stop, discount) ) if isinstance(base, CurrencyWithTax): return CurrencyWithTax( net=fixed_discount(base.net, discount), gross=fixed_discount(base.gross, discount), ) if isinstance(base, Currency): return max(base - discount, Currency(0, base.currency)) raise TypeError("Unknown base for fixed_discount: %r" % (base,))
def test_comparison(): price1 = CurrencyWithTax(Currency(10, "EUR"), Currency(15, "EUR")) price2 = CurrencyWithTax(Currency(30, "EUR"), Currency(45, "EUR")) price_range1 = CurrencyRangeTax(price1, price2) price3 = CurrencyWithTax(Currency(40, "EUR"), Currency(60, "EUR")) price4 = CurrencyWithTax(Currency(80, "EUR"), Currency(120, "EUR")) price_range2 = CurrencyRangeTax(price3, price4) assert price_range1 == CurrencyRangeTax(price1, price2) assert price_range1 != price_range2 assert price_range1 != CurrencyRangeTax(price1, price1) assert price_range1 != CurrencyRangeTax( CurrencyWithTax(Currency(10, "USD"), Currency(15, "USD")), CurrencyWithTax(Currency(30, "USD"), Currency(45, "USD")), ) assert price_range1 != price1
def test_tax(): price = CurrencyWithTax(Currency(10, "USD"), Currency(15, "USD")) assert price.tax == Currency(5, "USD")
def test_str(): currency = CurrencyWithTax(net=Currency(1, "USD"), gross=Currency(1, "USD")) assert str(currency) == "CurrencyWithTax(net=1 USD, gross=1 USD)"
def test_addition_with_other_types(): price1 = CurrencyWithTax(Currency(10, "EUR"), Currency(15, "EUR")) price2 = CurrencyWithTax(Currency(30, "EUR"), Currency(45, "EUR")) price_range = CurrencyRangeTax(price1, price2) with pytest.raises(TypeError): price_range + 1
def test_mult(): currency1 = CurrencyWithTax(Currency(10, "EUR"), Currency(20, "EUR")) assert currency1 * 2 == CurrencyWithTax(Currency(20, "EUR"), Currency(40, "EUR")) assert 2 * currency1 == currency1 * 2 with pytest.raises(TypeError): currency1 * currency1
def test_div(): currency1 = CurrencyWithTax(Currency(10, "EUR"), Currency(20, "EUR")) assert currency1 / 2 == CurrencyWithTax(Currency(5, "EUR"), Currency(10, "EUR")) with pytest.raises(TypeError): currency1 / currency1
def test_comparison(): currency = CurrencyWithTax(Currency(10, "EUR"), Currency(15, "EUR")) assert currency == CurrencyWithTax(Currency(10, "EUR"), Currency(15, "EUR")) assert currency != CurrencyWithTax(Currency(20, "EUR"), Currency(30, "EUR")) assert currency != CurrencyWithTax(Currency(10, "GBP"), Currency(15, "GBP")) assert currency != Currency(10, "EUR") assert currency < CurrencyWithTax(Currency(20, "EUR"), Currency(30, "EUR")) assert currency <= CurrencyWithTax(Currency(10, "EUR"), Currency(15, "EUR")) assert currency <= CurrencyWithTax(Currency(20, "EUR"), Currency(30, "EUR")) assert currency > CurrencyWithTax(Currency(1, "EUR"), Currency(1, "EUR")) assert currency >= CurrencyWithTax(Currency(10, "EUR"), Currency(15, "EUR")) assert currency >= CurrencyWithTax(Currency(1, "EUR"), Currency(1, "EUR")) assert not currency <= CurrencyWithTax(Currency(1, "EUR"), Currency(1, "EUR")) assert not currency >= CurrencyWithTax(Currency(20, "EUR"), Currency(30, "EUR")) with pytest.raises(ValueError): currency < CurrencyWithTax(Currency(10, "GBP"), Currency(15, "GBP")) with pytest.raises(TypeError): currency >= Currency(1, "EUR") with pytest.raises(TypeError): currency < Currency(20, "EUR")
def test_currency(): price1 = CurrencyWithTax(Currency(10, "EUR"), Currency(15, "EUR")) price2 = CurrencyWithTax(Currency(30, "EUR"), Currency(45, "EUR")) price_range = CurrencyRangeTax(price1, price2) assert price_range.currency == "EUR"
def test_quantize(): price = CurrencyWithTax(Currency("1.001", "EUR"), Currency("1.001", "EUR")) assert price.quantize() == ( CurrencyWithTax(Currency("1.00", "EUR"), Currency("1.00", "EUR")) )
def test_currency(): price = CurrencyWithTax(Currency(1, "PLN"), Currency(1, "PLN")) assert price.currency == "PLN"
def test_subtraction_with_other_types(): price1 = CurrencyWithTax(Currency(40, "EUR"), Currency(60, "EUR")) price2 = CurrencyWithTax(Currency(80, "EUR"), Currency(120, "EUR")) price_range = CurrencyRangeTax(price1, price2) with pytest.raises(TypeError): price_range - 1