Skip to content

Commit

Permalink
Added tests for the basketline formset to make sure the queryset is o…
Browse files Browse the repository at this point in the history
…rdered
  • Loading branch information
Martijn Jacobs committed Jul 11, 2016
1 parent dc98a14 commit 3b6bbd4
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 6 deletions.
55 changes: 55 additions & 0 deletions tests/integration/basket/form_tests.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
from decimal import Decimal as D

from django.test import TestCase
from django.conf import settings
import mock

from oscar.apps.basket import forms
from oscar.apps.offer.utils import Applicator
from oscar.core.loading import get_model
from oscar.test import factories
from oscar.test.basket import add_product
from oscar.test.factories import (
BenefitFactory, ConditionalOfferFactory, ConditionFactory, RangeFactory)


Line = get_model('basket', 'Line')


class TestBasketLineForm(TestCase):

def setUp(self):
self.applicator = Applicator()
rng = RangeFactory(includes_all_products=True)
self.condition = ConditionFactory(
range=rng, type=ConditionFactory._meta.model.VALUE,
value=D('100'), proxy_class=None)
self.benefit = BenefitFactory(
range=rng, type=BenefitFactory._meta.model.FIXED,
value=D('10'), max_affected_items=1)
self.basket = factories.create_basket()
self.line = self.basket.all_lines()[0]

Expand Down Expand Up @@ -47,6 +65,43 @@ def test_enforces_max_line_quantity(self):
form = self.build_form(quantity=invalid_qty)
self.assertFalse(form.is_valid())

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 = forms.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 = forms.BasketLineFormSet(
strategy=self.basket.strategy,
queryset=self.basket.all_lines())
self.assertTrue(formset.forms[0].instance.has_discount)


class TestAddToBasketForm(TestCase):

Expand Down
21 changes: 15 additions & 6 deletions tests/unit/basket/model_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from oscar.apps.basket.models import Basket
from oscar.apps.partner import strategy
from oscar.test.factories import (
BasketFactory, BasketLineAttributeFactory, ProductFactory, OptionFactory)
BasketFactory, BasketLineAttributeFactory, OptionFactory, ProductFactory)


class TestANewBasket(TestCase):
Expand Down Expand Up @@ -54,13 +54,22 @@ def test_description_with_attributes(self):
BasketLineAttributeFactory(
line=line, value=u'\u2603', option__name='with')
self.assertEqual(line.description, u"A product (with = '\u2603')")

def test_create_line_reference(self):
basket = BasketFactory()
product = ProductFactory(title="A product")
option = OptionFactory(name="product_option", code="product_option")
option_product = ProductFactory(title=u'Asunción')
options = [{'option' : option, 'value': option_product}]
basket.add_product(product, options = options)


options = [{'option': option, 'value': option_product}]
basket.add_product(product, options=options)

def test_basket_lines_queryset_is_ordered(self):
# This is needed to make sure a formset is not performing the query
# again with an order_by clause (losing all calculated discounts)
basket = BasketFactory()
product = ProductFactory(title="A product")
another_product = ProductFactory(title="Another product")
basket.add_product(product)
basket.add_product(another_product)
queryset = basket.all_lines()
self.assertTrue(queryset.ordered)

0 comments on commit 3b6bbd4

Please sign in to comment.