유닛테스트 refresh_from_db

hyuckhoon.ko·2020년 12월 30일
0

What I learned in first year

목록 보기
24/146

1. refresh_from_db 반영 전

def test_empty_user_add_product_to_cart_success(self):
        """빈 장바구니에 제품 추가, 제품 점수"""

        # valid 제품 1개 추가
        product_param = {
            "user": self.user,
            "brand": self.brand,
            "product_status": self.product_status_6
        }
        sample_product = create_sample_product(**product_param)
        payload = {"product_id": sample_product.id}
        res = self.client.post(CART_CREATE_URL, payload)
       
        num_of_cart = models.Cart.objects.filter(user=self.user).count()
        product_point = sample_product.hit
        self.assertEqual(res.status_code, 201)
        self.assertEqual(res.data, None)
        self.assertEqual(num_of_cart, 1)
        self.assertEqual(product_point, 4)

결과





2. refresh_from_db 반영 후

sample_product.refresh_from_db() 코드 한 줄 추가

    def test_empty_user_add_product_to_cart_success(self):
        """빈 장바구니에 제품 추가, 제품 점수"""

        # valid 제품 1개 추가
        product_param = {
            "user": self.user,
            "brand": self.brand,
            "product_status": self.product_status_6
        }
        sample_product = create_sample_product(**product_param)
        payload = {"product_id": sample_product.id}
        res = self.client.post(CART_CREATE_URL, payload)
        sample_product.refresh_from_db()
        num_of_cart = models.Cart.objects.filter(user=self.user).count()
        product_point = sample_product.hit
        self.assertEqual(res.status_code, 201)
        self.assertEqual(res.data, None)
        self.assertEqual(num_of_cart, 1)
        self.assertEqual(product_point, 4)

0개의 댓글