renamed template moved tests

pull/2/head
mtyton 2023-05-22 22:11:42 +02:00
rodzic 9b5e4a954b
commit 7e7f9ee4b3
7 zmienionych plików z 49 dodań i 103 usunięć

1
.gitignore vendored
Wyświetl plik

@ -139,3 +139,4 @@ GitHub.sublime-settings
#postgres pass files
*.my_pgpass
*.sql
artel/static/

Wyświetl plik

@ -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)

Wyświetl plik

@ -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
return _items

Wyświetl plik

@ -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() {
});
});
}
}

Wyświetl plik

@ -0,0 +1,8 @@
{% extends "base.html" %}
{% block content %}
<h1>Shopping Cart</h1>
<h2>Cart Items</h2>
<ul id="cart-items"></ul>
{% endblock %}

Wyświetl plik

@ -1,64 +0,0 @@
{% extends "base.html" %}
{% block content %}
<h1>Shopping Cart</h1>
<h2>Cart Items</h2>
<ul id="cart-items"></ul>
<h2>Add Item to Cart</h2>
<form id="add-to-cart-form" method="POST" action="/cart/item/">
{% csrf_token %}
<div>
<label for="id_product_id">Product ID:</label>
<input type="number" id="id_product_id" name="product_id" required>
</div>
<div>
<label for="id_quantity">Quantity:</label>
<input type="number" id="id_quantity" name="quantity" required>
</div>
<button type="submit">Add to Cart</button>
</form>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
// Fetch cart items and display them on page load
$(document).ready(function() {
fetchCartItems();
});
// Add item to cart on form submission
$('#add-to-cart-form').on('submit', function(event) {
event.preventDefault();
const form = $(this);
const formData = new FormData(form[0]); // Serialize the form data correctly
$.ajax({
type: form.attr('method'),
url: form.attr('action'),
data: formData, // Use the serialized form data
dataType: 'json',
processData: false, // Prevent jQuery from processing the data
contentType: false, // Let the browser set the content type
success: function(data) {
alert(data.message);
fetchCartItems();
}
});
});
// Fetch cart items and update the display
function fetchCartItems() {
fetch('/cart/')
.then(response => response.json())
.then(data => {
const cartItems = data.cart_items;
const cartItemsList = $('#cart-items');
cartItemsList.empty();
cartItems.forEach(item => {
const li = $('<li>').text(`Product ID: ${item.product_id}, Quantity: ${item.quantity}`);
cartItemsList.append(li);
});
});
}
</script>
{% endblock %}

Wyświetl plik

@ -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)