Adding test for ProductDetailView.

This commit is contained in:
Víðir Valberg Guðmundsson 2019-03-29 11:49:04 +01:00
parent b04e7235d9
commit 32b4cdfbee

View file

@ -1,14 +1,14 @@
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.test import TestCase from django.test import TestCase
from django.urls import reverse
from django.utils import timezone from django.utils import timezone
from psycopg2.extras import DateTimeTZRange from psycopg2.extras import DateTimeTZRange
from shop.forms import OrderProductRelationForm from shop.forms import OrderProductRelationForm
from .factories import ( from utils.factories import UserFactory
ProductFactory, from .factories import ProductFactory, OrderProductRelationFactory
OrderProductRelationFactory,
)
class ProductAvailabilityTest(TestCase): class ProductAvailabilityTest(TestCase):
@ -41,7 +41,6 @@ class ProductAvailabilityTest(TestCase):
self.assertTrue(product.is_stock_available) self.assertTrue(product.is_stock_available)
self.assertTrue(product.is_available()) self.assertTrue(product.is_available())
def test_product_available_by_time(self): def test_product_available_by_time(self):
""" The product is available if now is in the right timeframe. """ """ The product is available if now is in the right timeframe. """
product = ProductFactory() product = ProductFactory()
@ -53,7 +52,7 @@ class ProductAvailabilityTest(TestCase):
""" The product is not available if now is outside the timeframe. """ """ The product is not available if now is outside the timeframe. """
available_in = DateTimeTZRange( available_in = DateTimeTZRange(
lower=timezone.now() - timezone.timedelta(5), lower=timezone.now() - timezone.timedelta(5),
upper=timezone.now() - timezone.timedelta(1) upper=timezone.now() - timezone.timedelta(1),
) )
product = ProductFactory(available_in=available_in) product = ProductFactory(available_in=available_in)
# The factory defines the timeframe as now and 31 days forward. # The factory defines the timeframe as now and 31 days forward.
@ -62,9 +61,7 @@ class ProductAvailabilityTest(TestCase):
def test_product_is_not_available_yet(self): def test_product_is_not_available_yet(self):
""" The product is not available because we are before lower bound. """ """ The product is not available because we are before lower bound. """
available_in = DateTimeTZRange( available_in = DateTimeTZRange(lower=timezone.now() + timezone.timedelta(5))
lower=timezone.now() + timezone.timedelta(5)
)
product = ProductFactory(available_in=available_in) product = ProductFactory(available_in=available_in)
# Make sure there is no upper - just in case. # Make sure there is no upper - just in case.
self.assertEqual(product.available_in.upper, None) self.assertEqual(product.available_in.upper, None)
@ -74,9 +71,7 @@ class ProductAvailabilityTest(TestCase):
def test_product_is_available_from_now_on(self): def test_product_is_available_from_now_on(self):
""" The product is available because we are after lower bound. """ """ The product is available because we are after lower bound. """
available_in = DateTimeTZRange( available_in = DateTimeTZRange(lower=timezone.now() - timezone.timedelta(1))
lower=timezone.now() - timezone.timedelta(1)
)
product = ProductFactory(available_in=available_in) product = ProductFactory(available_in=available_in)
# Make sure there is no upper - just in case. # Make sure there is no upper - just in case.
self.assertEqual(product.available_in.upper, None) self.assertEqual(product.available_in.upper, None)
@ -86,7 +81,6 @@ class ProductAvailabilityTest(TestCase):
class TestOrderProductRelationForm(TestCase): class TestOrderProductRelationForm(TestCase):
def test_clean_quantity_succeeds_when_stock_not_exceeded(self): def test_clean_quantity_succeeds_when_stock_not_exceeded(self):
product = ProductFactory(stock_amount=2) product = ProductFactory(stock_amount=2)
@ -112,3 +106,48 @@ class TestOrderProductRelationForm(TestCase):
form = OrderProductRelationForm(instance=opr2) form = OrderProductRelationForm(instance=opr2)
self.assertFalse(form.is_valid()) self.assertFalse(form.is_valid())
class TestProductDetailView(TestCase):
def setUp(self):
self.user = UserFactory()
def test_product_is_available(self):
product = ProductFactory()
self.client.force_login(self.user)
response = self.client.get(
reverse("shop:product_detail", kwargs={"slug": product.slug})
)
self.assertIn("Add to order", str(response.content))
self.assertEqual(response.status_code, 200)
def test_product_is_available_with_stock_left(self):
product = ProductFactory(stock_amount=2)
opr1 = OrderProductRelationFactory(product=product, quantity=1)
opr1.order.open = None
opr1.order.save()
self.client.force_login(self.user)
response = self.client.get(
reverse("shop:product_detail", kwargs={"slug": product.slug})
)
self.assertIn("<bold>1</bold> available", str(response.content))
self.assertEqual(response.status_code, 200)
def test_product_is_sold_out(self):
product = ProductFactory(stock_amount=1)
opr1 = OrderProductRelationFactory(product=product, quantity=1)
opr1.order.open = None
opr1.order.save()
self.client.force_login(self.user)
response = self.client.get(
reverse("shop:product_detail", kwargs={"slug": product.slug})
)
self.assertIn("Sold out.", str(response.content))
self.assertEqual(response.status_code, 200)