diff --git a/.gitignore b/.gitignore index 105dd82..c815dc2 100644 --- a/.gitignore +++ b/.gitignore @@ -139,3 +139,4 @@ GitHub.sublime-settings #postgres pass files *.my_pgpass *.sql +artel/static/ diff --git a/artel/cart/tests.py b/artel/cart/tests.py deleted file mode 100644 index f433f8c..0000000 --- a/artel/cart/tests.py +++ /dev/null @@ -1,33 +0,0 @@ -from django.test import TestCase -from django.urls import reverse -from store.models import ProductAuthor, ProductCategory, ProductTemplate, Product - - -class StoreTestCase(TestCase): - def setUp(self): - self.productid = 1 - self.author = ProductAuthor.objects.create(name='Test Author') - self.category = ProductCategory.objects.create(name='Test Category') - self.template = ProductTemplate.objects.create(category=self.category, - author=self.author, - title='Test title', - code='Test code', - description='Test description' - ) - self.product = Product.objects.create(template=self.template, - price=10.99) - self.cart_url = reverse('view_cart') - - def test_add_to_cart(self): - response = self.client.post(reverse('add_to_cart', - args=[self.productid])) - self.assertEqual(response.status_code, 302) - - def test_remove_from_cart(self): - response = self.client.post(reverse('remove_from_cart', - args=[self.productid])) - self.assertEqual(response.status_code, 302) - - def test_view_cart(self): - response = self.client.get(self.cart_url) - self.assertEqual(response.status_code, 200) diff --git a/artel/store/cart.py b/artel/store/cart.py index 9020277..527d85f 100644 --- a/artel/store/cart.py +++ b/artel/store/cart.py @@ -46,7 +46,7 @@ class SessionCart(BaseCart): if not self.session.get(settings.CART_SESSION_ID): self.session[settings.CART_SESSION_ID] = {} - def add_item(self, item_id, quantity): + def add_item(self, item_id: int, quantity: int) -> None: # TODO - add logging self.validate_item_id(item_id) @@ -55,7 +55,7 @@ class SessionCart(BaseCart): else: self.update_item_quantity(item_id, quantity) - def remove_item(self, item_id): + def remove_item(self, item_id: int) -> None: self.validate_item_id(item_id) try: self.session[settings.CART_SESSION_ID].pop(item_id) @@ -63,7 +63,7 @@ class SessionCart(BaseCart): # TODO - add logging ... - def update_item_quantity(self, item_id, change): + def update_item_quantity(self, item_id: int, change: int) -> None: self.validate_item_id(item_id) try: self.session[settings.CART_SESSION_ID][item_id] += change @@ -75,4 +75,4 @@ class SessionCart(BaseCart): _items = [] for item_id, quantity in self.session[settings.CART_SESSION_ID].items(): _items.append(CartItem(quantity=quantity, product=Product.objects.get(id=item_id))) - return _items \ No newline at end of file + return _items diff --git a/artel/store/static/js/cart.js b/artel/store/static/js/cart.js index 5d49051..20a562c 100644 --- a/artel/store/static/js/cart.js +++ b/artel/store/static/js/cart.js @@ -17,6 +17,7 @@ $('#add-to-cart-form').on('submit', function(event) { }); }); + // Fetch cart items and update the display function fetchCartItems() { fetch('/cart/') @@ -31,4 +32,4 @@ function fetchCartItems() { }); }); -} \ No newline at end of file +} diff --git a/artel/store/templates/store/cart.html b/artel/store/templates/store/cart.html new file mode 100644 index 0000000..e51c93e --- /dev/null +++ b/artel/store/templates/store/cart.html @@ -0,0 +1,8 @@ +{% extends "base.html" %} + {% block content %} +

Shopping Cart

+ +

Cart Items

+ + + {% endblock %} diff --git a/artel/store/templates/store/index.html b/artel/store/templates/store/index.html deleted file mode 100644 index 9e03013..0000000 --- a/artel/store/templates/store/index.html +++ /dev/null @@ -1,64 +0,0 @@ -{% extends "base.html" %} - {% block content %} -

Shopping Cart

- -

Cart Items

- - -

Add Item to Cart

-
- {% csrf_token %} -
- - -
-
- - -
- -
- - - - {% endblock %} \ No newline at end of file diff --git a/artel/store/tests.py b/artel/store/tests.py index 7ce503c..ee19e98 100644 --- a/artel/store/tests.py +++ b/artel/store/tests.py @@ -1,3 +1,36 @@ from django.test import TestCase +from django.urls import reverse +from store.models import ProductAuthor, ProductCategory, ProductTemplate, Product -# Create your tests here. + +# TODO - this is fine for now, but we'll want to use factoryboy for this: +# https://factoryboy.readthedocs.io/en/stable/ +# TODO - test have to rewritten - I'll do it tommorow +class CartTestCase(TestCase): + def setUp(self): + self.productid = 1 + self.author = ProductAuthor.objects.create(name='Test Author') + self.category = ProductCategory.objects.create(name='Test Category') + self.template = ProductTemplate.objects.create(category=self.category, + author=self.author, + title='Test title', + code='Test code', + description='Test description' + ) + self.product = Product.objects.create(template=self.template, + price=10.99) + self.cart_url = reverse('view_cart') + + def test_add_to_cart(self): + response = self.client.post(reverse('add_to_cart', + args=[self.productid])) + self.assertEqual(response.status_code, 302) + + def test_remove_from_cart(self): + response = self.client.post(reverse('remove_from_cart', + args=[self.productid])) + self.assertEqual(response.status_code, 302) + + def test_view_cart(self): + response = self.client.get(self.cart_url) + self.assertEqual(response.status_code, 200)