Exemple #1
0
class ParrotTaxTest(TestCase):
    def setUp(self):
        self.macaw = DeadParrot.objects.create(slug='macaw',
                                               species="Hyacinth Macaw")
        self.cockatoo = DeadParrot.objects.create(slug='cockatoo',
                                                  species="White Cockatoo")
        self.macaw_blue_a = self.macaw.variants.create(color='blue',
                                                       sku='M-BL-A',
                                                       looks_alive=True)
        self.macaw_blue_d = self.macaw.variants.create(color='blue',
                                                       sku='M-BL-D',
                                                       looks_alive=False)
        self.cockatoo_white_a = self.cockatoo.variants.create(color='white',
                                                              sku='C-WH-A',
                                                              looks_alive=True)
        self.cockatoo_green_a = self.cockatoo.variants.create(color='green',
                                                              sku='C-GR-A-',
                                                              looks_alive=True)
        macaw_price = ProductPrice.objects.create(product=self.macaw,
                                                  price=Decimal('10.0'))
        macaw_price.offsets.create(variant=self.macaw_blue_a,
                                   price_offset=Decimal('2.0'))
        cockatoo_price = ProductPrice.objects.create(product=self.cockatoo,
                                                     price=Decimal('20.0'))
        cockatoo_price.offsets.create(variant=self.cockatoo_green_a,
                                      price_offset=Decimal('5.0'))
        # create tax groups
        self.vat8 = models.TaxGroup.objects.create(name="VAT 8%", rate=8,
                                                   rate_name="8%")
        self.vat23 = models.TaxGroup.objects.create(name="VAT 23%", rate=23,
                                                    rate_name="23%")
        self.vat8.products.add(self.macaw)
        # set the pricing pipeline
        self.pricing_queue = PricingQueue(
            'satchless.contrib.pricing.simpleqty.SimpleQtyPricingHandler',
            'satchless.contrib.tax.flatgroups.FlatGroupPricingHandler')

    def test_nodefault(self):
        # these have 8% VAT
        self.assertEqual(self.pricing_queue.get_variant_price(self.macaw_blue_d,
                                                   currency='PLN'),
                         Price(10, Decimal('10.80'), currency='PLN'))
        self.assertEqual(self.pricing_queue.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(self.pricing_queue.get_variant_price(self.cockatoo_white_a,
                                                   currency='PLN'),
                         Price(20, 20, currency='PLN'))
        self.assertEqual(self.pricing_queue.get_variant_price(self.cockatoo_green_a,
                                                   currency='PLN'),
                         Price(25, 25, currency='PLN'))
        pr = self.pricing_queue.get_product_price_range(self.cockatoo, currency='PLN')
        self.assertEqual(pr, PriceRange(min_price=Price(20, 20,
                                                        currency='PLN'),
                                        max_price=Price(25, 25,
                                                        currency='PLN')))

    def test_default(self):
        self.vat23.default = True
        self.vat23.save()
        # these have 8% VAT
        self.assertEqual(self.pricing_queue.get_variant_price(self.macaw_blue_d,
                                                   currency='PLN'),
                         Price(10, Decimal('10.80'), currency='PLN'))
        self.assertEqual(self.pricing_queue.get_variant_price(self.macaw_blue_a,
                                                   currency='PLN'),
                         Price(12, Decimal('12.96'), currency='PLN'))
        # while these have default 23% VAT
        self.assertEqual(self.pricing_queue.get_variant_price(self.cockatoo_white_a,
                                                   currency='PLN'),
                         Price(20, Decimal('24.60'), currency='PLN'))
        self.assertEqual(self.pricing_queue.get_variant_price(self.cockatoo_green_a,
                                                   currency='PLN'),
                         Price(25, Decimal('30.75'), currency='PLN'))
        pr = self.pricing_queue.get_product_price_range(self.cockatoo, currency='PLN')
        self.assertEqual(pr, PriceRange(min_price=Price(20, Decimal('24.60'),
                                                        currency='PLN'),
                                        max_price=Price(25, Decimal('30.75'),
                                                        currency='PLN')))
Exemple #2
0
class ParrotTest(TestCase):
    TEST_PRICING_HANDLERS = [
        'satchless.contrib.pricing.simpleqty.SimpleQtyPricingHandler',
    ]

    def setUp(self):
        self.macaw = DeadParrot.objects.create(slug='macaw',
                                               species="Hyacinth Macaw")
        self.cockatoo = DeadParrot.objects.create(slug='cockatoo',
                                                  species="White Cockatoo")
        self.macaw_blue_a = self.macaw.variants.create(color='blue',
                                                       sku='M-BL-A',
                                                       looks_alive=True)
        self.macaw_blue_d = self.macaw.variants.create(color='blue',
                                                       sku='M-BL-D',
                                                       looks_alive=False)
        self.macaw_red_a = self.macaw.variants.create(color='red',
                                                      sku='M-RD-A',
                                                      looks_alive=True)
        self.macaw_red_d = self.macaw.variants.create(color='red',
                                                      sku='M-RD-D',
                                                      looks_alive=False)
        self.cockatoo_white_a = self.cockatoo.variants.create(color='white',
                                                              sku='C-WH-A',
                                                              looks_alive=True)
        self.cockatoo_white_d = self.cockatoo.variants.create(color='white',
                                                              sku='C-WH-D',
                                                              looks_alive=False)
        self.cockatoo_green_a = self.cockatoo.variants.create(color='green',
                                                              sku='C-GR-A',
                                                              looks_alive=True)
        self.cockatoo_green_d = self.cockatoo.variants.create(color='green',
                                                              sku='C-GR-D',
                                                              looks_alive=False)

        self.original_pricing_handlers = settings.SATCHLESS_PRICING_HANDLERS
        self.pricing_queue = PricingQueue(*self.TEST_PRICING_HANDLERS)

    def tearDown(self):
        ProductPrice.objects.all().delete()
        self.pricing_queue = PricingQueue(*self.TEST_PRICING_HANDLERS)

    def test_price(self):
        p1 = Price(10)
        p2 = Price(10)
        self.assertEqual(p1, p2)
        p1 = Price(10,20)
        p2 = Price(10,20)
        self.assertEqual(p1, p2)
        p1 = Price(10,20)
        p2 = Price(20,10)
        self.assertNotEqual(p1, p2)
        self.assertEqual(p1 + p2, Price(30,30))
        self.assertEqual(p1 * 3, Price(30,60))

    def test_basicprices(self):
        macaw_price = ProductPrice.objects.create(product=self.macaw,
                                                  price=Decimal('10.0'))
        macaw_price.qty_overrides.create(min_qty=5, price=Decimal('9.0'))
        macaw_price.qty_overrides.create(min_qty=10, price=Decimal('8.0'))
        macaw_price.offsets.create(variant=self.macaw_blue_a,
                                   price_offset=Decimal('2.0'))
        self.assertEqual(self.pricing_queue.get_variant_price(self.macaw_blue_d, currency='BTC',
                                           quantity=1),
                         Price(Decimal('10.0'), currency='BTC'))
        self.assertEqual(self.pricing_queue.get_variant_price(self.macaw_blue_d, currency='BTC',
                                           quantity=Decimal('4.9999')),
                         Price(Decimal('10.0'), currency='BTC'))
        self.assertEqual(self.pricing_queue.get_variant_price(self.macaw_blue_d, currency='BTC',
                                           quantity=5),
                         Price(Decimal('9.0'), currency='BTC'))
        self.assertEqual(self.pricing_queue.get_variant_price(self.macaw_blue_d, currency='BTC',
                                           quantity=Decimal('9.9999')),
                         Price(Decimal('9.0'), currency='BTC'))
        self.assertEqual(self.pricing_queue.get_variant_price(self.macaw_blue_d, currency='BTC',
                                           quantity=10),
                         Price(Decimal('8.0'), currency='BTC'))
        self.assertEqual(self.pricing_queue.get_variant_price(self.macaw_blue_d, currency='BTC',
                                           quantity=100),
                         Price(Decimal('8.0'), currency='BTC'))
        self.assertEqual(self.pricing_queue.get_variant_price(self.macaw_blue_a, currency='BTC',
                                           quantity=1),
                         Price(Decimal('12.0'), currency='BTC'))
        self.assertEqual(self.pricing_queue.get_variant_price(self.macaw_blue_a, currency='BTC',
                                           quantity=Decimal('4.9999')),
                         Price(Decimal('12.0'), currency='BTC'))
        self.assertEqual(self.pricing_queue.get_variant_price(self.macaw_blue_a, currency='BTC',
                                           quantity=5),
                         Price(Decimal('11.0'), currency='BTC'))
        self.assertEqual(self.pricing_queue.get_variant_price(self.macaw_blue_a, currency='BTC',
                                           quantity=Decimal('9.9999')),
                         Price(Decimal('11.0'), currency='BTC'))
        self.assertEqual(self.pricing_queue.get_variant_price(self.macaw_blue_a, currency='BTC',
                                           quantity=10),
                         Price(Decimal('10.0'), currency='BTC'))
        self.assertEqual(self.pricing_queue.get_variant_price(self.macaw_blue_a, currency='BTC',
                                           quantity=100),
                         Price(Decimal('10.0'), currency='BTC'))

    def test_basicranges(self):
        macaw_price = ProductPrice.objects.create(product=self.macaw,
                                                  price=Decimal('10.0'))
        macaw_price.offsets.create(variant=self.macaw_blue_a,
                                   price_offset=Decimal('2.0'))
        macaw_price.offsets.create(variant=self.macaw_red_d,
                                   price_offset=Decimal('3.0'))
        macaw_price.offsets.create(variant=self.macaw_red_a,
                                   price_offset=Decimal('6.0'))
        cockatoo_price = ProductPrice.objects.create(product=self.cockatoo,
                                                     price=Decimal('12.0'))
        cockatoo_price.offsets.create(variant=self.cockatoo_white_d,
                                      price_offset=Decimal('-5.0'))
        cockatoo_price.offsets.create(variant=self.cockatoo_green_d,
                                      price_offset=Decimal('-8.0'))
        cockatoo_price.offsets.create(variant=self.cockatoo_green_a,
                                      price_offset=Decimal('4.0'))
        self.assertEqual(self.pricing_queue.get_product_price_range(self.macaw, currency='BTC'),
                        PriceRange(min_price=Price(Decimal('10.0'),
                                                   currency='BTC'),
                                   max_price=Price(Decimal('16.0'),
                                                   currency='BTC')))
        self.assertEqual(self.pricing_queue.get_product_price_range(self.cockatoo, currency='BTC'),
                        PriceRange(min_price=Price(Decimal('4.0'),
                                                   currency='BTC'),
                                   max_price=Price(Decimal('16.0'),
                                                   currency='BTC')))

    def test_cartprices(self):
        macaw_price = ProductPrice.objects.create(product=self.macaw,
                                                  price=Decimal('10.0'),
                                                  qty_mode='product')
        macaw_price.qty_overrides.create(min_qty=9, price=Decimal('9.0'))
        macaw_price.offsets.create(variant=self.macaw_blue_a,
                                   price_offset=Decimal('2.0'))
        cart = Cart.objects.create(typ='test')
        cart.set_quantity(self.macaw_blue_a, 4)
        cart.set_quantity(self.macaw_blue_d, 4)
        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(item_macaw_blue_d.get_unit_price(currency='BTC'),
                         Price(Decimal('10.0'), currency='BTC'))
        self.assertEqual(item_macaw_blue_a.get_unit_price(currency='BTC'),
                         Price(Decimal('12.0'), currency='BTC'))
        cart.add_quantity(self.macaw_blue_a, 1)
        cart.add_quantity(self.macaw_blue_d, 1)
        item_macaw_blue_a = cart.items.get(variant=self.macaw_blue_a)
        item_macaw_blue_d = cart.items.get(variant=self.macaw_blue_d)

        macaw_variant = item_macaw_blue_a.variant.get_subtype_instance()
        self.assertEqual(self.pricing_queue.get_variant_price(macaw_variant, currency='BTC',
                                           cart=cart),
                         Price(Decimal('11.0'), currency='BTC'))
        # contextless product
        self.assertEqual(self.pricing_queue.get_variant_price(macaw_variant, currency='BTC'),
                         Price(Decimal('12.0'), currency='BTC'))