def test_quantity(self): 'Is AddToCartForm works with correct quantity value on empty cart' form = AddToCartForm(self.post, cart=self.cart, product=stock_product) self.assertTrue(form.is_valid()) self.assertFalse(self.cart) form.save() product_quantity = self.cart.get_line(stock_product).quantity self.assertEqual(product_quantity, 5, 'Bad quantity')
def test_clean_quantity_product(self): 'Is AddToCartForm works with not stocked product' cart = Cart() self.post['quantity'] = 10000 form = AddToCartForm(self.post, cart=cart, product=digital_product) self.assertTrue(form.is_valid(), 'Form doesn\'t valitate') self.assertFalse(cart, 'Cart isn\'t empty') form.save() self.assertTrue(cart, 'Cart is empty')
def test_max_quantity(self): 'Is AddToCartForm works with correct product stock value' form = AddToCartForm(self.post, cart=self.cart, product=stock_product) self.assertTrue(form.is_valid()) form.save() form = AddToCartForm(self.post, cart=self.cart, product=stock_product) self.assertTrue(form.is_valid()) form.save() product_quantity = self.cart.get_line(stock_product).quantity self.assertEqual(product_quantity, 10, '%s is the bad quantity value' % (product_quantity, ))
def test_max_quantity(self): 'Is AddToCartForm works with correct product stock value' form = AddToCartForm(self.post, cart=self.cart, product=stock_product) self.assertTrue(form.is_valid()) form.save() form = AddToCartForm(self.post, cart=self.cart, product=stock_product) self.assertTrue(form.is_valid()) form.save() product_quantity = self.cart.get_line(stock_product).quantity self.assertEqual(product_quantity, 10, '%s is the bad quantity value' % (product_quantity,))
def add_to_cart(): if request.form: try: product = get_or_404(Product, request.form['product_id']) except KeyError: return redirect(request.referrer) shoppingcart = ShoppingCart.for_session_cart(request.cart) form = AddToCartForm(request.form, product=product, cart=shoppingcart) if form.validate(): flash('Added {0} {1} to the cart'.format( form.quantity.data, product.name)) form.save() return redirect(url_for('.index')) return redirect(request.referrer)