Adding more tests, finding more errors.

This commit is contained in:
Víðir Valberg Guðmundsson 2019-03-29 17:06:53 +01:00
parent 9d97a7184a
commit 2ce2205bd3
2 changed files with 29 additions and 28 deletions

View file

@ -109,12 +109,11 @@ class TestProductDetailView(TestCase):
def setUp(self): def setUp(self):
self.user = UserFactory() self.user = UserFactory()
self.product = ProductFactory() self.product = ProductFactory()
self.path = reverse("shop:product_detail", kwargs={"slug": self.product.slug})
def test_product_is_available(self): def test_product_is_available(self):
self.client.force_login(self.user) self.client.force_login(self.user)
response = self.client.get( response = self.client.get(self.path)
reverse("shop:product_detail", kwargs={"slug": self.product.slug})
)
self.assertContains(response, "Add to order") self.assertContains(response, "Add to order")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
@ -126,9 +125,7 @@ class TestProductDetailView(TestCase):
OrderProductRelationFactory(product=self.product, quantity=1, order__open=None) OrderProductRelationFactory(product=self.product, quantity=1, order__open=None)
self.client.force_login(self.user) self.client.force_login(self.user)
response = self.client.get( response = self.client.get(self.path)
reverse("shop:product_detail", kwargs={"slug": self.product.slug})
)
self.assertContains(response, "<bold>1</bold> available") self.assertContains(response, "<bold>1</bold> available")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
@ -140,9 +137,7 @@ class TestProductDetailView(TestCase):
OrderProductRelationFactory(product=self.product, quantity=1, order__open=None) OrderProductRelationFactory(product=self.product, quantity=1, order__open=None)
self.client.force_login(self.user) self.client.force_login(self.user)
response = self.client.get( response = self.client.get(self.path)
reverse("shop:product_detail", kwargs={"slug": self.product.slug})
)
self.assertContains(response, "Sold out.") self.assertContains(response, "Sold out.")
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
@ -150,8 +145,7 @@ class TestProductDetailView(TestCase):
def test_adding_product_to_new_order(self): def test_adding_product_to_new_order(self):
self.client.force_login(self.user) self.client.force_login(self.user)
path = reverse("shop:product_detail", kwargs={"slug": self.product.slug}) response = self.client.post(self.path, data={'quantity': 1})
response = self.client.post(path, data={'quantity': 1})
order = self.user.orders.get() order = self.user.orders.get()
@ -162,9 +156,7 @@ class TestProductDetailView(TestCase):
OrderProductRelationFactory(product=self.product, quantity=1, order__open=True, order__user=self.user) OrderProductRelationFactory(product=self.product, quantity=1, order__open=True, order__user=self.user)
self.client.force_login(self.user) self.client.force_login(self.user)
response = self.client.get( response = self.client.get(self.path)
reverse("shop:product_detail", kwargs={"slug": self.product.slug})
)
self.assertContains(response, "Update order") self.assertContains(response, "Update order")
@ -177,9 +169,14 @@ class TestProductDetailView(TestCase):
self.client.force_login(self.user) self.client.force_login(self.user)
path = reverse("shop:product_detail", kwargs={"slug": self.product.slug}) response = self.client.post(self.path, data={'quantity': 2})
response = self.client.post(path, data={'quantity': 2})
self.assertRedirects(response, reverse('shop:order_detail', kwargs={"pk": opr.order.pk})) self.assertRedirects(response, reverse('shop:order_detail', kwargs={"pk": opr.order.pk}))
opr.refresh_from_db() opr.refresh_from_db()
self.assertEquals(opr.quantity, 2) self.assertEquals(opr.quantity, 2)
def test_product_category_not_public(self):
self.product.category.public = False
self.product.category.save()
response = self.client.get(self.path)
self.assertEquals(response.status_code, 404)

View file

@ -228,7 +228,9 @@ class ProductDetailView(FormView, DetailView):
return kwargs return kwargs
def get_initial(self): def get_initial(self):
if self.opr:
return {'quantity': self.opr.quantity} return {'quantity': self.opr.quantity}
return super().get_initial()
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
if hasattr(self.opr, 'order'): if hasattr(self.opr, 'order'):
@ -239,6 +241,8 @@ class ProductDetailView(FormView, DetailView):
def dispatch(self, request, *args, **kwargs): def dispatch(self, request, *args, **kwargs):
self.object = self.get_object() self.object = self.get_object()
self.opr = None
if self.request.user.is_authenticated:
try: try:
self.opr = OrderProductRelation.objects.get( self.opr = OrderProductRelation.objects.get(
order__user=self.request.user, order__user=self.request.user,