tests.py 文件源码

python
阅读 19 收藏 0 点赞 0 评论 0

项目:beg-django-e-commerce 作者: Apress 项目源码 文件源码
def test_add_product(self):
        """ POST request to a product page augments the CartItem instance count """
        quantity = 2
        product_url = self.product.get_absolute_url()
        response = self.client.get(product_url)
        self.assertEqual(response.status_code, httplib.OK )

        # store count in cart_count variable
        cart_item_count = self.get_cart_item_count()
        # assert that the cart item count is zero
        self.failUnlessEqual(cart_item_count, 0)

        # perform the post of adding to the cart
        cookie = self.client.cookies[settings.SESSION_COOKIE_NAME]
        csrf_token = csrf.middleware._make_token(cookie.value)
        #self.failUnlessEqual(csrf_token, None)
        postdata = {'product_slug': self.product.slug, 
                    'quantity': quantity,
                    'csrfmiddlewaretoken': csrf_token }
        response = self.client.post(product_url, postdata )

        # assert redirected to cart page - 302 then 200?
        cart_url = urlresolvers.reverse('show_cart')
        self.assertRedirects(response, cart_url, status_code=httplib.FOUND, target_status_code=httplib.OK)

        # assert cart item count is incremented by one
        self.assertEqual(self.get_cart_item_count(), cart_item_count + 1)

        cart_id = self.get_cart_id()
        last_item = CartItem.objects.filter(cart_id=cart_id).latest('date_added')
        # assert the latest cart item has a quantity of two
        self.failUnlessEqual(last_item.quantity, quantity)
        # assert the latest cart item is the correct product
        self.failUnlessEqual(last_item.product, self.product)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号