Exemplo n.º 1
0
 def test_default_site(self):
     add_product(self.basket, D('12.00'))
     place_order(self.creator,
                 basket=self.basket,
                 order_number='1234')
     order = Order.objects.get(number='1234')
     self.assertEqual(order.site_id, 1)
Exemplo n.º 2
0
 def test_not_discountable_product_fails_condition(self):
     prod1, prod2 = factories.create_product(), factories.create_product()
     prod2.is_discountable = False
     prod2.save()
     add_product(self.basket, product=prod1)
     add_product(self.basket, product=prod2)
     self.assertFalse(self.condition.is_satisfied(self.offer, self.basket))
Exemplo n.º 3
0
 def setUp(self):
     self.order = factories.create_order()
     self.handler = EventHandler()
     basket = factories.create_basket(empty=True)
     add_product(basket, D('10.00'), 5)
     self.order = factories.create_order(basket=basket)
     self.settled = models.PaymentEventType.objects.create(name='Settled')
Exemplo n.º 4
0
 def test_uses_default_line_status_from_settings(self):
     add_product(self.basket, D('12.00'))
     with override_settings(IZI_INITIAL_LINE_STATUS='A'):
         place_order(self.creator, basket=self.basket, order_number='1234')
     order = Order.objects.get(number='1234')
     line = order.lines.all()[0]
     self.assertEqual('A', line.status)
Exemplo n.º 5
0
 def test_applies_correctly_to_basket_which_exceeds_condition_but_matches_on_boundary(
         self):
     add_product(self.basket, D('5.00'), 3)
     result = self.benefit.apply(self.basket, self.condition, self.offer)
     self.assertEqual(1 * D('5.00') * D('0.2'), result.discount)
     self.assertEqual(2, self.basket.num_items_with_discount)
     self.assertEqual(1, self.basket.num_items_without_discount)
Exemplo n.º 6
0
 def setUp(self):
     self.product = factories.create_product()
     self.stockrecord = factories.create_stockrecord(self.product,
                                                     D('12.00'),
                                                     num_in_stock=5)
     self.basket = factories.create_basket(empty=True)
     add_product(self.basket, product=self.product)
Exemplo n.º 7
0
 def test_applies_correctly_to_basket_with_no_discountable_products(self):
     product = factories.create_product(is_discountable=False)
     add_product(self.basket, D('12.00'), 2, product=product)
     result = self.benefit.apply(self.basket, self.condition, self.offer)
     self.assertEqual(D('0.00'), result.discount)
     self.assertEqual(0, self.basket.num_items_with_discount)
     self.assertEqual(2, self.basket.num_items_without_discount)
Exemplo n.º 8
0
    def test_moving_from_saved_basket_more_than_stocklevel_raises(self):
        self.user = User.objects.create_user(username='******',
                                             password='******',
                                             email='*****@*****.**')
        product = create_product(price=D('10.00'), num_in_stock=1)
        basket, created = Basket.open.get_or_create(owner=self.user)
        add_product(basket, product=product)

        saved_basket, created = Basket.saved.get_or_create(owner=self.user)
        add_product(saved_basket, product=product)

        response = self.get(reverse('basket:summary'))
        saved_formset = response.context['saved_formset']
        saved_form = saved_formset.forms[0]

        data = {
            saved_formset.add_prefix('INITIAL_FORMS'): 1,
            saved_formset.add_prefix('MAX_NUM_FORMS'): 1,
            saved_formset.add_prefix('TOTAL_FORMS'): 1,
            saved_form.add_prefix('id'): saved_form.initial['id'],
            saved_form.add_prefix('move_to_basket'): True,
        }
        response = self.post(reverse('basket:saved'), params=data)
        # we can't add more than stock level into basket
        self.assertEqual(
            Basket.open.get(id=basket.id).lines.get(product=product).quantity,
            1)
        self.assertRedirects(response, reverse('basket:summary'))
 def test_applies_correctly_to_basket_which_is_worth_the_same_as_value(
         self):
     add_product(self.basket, D('5.00'), 4)
     result = self.benefit.apply(self.basket, self.condition, self.offer)
     self.assertEqual(D('0.00'), result.discount)
     self.assertEqual(0, self.basket.num_items_with_discount)
     self.assertEqual(4, self.basket.num_items_without_discount)
Exemplo n.º 10
0
    def test_basketline_formset_ordering(self):
        # when we use a unordered queryset in the Basketlineformset, the
        # discounts will be lost because django will query the database
        # again to enforce ordered results
        add_product(self.basket, D('100'), 5)
        offer = ConditionalOfferFactory(pk=1,
                                        condition=self.condition,
                                        benefit=self.benefit)

        # now we force an unordered queryset so we can see that our discounts
        # will disappear due to a new ordering query (see django/forms/model.py)
        default_line_ordering = Line._meta.ordering
        Line._meta.ordering = []
        self.basket._lines = self.basket.lines.all()

        self.applicator.apply_offers(self.basket, [offer])
        formset = formsets.BasketLineFormSet(strategy=self.basket.strategy,
                                             queryset=self.basket.all_lines())

        # the discount is in all_lines():
        self.assertTrue(self.basket.all_lines()[0].has_discount)

        # but not in the formset
        self.assertFalse(formset.forms[0].instance.has_discount)

        # Restore the ordering on the line
        Line._meta.ordering = default_line_ordering

        # clear the cached lines and apply the offer again
        self.basket._lines = None
        self.applicator.apply_offers(self.basket, [offer])

        formset = formsets.BasketLineFormSet(strategy=self.basket.strategy,
                                             queryset=self.basket.all_lines())
        self.assertTrue(formset.forms[0].instance.has_discount)
Exemplo n.º 11
0
 def test_line_quantity_max_attribute_per_basket_threshold(self):
     self.basket.flush()
     product = factories.create_product(num_in_stock=20)
     add_product(self.basket, D('100'), 4, product)
     self.line = self.basket.all_lines()[0]
     form = self.build_form()
     self.assertIn('max="6"', str(form['quantity']))
Exemplo n.º 12
0
    def test_multiple_payment_events(self):
        basket = factories.create_basket(empty=True)
        user = factories.UserFactory()
        add_product(basket, D('100.00'))
        order_placement = OrderPlacementMixin()
        order_placement.add_payment_event('Gift Card Payment', D('10'))
        order_placement.add_payment_event('Credit Card Payment', D('90'))
        shipping_method = Free()
        shipping_charge = shipping_method.calculate(basket)
        order_total = OrderTotalCalculator().calculate(basket, shipping_charge)

        billing_address = factories.BillingAddressFactory()
        shipping_address = factories.ShippingAddressFactory()
        order_submission_data = {
            'user': user,
            'order_number': '12345',
            'basket': basket,
            'shipping_method': shipping_method,
            'shipping_charge': shipping_charge,
            'order_total': order_total,
            'billing_address': billing_address,
            'shipping_address': shipping_address
        }
        order_placement.place_order(**order_submission_data)
        order1 = Order.objects.get(number='12345')
        self.assertEqual(order1.payment_events.count(), 2)
        event1 = order1.payment_events.all()[0]
        event2 = order1.payment_events.all()[1]
        self.assertEqual(event1.event_type.name, 'Credit Card Payment')
        self.assertEqual(event1.amount, D('90'))
        self.assertEqual(event1.lines.count(), 1)
        self.assertEqual(event2.event_type.name, 'Gift Card Payment')
        self.assertEqual(event2.amount, D('10'))
        self.assertEqual(event2.lines.count(), 1)
Exemplo n.º 13
0
 def test_applies_offer_multiple_times_by_default(self):
     add_product(self.basket, D('100'), 5)
     offer = ConditionalOfferFactory(pk=1,
                                     condition=self.condition,
                                     benefit=self.benefit)
     self.applicator.apply_offers(self.basket, [offer])
     applications = self.basket.offer_applications.applications
     self.assertEqual(5, applications[1]['freq'])
Exemplo n.º 14
0
    def test_wraps_shipping_method_from_repository(self):
        add_product(self.basket, D('12.00'), 1)
        utils.Applicator().apply(self.basket)
        methods = StubRepository().get_shipping_methods(self.basket)
        method = methods[0]

        charge = method.calculate(self.basket)
        self.assertEqual(D('1.00'), charge.incl_tax)
Exemplo n.º 15
0
 def test_respects_maximum_applications_field(self):
     add_product(self.basket, D('100'), 5)
     offer = ConditionalOfferFactory(pk=1,
                                     condition=self.condition,
                                     benefit=self.benefit,
                                     max_basket_applications=1)
     self.applicator.apply_offers(self.basket, [offer])
     applications = self.basket.offer_applications.applications
     self.assertEqual(1, applications[1]['freq'])
Exemplo n.º 16
0
 def setUp(self):
     basket = create_basket(empty=True)
     add_product(basket, D('10.00'), 4)
     self.order = create_order(number='100002', basket=basket)
     self.line = self.order.lines.all()[0]
     self.order_placed, __ = ShippingEventType.objects.get_or_create(
         code='order_placed', name='Order placed')
     self.dispatched, __ = ShippingEventType.objects.get_or_create(
         code='dispatched', name='Dispatched')
Exemplo n.º 17
0
    def test_succcessful_application_consumes_correctly(self):
        add_product(self.basket, product=self.condition_product, quantity=2)
        add_product(self.basket, product=self.benefit_product, quantity=1)

        self.applicator.apply_offers(self.basket, [self.offer])

        discounts = self.basket.offer_applications.offer_discounts
        self.assertEqual(len(discounts), 1)
        self.assertEqual(discounts[0]['freq'], 1)
Exemplo n.º 18
0
 def setUp(self):
     self.product = create_product(num_in_stock=10)
     self.user_basket = Basket()
     self.user_basket.strategy = strategy.Default()
     add_product(self.user_basket, product=self.product)
     self.cookie_basket = Basket()
     self.cookie_basket.strategy = strategy.Default()
     add_product(self.cookie_basket, quantity=2, product=self.product)
     self.user_basket.merge(self.cookie_basket, add_quantities=False)
Exemplo n.º 19
0
    def setUp(self):
        basket = create_basket(empty=True)
        add_product(basket, D('10.00'), 4)
        self.order = create_order(number='100002', basket=basket)
        self.line = self.order.lines.all()[0]

        self.shipped, __ = ShippingEventType.objects.get_or_create(
            name='Shipped')
        self.returned, __ = ShippingEventType.objects.get_or_create(
            name='Returned')
Exemplo n.º 20
0
    def test_respects_effective_price_when_taxes_not_known(self):
        # Assign US style strategy (no tax known)
        self.basket.strategy = strategy.US()

        # Add sufficient products to meet condition
        add_product(self.basket, price=D('6'), quantity=2)

        # Ensure discount is correct
        result = self.offer.apply_benefit(self.basket)
        self.assertEqual(D('2.40'), result.discount)
Exemplo n.º 21
0
    def test_respects_effective_price_when_taxes_are_known(self):
        # Assign UK style strategy (20% tax)
        self.basket.strategy = strategy.UK()

        # Add sufficient products to meet condition
        add_product(self.basket, price=D('6'), quantity=2)

        # Ensure discount is calculated against tax-inclusive price
        result = self.offer.apply_benefit(self.basket)
        self.assertEqual(2 * D('6.00') * D('1.2') * D('0.20'), result.discount)
Exemplo n.º 22
0
    def test_consumes_correct_number_of_products_for_3_product_basket(self):
        basket = factories.create_basket(empty=True)
        add_product(basket, D('1'), 3)

        self.assertTrue(self.offer.is_condition_satisfied(basket))
        discount = self.offer.apply_benefit(basket)
        self.assertTrue(discount.discount > 0)
        self.assertEqual(3, basket.num_items_with_discount)
        self.assertEqual(0, basket.num_items_without_discount)
        self.assertFalse(self.offer.is_condition_satisfied(basket))
Exemplo n.º 23
0
    def test_condition_is_consumed_correctly(self):
        # Testing an error case reported on the mailing list
        add_product(self.basket, product=self.condition_product, quantity=3)
        add_product(self.basket, product=self.benefit_product, quantity=2)

        self.applicator.apply_offers(self.basket, [self.offer])

        discounts = self.basket.offer_applications.offer_discounts
        self.assertEqual(len(discounts), 1)
        self.assertEqual(discounts[0]['freq'], 1)
Exemplo n.º 24
0
 def test_enforce_max_line_quantity_for_existing_product(self):
     self.basket.flush()
     product = factories.create_product(num_in_stock=20)
     add_product(self.basket, D('100'), 4, product)
     self.line = self.basket.all_lines()[0]
     form = self.build_form(quantity=6)
     self.assertTrue(form.is_valid())
     form.save()
     form = self.build_form(quantity=11)
     self.assertFalse(form.is_valid())
Exemplo n.º 25
0
 def test_partner_name_is_optional(self):
     for partner_name, order_number in [('', 'A'), ('p1', 'B')]:
         self.basket = factories.create_basket(empty=True)
         product = factories.create_product(partner_name=partner_name)
         add_product(self.basket, D('12.00'), product=product)
         place_order(
             self.creator, basket=self.basket, order_number=order_number)
         line = Order.objects.get(number=order_number).lines.all()[0]
         partner = product.stockrecords.all()[0].partner
         self.assertTrue(partner_name == line.partner_name == partner.name)
Exemplo n.º 26
0
    def test_prevents_event_quantities_higher_than_original_line(self):
        basket = factories.create_basket(empty=True)
        add_product(basket, D('10.00'), 5)
        order = factories.create_order(basket=basket)

        # First shipping event
        lines = order.lines.all()
        self.handler.handle_shipping_event(order, self.shipped, lines, [4])

        with self.assertRaises(exceptions.InvalidShippingEvent):
            self.handler.handle_shipping_event(order, self.shipped, lines, [4])
    def test_applies_correctly_to_shipping_method_without_tax(self):
        add_product(self.basket, D('12.00'), 3)

        # Apply offers to basket
        utils.Applicator().apply_offers(self.basket, [self.offer])

        repo = repository.Repository()
        raw_method = ExcludingTax()
        method = repo.apply_shipping_offer(self.basket, raw_method, self.offer)
        charge = method.calculate(self.basket)
        self.assertEqual(D('5.00'), charge.excl_tax)
Exemplo n.º 28
0
    def test_are_stock_allocations_available_track_stock_off(self):
        product_class = factories.ProductClassFactory(requires_shipping=False,
                                                      track_stock=False)
        product = factories.ProductFactory(product_class=product_class)
        basket = factories.create_basket(empty=True)
        add_product(basket, D('10.00'), 5, product=product)
        order = factories.create_order(basket=basket)

        line = order.lines.get()
        self.assertEqual(
            self.handler.are_stock_allocations_available([line], [105]),
            True,
        )
Exemplo n.º 29
0
    def test_applies_correctly_to_basket_which_matches_condition_with_one_line(
            self):
        add_product(self.basket, price=D('12.00'), quantity=2)
        result = self.benefit.apply(self.basket, self.condition, self.offer)
        self.assertEqual(D('3.00'), result.discount)
        self.assertEqual(2, self.basket.num_items_with_discount)
        self.assertEqual(0, self.basket.num_items_without_discount)

        # Check the discount is applied equally to each item in the line
        line = self.basket.all_lines()[0]
        prices = line.get_price_breakdown()
        self.assertEqual(1, len(prices))
        self.assertEqual(D('10.50'), prices[0][0])
Exemplo n.º 30
0
    def test_does_not_allocate_stock(self):
        ProductClass.objects.create(
            name="Digital", track_stock=False)
        product = factories.create_product(product_class="Digital")
        record = factories.create_stockrecord(product, num_in_stock=None)
        self.assertTrue(record.num_allocated is None)

        add_product(self.basket, D('12.00'), product=product)
        place_order(self.creator, basket=self.basket, order_number='1234')

        product = Product.objects.get(id=product.id)
        stockrecord = product.stockrecords.all()[0]
        self.assertTrue(stockrecord.num_in_stock is None)
        self.assertTrue(stockrecord.num_allocated is None)