def test_product_discount(self): # these have 30% discount self.assertEqual(handler.get_variant_price(self.macaw_blue_a, currency='BTW'), Price(7, Decimal('7.0'), currency='BTW')) # while these have no tax group, hence the discount is zero self.assertEqual(handler.get_variant_price(self.cockatoo_white_a, currency='BTW'), Price(20, 20, currency='BTW'))
def test_undiscounted_product_price_range(self): token = mock.Mock() token.split_contents.return_value = ('product_price_range', 'product', 'pricing_handler', 'currency=PLN', 'discount=0', 'as', 'price') parser = mock.Mock() def side_effect(arg): return template.Variable(arg) parser.compile_filter = mock.Mock(side_effect=side_effect) node = product_prices.product_price_range(parser, token) context = { 'product': 'product', 'PLN': 'PLN', '0': 0, 'pricing_handler': self.pricing_handler } node.render(context) self.assertEqual( context['price'].min_price, Price(net=5, gross=5 * decimal.Decimal('1.9'), currency=u'PLN')) self.assertEqual( context['price'].max_price, Price(net=5, gross=5 * decimal.Decimal('1.9'), currency=u'PLN'))
def test_nodefault(self): # these have 8% VAT self.assertEqual(handler.get_variant_price(self.macaw_blue_d, currency='PLN'), Price(10, Decimal('10.80'), currency='PLN')) self.assertEqual(handler.get_variant_price(self.macaw_blue_a, currency='PLN'), Price(12, Decimal('12.96'), currency='PLN')) # while these have no tax group, hence the tax is zero self.assertEqual(handler.get_variant_price(self.cockatoo_white_a, currency='PLN'), Price(20, 20, currency='PLN')) self.assertEqual(handler.get_variant_price(self.cockatoo_green_a, currency='PLN'), Price(25, 25, currency='PLN'))
def test_default(self): self.vat23.default = True self.vat23.save() # these have 8% VAT self.assertEqual(handler.get_variant_price(self.macaw_blue_d, currency='PLN'), Price(10, Decimal('10.80'), currency='PLN')) self.assertEqual(handler.get_variant_price(self.macaw_blue_a, currency='PLN'), Price(12, Decimal('12.96'), currency='PLN')) # while these have default 23% VAT self.assertEqual(handler.get_variant_price(self.cockatoo_white_a, currency='PLN'), Price(20, Decimal('24.60'), currency='PLN')) self.assertEqual(handler.get_variant_price(self.cockatoo_green_a, currency='PLN'), Price(25, Decimal('30.75'), currency='PLN'))
def test_cart_item_discounts(self): # same in cart cart = Cart.objects.create(typ='test') cart.set_quantity(self.macaw_blue_a, 1) item_macaw_blue_a = cart.items.get(variant=self.macaw_blue_a) self.assertEqual( handler.get_cartitem_unit_price(item_macaw_blue_a, currency='BTW'), Price(7, Decimal('7.0'), currency='BTW')) cart.set_quantity(self.cockatoo_white_a, 1) item_cockatoo_white_a = cart.items.get(variant=self.cockatoo_white_a) self.assertEqual( handler.get_cartitem_unit_price(item_cockatoo_white_a, currency='BTW'), Price(20, 20, currency='BTW'))
def test_undiscounted_price(self): price = self.pricing_queue.get_variant_price(None, u'PLN', quantity=1, discount=False) self.assertEqual(price, Price(net=5, gross=5*decimal.Decimal('1.9'), currency=u'PLN'))
def test_variant_price_tag_with_asvar(self): token = mock.Mock() token.split_contents.return_value = ('variant_price', 'product', 'pricing_handler', 'currency=PLN', 'as', 'price') parser = mock.Mock() def side_effect(arg): return template.Variable(arg) parser.compile_filter = mock.Mock(side_effect=side_effect) node = product_prices.variant_price(parser, token) self.assertEqual(node.item.var, 'product') self.assertEqual(node.kwargs['currency'].var, 'PLN') self.assertEqual(node.asvar, 'price') context = { 'product': 'product', 'PLN': 'PLN', '0': 0, 'pricing_handler': self.pricing_handler } node.render(context) self.assertEqual( context['price'], Price(net=5 * decimal.Decimal('0.9'), gross=(5 * decimal.Decimal('1.9') * decimal.Decimal('0.9')), currency=u'PLN'))
def test_undiscounted_variant_price_tag_without_asvar(self): token = mock.Mock() token.split_contents.return_value = ('variant_price', 'product', 'pricing_handler', 'currency=PLN', 'discount=0') parser = mock.Mock() def side_effect(arg): return template.Variable(arg) parser.compile_filter = mock.Mock(side_effect=side_effect) node = product_prices.variant_price(parser, token) #testing simple expresion self.assertEqual(node.item.var, 'product') self.assertEqual(node.pricing_handler.var, 'pricing_handler') self.assertEqual(node.kwargs['currency'].var, 'PLN') self.assertEqual(node.kwargs['discount'].var, '0') context = { 'product': 'product', 'PLN': 'PLN', '0': 0, 'pricing_handler': self.pricing_handler } result = node.render(context) self.assertEqual( result, Price(net=5, gross=5 * decimal.Decimal('1.9'), currency=u'PLN'))
def test_product_discount(self): discount = models.DiscountGroup.objects.create( name="40% discount", rate=40, rate_name="40% discount") product = TestProduct.objects.create(slug='product') variant = product.variants.create() discounted_product = TestProduct.objects.create( slug='discounted-product', discount=discount) discounted_variant = discounted_product.variants.create() self.assertEqual( self.pricing_queue.get_variant_price(variant, currency='PLN'), Price(5, Decimal('5.0'), currency='PLN')) self.assertEqual( self.pricing_queue.get_variant_price(discounted_variant, currency='PLN'), Price(3, 3, currency='PLN'))
def test_discounted_price(self): price = self.pricing_handler.get_variant_price(None, u'PLN', quantity=1, discount=True) self.assertEqual( price, Price(net=5 * decimal.Decimal('0.9'), gross=(5 * decimal.Decimal('1.9') * decimal.Decimal('0.9')), currency=u'PLN'))
def get_discount_amount(self, price): """ For Price objects multiplies only the gross amount. """ multi = (Decimal('100') - self.rate) / Decimal('100') if isinstance(price, Price): return Price(net=price.net * multi, gross=price.gross * multi, currency=price.currency) else: price = price * multi return price
def get_tax_amount(self, price): """ For Price objects multiplies only the gross amount. """ multi = (self.rate + Decimal('100')) / Decimal('100') if isinstance(price, Price): return Price(net=price.net, gross=price.gross * multi, tax_name=self.rate_name, currency=price.currency) else: price = price * multi price.tax_name = self.rate_name return price
def test_product_discount(self): discount = models.DiscountGroup.objects.create( name="40% discount", rate=40, rate_name="40% discount") hat = Hat.objects.create(name='top hat', slug='top-hat', price=5) variant = hat.variants.create(sku='1') discounted_product = Hat.objects.create(name='beret', slug='beret', price=5, discount=discount) discounted_variant = discounted_product.variants.create(sku='2') self.assertEqual( self.pricing_queue.get_variant_price(variant, currency='PLN'), Price(5, Decimal('5.0'), currency='PLN')) self.assertEqual( self.pricing_queue.get_variant_price(discounted_variant, currency='PLN'), Price(3, 3, currency='PLN'))
def test_default(self): self.vat23.default = True self.vat23.save() # these have 8% VAT self.assertEqual( handler.get_variant_price(self.macaw_blue_d, currency='PLN'), Price(10, Decimal('10.80'), currency='PLN')) self.assertEqual( handler.get_variant_price(self.macaw_blue_a, currency='PLN'), Price(12, Decimal('12.96'), currency='PLN')) # while these have default 23% VAT self.assertEqual( handler.get_variant_price(self.cockatoo_white_a, currency='PLN'), Price(20, Decimal('24.60'), currency='PLN')) self.assertEqual( handler.get_variant_price(self.cockatoo_green_a, currency='PLN'), Price(25, Decimal('30.75'), currency='PLN')) # same in cart cart = Cart.objects.create(typ='test') cart.set_quantity(self.macaw_blue_a, 3) cart.set_quantity(self.macaw_blue_d, 5) item_macaw_blue_a = cart.items.get(variant=self.macaw_blue_a) item_macaw_blue_d = cart.items.get(variant=self.macaw_blue_d) self.assertEqual( handler.get_cartitem_unit_price(item_macaw_blue_a, currency='PLN'), Price(12, Decimal('12.96'), currency='PLN')) self.assertEqual( handler.get_cartitem_unit_price(item_macaw_blue_d, currency='PLN'), Price(10, Decimal('10.80'), currency='PLN')) cart.set_quantity(self.cockatoo_white_a, 3) cart.set_quantity(self.cockatoo_green_a, 5) item_cockatoo_white_a = cart.items.get(variant=self.cockatoo_white_a) item_cockatoo_green_a = cart.items.get(variant=self.cockatoo_green_a) self.assertEqual( handler.get_cartitem_unit_price(item_cockatoo_white_a, currency='PLN'), Price(20, Decimal('24.60'), currency='PLN')) self.assertEqual( handler.get_cartitem_unit_price(item_cockatoo_green_a, currency='PLN'), Price(25, Decimal('30.75'), currency='PLN'))
def test_nodefault(self): # these have 8% VAT self.assertEqual( handler.get_variant_price(self.macaw_blue_d, currency='PLN'), Price(10, Decimal('10.80'), currency='PLN')) self.assertEqual( handler.get_variant_price(self.macaw_blue_a, currency='PLN'), Price(12, Decimal('12.96'), currency='PLN')) # while these have no tax group, hence the tax is zero self.assertEqual( handler.get_variant_price(self.cockatoo_white_a, currency='PLN'), Price(20, 20, currency='PLN')) self.assertEqual( handler.get_variant_price(self.cockatoo_green_a, currency='PLN'), Price(25, 25, currency='PLN')) # same in cart cart = Cart.objects.create(typ='test') cart.set_quantity(self.macaw_blue_a, 3) cart.set_quantity(self.macaw_blue_d, 5) item_macaw_blue_a = cart.items.get(variant=self.macaw_blue_a) item_macaw_blue_d = cart.items.get(variant=self.macaw_blue_d) self.assertEqual( handler.get_cartitem_unit_price(item_macaw_blue_a, currency='PLN'), Price(12, Decimal('12.96'), currency='PLN')) self.assertEqual( handler.get_cartitem_unit_price(item_macaw_blue_d, currency='PLN'), Price(10, Decimal('10.80'), currency='PLN')) cart.set_quantity(self.cockatoo_white_a, 3) cart.set_quantity(self.cockatoo_green_a, 5) item_cockatoo_white_a = cart.items.get(variant=self.cockatoo_white_a) item_cockatoo_green_a = cart.items.get(variant=self.cockatoo_green_a) self.assertEqual( handler.get_cartitem_unit_price(item_cockatoo_white_a, currency='PLN'), Price(20, 20, currency='PLN')) self.assertEqual( handler.get_cartitem_unit_price(item_cockatoo_green_a, currency='PLN'), Price(25, 25, currency='PLN'))
def _discount(self, price): return Price(currency=price.currency, net=price.net * decimal.Decimal('0.9'), gross=price.gross * decimal.Decimal('0.9'))
def _tax(self, price): return Price(currency=price.currency, net=price.net, gross=price.gross * decimal.Decimal('1.9'))
def get_product_price_range(self, *args, **kwargs): return PriceRange(min_price=Price(net=5, gross=5, currency=u'PLN'), max_price=Price(net=5, gross=5, currency=u'PLN'))
def get_product_price_range(cls, *args, **kwargs): return (Price(net=5, gross=5, currency=u'PLN'), Price(net=5, gross=5, currency=u'PLN'))
def get_variant_price(self, *args, **kwargs): return Price(net=5, gross=5, currency=u'PLN')