Skip to content

Commit

Permalink
feat(carts): fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamad-liyaghi committed Aug 8, 2024
1 parent 8d4480c commit dbf7ec4
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 28 deletions.
8 changes: 8 additions & 0 deletions apps/carts/tests/fixtures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import pytest
from carts.services import CartService


@pytest.fixture(scope="class")
def cart(user, available_food_product):
CartService.add_item(user, available_food_product, 1)
return CartService.get_items(user)
3 changes: 1 addition & 2 deletions apps/carts/tests/test_services/test_delete_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ def setup(self, available_food_product, another_user):
self.product = available_food_product
self.quantity = 1

def test_delete_item(self):
CartService.add_item(self.user, self.product, self.quantity)
def test_delete_item(self, cart):
CartService.remove_item(self.user, self.product)
assert not cache.get(config("CART_CACHE_KEY").format(user_id=self.user.id, product_id=self.product.id))
7 changes: 3 additions & 4 deletions apps/carts/tests/test_services/test_get_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@
@pytest.mark.django_db
class TestCartListItem:
@pytest.fixture(autouse=True)
def setup(self, available_food_product, another_user):
self.user = another_user
def setup(self, available_food_product, user):
self.user = user
self.product = available_food_product
self.quantity = 1

def test_get_empty_cart(self):
items = CartService.get_items(self.user)
assert items == {}

def test_get_items(self):
CartService.add_item(self.user, self.product, self.quantity)
def test_get_items(self, cart):
items = CartService.get_items(self.user)
assert items[self.product.id]["quantity"] == self.quantity
7 changes: 3 additions & 4 deletions apps/carts/tests/test_services/test_update_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
@pytest.mark.django_db
class TestCartUpdateItem:
@pytest.fixture(autouse=True)
def setup(self, available_food_product, another_user):
self.user = another_user
def setup(self, available_food_product, user):
self.user = user
self.product = available_food_product
self.quantity = 1

def test_update_item(self):
CartService.add_item(self.user, self.product, self.quantity)
def test_update_item(self, cart):
updated_quantity = 2
result = CartService.update_item(self.user, self.product, updated_quantity)
assert result["quantity"] == updated_quantity
Expand Down
3 changes: 1 addition & 2 deletions apps/carts/tests/test_views/test_delete_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ def test_delete_unauthorized_fails(self):
response = self.client.delete(self.url)
assert response.status_code == status.HTTP_403_FORBIDDEN

def test_delete_item(self):
CartService.add_item(self.user, self.product, 1)
def test_delete_item(self, cart):
self.client.force_authenticate(user=self.user)
response = self.client.delete(self.url)
assert response.status_code == status.HTTP_204_NO_CONTENT
Expand Down
4 changes: 1 addition & 3 deletions apps/carts/tests/test_views/test_list_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ def test_get_empty_cart(self):
assert response.status_code == status.HTTP_200_OK
assert response.data == {}

def test_get_items(self):
CartService.add_item(self.user, self.product, self.quantity)
def test_get_items(self, cart):
self.client.force_authenticate(user=self.user)
response = self.client.get(self.url)
assert response.status_code == status.HTTP_200_OK
assert response.data[self.product.id]["quantity"] == self.quantity
11 changes: 3 additions & 8 deletions apps/carts/tests/test_views/test_update_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
@pytest.mark.django_db
class TestCarItemUpdateView:
@pytest.fixture(autouse=True)
def setup(self, api_client, available_food_product, user):
def setup(self, api_client, available_food_product, user, cart):
self.url = reverse("carts:update-delete", kwargs={"product_uuid": available_food_product.uuid})
self.client = api_client
self.cart = cart
self.user = user
self.product = available_food_product

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

def test_update_with_maximum_quantity_fails(self):
self.client.force_authenticate(user=self.user)
CartService.add_item(self.user, self.product, 1)
response = self.client.put(
self.url,
data={"quantity": 20},
format="json",
)
response = self.client.put(self.url, data={"quantity": 20}, format="json")
assert response.status_code == status.HTTP_400_BAD_REQUEST
assert response.data["error"] == "Maximum quantity exceeded"

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

def test_update_item(self):
self.client.force_authenticate(user=self.user)
CartService.add_item(self.user, self.product, 1)
response = self.client.put(self.url, data={"quantity": 2}, format="json")
assert response.status_code == status.HTTP_200_OK
assert response.data["quantity"] == 2
5 changes: 2 additions & 3 deletions apps/orders/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ def test_create_with_empty_cart_fails(self, user):
with pytest.raises(EmptyCartException):
Order.create_order(user, {})

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

Expand Down
3 changes: 1 addition & 2 deletions apps/orders/tests/test_views/test_customer/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ def test_create_with_empty_cart_fails(self):
assert response.status_code == status.HTTP_400_BAD_REQUEST
assert response.json() == ["Cart is empty"]

def test_create_with_items_succeeds(self, available_drink_product):
CartService.add_item(self.user, available_drink_product, 2)
def test_create_with_items_succeeds(self, cart):
self.client.force_authenticate(self.user)
response = self.client.post(self.url, self.data)
assert response.status_code == status.HTTP_201_CREATED
1 change: 1 addition & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from apps.products.tests.fixtures import * # noqa
from apps.transactions.tests.fixtures import * # noqa
from apps.orders.tests.fixtures import * # noqa
from apps.carts.tests.fixtures import * # noqa


@pytest.fixture(scope="class")
Expand Down

0 comments on commit dbf7ec4

Please sign in to comment.