Skip to content

Commit dbf7ec4

Browse files
feat(carts): fixtures
1 parent 8d4480c commit dbf7ec4

File tree

10 files changed

+24
-28
lines changed

10 files changed

+24
-28
lines changed

apps/carts/tests/fixtures.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import pytest
2+
from carts.services import CartService
3+
4+
5+
@pytest.fixture(scope="class")
6+
def cart(user, available_food_product):
7+
CartService.add_item(user, available_food_product, 1)
8+
return CartService.get_items(user)

apps/carts/tests/test_services/test_delete_item.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ def setup(self, available_food_product, another_user):
1212
self.product = available_food_product
1313
self.quantity = 1
1414

15-
def test_delete_item(self):
16-
CartService.add_item(self.user, self.product, self.quantity)
15+
def test_delete_item(self, cart):
1716
CartService.remove_item(self.user, self.product)
1817
assert not cache.get(config("CART_CACHE_KEY").format(user_id=self.user.id, product_id=self.product.id))

apps/carts/tests/test_services/test_get_items.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@
55
@pytest.mark.django_db
66
class TestCartListItem:
77
@pytest.fixture(autouse=True)
8-
def setup(self, available_food_product, another_user):
9-
self.user = another_user
8+
def setup(self, available_food_product, user):
9+
self.user = user
1010
self.product = available_food_product
1111
self.quantity = 1
1212

1313
def test_get_empty_cart(self):
1414
items = CartService.get_items(self.user)
1515
assert items == {}
1616

17-
def test_get_items(self):
18-
CartService.add_item(self.user, self.product, self.quantity)
17+
def test_get_items(self, cart):
1918
items = CartService.get_items(self.user)
2019
assert items[self.product.id]["quantity"] == self.quantity

apps/carts/tests/test_services/test_update_item.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66
@pytest.mark.django_db
77
class TestCartUpdateItem:
88
@pytest.fixture(autouse=True)
9-
def setup(self, available_food_product, another_user):
10-
self.user = another_user
9+
def setup(self, available_food_product, user):
10+
self.user = user
1111
self.product = available_food_product
1212
self.quantity = 1
1313

14-
def test_update_item(self):
15-
CartService.add_item(self.user, self.product, self.quantity)
14+
def test_update_item(self, cart):
1615
updated_quantity = 2
1716
result = CartService.update_item(self.user, self.product, updated_quantity)
1817
assert result["quantity"] == updated_quantity

apps/carts/tests/test_views/test_delete_item.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ def test_delete_unauthorized_fails(self):
1919
response = self.client.delete(self.url)
2020
assert response.status_code == status.HTTP_403_FORBIDDEN
2121

22-
def test_delete_item(self):
23-
CartService.add_item(self.user, self.product, 1)
22+
def test_delete_item(self, cart):
2423
self.client.force_authenticate(user=self.user)
2524
response = self.client.delete(self.url)
2625
assert response.status_code == status.HTTP_204_NO_CONTENT

apps/carts/tests/test_views/test_list_items.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ def test_get_empty_cart(self):
2424
assert response.status_code == status.HTTP_200_OK
2525
assert response.data == {}
2626

27-
def test_get_items(self):
28-
CartService.add_item(self.user, self.product, self.quantity)
27+
def test_get_items(self, cart):
2928
self.client.force_authenticate(user=self.user)
3029
response = self.client.get(self.url)
3130
assert response.status_code == status.HTTP_200_OK
32-
assert response.data[self.product.id]["quantity"] == self.quantity

apps/carts/tests/test_views/test_update_view.py

+3-8
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
@pytest.mark.django_db
99
class TestCarItemUpdateView:
1010
@pytest.fixture(autouse=True)
11-
def setup(self, api_client, available_food_product, user):
11+
def setup(self, api_client, available_food_product, user, cart):
1212
self.url = reverse("carts:update-delete", kwargs={"product_uuid": available_food_product.uuid})
1313
self.client = api_client
14+
self.cart = cart
1415
self.user = user
1516
self.product = available_food_product
1617

@@ -20,12 +21,7 @@ def test_update_unauthorized_fails(self):
2021

2122
def test_update_with_maximum_quantity_fails(self):
2223
self.client.force_authenticate(user=self.user)
23-
CartService.add_item(self.user, self.product, 1)
24-
response = self.client.put(
25-
self.url,
26-
data={"quantity": 20},
27-
format="json",
28-
)
24+
response = self.client.put(self.url, data={"quantity": 20}, format="json")
2925
assert response.status_code == status.HTTP_400_BAD_REQUEST
3026
assert response.data["error"] == "Maximum quantity exceeded"
3127

@@ -43,7 +39,6 @@ def test_update_with_product_not_in_cart_fails(self, available_drink_product):
4339

4440
def test_update_item(self):
4541
self.client.force_authenticate(user=self.user)
46-
CartService.add_item(self.user, self.product, 1)
4742
response = self.client.put(self.url, data={"quantity": 2}, format="json")
4843
assert response.status_code == status.HTTP_200_OK
4944
assert response.data["quantity"] == 2

apps/orders/tests/test_models.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ def test_create_with_empty_cart_fails(self, user):
1313
with pytest.raises(EmptyCartException):
1414
Order.create_order(user, {})
1515

16-
def test_create_only_one_order(self, user, available_food_product):
17-
CartService.add_item(user, available_food_product, 1)
18-
Order.create_order(user, CartService.get_items(user))
16+
def test_create_only_one_order(self, user, available_food_product, cart):
17+
Order.create_order(user, cart)
1918
assert Order.objects.filter(user=user, restaurant=available_food_product.restaurant).exists()
2019
assert not cache.get(config("CART_CACHE_KEY").format(user_id=user.id, product_id=available_food_product.id))
2120

apps/orders/tests/test_views/test_customer/test_create.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ def test_create_with_empty_cart_fails(self):
2323
assert response.status_code == status.HTTP_400_BAD_REQUEST
2424
assert response.json() == ["Cart is empty"]
2525

26-
def test_create_with_items_succeeds(self, available_drink_product):
27-
CartService.add_item(self.user, available_drink_product, 2)
26+
def test_create_with_items_succeeds(self, cart):
2827
self.client.force_authenticate(self.user)
2928
response = self.client.post(self.url, self.data)
3029
assert response.status_code == status.HTTP_201_CREATED

conftest.py

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from apps.products.tests.fixtures import * # noqa
1010
from apps.transactions.tests.fixtures import * # noqa
1111
from apps.orders.tests.fixtures import * # noqa
12+
from apps.carts.tests.fixtures import * # noqa
1213

1314

1415
@pytest.fixture(scope="class")

0 commit comments

Comments
 (0)