def test_cart_add(self): c = Cart() p1 = Product.objects.get(id=0) l = [{ 'id': p1.id, 'title': p1.title, 'price': p1.price, 'quantity': 1, }] c.add(p1) self.assertEqual(c.products, l) self.assertEqual(c.total, p1.price * 1) c.add(title='Apple', id=0, quantity=2) l[0]['quantity'] += 2 self.assertEqual(c.products, l) self.assertEqual(c.total, p1.price * 3) p2 = Product.objects.get(id=1) c.add(id=1, quantity=4) l += [{ 'id': p2.id, 'title': p2.title, 'price': p2.price, 'quantity': 4, }] self.assertEqual(c.products, l) self.assertEqual(c.total, p1.price * 3 + p2.price * 4)
def test_cart_complete(self): c = Cart() p1 = Product.objects.get(id=0) p2 = Product.objects.get(id=2) c.add(p1, quantity=4) c.add(p2, quantity=1) c.complete() self.assertEqual(c.products, []) self.assertEqual(c.total, 0) self.assertEqual(c.cart.products.count(), 0) self.assertEqual(Product.objects.get(id=0).inventory_count, 1) self.assertEqual(Product.objects.get(id=2).inventory_count, 0) """ If any product in cart has quantity exceeding its inventory count, Cart is not completed and not cleared. """ p3 = Product.objects.get(id=3) c.add(p1, quantity=2) c.add(p3) cartModelList = c.cart.products l = c.products c.complete() self.assertEqual(c.products, l) self.assertEqual(c.total, p1.price * 2 + p3.price) self.assertEqual(c.cart.products, cartModelList) self.assertEqual(Product.objects.get(id=0).inventory_count, 1) self.assertEqual(Product.objects.get(id=3).inventory_count, 5)