def testCartId(self): # def test that every character that can go into a cart-id can be saved to the database field for char in CartItem.CART_ID_CHARS: cart_id = char ci = create_cart_item(cart_id=cart_id) self.assertEqual(cart_id, ci.cart_id) # test that the cart id cannot be none, and has no default with self.assertRaises(ValidationError) as cm: ci = CartItem(cart_id=None, product=create_product(), quantity=2) ci.full_clean() self.assertIn("This field cannot be null", str(cm.exception)) with self.assertRaises(ValidationError) as cm: ci = CartItem(product=create_product(), quantity=2) ci.full_clean() self.assertIn("This field cannot be blank", 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