Exemple #1
0
class CartTestCase(TestCase):
    PRODUCT_PRICE = Decimal('100')
    TEN_PERCENT = Decimal(10) / Decimal(100)
    
    def create_fixtures(self):
        
        cart_modifiers_pool.USE_CACHE=False
        
        self.user = User.objects.create(username="******", email="*****@*****.**")
        self.product = Product()
        self.product.name = "TestPrduct"
        self.product.slug = "TestPrduct"
        self.product.short_description = "TestPrduct"
        self.product.long_description = "TestPrduct"
        self.product.active = True
        self.product.unit_price = self.PRODUCT_PRICE
        self.product.save()
        
        self.cart = Cart()
        self.cart.user = self.user
        self.cart.save()
    
    def test_01_empty_cart_costs_0(self):
        self.create_fixtures()
        with SettingsOverride(SHOP_CART_MODIFIERS=[]):
            
            self.cart.update()
            
            self.assertEqual(self.cart.subtotal_price, Decimal('0.0'))
            self.assertEqual(self.cart.total_price, Decimal('0.0'))
            
    def test_02_one_object_no_modifiers(self):
        self.create_fixtures()
        with SettingsOverride(SHOP_CART_MODIFIERS=[]):
            self.cart.add_product(self.product)
            self.cart.save()
            self.cart.update()
            self.cart.save()
            
            self.assertEqual(self.cart.subtotal_price, self.PRODUCT_PRICE)
            self.assertEqual(self.cart.total_price, self.PRODUCT_PRICE)
    
    def test_03_two_objects_no_modifier(self):
        self.create_fixtures()
        with SettingsOverride(SHOP_CART_MODIFIERS=[]):
            
            # We add two objects now :)
            self.cart.add_product(self.product,2)
            self.cart.update()
            self.cart.save()
            
            self.assertEqual(self.cart.subtotal_price, self.PRODUCT_PRICE*2)
            self.assertEqual(self.cart.total_price, self.PRODUCT_PRICE*2)
            
    def test_04_one_object_simple_modifier(self):
        self.create_fixtures()
        MODIFIERS = ['shop.cart.modifiers.tax_modifiers.TenPercentTaxModifier']
        with SettingsOverride(SHOP_CART_MODIFIERS=MODIFIERS):
            self.cart.add_product(self.product)
            self.cart.update()
            self.cart.save()
            
            self.assertEqual(self.cart.subtotal_price, self.PRODUCT_PRICE)
            self.assertEqual(self.cart.total_price, (self.TEN_PERCENT*self.PRODUCT_PRICE)+self.PRODUCT_PRICE)
            
    def test_05_one_object_two_modifiers_no_rebate(self):
        self.create_fixtures()
        MODIFIERS = ['shop.cart.modifiers.tax_modifiers.TenPercentTaxModifier',
                     'shop.cart.modifiers.rebate_modifiers.BulkRebateModifier']
        with SettingsOverride(SHOP_CART_MODIFIERS=MODIFIERS):
            self.cart.add_product(self.product)
            
            self.cart.update()
            self.cart.save()
            
            self.assertEqual(self.cart.subtotal_price, self.PRODUCT_PRICE)
            self.assertEqual(self.cart.total_price, (self.TEN_PERCENT*self.PRODUCT_PRICE)+self.PRODUCT_PRICE)
            
    def test_06_one_object_two_modifiers_with_rebate(self):
        self.create_fixtures()
        MODIFIERS = ['shop.cart.modifiers.tax_modifiers.TenPercentTaxModifier',
                     'shop.cart.modifiers.rebate_modifiers.BulkRebateModifier']
        with SettingsOverride(SHOP_CART_MODIFIERS=MODIFIERS):
            # We add 6 objects now :)
            self.cart.add_product(self.product,6)
            self.cart.update()
            self.cart.save()
            
            #subtotal is 600 - 10% = 540
            sub_should_be = (6*self.PRODUCT_PRICE) - (self.TEN_PERCENT*(6*self.PRODUCT_PRICE)) 
            total_should_be = sub_should_be + (self.TEN_PERCENT*sub_should_be) 
            
            self.assertEqual(self.cart.subtotal_price, sub_should_be)
            self.assertEqual(self.cart.total_price, total_should_be)
            
    def test_07_add_same_object_twice(self):
        self.create_fixtures()
        with SettingsOverride(SHOP_CART_MODIFIERS=[]):
            self.cart.add_product(self.product)
            self.cart.add_product(self.product)
            self.cart.update()
            self.cart.save()
            
            self.assertEqual(len(self.cart.items.all()),1)
            self.assertEqual(self.cart.items.all()[0].quantity, 2)
            
    def test_08_add_product_updates_last_updated(self):
        self.create_fixtures()
        initial = self.cart.last_updated
        self.cart.add_product(self.product)
        self.assertNotEqual(initial, self.cart.last_updated)
        
Exemple #2
0
class OrderConversionTestCase(TestCase):
    
    PRODUCT_PRICE = Decimal('100')
    TEN_PERCENT = Decimal(10) / Decimal(100)
    
    def create_fixtures(self):
        
        cart_modifiers_pool.USE_CACHE=False
        
        self.user = User.objects.create(username="******", 
                                        email="*****@*****.**",
                                        first_name="Test",
                                        last_name = "Toto")
        
        self.product = Product()
        self.product.name = "TestPrduct"
        self.product.slug = "TestPrduct"
        self.product.short_description = "TestPrduct"
        self.product.long_description = "TestPrduct"
        self.product.active = True
        self.product.unit_price = self.PRODUCT_PRICE
        self.product.save()
        
        self.cart = Cart()
        self.cart.user = self.user
        self.cart.save()
        
        self.client = Client()
        self.client.user = self.user
        self.client.save()
        
        self.country = Country.objects.create(name='CH')
        
        self.address = Address()
        self.address.client = self.client
        self.address.address = 'address'
        self.address.address2 = 'address2'
        self.address.zip_code = '1234'
        self.address.state = 'ZH'
        self.address.country = self.country
        self.address.is_billing = True
        self.address.is_shipping = True
        self.address.save()
        
        self.address2 = Address()
        self.address2.client = self.client
        self.address2.address = '2address'
        self.address2.address2 = '2address2'
        self.address2.zip_code = '21234'
        self.address2.state = '2ZH'
        self.address2.country = self.country
        self.address2.is_billing = True
        self.address2.is_shipping = False
        self.address2.save()
    
    def test_01_create_order_from_simple_cart(self):
        '''
        Let's make sure that all the info is copied over properly when using
        Order.objects.create_from_cart()
        '''
        self.create_fixtures()
        self.cart.add_product(self.product)
        self.cart.update()
        self.cart.save()
        
        o = Order.objects.create_from_cart(self.cart)
        
        self.assertNotEqual(o, None)
        
        ois = OrderItem.objects.filter(order=o)
        cis = CartItem.objects.filter(cart=self.cart)
        self.assertEqual(len(ois), len(cis))
        
        self.assertEqual(o.order_subtotal, self.cart.subtotal_price)
        self.assertEqual(o.order_total, self.cart.total_price)

    def test_02_create_order_from_taxed_cart(self):
        '''
        This time assert that everything is consistent with a tax cart modifier
        '''
        self.create_fixtures()
        MODIFIERS = ['shop.cart.modifiers.tax_modifiers.TenPercentTaxModifier']
        
        with SettingsOverride(SHOP_PRICE_MODIFIERS=MODIFIERS):

            self.cart.add_product(self.product)
            self.cart.update()
            self.cart.save()
            
            o = Order.objects.create_from_cart(self.cart,)
            
            # Must not return None, obviously
            self.assertNotEqual(o, None)
            
            # Compare all the OrderItems to all CartItems (length)
            ois = OrderItem.objects.filter(order=o)
            cis = CartItem.objects.filter(cart=self.cart)
            self.assertEqual(len(ois), len(cis))
            
            # Assert that there are as many extra_cart_price_fields than there
            # are extra order price fields
            e_cart_fields = self.cart.extra_price_fields
            e_order_fields = ExtraOrderPriceField.objects.filter(order=o)
            self.assertEqual(len(e_cart_fields), len(e_order_fields))
            
            # Check that totals match
            self.assertEqual(o.order_subtotal, self.cart.subtotal_price)
            self.assertEqual(o.order_total, self.cart.total_price)
            
    def test_03_order_addresses_match_user_preferences(self):
        self.create_fixtures()
        self.cart.add_product(self.product)
        self.cart.update()
        self.cart.save()
        
        self.address.is_billing = False
        self.address.save()
        
        o = Order.objects.create_from_cart(self.cart)
        # Must not return None, obviously
        self.assertNotEqual(o, None)
        # Check that addresses are transfered properly
        self.assertEqual(o.shipping_name, "%s %s" % (self.user.first_name, self.user.last_name))
        self.assertEqual(o.shipping_address, self.address.address)
        self.assertEqual(o.shipping_address2, self.address.address2)
        self.assertEqual(o.shipping_zip_code, self.address.zip_code)
        self.assertEqual(o.shipping_state, self.address.state)    
        self.assertEqual(o.shipping_country, self.address.country.name)
        
        self.assertEqual(o.billing_name, "%s %s" % (self.user.first_name, self.user.last_name))
        self.assertEqual(o.billing_address, self.address2.address)
        self.assertEqual(o.billing_address2, self.address2.address2)
        self.assertEqual(o.billing_zip_code, self.address2.zip_code)
        self.assertEqual(o.billing_state, self.address2.state)    
        self.assertEqual(o.billing_country, self.address2.country.name)
        
    def test_04_order_saves_item_pk_as_a_string(self):
        '''
        That's needed in case shipment or payment backends need to make fancy 
        calculations on products (i.e. shipping based on weight/size...)
        '''
        self.create_fixtures()
        self.cart.add_product(self.product)
        self.cart.update()
        self.cart.save()
        
        self.address.is_billing = False
        self.address.save()
        
        o = Order.objects.create_from_cart(self.cart)
        
        # Must not return None, obviously
        self.assertNotEqual(o, None)
        
        # take the first item from the order:
        oi = OrderItem.objects.filter(order=o)[0]
        self.assertEqual(oi.product_reference, str(self.product.id))
        
        # Lookup works?
        prod = oi.product
        self.assertEqual(prod,self.product)
class CartTestCase(TestCase):
    PRODUCT_PRICE = Decimal('100')
    TEN_PERCENT = Decimal(10) / Decimal(100)

    def setUp(self):

        cart_modifiers_pool.USE_CACHE=False

        self.user = User.objects.create(username="******", email="*****@*****.**")
        self.product = Product()
        self.product.name = "TestPrduct"
        self.product.slug = "TestPrduct"
        self.product.short_description = "TestPrduct"
        self.product.long_description = "TestPrduct"
        self.product.active = True
        self.product.unit_price = self.PRODUCT_PRICE
        self.product.save()

        self.cart = Cart()
        self.cart.user = self.user
        self.cart.save()

    def test_empty_cart_costs_0_quantity_0(self):
        with SettingsOverride(SHOP_CART_MODIFIERS=[]):

            self.cart.update()

            self.assertEqual(self.cart.subtotal_price, Decimal('0.0'))
            self.assertEqual(self.cart.total_price, Decimal('0.0'))
            self.assertEqual(self.cart.total_quantity, 0)

    def test_one_object_no_modifiers(self):
        with SettingsOverride(SHOP_CART_MODIFIERS=[]):
            self.cart.add_product(self.product)
            self.cart.save()
            self.cart.update()
            self.cart.save()

            self.assertEqual(self.cart.subtotal_price, self.PRODUCT_PRICE)
            self.assertEqual(self.cart.total_price, self.PRODUCT_PRICE)
            self.assertEqual(self.cart.total_quantity, 1)

    def test_two_objects_no_modifier(self):
        with SettingsOverride(SHOP_CART_MODIFIERS=[]):

            # We add two objects now :)
            self.cart.add_product(self.product,2)
            self.cart.update()
            self.cart.save()

            self.assertEqual(self.cart.subtotal_price, self.PRODUCT_PRICE*2)
            self.assertEqual(self.cart.total_price, self.PRODUCT_PRICE*2)
            self.assertEqual(self.cart.total_quantity, 2)

    def test_one_object_simple_modifier(self):
        MODIFIERS = ['shop.cart.modifiers.tax_modifiers.TenPercentGlobalTaxModifier']
        with SettingsOverride(SHOP_CART_MODIFIERS=MODIFIERS):
            self.cart.add_product(self.product)
            self.cart.update()
            self.cart.save()

            self.assertEqual(self.cart.subtotal_price, self.PRODUCT_PRICE)
            self.assertEqual(self.cart.total_price, (self.TEN_PERCENT*self.PRODUCT_PRICE)+self.PRODUCT_PRICE)

    def test_one_object_two_modifiers_no_rebate(self):
        MODIFIERS = ['shop.cart.modifiers.tax_modifiers.TenPercentGlobalTaxModifier',
                     'shop.cart.modifiers.rebate_modifiers.BulkRebateModifier']
        with SettingsOverride(SHOP_CART_MODIFIERS=MODIFIERS):
            self.cart.add_product(self.product)

            self.cart.update()
            self.cart.save()

            self.assertEqual(self.cart.subtotal_price, self.PRODUCT_PRICE)
            self.assertEqual(self.cart.total_price, (self.TEN_PERCENT*self.PRODUCT_PRICE)+self.PRODUCT_PRICE)

    def test_one_object_two_modifiers_with_rebate(self):
        MODIFIERS = ['shop.cart.modifiers.tax_modifiers.TenPercentGlobalTaxModifier',
                     'shop.cart.modifiers.rebate_modifiers.BulkRebateModifier']
        with SettingsOverride(SHOP_CART_MODIFIERS=MODIFIERS):
            # We add 6 objects now :)
            self.cart.add_product(self.product,6)
            self.cart.update()
            self.cart.save()

            #subtotal is 600 - 10% = 540
            sub_should_be = (6*self.PRODUCT_PRICE) - (self.TEN_PERCENT*(6*self.PRODUCT_PRICE))

            total_should_be = sub_should_be + (self.TEN_PERCENT*sub_should_be)

            self.assertEqual(self.cart.subtotal_price, sub_should_be)
            self.assertEqual(self.cart.total_price, total_should_be)

    def test_add_same_object_twice(self):
        with SettingsOverride(SHOP_CART_MODIFIERS=[]):
            self.assertEqual(self.cart.total_quantity, 0)
            self.cart.add_product(self.product)
            self.cart.add_product(self.product)
            self.cart.update()
            self.cart.save()

            self.assertEqual(len(self.cart.items.all()),1)
            self.assertEqual(self.cart.items.all()[0].quantity, 2)
            self.assertEqual(self.cart.total_quantity, 2)

    def test_add_same_object_twice_no_merge(self):
        with SettingsOverride(SHOP_CART_MODIFIERS=[]):
            self.assertEqual(self.cart.total_quantity, 0)
            self.cart.add_product(self.product, merge=False)
            self.cart.add_product(self.product, merge=False)
            self.cart.update()
            self.cart.save()

            self.assertEqual(len(self.cart.items.all()),2)
            self.assertEqual(self.cart.items.all()[0].quantity, 1)
            self.assertEqual(self.cart.items.all()[1].quantity, 1)

    def test_add_product_updates_last_updated(self):
        with SettingsOverride(SHOP_CART_MODIFIERS=[]):
            initial = self.cart.last_updated
            self.cart.add_product(self.product)
            self.assertNotEqual(initial, self.cart.last_updated)

    def test_cart_item_should_use_specific_type_to_get_price(self):
        base_product = BaseProduct.objects.create(unit_price=self.PRODUCT_PRICE)
        variation = base_product.productvariation_set.create(
                name="Variation 1"
                )
        with SettingsOverride(SHOP_CART_MODIFIERS=[]):
            self.cart.add_product(variation)
            self.cart.update()
            self.cart.save()
            self.assertEqual(self.cart.subtotal_price, self.PRODUCT_PRICE)

    def test_update_quantity_deletes(self):
        with SettingsOverride(SHOP_CART_MODIFIERS=[]):
            self.assertEqual(self.cart.total_quantity, 0)
            self.cart.add_product(self.product)
            self.cart.add_product(self.product)
            self.cart.update()
            self.cart.save()

            self.assertEqual(len(self.cart.items.all()),1)
            self.cart.update_quantity(self.cart.items.all()[0].id, 0)
            self.assertEqual(len(self.cart.items.all()),0)

    def test_custom_queryset_is_used_when_passed_to_method(self):
        with SettingsOverride(SHOP_CART_MODIFIERS=[]):
            # first we add any product
            self.cart.add_product(self.product)

            # now we try to select a CartItem that does not exist yet. This
            # could be an item with a yet unused combination of variations.
            qs = CartItem.objects.filter(cart=self.cart, product=self.product,
                                         quantity=42)
            # although we add the same product and have merge=True, there should
            # be a new CartItem being created now.
            self.cart.add_product(self.product, queryset=qs)
            self.assertEqual(len(self.cart.items.all()),2)
class CartModifiersTestCase(TestCase):

    PRODUCT_PRICE = Decimal('100')

    def setUp(self):
        cart_modifiers_pool.USE_CACHE = False
        self.user = User.objects.create(username="******",
            email="*****@*****.**")
        self.product = Product()
        self.product.name = "TestPrduct"
        self.product.slug = "TestPrduct"
        self.product.short_description = "TestPrduct"
        self.product.long_description = "TestPrduct"
        self.product.active = True
        self.product.unit_price = self.PRODUCT_PRICE
        self.product.save()

        self.cart = Cart()
        self.cart.user = self.user
        self.cart.save()

    def test_01_cart_modifier_pool_loads_modifiers_properly(self):
        """
        Let's add a price modifier to the settings, then load it,
        then call a method on it to make sure it works.
        """
        MODIFIERS = [
            'shop.cart.modifiers.tax_modifiers.TenPercentGlobalTaxModifier']
        with SettingsOverride(SHOP_CART_MODIFIERS=MODIFIERS):
            thelist = modifiers_pool.cart_modifiers_pool.get_modifiers_list()
            self.assertEqual(len(thelist), 1)
            instance = thelist[0]
            self.assertTrue(hasattr(instance, 'TAX_PERCENTAGE'))

    def test_02_cart_modifiers_pool_handles_wrong_path(self):
        MODIFIERS = ['shop2.cart.modifiers.tax_modifiers']  # wrong path
        with SettingsOverride(SHOP_CART_MODIFIERS=MODIFIERS):
            raised = False
            try:
                modifiers_pool.cart_modifiers_pool.get_modifiers_list()
            except:
                raised = True
            self.assertTrue(raised)

    def test_03_cart_modifiers_pool_handles_wrong_module(self):
        MODIFIERS = ['shop.cart.modifiers.tax_modifiers.IdontExist']
        with SettingsOverride(SHOP_CART_MODIFIERS=MODIFIERS):
            raised = False
            try:
                modifiers_pool.cart_modifiers_pool.get_modifiers_list()
            except ImproperlyConfigured:
                raised = True
            self.assertTrue(raised)

    def test_03_cart_modifiers_pool_handles_not_a_path(self):
        MODIFIERS = ['shop']
        with SettingsOverride(SHOP_CART_MODIFIERS=MODIFIERS):
            raised = False
            try:
                modifiers_pool.cart_modifiers_pool.get_modifiers_list()
            except ImproperlyConfigured:
                raised = True
            self.assertTrue(raised)

    def test_state_is_passed_around_properly(self):
        MODIFIERS = ['shop.tests.cart_modifiers.CarModifierUsingStatePassing']
        with SettingsOverride(SHOP_CART_MODIFIERS=MODIFIERS):
            self.cart.add_product(self.product)
            self.cart.save()
            self.cart.update()  # This should raise if the state isn't passed
class ProductOptionsTestCase(TestCase):

    PRODUCT_PRICE = Decimal("100")
    AWESOME_OPTION_PRICE = Decimal("50")  # The price of awesome?
    TEN_PERCENT = Decimal(10) / Decimal(100)

    def create_fixtures(self):
        cart_modifiers_pool.USE_CACHE = False

        self.user = User.objects.create(username="******", email="*****@*****.**", first_name="Test", last_name="Toto")

        self.product = Product()
        self.product.name = "TestPrduct"
        self.product.slug = "TestPrduct"
        self.product.short_description = "TestPrduct"
        self.product.long_description = "TestPrduct"
        self.product.active = True
        self.product.unit_price = self.PRODUCT_PRICE
        self.product.save()

        self.ogroup = OptionGroup()
        self.ogroup.product = self.product
        self.ogroup.name = "Test group"
        self.ogroup.save()

        self.option = Option()
        self.option.group = self.ogroup
        self.option.name = "Awesome"
        self.option.price = self.AWESOME_OPTION_PRICE
        self.option.save()

        self.cart = Cart()
        self.cart.user = self.user
        self.cart.save()

        self.cartitem = CartItem()
        self.cartitem.cart = self.cart
        self.cartitem.quantity = 1
        self.cartitem.product = self.product
        self.cartitem.save()

    def test_01_no_options_yield_normal_price(self):
        self.create_fixtures()
        MODIFIERS = ["shop.cart.modifiers.product_options.ProductOptionsModifier"]
        with SettingsOverride(SHOP_CART_MODIFIERS=MODIFIERS):
            # No need to add a product there is already on in the fixtures
            self.cart.update()
            self.cart.save()
            sub_should_be = 1 * self.PRODUCT_PRICE
            total_should_be = sub_should_be

            self.assertEqual(self.cart.subtotal_price, sub_should_be)
            self.assertEqual(self.cart.total_price, total_should_be)

    def test_02_awesome_option_increases_price_by_its_value(self):
        self.create_fixtures()

        c_item_option = CartItemOption()
        c_item_option.option = self.option
        c_item_option.cartitem = self.cartitem
        c_item_option.save()

        MODIFIERS = ["shop.cart.modifiers.product_options.ProductOptionsModifier"]
        with SettingsOverride(SHOP_CART_MODIFIERS=MODIFIERS):
            self.cart.update()
            self.cart.save()
            sub_should_be = (1 * self.PRODUCT_PRICE) + (1 * self.AWESOME_OPTION_PRICE)
            total_should_be = sub_should_be

            self.assertEqual(self.cart.subtotal_price, sub_should_be)
            self.assertEqual(self.cart.total_price, total_should_be)
Exemple #6
0
class OrderConversionTestCase(TestCase):

    PRODUCT_PRICE = Decimal('100')
    TEN_PERCENT = Decimal(10) / Decimal(100)

    def setUp(self):
        cart_modifiers_pool.USE_CACHE = False
        self.user = User.objects.create(username="******",
                                        email="*****@*****.**",
                                        first_name="Test",
                                        last_name="Toto")
        self.product = Product()
        self.product.name = "TestPrduct"
        self.product.slug = "TestPrduct"
        self.product.short_description = "TestPrduct"
        self.product.long_description = "TestPrduct"
        self.product.active = True
        self.product.unit_price = self.PRODUCT_PRICE
        self.product.save()

        self.cart = Cart()
        self.cart.user = self.user
        self.cart.save()

        #self.client.user = self.user
        #self.client.save()

        self.country = Country.objects.create(name='CH')

        self.address = Address()
        self.address.name = 'Test Toto'
        self.address.address = 'address'
        self.address.address2 = 'address2'
        self.address.zip_code = '1234'
        self.address.state = 'ZH'
        self.address.country = self.country
        self.address.is_billing = True
        self.address.is_shipping = True
        self.address.save()

        self.address2 = Address()
        self.address2.name = 'Test Toto'
        self.address2.address = '2address'
        self.address2.address2 = '2address2'
        self.address2.zip_code = '21234'
        self.address2.state = '2ZH'
        self.address2.country = self.country
        self.address2.is_billing = True
        self.address2.is_shipping = False
        self.address2.save()

    def test_create_order_from_simple_cart(self):
        """
        Let's make sure that all the info is copied over properly when using
        Order.objects.create_from_cart()
        """
        self.cart.add_product(self.product)
        self.cart.update()
        self.cart.save()

        o = Order.objects.create_from_cart(self.cart)

        self.assertNotEqual(o, None)

        ois = OrderItem.objects.filter(order=o)
        cis = CartItem.objects.filter(cart=self.cart)
        self.assertEqual(len(ois), len(cis))

        self.assertEqual(ois[0].line_subtotal, self.PRODUCT_PRICE)
        self.assertEqual(ois[0].line_total, self.PRODUCT_PRICE)

        self.assertEqual(o.order_subtotal, self.cart.subtotal_price)
        self.assertEqual(o.order_total, self.cart.total_price)

    def test_create_order_order_items_proper_product_name(self):
        baseproduct = BaseProduct.objects.create(name="Table",
                                                 unit_price=self.PRODUCT_PRICE)
        variation = ProductVariation.objects.create(baseproduct=baseproduct,
                                                    name="white")
        self.cart.add_product(variation)
        self.cart.update()
        self.cart.save()

        o = Order.objects.create_from_cart(self.cart)
        ois = OrderItem.objects.filter(order=o)
        self.assertEqual(ois[0].product_name, "Table - white")

    def test_create_order_from_taxed_cart(self):
        """
        This time assert that everything is consistent with a tax cart modifier
        """
        MODIFIERS = [
            'shop.cart.modifiers.tax_modifiers.TenPercentGlobalTaxModifier'
        ]

        with SettingsOverride(SHOP_CART_MODIFIERS=MODIFIERS):

            self.cart.add_product(self.product)
            self.cart.update()
            self.cart.save()

            o = Order.objects.create_from_cart(self.cart, )

            # Must not return None, obviously
            self.assertNotEqual(o, None)

            # Compare all the OrderItems to all CartItems (length)
            ois = OrderItem.objects.filter(order=o)
            cis = CartItem.objects.filter(cart=self.cart)
            self.assertEqual(len(ois), len(cis))

            self.assertEqual(ois[0].line_subtotal, self.PRODUCT_PRICE)
            self.assertEqual(ois[0].line_total, self.PRODUCT_PRICE)

            # Assert that there are as many extra_cart_price_fields than there
            # are extra order price fields
            e_cart_fields = self.cart.extra_price_fields
            e_order_fields = ExtraOrderPriceField.objects.filter(order=o)
            self.assertEqual(len(e_cart_fields), len(e_order_fields))

            # Check that totals match
            self.assertEqual(o.order_subtotal, self.cart.subtotal_price)
            self.assertEqual(o.order_total, self.cart.total_price)
            self.assertNotEqual(o.order_subtotal, Decimal("0"))
            self.assertNotEqual(o.order_total, Decimal("0"))

    def test_order_addresses_match_user_preferences(self):
        self.cart.add_product(self.product)
        self.cart.update()
        self.cart.save()

        self.address.is_billing = False
        self.address.save()

        o = Order.objects.create_from_cart(self.cart)
        # Must not return None, obviously
        self.assertNotEqual(o, None)

        o.set_shipping_address(self.address)
        o.set_billing_address(self.address2)

        self.assertEqual(o.shipping_address_text, self.address.as_text())
        self.assertEqual(o.billing_address_text, self.address2.as_text())

    def test_create_order_respects_product_specific_get_price_method(self):
        if SKIP_BASEPRODUCT_TEST:
            return
        baseproduct = BaseProduct.objects.create(unit_price=Decimal('10.0'))
        product = ProductVariation.objects.create(baseproduct=baseproduct)

        self.cart.add_product(product)
        self.cart.update()
        self.cart.save()
        o = Order.objects.create_from_cart(self.cart)
        oi = OrderItem.objects.filter(order=o)[0]
        self.assertEqual(oi.unit_price, baseproduct.unit_price)

    def test_create_from_cart_respects_get_product_reference(self):
        self.cart.add_product(self.product)
        self.cart.update()
        self.cart.save()

        o = Order.objects.create_from_cart(self.cart)
        oi = OrderItem.objects.filter(order=o)[0]
        self.assertEqual(oi.product_reference,
                         self.product.get_product_reference())
Exemple #7
0
class OrderConversionTestCase(TestCase):

    PRODUCT_PRICE = Decimal('100')
    TEN_PERCENT = Decimal(10) / Decimal(100)

    def setUp(self):
        cart_modifiers_pool.USE_CACHE = False
        self.user = User.objects.create(username="******",
                                        email="*****@*****.**",
                                        first_name="Test",
                                        last_name="Toto")
        self.product = Product()
        self.product.name = "TestPrduct"
        self.product.slug = "TestPrduct"
        self.product.short_description = "TestPrduct"
        self.product.long_description = "TestPrduct"
        self.product.active = True
        self.product.unit_price = self.PRODUCT_PRICE
        self.product.save()

        self.cart = Cart()
        self.cart.user = self.user
        self.cart.save()

        #self.client.user = self.user
        #self.client.save()

        self.country = Country.objects.create(name='CH')

        self.address = Address()
        self.address.name = 'Test Toto'
        self.address.address = 'address'
        self.address.address2 = 'address2'
        self.address.zip_code = '1234'
        self.address.state = 'ZH'
        self.address.country = self.country
        self.address.is_billing = True
        self.address.is_shipping = True
        self.address.save()

        self.address2 = Address()
        self.address2.name = 'Test Toto'
        self.address2.address = '2address'
        self.address2.address2 = '2address2'
        self.address2.zip_code = '21234'
        self.address2.state = '2ZH'
        self.address2.country = self.country
        self.address2.is_billing = True
        self.address2.is_shipping = False
        self.address2.save()

    def test_create_order_from_simple_cart(self):
        """
        Let's make sure that all the info is copied over properly when using
        Order.objects.create_from_cart()
        """
        self.cart.add_product(self.product)
        self.cart.update()
        self.cart.save()

        o = Order.objects.create_from_cart(self.cart)

        self.assertNotEqual(o, None)

        ois = OrderItem.objects.filter(order=o)
        cis = CartItem.objects.filter(cart=self.cart)
        self.assertEqual(len(ois), len(cis))

        self.assertEqual(ois[0].line_subtotal, self.PRODUCT_PRICE)
        self.assertEqual(ois[0].line_total, self.PRODUCT_PRICE)

        self.assertEqual(o.order_subtotal, self.cart.subtotal_price)
        self.assertEqual(o.order_total, self.cart.total_price)

    def test_create_order_order_items_proper_product_name(self):
        baseproduct = BaseProduct.objects.create(
                name="Table",
                unit_price=self.PRODUCT_PRICE
                )
        variation = ProductVariation.objects.create(
                baseproduct=baseproduct,
                name="white"
                )
        self.cart.add_product(variation)
        self.cart.update()
        self.cart.save()

        o = Order.objects.create_from_cart(self.cart)
        ois = OrderItem.objects.filter(order=o)
        self.assertEqual(ois[0].product_name, "Table - white")

    def test_create_order_from_taxed_cart(self):
        """
        This time assert that everything is consistent with a tax cart modifier
        """
        MODIFIERS = [
            'shop.cart.modifiers.tax_modifiers.TenPercentGlobalTaxModifier']

        with SettingsOverride(SHOP_CART_MODIFIERS=MODIFIERS):

            self.cart.add_product(self.product)
            self.cart.update()
            self.cart.save()

            o = Order.objects.create_from_cart(self.cart,)

            # Must not return None, obviously
            self.assertNotEqual(o, None)

            # Compare all the OrderItems to all CartItems (length)
            ois = OrderItem.objects.filter(order=o)
            cis = CartItem.objects.filter(cart=self.cart)
            self.assertEqual(len(ois), len(cis))

            self.assertEqual(ois[0].line_subtotal, self.PRODUCT_PRICE)
            self.assertEqual(ois[0].line_total, self.PRODUCT_PRICE)

            # Assert that there are as many extra_cart_price_fields than there
            # are extra order price fields
            e_cart_fields = self.cart.extra_price_fields
            e_order_fields = ExtraOrderPriceField.objects.filter(order=o)
            self.assertEqual(len(e_cart_fields), len(e_order_fields))

            # Check that totals match
            self.assertEqual(o.order_subtotal, self.cart.subtotal_price)
            self.assertEqual(o.order_total, self.cart.total_price)
            self.assertNotEqual(o.order_subtotal, Decimal("0"))
            self.assertNotEqual(o.order_total, Decimal("0"))

    def test_order_addresses_match_user_preferences(self):
        self.cart.add_product(self.product)
        self.cart.update()
        self.cart.save()

        self.address.is_billing = False
        self.address.save()

        o = Order.objects.create_from_cart(self.cart)
        # Must not return None, obviously
        self.assertNotEqual(o, None)

        o.set_shipping_address(self.address)
        o.set_billing_address(self.address2)

        self.assertEqual(o.shipping_address_text, self.address.as_text())
        self.assertEqual(o.billing_address_text, self.address2.as_text())

    def test_create_order_respects_product_specific_get_price_method(self):
        if SKIP_BASEPRODUCT_TEST:
            return
        baseproduct = BaseProduct.objects.create(unit_price=Decimal('10.0'))
        product = ProductVariation.objects.create(baseproduct=baseproduct)

        self.cart.add_product(product)
        self.cart.update()
        self.cart.save()
        o = Order.objects.create_from_cart(self.cart)
        oi = OrderItem.objects.filter(order=o)[0]
        self.assertEqual(oi.unit_price, baseproduct.unit_price)

    def test_create_from_cart_respects_get_product_reference(self):
        self.cart.add_product(self.product)
        self.cart.update()
        self.cart.save()

        o = Order.objects.create_from_cart(self.cart)
        oi = OrderItem.objects.filter(order=o)[0]
        self.assertEqual(oi.product_reference, self.product.get_product_reference())
Exemple #8
0
class CartTestCase(TestCase):
    PRODUCT_PRICE = Decimal('100')
    TEN_PERCENT = Decimal(10) / Decimal(100)

    def setUp(self):
        cart_modifiers_pool.USE_CACHE = False
        self.user = User.objects.create(username="******",
                                        email="*****@*****.**")
        self.product = Product()
        self.product.name = "TestPrduct"
        self.product.slug = "TestPrduct"
        self.product.short_description = "TestPrduct"
        self.product.long_description = "TestPrduct"
        self.product.active = True
        self.product.unit_price = self.PRODUCT_PRICE
        self.product.save()

        self.cart = Cart()
        self.cart.user = self.user
        self.cart.save()

    def test_empty_cart_costs_0_quantity_0(self):
        with SettingsOverride(SHOP_CART_MODIFIERS=[]):

            self.cart.update()

            self.assertEqual(self.cart.subtotal_price, Decimal('0.0'))
            self.assertEqual(self.cart.total_price, Decimal('0.0'))
            self.assertEqual(self.cart.total_quantity, 0)

    def test_one_object_no_modifiers(self):
        with SettingsOverride(SHOP_CART_MODIFIERS=[]):
            self.cart.add_product(self.product)
            self.cart.save()
            self.cart.update()
            self.cart.save()

            self.assertEqual(self.cart.subtotal_price, self.PRODUCT_PRICE)
            self.assertEqual(self.cart.total_price, self.PRODUCT_PRICE)
            self.assertEqual(self.cart.total_quantity, 1)

    def test_two_objects_no_modifier(self):
        with SettingsOverride(SHOP_CART_MODIFIERS=[]):

            # We add two objects now :)
            self.cart.add_product(self.product, 2)
            self.cart.update()
            self.cart.save()

            self.assertEqual(self.cart.subtotal_price, self.PRODUCT_PRICE * 2)
            self.assertEqual(self.cart.total_price, self.PRODUCT_PRICE * 2)
            self.assertEqual(self.cart.total_quantity, 2)

    def test_one_object_simple_modifier(self):
        MODIFIERS = [
            'shop.cart.modifiers.tax_modifiers.TenPercentGlobalTaxModifier'
        ]
        with SettingsOverride(SHOP_CART_MODIFIERS=MODIFIERS):
            self.cart.add_product(self.product)
            self.cart.update()
            self.cart.save()

            self.assertEqual(self.cart.subtotal_price, self.PRODUCT_PRICE)
            self.assertEqual(self.cart.total_price,
                             (self.TEN_PERCENT * self.PRODUCT_PRICE) +
                             self.PRODUCT_PRICE)

    def test_one_object_two_modifiers_no_rebate(self):
        MODIFIERS = [
            'shop.cart.modifiers.tax_modifiers.TenPercentGlobalTaxModifier',
            'shop.cart.modifiers.rebate_modifiers.BulkRebateModifier'
        ]
        with SettingsOverride(SHOP_CART_MODIFIERS=MODIFIERS):
            self.cart.add_product(self.product)

            self.cart.update()
            self.cart.save()

            self.assertEqual(self.cart.subtotal_price, self.PRODUCT_PRICE)
            self.assertEqual(self.cart.total_price,
                             (self.TEN_PERCENT * self.PRODUCT_PRICE) +
                             self.PRODUCT_PRICE)

    def test_one_object_two_modifiers_with_rebate(self):
        MODIFIERS = [
            'shop.cart.modifiers.tax_modifiers.TenPercentGlobalTaxModifier',
            'shop.cart.modifiers.rebate_modifiers.BulkRebateModifier'
        ]
        with SettingsOverride(SHOP_CART_MODIFIERS=MODIFIERS):
            # We add 6 objects now :)
            self.cart.add_product(self.product, 6)
            self.cart.update()
            self.cart.save()

            #subtotal is 600 - 10% = 540
            sub_should_be = (6 *
                             self.PRODUCT_PRICE) - (self.TEN_PERCENT *
                                                    (6 * self.PRODUCT_PRICE))

            total_should_be = sub_should_be + (self.TEN_PERCENT *
                                               sub_should_be)

            self.assertEqual(self.cart.subtotal_price, sub_should_be)
            self.assertEqual(self.cart.total_price, total_should_be)

    def test_add_same_object_twice(self):
        with SettingsOverride(SHOP_CART_MODIFIERS=[]):
            self.assertEqual(self.cart.total_quantity, 0)
            self.cart.add_product(self.product)
            self.cart.add_product(self.product)
            self.cart.update()
            self.cart.save()

            self.assertEqual(len(self.cart.items.all()), 1)
            self.assertEqual(self.cart.items.all()[0].quantity, 2)
            self.assertEqual(self.cart.total_quantity, 2)

    def test_add_same_object_twice_no_merge(self):
        with SettingsOverride(SHOP_CART_MODIFIERS=[]):
            self.assertEqual(self.cart.total_quantity, 0)
            self.cart.add_product(self.product, merge=False)
            self.cart.add_product(self.product, merge=False)
            self.cart.update()
            self.cart.save()

            self.assertEqual(len(self.cart.items.all()), 2)
            self.assertEqual(self.cart.items.all()[0].quantity, 1)
            self.assertEqual(self.cart.items.all()[1].quantity, 1)

    def test_add_product_updates_last_updated(self):
        with SettingsOverride(SHOP_CART_MODIFIERS=[]):
            initial = self.cart.last_updated
            self.cart.add_product(self.product)
            self.assertNotEqual(initial, self.cart.last_updated)

    def test_cart_item_should_use_specific_type_to_get_price(self):
        if SKIP_BASEPRODUCT_TEST:
            return
        base_product = BaseProduct.objects.create(
            unit_price=self.PRODUCT_PRICE)
        variation = base_product.productvariation_set.create(
            name="Variation 1")
        with SettingsOverride(SHOP_CART_MODIFIERS=[]):
            self.cart.add_product(variation)
            self.cart.update()
            self.cart.save()
            self.assertEqual(self.cart.subtotal_price, self.PRODUCT_PRICE)

    def test_update_quantity_deletes(self):
        with SettingsOverride(SHOP_CART_MODIFIERS=[]):
            self.assertEqual(self.cart.total_quantity, 0)
            self.cart.add_product(self.product)
            self.cart.add_product(self.product)
            self.cart.update()
            self.cart.save()

            self.assertEqual(len(self.cart.items.all()), 1)
            self.cart.update_quantity(self.cart.items.all()[0].pk, 0)
            self.assertEqual(len(self.cart.items.all()), 0)

    def test_custom_queryset_is_used_when_passed_to_method(self):
        with SettingsOverride(SHOP_CART_MODIFIERS=[]):
            # first we add any product
            self.cart.add_product(self.product)

            # now we try to select a CartItem that does not exist yet. This
            # could be an item with a yet unused combination of variations.
            qs = CartItem.objects.filter(cart=self.cart,
                                         product=self.product,
                                         quantity=42)
            # although we add the same product and have merge=True, there
            # should be a new CartItem being created now.
            self.cart.add_product(self.product, queryset=qs)
            self.assertEqual(len(self.cart.items.all()), 2)

    def test_get_updated_cart_items(self):
        self.cart.add_product(self.product)
        self.cart.update()
        cached_cart_items = self.cart.get_updated_cart_items()

        cart_items = CartItem.objects.filter(cart=self.cart)
        for item in cart_items:
            item.update({})

        self.assertEqual(len(cached_cart_items), len(cart_items))
        self.assertEqual(cached_cart_items[0].line_total,
                         cart_items[0].line_total)

    def test_get_updated_cart_items_without_updating_cart(self):
        with self.assertRaises(AssertionError):
            self.cart.get_updated_cart_items()
Exemple #9
0
class CartTestCase(TestCase):
    PRODUCT_PRICE = Decimal('100')
    TEN_PERCENT = Decimal(10) / Decimal(100)

    def create_fixtures(self):

        cart_modifiers_pool.USE_CACHE = False

        self.user = User.objects.create(username="******",
                                        email="*****@*****.**")
        self.product = Product()
        self.product.name = "TestPrduct"
        self.product.slug = "TestPrduct"
        self.product.short_description = "TestPrduct"
        self.product.long_description = "TestPrduct"
        self.product.active = True
        self.product.unit_price = self.PRODUCT_PRICE
        self.product.save()

        self.cart = Cart()
        self.cart.user = self.user
        self.cart.save()

    def test_01_empty_cart_costs_0(self):
        self.create_fixtures()
        with SettingsOverride(SHOP_CART_MODIFIERS=[]):

            self.cart.update()

            self.assertEqual(self.cart.subtotal_price, Decimal('0.0'))
            self.assertEqual(self.cart.total_price, Decimal('0.0'))

    def test_02_one_object_no_modifiers(self):
        self.create_fixtures()
        with SettingsOverride(SHOP_CART_MODIFIERS=[]):
            self.cart.add_product(self.product)
            self.cart.save()
            self.cart.update()
            self.cart.save()

            self.assertEqual(self.cart.subtotal_price, self.PRODUCT_PRICE)
            self.assertEqual(self.cart.total_price, self.PRODUCT_PRICE)

    def test_03_two_objects_no_modifier(self):
        self.create_fixtures()
        with SettingsOverride(SHOP_CART_MODIFIERS=[]):

            # We add two objects now :)
            self.cart.add_product(self.product, 2)
            self.cart.update()
            self.cart.save()

            self.assertEqual(self.cart.subtotal_price, self.PRODUCT_PRICE * 2)
            self.assertEqual(self.cart.total_price, self.PRODUCT_PRICE * 2)

    def test_04_one_object_simple_modifier(self):
        self.create_fixtures()
        MODIFIERS = [
            'shop.cart.modifiers.tax_modifiers.TenPercentGlobalTaxModifier'
        ]
        with SettingsOverride(SHOP_CART_MODIFIERS=MODIFIERS):
            self.cart.add_product(self.product)
            self.cart.update()
            self.cart.save()

            self.assertEqual(self.cart.subtotal_price, self.PRODUCT_PRICE)
            self.assertEqual(self.cart.total_price,
                             (self.TEN_PERCENT * self.PRODUCT_PRICE) +
                             self.PRODUCT_PRICE)

    def test_05_one_object_two_modifiers_no_rebate(self):
        self.create_fixtures()
        MODIFIERS = [
            'shop.cart.modifiers.tax_modifiers.TenPercentGlobalTaxModifier',
            'shop.cart.modifiers.rebate_modifiers.BulkRebateModifier'
        ]
        with SettingsOverride(SHOP_CART_MODIFIERS=MODIFIERS):
            self.cart.add_product(self.product)

            self.cart.update()
            self.cart.save()

            self.assertEqual(self.cart.subtotal_price, self.PRODUCT_PRICE)
            self.assertEqual(self.cart.total_price,
                             (self.TEN_PERCENT * self.PRODUCT_PRICE) +
                             self.PRODUCT_PRICE)

    def test_06_one_object_two_modifiers_with_rebate(self):
        self.create_fixtures()
        MODIFIERS = [
            'shop.cart.modifiers.tax_modifiers.TenPercentGlobalTaxModifier',
            'shop.cart.modifiers.rebate_modifiers.BulkRebateModifier'
        ]
        with SettingsOverride(SHOP_CART_MODIFIERS=MODIFIERS):
            # We add 6 objects now :)
            self.cart.add_product(self.product, 6)
            self.cart.update()
            self.cart.save()

            #subtotal is 600 - 10% = 540
            sub_should_be = (6 *
                             self.PRODUCT_PRICE) - (self.TEN_PERCENT *
                                                    (6 * self.PRODUCT_PRICE))

            total_should_be = sub_should_be + (self.TEN_PERCENT *
                                               sub_should_be)

            self.assertEqual(self.cart.subtotal_price, sub_should_be)
            self.assertEqual(self.cart.total_price, total_should_be)

    def test_07_add_same_object_twice(self):
        self.create_fixtures()
        with SettingsOverride(SHOP_CART_MODIFIERS=[]):
            self.cart.add_product(self.product)
            self.cart.add_product(self.product)
            self.cart.update()
            self.cart.save()

            self.assertEqual(len(self.cart.items.all()), 1)
            self.assertEqual(self.cart.items.all()[0].quantity, 2)

    def test_08_add_product_updates_last_updated(self):
        self.create_fixtures()
        with SettingsOverride(SHOP_CART_MODIFIERS=[]):
            initial = self.cart.last_updated
            self.cart.add_product(self.product)
            self.assertNotEqual(initial, self.cart.last_updated)

    def test_09_cart_item_should_use_specific_type_to_get_price(self):
        self.create_fixtures()
        base_product = BaseProduct.objects.create(
            unit_price=self.PRODUCT_PRICE)
        variation = base_product.productvariation_set.create(
            name="Variation 1")
        with SettingsOverride(SHOP_CART_MODIFIERS=[]):
            self.cart.add_product(variation)
            self.cart.update()
            self.cart.save()
            self.assertEqual(self.cart.subtotal_price, self.PRODUCT_PRICE)
Exemple #10
0
class ProductOptionsTestCase(TestCase):

    PRODUCT_PRICE = Decimal('100')
    AWESOME_OPTION_PRICE = Decimal('50')  # The price of awesome?
    TEN_PERCENT = Decimal(10) / Decimal(100)

    def create_fixtures(self):
        cart_modifiers_pool.USE_CACHE = False

        self.user = User.objects.create(username="******",
                                        email="*****@*****.**",
                                        first_name="Test",
                                        last_name="Toto")

        self.product = Product()
        self.product.name = "TestPrduct"
        self.product.slug = "TestPrduct"
        self.product.short_description = "TestPrduct"
        self.product.long_description = "TestPrduct"
        self.product.active = True
        self.product.unit_price = self.PRODUCT_PRICE
        self.product.save()

        self.ogroup = OptionGroup()
        self.ogroup.product = self.product
        self.ogroup.name = 'Test group'
        self.ogroup.save()

        self.option = Option()
        self.option.group = self.ogroup
        self.option.name = "Awesome"
        self.option.price = self.AWESOME_OPTION_PRICE
        self.option.save()

        self.cart = Cart()
        self.cart.user = self.user
        self.cart.save()

        self.cartitem = CartItem()
        self.cartitem.cart = self.cart
        self.cartitem.quantity = 1
        self.cartitem.product = self.product
        self.cartitem.save()

    def test_01_no_options_yield_normal_price(self):
        self.create_fixtures()
        MODIFIERS = [
            'shop.cart.modifiers.product_options.ProductOptionsModifier'
        ]
        with SettingsOverride(SHOP_CART_MODIFIERS=MODIFIERS):
            #No need to add a product there is already on in the fixtures
            self.cart.update()
            self.cart.save()
            sub_should_be = 1 * self.PRODUCT_PRICE
            total_should_be = sub_should_be

            self.assertEqual(self.cart.subtotal_price, sub_should_be)
            self.assertEqual(self.cart.total_price, total_should_be)

    def test_02_awesome_option_increases_price_by_its_value(self):
        self.create_fixtures()

        c_item_option = CartItemOption()
        c_item_option.option = self.option
        c_item_option.cartitem = self.cartitem
        c_item_option.save()

        MODIFIERS = [
            'shop.cart.modifiers.product_options.ProductOptionsModifier'
        ]
        with SettingsOverride(SHOP_CART_MODIFIERS=MODIFIERS):
            self.cart.update()
            self.cart.save()
            sub_should_be = (1 * self.PRODUCT_PRICE) + (
                1 * self.AWESOME_OPTION_PRICE)
            total_should_be = sub_should_be

            self.assertEqual(self.cart.subtotal_price, sub_should_be)
            self.assertEqual(self.cart.total_price, total_should_be)
Exemple #11
0
class CartModifiersTestCase(TestCase):

    PRODUCT_PRICE = Decimal('100')

    def setUp(self):
        cart_modifiers_pool.USE_CACHE = False
        self.user = User.objects.create(username="******",
                                        email="*****@*****.**")
        self.product = Product()
        self.product.name = "TestPrduct"
        self.product.slug = "TestPrduct"
        self.product.short_description = "TestPrduct"
        self.product.long_description = "TestPrduct"
        self.product.active = True
        self.product.unit_price = self.PRODUCT_PRICE
        self.product.save()

        self.cart = Cart()
        self.cart.user = self.user
        self.cart.save()

    def test_01_cart_modifier_pool_loads_modifiers_properly(self):
        """
        Let's add a price modifier to the settings, then load it,
        then call a method on it to make sure it works.
        """
        MODIFIERS = [
            'shop.cart.modifiers.tax_modifiers.TenPercentGlobalTaxModifier'
        ]
        with SettingsOverride(SHOP_CART_MODIFIERS=MODIFIERS):
            thelist = modifiers_pool.cart_modifiers_pool.get_modifiers_list()
            self.assertEqual(len(thelist), 1)
            instance = thelist[0]
            self.assertTrue(hasattr(instance, 'TAX_PERCENTAGE'))

    def test_02_cart_modifiers_pool_handles_wrong_path(self):
        MODIFIERS = ['shop2.cart.modifiers.tax_modifiers']  # wrong path
        with SettingsOverride(SHOP_CART_MODIFIERS=MODIFIERS):
            raised = False
            try:
                modifiers_pool.cart_modifiers_pool.get_modifiers_list()
            except:
                raised = True
            self.assertTrue(raised)

    def test_03_cart_modifiers_pool_handles_wrong_module(self):
        MODIFIERS = ['shop.cart.modifiers.tax_modifiers.IdontExist']
        with SettingsOverride(SHOP_CART_MODIFIERS=MODIFIERS):
            raised = False
            try:
                modifiers_pool.cart_modifiers_pool.get_modifiers_list()
            except ImproperlyConfigured:
                raised = True
            self.assertTrue(raised)

    def test_03_cart_modifiers_pool_handles_not_a_path(self):
        MODIFIERS = ['shop']
        with SettingsOverride(SHOP_CART_MODIFIERS=MODIFIERS):
            raised = False
            try:
                modifiers_pool.cart_modifiers_pool.get_modifiers_list()
            except ImproperlyConfigured:
                raised = True
            self.assertTrue(raised)

    def test_state_is_passed_around_properly(self):
        MODIFIERS = ['shop.tests.cart_modifiers.CarModifierUsingStatePassing']
        with SettingsOverride(SHOP_CART_MODIFIERS=MODIFIERS):
            self.cart.add_product(self.product)
            self.cart.save()
            self.cart.update()  # This should raise if the state isn't passed
Exemple #12
0
class OrderConversionTestCase(TestCase):

    PRODUCT_PRICE = Decimal("100")
    TEN_PERCENT = Decimal(10) / Decimal(100)

    def setUp(self):

        cart_modifiers_pool.USE_CACHE = False

        self.user = User.objects.create(username="******", email="*****@*****.**", first_name="Test", last_name="Toto")

        self.product = Product()
        self.product.name = "TestPrduct"
        self.product.slug = "TestPrduct"
        self.product.short_description = "TestPrduct"
        self.product.long_description = "TestPrduct"
        self.product.active = True
        self.product.unit_price = self.PRODUCT_PRICE
        self.product.save()

        self.cart = Cart()
        self.cart.user = self.user
        self.cart.save()

        # self.client.user = self.user
        # self.client.save()

        self.country = Country.objects.create(name="CH")

        self.address = Address()
        self.address.name = "Test Toto"
        self.address.address = "address"
        self.address.address2 = "address2"
        self.address.zip_code = "1234"
        self.address.state = "ZH"
        self.address.country = self.country
        self.address.is_billing = True
        self.address.is_shipping = True
        self.address.save()

        self.address2 = Address()
        self.address2.name = "Test Toto"
        self.address2.address = "2address"
        self.address2.address2 = "2address2"
        self.address2.zip_code = "21234"
        self.address2.state = "2ZH"
        self.address2.country = self.country
        self.address2.is_billing = True
        self.address2.is_shipping = False
        self.address2.save()

    def test_create_order_from_simple_cart(self):
        """
        Let's make sure that all the info is copied over properly when using
        Order.objects.create_from_cart()
        """
        self.cart.add_product(self.product)
        self.cart.update()
        self.cart.save()

        o = Order.objects.create_from_cart(self.cart)

        self.assertNotEqual(o, None)

        ois = OrderItem.objects.filter(order=o)
        cis = CartItem.objects.filter(cart=self.cart)
        self.assertEqual(len(ois), len(cis))

        self.assertEqual(ois[0].line_subtotal, self.PRODUCT_PRICE)
        self.assertEqual(ois[0].line_total, self.PRODUCT_PRICE)

        self.assertEqual(o.order_subtotal, self.cart.subtotal_price)
        self.assertEqual(o.order_total, self.cart.total_price)

    def test_create_order_from_taxed_cart(self):
        """
        This time assert that everything is consistent with a tax cart modifier
        """
        MODIFIERS = ["shop.cart.modifiers.tax_modifiers.TenPercentGlobalTaxModifier"]

        with SettingsOverride(SHOP_CART_MODIFIERS=MODIFIERS):

            self.cart.add_product(self.product)
            self.cart.update()
            self.cart.save()

            o = Order.objects.create_from_cart(self.cart)

            # Must not return None, obviously
            self.assertNotEqual(o, None)

            # Compare all the OrderItems to all CartItems (length)
            ois = OrderItem.objects.filter(order=o)
            cis = CartItem.objects.filter(cart=self.cart)
            self.assertEqual(len(ois), len(cis))

            self.assertEqual(ois[0].line_subtotal, self.PRODUCT_PRICE)
            self.assertEqual(ois[0].line_total, self.PRODUCT_PRICE)

            # Assert that there are as many extra_cart_price_fields than there
            # are extra order price fields
            e_cart_fields = self.cart.extra_price_fields
            e_order_fields = ExtraOrderPriceField.objects.filter(order=o)
            self.assertEqual(len(e_cart_fields), len(e_order_fields))

            # Check that totals match
            self.assertEqual(o.order_subtotal, self.cart.subtotal_price)
            self.assertEqual(o.order_total, self.cart.total_price)
            self.assertNotEqual(o.order_subtotal, Decimal("0"))
            self.assertNotEqual(o.order_total, Decimal("0"))

    def test_order_addresses_match_user_preferences(self):
        self.cart.add_product(self.product)
        self.cart.update()
        self.cart.save()

        self.address.is_billing = False
        self.address.save()

        o = Order.objects.create_from_cart(self.cart)
        # Must not return None, obviously
        self.assertNotEqual(o, None)

        o.set_shipping_address(
            self.address.address,
            self.address.city,
            self.address.zip_code,
            self.address.state,
            self.address.country,
            self.address.name,
            self.address.address2,
        )

        o.set_billing_address(
            self.address2.address,
            self.address2.city,
            self.address2.zip_code,
            self.address2.state,
            self.address2.country,
            self.address2.name,
            self.address2.address2,
        )

        # Check that addresses are transfered properly
        self.assertEqual(o.shipping_name, "%s %s" % (self.user.first_name, self.user.last_name))
        self.assertEqual(o.shipping_address, self.address.address)
        self.assertEqual(o.shipping_address2, self.address.address2)
        self.assertEqual(o.shipping_zip_code, self.address.zip_code)
        self.assertEqual(o.shipping_state, self.address.state)
        self.assertEqual(o.shipping_country, self.address.country.name)

        self.assertEqual(o.billing_name, "%s %s" % (self.user.first_name, self.user.last_name))
        self.assertEqual(o.billing_address, self.address2.address)
        self.assertEqual(o.billing_address2, self.address2.address2)
        self.assertEqual(o.billing_zip_code, self.address2.zip_code)
        self.assertEqual(o.billing_state, self.address2.state)
        self.assertEqual(o.billing_country, self.address2.country.name)

    def test_order_saves_item_pk_as_a_string(self):
        """
        That's needed in case shipment or payment backends need to make fancy 
        calculations on products (i.e. shipping based on weight/size...)
        """
        # Add another product to the database, so it's ID isn't 1
        product2 = Product.objects.create(
            name="TestPrduct2", slug="TestPrduct2", active=True, unit_price=self.PRODUCT_PRICE
        )

        self.cart.add_product(product2)
        self.cart.update()
        self.cart.save()

        self.address.is_billing = False
        self.address.save()

        o = Order.objects.create_from_cart(self.cart)

        # Must not return None, obviously
        self.assertNotEqual(o, None)

        # take the first item from the order:
        oi = OrderItem.objects.filter(order=o)[0]

        self.assertEqual(oi.product_reference, str(product2.id))

        # Lookup works?
        prod = oi.product
        self.assertEqual(prod, product2)

    def test_create_order_respects_product_specific_get_price_method(self):
        baseproduct = BaseProduct.objects.create(unit_price=Decimal("10.0"))
        product = ProductVariation.objects.create(baseproduct=baseproduct)

        self.cart.add_product(product)
        self.cart.update()
        self.cart.save()
        o = Order.objects.create_from_cart(self.cart)
        oi = OrderItem.objects.filter(order=o)[0]
        self.assertEqual(oi.unit_price, baseproduct.unit_price)
Exemple #13
0
class CartTestCase(TestCase):
    PRODUCT_PRICE = Decimal('100')
    TEN_PERCENT = Decimal(10) / Decimal(100)
    
    def setUp(self):
        
        cart_modifiers_pool.USE_CACHE=False
        
        self.user = User.objects.create(username="******", email="*****@*****.**")
        self.product = Product()
        self.product.name = "TestPrduct"
        self.product.slug = "TestPrduct"
        self.product.short_description = "TestPrduct"
        self.product.long_description = "TestPrduct"
        self.product.active = True
        self.product.unit_price = self.PRODUCT_PRICE
        self.product.save()
        
        self.cart = Cart()
        self.cart.user = self.user
        self.cart.save()
    
    def test_empty_cart_costs_0_quantity_0(self):
        with SettingsOverride(SHOP_CART_MODIFIERS=[]):
            
            self.cart.update()
            
            self.assertEqual(self.cart.subtotal_price, Decimal('0.0'))
            self.assertEqual(self.cart.total_price, Decimal('0.0'))
            self.assertEqual(self.cart.total_quantity, 0)
            
    def test_one_object_no_modifiers(self):
        with SettingsOverride(SHOP_CART_MODIFIERS=[]):
            self.cart.add_product(self.product)
            self.cart.save()
            self.cart.update()
            self.cart.save()
            
            self.assertEqual(self.cart.subtotal_price, self.PRODUCT_PRICE)
            self.assertEqual(self.cart.total_price, self.PRODUCT_PRICE)
            self.assertEqual(self.cart.total_quantity, 1)
    
    def test_two_objects_no_modifier(self):
        with SettingsOverride(SHOP_CART_MODIFIERS=[]):
            
            # We add two objects now :)
            self.cart.add_product(self.product,2)
            self.cart.update()
            self.cart.save()
            
            self.assertEqual(self.cart.subtotal_price, self.PRODUCT_PRICE*2)
            self.assertEqual(self.cart.total_price, self.PRODUCT_PRICE*2)
            self.assertEqual(self.cart.total_quantity, 2)
            
    def test_one_object_simple_modifier(self):
        MODIFIERS = ['shop.cart.modifiers.tax_modifiers.TenPercentGlobalTaxModifier']
        with SettingsOverride(SHOP_CART_MODIFIERS=MODIFIERS):
            self.cart.add_product(self.product)
            self.cart.update()
            self.cart.save()
            
            self.assertEqual(self.cart.subtotal_price, self.PRODUCT_PRICE)
            self.assertEqual(self.cart.total_price, (self.TEN_PERCENT*self.PRODUCT_PRICE)+self.PRODUCT_PRICE)
            
    def test_one_object_two_modifiers_no_rebate(self):
        MODIFIERS = ['shop.cart.modifiers.tax_modifiers.TenPercentGlobalTaxModifier',
                     'shop.cart.modifiers.rebate_modifiers.BulkRebateModifier']
        with SettingsOverride(SHOP_CART_MODIFIERS=MODIFIERS):
            self.cart.add_product(self.product)
            
            self.cart.update()
            self.cart.save()
            
            self.assertEqual(self.cart.subtotal_price, self.PRODUCT_PRICE)
            self.assertEqual(self.cart.total_price, (self.TEN_PERCENT*self.PRODUCT_PRICE)+self.PRODUCT_PRICE)
            
    def test_one_object_two_modifiers_with_rebate(self):
        MODIFIERS = ['shop.cart.modifiers.tax_modifiers.TenPercentGlobalTaxModifier',
                     'shop.cart.modifiers.rebate_modifiers.BulkRebateModifier']
        with SettingsOverride(SHOP_CART_MODIFIERS=MODIFIERS):
            # We add 6 objects now :)
            self.cart.add_product(self.product,6)
            self.cart.update()
            self.cart.save()
            
            #subtotal is 600 - 10% = 540
            sub_should_be = (6*self.PRODUCT_PRICE) - (self.TEN_PERCENT*(6*self.PRODUCT_PRICE)) 
            
            total_should_be = sub_should_be + (self.TEN_PERCENT*sub_should_be) 
            
            self.assertEqual(self.cart.subtotal_price, sub_should_be)
            self.assertEqual(self.cart.total_price, total_should_be)
            
    def test_add_same_object_twice(self):
        with SettingsOverride(SHOP_CART_MODIFIERS=[]):
            self.assertEqual(self.cart.total_quantity, 0)
            self.cart.add_product(self.product)
            self.cart.add_product(self.product)
            self.cart.update()
            self.cart.save()
            
            self.assertEqual(len(self.cart.items.all()),1)
            self.assertEqual(self.cart.items.all()[0].quantity, 2)
            self.assertEqual(self.cart.total_quantity, 2)
    
    def test_add_same_object_twice_no_merge(self):
        with SettingsOverride(SHOP_CART_MODIFIERS=[]):
            self.assertEqual(self.cart.total_quantity, 0)
            self.cart.add_product(self.product, merge=False)
            self.cart.add_product(self.product, merge=False)
            self.cart.update()
            self.cart.save()
            
            self.assertEqual(len(self.cart.items.all()),2)
            self.assertEqual(self.cart.items.all()[0].quantity, 1)
            self.assertEqual(self.cart.items.all()[1].quantity, 1)
            
    def test_add_product_updates_last_updated(self):
        with SettingsOverride(SHOP_CART_MODIFIERS=[]):
            initial = self.cart.last_updated
            self.cart.add_product(self.product)
            self.assertNotEqual(initial, self.cart.last_updated)

    def test_cart_item_should_use_specific_type_to_get_price(self):
        base_product = BaseProduct.objects.create(unit_price=self.PRODUCT_PRICE)
        variation = base_product.productvariation_set.create(
                name="Variation 1"
                )
        with SettingsOverride(SHOP_CART_MODIFIERS=[]):
            self.cart.add_product(variation)
            self.cart.update()
            self.cart.save()
            self.assertEqual(self.cart.subtotal_price, self.PRODUCT_PRICE)
    
    def test_update_quantity_deletes(self):
        with SettingsOverride(SHOP_CART_MODIFIERS=[]):
            self.assertEqual(self.cart.total_quantity, 0)
            self.cart.add_product(self.product)
            self.cart.add_product(self.product)
            self.cart.update()
            self.cart.save()

            self.assertEqual(len(self.cart.items.all()),1)
            self.cart.update_quantity(self.cart.items.all()[0].id, 0)
            self.assertEqual(len(self.cart.items.all()),0)