def testQuantity(self): # test that quantity must be greater than zero for quantity in [0, -1, -18]: with self.assertRaises(ValidationError) as cm: create_cart_item(quantity=quantity) self.assertIn("Ensure this value is greater than or equal to 1", str(cm.exception)) # test that quantity defaults to 1 product = create_product() ci = CartItem(product=product, cart_id=CartItem.generate_cart_id()) self.assertEqual(1, ci.quantity) # test that the quantity cannot be nulled out with self.assertRaises(ValidationError) as cm: create_cart_item(quantity=None) self.assertIn("This field cannot be null", str(cm.exception))
def create_cart_item(**kwargs): cart_id = kwargs.pop('cart_id', None) if not cart_id: cart_id = CartItem.generate_cart_id() product = kwargs.pop('product', None) if not product: product = create_product() quantity = kwargs.pop('quantity', 1) if len(kwargs) > 0: raise Exception("Extra keyword args in create_cart_item: %s" % kwargs) ci = CartItem(cart_id=cart_id, product=product, quantity=quantity) ci.full_clean() ci.save() return ci
def testGenerateCartId(self): for i in xrange(500): cart_id = CartItem.generate_cart_id() self.assertIsNotNone(cart_id) self.assertEqual(50, len(cart_id))