def test_application(): tax = LinearTax(1, name='2x Tax') result = tax.apply(Price(Amount(10, 'BTC'), Amount(10, 'BTC'))) assert result.net == Amount(10, 'BTC') assert result.gross == Amount(20, 'BTC') with pytest.raises(TypeError): tax.apply(1)
def test_quantize(): price1 = Price(Amount(10, 'EUR'), Amount(15, 'EUR')) price2 = Price(Amount(30, 'EUR'), Amount(45, 'EUR')) price_range = PriceRange(price1, price2) result = price_range.quantize() assert str(result.min_price.net.value) == '10.00' assert str(result.max_price.net.value) == '30.00'
def test_repr(): price1 = Price(Amount(10, 'EUR'), Amount(15, 'EUR')) price2 = Price(Amount(30, 'EUR'), Amount(45, 'EUR')) price_range = PriceRange(price1, price2) assert repr(price_range) == ( "PriceRange(Price(net=Amount('10', 'EUR'), gross=Amount('15', 'EUR')), Price(net=Amount('30', 'EUR'), gross=Amount('45', 'EUR')))" )
def test_membership(): price1 = Price(Amount(10, 'EUR'), Amount(15, 'EUR')) price2 = Price(Amount(30, 'EUR'), Amount(45, 'EUR')) price_range = PriceRange(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(): price1 = Price(Amount(10, 'USD'), Amount(15, 'USD')) price2 = Price(Amount(30, 'USD'), Amount(45, 'USD')) assert price2 - price1 == Price(Amount(20, 'USD'), Amount(30, 'USD')) with pytest.raises(ValueError): price1 - Price(Amount(10, 'GBP'), Amount(10, 'GBP')) with pytest.raises(TypeError): price1 - 1
def test_addition(): price1 = Price(Amount(10, 'USD'), Amount(15, 'USD')) price2 = Price(Amount(20, 'USD'), Amount(30, 'USD')) assert price2 + price1 == Price(Amount(30, 'USD'), Amount(45, 'USD')) with pytest.raises(ValueError): price1 + Price(Amount(10, 'GBP'), Amount(10, 'GBP')) with pytest.raises(TypeError): price1 + 1
def test_application(): price = Price(Amount(30, 'BTC'), Amount(30, 'BTC')) discount = FixedDiscount(Amount(10, 'BTC'), name='Ten off') result = discount.apply(price) assert result.net == Amount(20, 'BTC') assert result.gross == Amount(20, 'BTC') price_range = PriceRange(price, price) result = discount.apply(price_range) assert result.min_price == Price(Amount(20, 'BTC'), Amount(20, 'BTC')) assert result.max_price == Price(Amount(20, 'BTC'), Amount(20, 'BTC')) with pytest.raises(TypeError): discount.apply(1)
def test_amount_field_from_db_value(): field = AmountField('price', currency='BTC', default='5', max_digits=9, decimal_places=2) assert field.from_db_value(7, None, None, None) == Amount(7, 'BTC')
def test_amount_field_get_prep_value(): field = AmountField('price', currency='BTC', default='5', max_digits=9, decimal_places=2) assert field.get_prep_value(Amount(5, 'BTC')) == Decimal(5)
def test_pricerange(): tax_name = '2x Tax' tax = LinearTax(1, name=tax_name) price_range = PriceRange( Price(Amount(10, 'BTC'), Amount(10, 'BTC')), Price(Amount(20, 'BTC'), Amount(20, 'BTC'))) result = tax.apply(price_range) assert result.min_price == Price(Amount(10, 'BTC'), Amount(20, 'BTC')) assert result.max_price == Price(Amount(20, 'BTC'), Amount(40, 'BTC'))
def test_amount_field_get_db_prep_save(): field = AmountField('price', currency='BTC', default='5', max_digits=9, decimal_places=2) value = field.get_db_prep_save(Amount(5, 'BTC'), connection) assert value == '5.00'
def test_discount(): price = Price(Amount(100, 'BTC'), Amount(100, 'BTC')) discount = FractionalDiscount(factor='0.25') result = discount.apply(price) assert result.net == Amount(75, 'BTC') assert result.gross == Amount(75, 'BTC') price_range = PriceRange(price, price) result = discount.apply(price_range) assert result.min_price == Price(Amount(75, 'BTC'), Amount(75, 'BTC')) assert result.max_price == Price(Amount(75, 'BTC'), Amount(75, 'BTC'))
def test_discount(): price = Price(Amount(100, 'BTC'), Amount(100, 'BTC')) discount = percentage_discount(value=10, name='Ten percent off') result = discount.apply(price) assert result.net == Amount(90, 'BTC') assert result.gross == Amount(90, 'BTC') price_range = PriceRange(price, price) result = discount.apply(price_range) assert result.min_price == Price(Amount(90, 'BTC'), Amount(90, 'BTC')) assert result.max_price == Price(Amount(90, 'BTC'), Amount(90, 'BTC'))
def test_amount_field_from_db_value_checks_currency(): field = AmountField('price', currency='BTC', default='5', max_digits=9, decimal_places=2) invalid = Amount(1, 'USD') with pytest.raises(ValueError): field.from_db_value(invalid, None, None, None)
def to_python(self, value): if isinstance(value, Amount): if value.currency != self.currency: raise ValueError('Invalid currency: %r (expected %r)' % (value.currency, self.currency)) return value value = super(AmountField, self).to_python(value) if value is None: return value return Amount(value, self.currency)
def test_addition(): amount = Amount(10, 'BTC') + Amount(20, 'BTC') assert amount.value == 30 assert amount == Amount(30, 'BTC') with pytest.raises(ValueError): Amount(10, 'BTC') + Amount(30, 'USD') with pytest.raises(TypeError): Amount(10, 'USD') + 1
def test_construction(): price1 = Price(Amount(10, 'EUR'), Amount(15, 'EUR')) price2 = Price(Amount(30, 'EUR'), Amount(45, 'EUR')) price_range = PriceRange(price1, price2) assert price_range.min_price == price1 assert price_range.max_price == price2 with pytest.raises(ValueError): PriceRange(price1, Price(Amount(20, 'PLN'), Amount(20, 'PLN'))) with pytest.raises(ValueError): PriceRange(price2, price1)
def test_replace(): price1 = Price(Amount(10, 'EUR'), Amount(15, 'EUR')) price2 = Price(Amount(30, 'EUR'), Amount(45, 'EUR')) price3 = Price(Amount(20, 'EUR'), Amount(30, 'EUR')) price_range = PriceRange(price1, price2) result = price_range.replace(max_price=price3) assert result.min_price == price1 assert result.max_price == price3 result = price_range.replace(min_price=price3) assert result.min_price == price3 assert result.max_price == price2
def test_instance_values_different_currency(db): with pytest.raises(ValueError): model = Model(price_gross=Amount(10, 'USD')) model.save()
def test_comparison(): price1 = Price(Amount(10, 'EUR'), Amount(15, 'EUR')) price2 = Price(Amount(30, 'EUR'), Amount(45, 'EUR')) price_range1 = PriceRange(price1, price2) price3 = Price(Amount(40, 'EUR'), Amount(60, 'EUR')) price4 = Price(Amount(80, 'EUR'), Amount(120, 'EUR')) price_range2 = PriceRange(price3, price4) assert price_range1 == PriceRange(price1, price2) assert price_range1 != price_range2 assert price_range1 != PriceRange(price1, price1) assert price_range1 != PriceRange( Price(Amount(10, 'USD'), Amount(15, 'USD')), Price(Amount(30, 'USD'), Amount(45, 'USD'))) assert price_range1 != price1
def test_subtraction(): price1 = Price(Amount(10, 'EUR'), Amount(15, 'EUR')) price2 = Price(Amount(30, 'EUR'), Amount(45, 'EUR')) price_range1 = PriceRange(price1, price2) price3 = Price(Amount(40, 'EUR'), Amount(60, 'EUR')) price4 = Price(Amount(80, 'EUR'), Amount(120, 'EUR')) price_range2 = PriceRange(price3, price4) result = price_range2 - price_range1 assert result.min_price == price3 - price1 assert result.max_price == price4 - price2 result = price_range2 - price1 assert result.min_price == price3 - price1 assert result.max_price == price4 - price1 with pytest.raises(ValueError): price_range2 - PriceRange(Price(Amount(1, 'BTC'), Amount(1, 'BTC')), Price(Amount(2, 'BTC'), Amount(2, 'BTC'))) with pytest.raises(ValueError): price_range2 - Price(Amount(1, 'BTC'), Amount(1, 'BTC')) with pytest.raises(TypeError): price_range2 - 1
def test_addition(): price1 = Price(Amount(10, 'EUR'), Amount(15, 'EUR')) price2 = Price(Amount(30, 'EUR'), Amount(45, 'EUR')) price_range1 = PriceRange(price1, price2) price3 = Price(Amount(40, 'EUR'), Amount(60, 'EUR')) price4 = Price(Amount(80, 'EUR'), Amount(120, 'EUR')) price_range2 = PriceRange(price3, price4) result = price_range1 + price_range2 assert result.min_price == price1 + price3 assert result.max_price == price2 + price4 result = price_range1 + price3 assert result.min_price == price1 + price3 assert result.max_price == price2 + price3 with pytest.raises(ValueError): price_range1 + PriceRange(Price(Amount(1, 'BTC'), Amount(1, 'BTC')), Price(Amount(2, 'BTC'), Amount(2, 'BTC'))) with pytest.raises(ValueError): price_range1 + Price(Amount(1, 'BTC'), Amount(1, 'BTC')) with pytest.raises(TypeError): price_range1 + 1
def test_set_instance_values(): instance = Model() instance.price = Price(Amount(25, 'BTC'), Amount(30, 'BTC')) assert instance.price_net == Amount(25, 'BTC') assert instance.price_gross == Amount(30, 'BTC')
def test_amount_field_init(): field = AmountField(currency='BTC', default='5', max_digits=9, decimal_places=2) assert field.get_default() == Amount(5, 'BTC')
def test_templatetag_discount_amount_for(): price = Price(Amount(30, 'BTC'), Amount(30, 'BTC')) discount = percentage_discount(50) discount_amount = prices.discount_amount_for(discount, price) assert discount_amount == Price(Amount(-15, 'BTC'), Amount(-15, 'BTC'))
def amount_fixture(): return Amount('10', 'USD')
def amount_with_decimals(): return Amount('10.20', 'USD')
def price_fixture(): return Price(net=Amount('10', 'USD'), gross=Amount('15', 'USD'))
def test_currency(): price1 = Price(Amount(10, 'EUR'), Amount(15, 'EUR')) price2 = Price(Amount(30, 'EUR'), Amount(45, 'EUR')) price_range = PriceRange(price1, price2) assert price_range.currency == 'EUR'
def price_with_decimals(): return Price(net=Amount('10.20', 'USD'), gross=Amount('15', 'USD'))