class TestANonEmptyBasket(TestCase):

    def setUp(self):
        self.basket = Basket()
        self.basket.strategy = strategy.Default()
        self.product = factories.create_product()
        self.record = factories.create_stockrecord(
            self.product, price_excl_tax=D('10.00'))
        self.purchase_info = factories.create_purchase_info(self.record)
        self.basket.add(self.product, 10)

    def test_can_be_flushed(self):
        self.basket.flush()
        self.assertEqual(self.basket.num_items, 0)

    def test_returns_correct_product_quantity(self):
        self.assertEqual(10, self.basket.product_quantity(
            self.product))

    def test_returns_correct_line_quantity_for_existing_product_and_stockrecord(self):
        self.assertEqual(10, self.basket.line_quantity(
            self.product, self.record))

    def test_returns_zero_line_quantity_for_alternative_stockrecord(self):
        record = factories.create_stockrecord(
            self.product, price_excl_tax=D('5.00'))
        self.assertEqual(0, self.basket.line_quantity(
            self.product, record))

    def test_returns_zero_line_quantity_for_missing_product_and_stockrecord(self):
        product = factories.create_product()
        record = factories.create_stockrecord(
            product, price_excl_tax=D('5.00'))
        self.assertEqual(0, self.basket.line_quantity(
            product, record))

    def test_returns_correct_quantity_for_existing_product_and_stockrecord_and_options(self):
        product = factories.create_product()
        record = factories.create_stockrecord(
            product, price_excl_tax=D('5.00'))
        option = Option.objects.create(name="Message")
        options = [{"option": option, "value": "2"}]

        self.basket.add(product, options=options)
        self.assertEqual(0, self.basket.line_quantity(
            product, record))
        self.assertEqual(1, self.basket.line_quantity(
            product, record, options))

    def test_total_sums_product_totals(self):
        product = factories.create_product()
        factories.create_stockrecord(
            product, price_excl_tax=D('5.00'))
        self.basket.add(product, 1)
        self.assertEqual(self.basket.total_excl_tax, 105)

    def test_total_excludes_unavailable_products_with_unknown_price(self):
        new_product = factories.create_product()
        factories.create_stockrecord(
            new_product, price_excl_tax=D('5.00'))
        self.basket.add(new_product, 1)

        class UnavailableProductStrategy(strategy.Default):
            """ A test strategy that makes a specific product unavailable """

            def availability_policy(self, product, stockrecord):
                if product == new_product:
                    return availability.Unavailable()
                return super(UnavailableProductStrategy, self).availability_policy(product, stockrecord)

            def pricing_policy(self, product, stockrecord):
                if product == new_product:
                    return prices.Unavailable()
                return super(UnavailableProductStrategy, self).pricing_policy(product, stockrecord)


        try:
            self.basket.strategy = UnavailableProductStrategy()
            self.assertEqual(self.basket.all_lines()[1].get_warning(), u"'D\xf9\uff4d\u03fb\u03d2 title' is no longer available")
            self.assertEqual(self.basket.total_excl_tax, 100)
        finally:
            self.basket.strategy = strategy.Default()
class TestANonEmptyBasket(TestCase):

    def setUp(self):
        self.basket = Basket()
        self.basket.strategy = strategy.Default()
        self.product = factories.create_product()
        self.record = factories.create_stockrecord(
            self.product, price_excl_tax=D('10.00'))
        self.purchase_info = factories.create_purchase_info(self.record)
        self.basket.add(self.product, 10)

    def test_can_be_flushed(self):
        self.basket.flush()
        self.assertEqual(self.basket.num_items, 0)

    def test_returns_correct_product_quantity(self):
        self.assertEqual(10, self.basket.product_quantity(
            self.product))

    def test_returns_correct_line_quantity_for_existing_product_and_stockrecord(self):
        self.assertEqual(10, self.basket.line_quantity(
            self.product, self.record))

    def test_returns_zero_line_quantity_for_alternative_stockrecord(self):
        record = factories.create_stockrecord(
            self.product, price_excl_tax=D('5.00'))
        self.assertEqual(0, self.basket.line_quantity(
            self.product, record))

    def test_returns_zero_line_quantity_for_missing_product_and_stockrecord(self):
        product = factories.create_product()
        record = factories.create_stockrecord(
            product, price_excl_tax=D('5.00'))
        self.assertEqual(0, self.basket.line_quantity(
            product, record))

    def test_returns_correct_quantity_for_existing_product_and_stockrecord_and_options(self):
        product = factories.create_product()
        record = factories.create_stockrecord(
            product, price_excl_tax=D('5.00'))
        option = Option.objects.create(name="Message")
        options = [{"option": option, "value": "2"}]

        self.basket.add(product, options=options)
        self.assertEqual(0, self.basket.line_quantity(
            product, record))
        self.assertEqual(1, self.basket.line_quantity(
            product, record, options))

    def test_total_sums_product_totals(self):
        product = factories.create_product()
        factories.create_stockrecord(
            product, price_excl_tax=D('5.00'))
        self.basket.add(product, 1)
        self.assertEqual(self.basket.total_excl_tax, 105)

    def test_totals_for_free_products(self):
        basket = Basket()
        basket.strategy = strategy.Default()
        # Add a zero-priced product to the basket
        product = factories.create_product()
        factories.create_stockrecord(
            product, price_excl_tax=D('0.00'), num_in_stock=10)
        basket.add(product, 1)

        self.assertEqual(basket.lines.count(), 1)
        self.assertEqual(basket.total_excl_tax, 0)
        self.assertEqual(basket.total_incl_tax, 0)

    def test_basket_prices_calculation_for_unavailable_pricing(self):
        new_product = factories.create_product()
        factories.create_stockrecord(
            new_product, price_excl_tax=D('5.00'))
        self.basket.add(new_product, 1)

        class UnavailableProductStrategy(strategy.Default):
            """ A test strategy that makes a specific product unavailable """

            def availability_policy(self, product, stockrecord):
                if product == new_product:
                    return availability.Unavailable()
                return super().availability_policy(product, stockrecord)

            def pricing_policy(self, product, stockrecord):
                if product == new_product:
                    return prices.Unavailable()
                return super().pricing_policy(product, stockrecord)

        self.basket.strategy = UnavailableProductStrategy()
        line = self.basket.all_lines()[1]
        self.assertEqual(line.get_warning(), "'D\xf9\uff4d\u03fb\u03d2 title' is no longer available")
        self.assertIsNone(line.line_price_excl_tax)
        self.assertIsNone(line.line_price_incl_tax)
        self.assertIsNone(line.line_price_excl_tax_incl_discounts)
        self.assertIsNone(line.line_price_incl_tax_incl_discounts)
        self.assertIsNone(line.line_tax)
        self.assertEqual(self.basket.total_excl_tax, 100)
        self.assertEqual(self.basket.total_incl_tax, 100)
        self.assertEqual(self.basket.total_excl_tax_excl_discounts, 100)
        self.assertEqual(self.basket.total_incl_tax_excl_discounts, 100)

    def test_max_allowed_quantity(self):
        self.basket.add_product(self.product, quantity=3)

        # max allowed here is 7 (20-10+3)
        with self.settings(OSCAR_MAX_BASKET_QUANTITY_THRESHOLD=20):
            max_allowed, basket_threshold = self.basket.max_allowed_quantity()
            self.assertEqual(max_allowed, 7)
            self.assertEqual(basket_threshold, 20)

        # but we can also completely disable the threshold
        with self.settings(OSCAR_MAX_BASKET_QUANTITY_THRESHOLD=None):
            max_allowed, basket_threshold = self.basket.max_allowed_quantity()
            self.assertEqual(max_allowed, None)
            self.assertEqual(basket_threshold, None)

    def test_is_quantity_allowed(self):
        with self.settings(OSCAR_MAX_BASKET_QUANTITY_THRESHOLD=20):
            # 7 or below is possible
            allowed, message = self.basket.is_quantity_allowed(qty=7)
            self.assertTrue(allowed)
            self.assertIsNone(message)
            # but above it's not
            allowed, message = self.basket.is_quantity_allowed(qty=11)
            self.assertFalse(allowed)
            self.assertIsNotNone(message)

        with self.settings(OSCAR_MAX_BASKET_QUANTITY_THRESHOLD=None):
            # with the treshold disabled all quantities are possible
            allowed, message = self.basket.is_quantity_allowed(qty=7)
            self.assertTrue(allowed)
            self.assertIsNone(message)
            allowed, message = self.basket.is_quantity_allowed(qty=5000)
            self.assertTrue(allowed)
            self.assertIsNone(message)
Exemple #3
0
class TestANonEmptyBasket(TestCase):
    def setUp(self):
        self.basket = Basket()
        self.basket.strategy = strategy.Default()
        self.product = factories.create_product()
        self.record = factories.create_stockrecord(self.product,
                                                   price_excl_tax=D('10.00'))
        self.purchase_info = factories.create_purchase_info(self.record)
        self.basket.add(self.product, 10)

    def test_can_be_flushed(self):
        self.basket.flush()
        self.assertEqual(self.basket.num_items, 0)

    def test_returns_correct_product_quantity(self):
        self.assertEqual(10, self.basket.product_quantity(self.product))

    def test_returns_correct_line_quantity_for_existing_product_and_stockrecord(
            self):
        self.assertEqual(10,
                         self.basket.line_quantity(self.product, self.record))

    def test_returns_zero_line_quantity_for_alternative_stockrecord(self):
        record = factories.create_stockrecord(self.product,
                                              price_excl_tax=D('5.00'))
        self.assertEqual(0, self.basket.line_quantity(self.product, record))

    def test_returns_zero_line_quantity_for_missing_product_and_stockrecord(
            self):
        product = factories.create_product()
        record = factories.create_stockrecord(product,
                                              price_excl_tax=D('5.00'))
        self.assertEqual(0, self.basket.line_quantity(product, record))

    def test_returns_correct_quantity_for_existing_product_and_stockrecord_and_options(
            self):
        product = factories.create_product()
        record = factories.create_stockrecord(product,
                                              price_excl_tax=D('5.00'))
        option = Option.objects.create(name="Message")
        options = [{"option": option, "value": "2"}]

        self.basket.add(product, options=options)
        self.assertEqual(0, self.basket.line_quantity(product, record))
        self.assertEqual(1, self.basket.line_quantity(product, record,
                                                      options))

    def test_total_sums_product_totals(self):
        product = factories.create_product()
        factories.create_stockrecord(product, price_excl_tax=D('5.00'))
        self.basket.add(product, 1)
        self.assertEqual(self.basket.total_excl_tax, 105)

    def test_total_excludes_unavailable_products_with_unknown_price(self):
        new_product = factories.create_product()
        factories.create_stockrecord(new_product, price_excl_tax=D('5.00'))
        self.basket.add(new_product, 1)

        class UnavailableProductStrategy(strategy.Default):
            """ A test strategy that makes a specific product unavailable """
            def availability_policy(self, product, stockrecord):
                if product == new_product:
                    return availability.Unavailable()
                return super(UnavailableProductStrategy,
                             self).availability_policy(product, stockrecord)

            def pricing_policy(self, product, stockrecord):
                if product == new_product:
                    return prices.Unavailable()
                return super(UnavailableProductStrategy,
                             self).pricing_policy(product, stockrecord)

        try:
            self.basket.strategy = UnavailableProductStrategy()
            self.assertEqual(
                self.basket.all_lines()[1].get_warning(),
                u"'D\xf9\uff4d\u03fb\u03d2 title' is no longer available")
            self.assertEqual(self.basket.total_excl_tax, 100)
        finally:
            self.basket.strategy = strategy.Default()