Tay Ray Chuan avatar Tay Ray Chuan committed a4a6df8

shop.tests: add test for per-product discounts

Comments (0)

Files changed (2)

satchmo/apps/product/fixtures/test_discount.yaml

     allValid: true
   model: product.discount
   pk: 6
+- fields:
+    active: true
+    allowedUses: null
+    amount: null
+    code: test10-product
+    description: Test Percent 10 (Product)
+    endDate: 5000-01-01
+    shipping: 'NONE'
+    minOrder: null
+    numUses: null
+    percentage: '10'
+    startDate: 2007-12-15
+    valid_products: [15]
+    site: 1
+    allValid: false
+  model: product.discount
+  pk: 7
 - fields: {description: Books, meta: null, name: Books, ordering: 0, parent: null,
     slug: book, site: 1}
   model: product.category

satchmo/apps/satchmo_store/shop/tests.py

         self.order = o
         self.small = small
 
+        rebuild_pricing()
+
     def tearDown(self):
         cache_delete()
 
         self.assertEqual(shiptotal, Decimal('0.00'))
         self.assertEqual(discount, Decimal('7.20'))
 
+    def testApplyPercentProducts(self):
+        """
+        Check that a percentage discount applies only to products specified in
+        valid_products.
+        """
+        #
+        # The discount "test10-product" applies only to 'neat-book-soft'.
+        #
+        # +-----------------+-----------+----------------------+
+        # | Product         | Price ($) | Discounted price ($) |
+        # +=================+===========+======================+
+        # | 'neat-book-hard'|     $5    |      $5              |
+        # +-----------------+-----------+----------------------+
+        # | 'neat-book-soft'|     $6    |      $5.40           |
+        # +-----------------+-----------+----------------------+
+        # |          Total: |    $11    |     $10.40           |
+        # +-----------------+-----------+----------------------+
+        #
+
+        discount_obj = Discount.objects.by_code("test10-product")
+        products = {}
+        for orderitem in self.order.orderitem_set.all():
+            products[orderitem.product.slug] = orderitem.product
+        self.assertFalse(discount_obj.valid_for_product(products['neat-book-hard']))
+        self.assertTrue(discount_obj.valid_for_product(products['neat-book-soft']))
+
+        self.order.discount_code=discount_obj.code
+        self.order.recalculate_total(save=False)
+        sub_total = self.order.sub_total
+        price = self.order.total
+        shipcost = self.order.shipping_cost
+        shiptotal = self.order.shipping_sub_total
+        discount = self.order.discount
+
+        self.assertEqual(sub_total, Decimal('11.00'))
+        self.assertEqual(price, Decimal('16.40'))
+        self.assertEqual(shipcost, Decimal('6.00'))
+        self.assertEqual(shiptotal, Decimal('6.00'))
+        self.assertEqual(discount, Decimal('0.60'))
+
     def make_order_payment(order, paytype=None, amount=None):
         if not paytype:
             paytype = active_gateways()[0][0]
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.