aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Lemon <y@yulqen.org>2024-09-14 20:20:48 +0100
committerMatthew Lemon <y@yulqen.org>2024-09-14 20:20:48 +0100
commit8a13e1543334f86803cd9909ba3fa5d4619074c0 (patch)
tree05498d5ddc76840d8168c98b979e21d854d25efa
parentf312ec6d4a577e71d76cf85632a198b0c1913703 (diff)
wip: testing adding resources to shopping cart
-rw-r--r--pyblackbird_cc/conftest.py5
-rw-r--r--pyblackbird_cc/payments/models.py3
-rw-r--r--pyblackbird_cc/payments/tests/test_models.py26
-rw-r--r--pyblackbird_cc/resources/tests/conftest.py8
4 files changed, 34 insertions, 8 deletions
diff --git a/pyblackbird_cc/conftest.py b/pyblackbird_cc/conftest.py
index ef8b62a..aca12b2 100644
--- a/pyblackbird_cc/conftest.py
+++ b/pyblackbird_cc/conftest.py
@@ -18,3 +18,8 @@ def user(db) -> User:
@pytest.fixture()
def resource():
return ResourceModelFactory()
+
+
+@pytest.fixture()
+def resources():
+ return ResourceModelFactory.create_batch(5)
diff --git a/pyblackbird_cc/payments/models.py b/pyblackbird_cc/payments/models.py
index 81bb1c8..b947a22 100644
--- a/pyblackbird_cc/payments/models.py
+++ b/pyblackbird_cc/payments/models.py
@@ -1,4 +1,5 @@
from django.conf import settings
+from django.core.exceptions import ValidationError
from django.db import models
from pyblackbird_cc.resources.models import Resource
@@ -27,6 +28,8 @@ class ShoppingCart(models.Model):
return f"Shopping Cart for {self.user.username}"
def add_resource(self, resource: Resource):
+ if CartItem.objects.filter(cart=self, resource=resource).exists():
+ raise ValidationError(f"{resource.name} is already in your shopping cart.")
item, created = CartItem.objects.get_or_create(
cart=self,
resource=resource,
diff --git a/pyblackbird_cc/payments/tests/test_models.py b/pyblackbird_cc/payments/tests/test_models.py
index fd26fea..4dab6e3 100644
--- a/pyblackbird_cc/payments/tests/test_models.py
+++ b/pyblackbird_cc/payments/tests/test_models.py
@@ -1,3 +1,4 @@
+from django.core.exceptions import ValidationError
import pytest
from allauth.account.signals import user_signed_up
from django.contrib.auth import get_user_model
@@ -86,3 +87,28 @@ def test_cart_item_is_created_when_resource_is_added_to_cart(user_data, resource
users_cart = ShoppingCart.objects.get(user=user)
users_cart.add_resource(resource)
assert users_cart.items.count() == 1 # type: ignore
+
+
+@pytest.mark.django_db
+def test_cannot_add_the_same_resource_to_cart_twice(user_data, resource):
+ user = User.objects.create_user(**user_data) # type: ignore
+ request = RequestFactory().get("/")
+ user_signed_up.send(sender=user.__class__, request=request, user=user)
+ users_cart = ShoppingCart.objects.get(user=user)
+ users_cart.add_resource(resource)
+ assert users_cart.items.count() == 1 # type: ignore
+ with pytest.raises(ValidationError):
+ users_cart.add_resource(resource)
+
+
+@pytest.mark.django_db
+def test_can_add_multiple_different_items_to_cart(user_data, resources):
+ user = User.objects.create_user(**user_data) # type: ignore
+ request = RequestFactory().get("/")
+ user_signed_up.send(sender=user.__class__, request=request, user=user)
+ users_cart = ShoppingCart.objects.get(user=user)
+ users_cart.add_resource(resources[0])
+ users_cart.add_resource(resources[1])
+ users_cart.add_resource(resources[2])
+ users_cart.add_resource(resources[3])
+ assert users_cart.items.count() == 4 # type: ignore
diff --git a/pyblackbird_cc/resources/tests/conftest.py b/pyblackbird_cc/resources/tests/conftest.py
deleted file mode 100644
index 0d1ed87..0000000
--- a/pyblackbird_cc/resources/tests/conftest.py
+++ /dev/null
@@ -1,8 +0,0 @@
-import pytest
-
-from pyblackbird_cc.resources.factories import ResourceModelFactory
-
-
-@pytest.fixture()
-def resources():
- return ResourceModelFactory.create_batch(5)