Added the first tests to the project. Using factory_boy for great success. (#219)

This commit is contained in:
Víðir Valberg Guðmundsson 2018-04-22 08:31:10 +02:00 committed by Thomas Steen Rasmussen
parent 9b2e164ddb
commit a250635132
5 changed files with 96 additions and 1 deletions

View file

@ -0,0 +1,3 @@
-r dev.txt
factory_boy==2.10.0

51
src/shop/factories.py Normal file
View file

@ -0,0 +1,51 @@
import factory
from factory.django import DjangoModelFactory
from django.utils import timezone
from psycopg2.extras import DateTimeTZRange
from utils.factories import UserFactory
class ProductCategoryFactory(DjangoModelFactory):
class Meta:
model = 'shop.ProductCategory'
name = factory.Faker('word')
class ProductFactory(DjangoModelFactory):
class Meta:
model = 'shop.Product'
name = factory.Faker('word')
slug = factory.Faker('word')
category = factory.SubFactory(ProductCategoryFactory)
description = factory.Faker('paragraph')
price = factory.Faker('pyint')
available_in = factory.LazyFunction(
lambda:
DateTimeTZRange(
lower=timezone.now(),
upper=timezone.now() + timezone.timedelta(31)
)
)
class OrderFactory(DjangoModelFactory):
class Meta:
model = 'shop.Order'
user = factory.SubFactory(UserFactory)
class OrderProductRelationFactory(DjangoModelFactory):
class Meta:
model = 'shop.OrderProductRelation'
product = factory.SubFactory(ProductFactory)
order = factory.SubFactory(OrderFactory)
quantity = 1
handed_out = False

View file

@ -351,7 +351,7 @@ class Product(CreatedUpdatedModel, UUIDModel):
""" """
now = timezone.now() now = timezone.now()
time_available = now in self.available_in time_available = now in self.available_in
stock_available = (self.stock_amount - self.left_in_stock()) > 0 stock_available = self.left_in_stock() > 0
return time_available and stock_available return time_available and stock_available
def is_old(self): def is_old(self):

30
src/shop/tests.py Normal file
View file

@ -0,0 +1,30 @@
from django.test import TestCase
from .factories import (
ProductFactory,
OrderFactory,
OrderProductRelationFactory,
)
class ProductAvailabilityTest(TestCase):
""" Test logic about availability of products. """
def test_product_available_by_stock(self):
""" If no orders have been made, the product is still available. """
product = ProductFactory(stock_amount=10)
self.assertEqual(product.left_in_stock(), 10)
self.assertTrue(product.is_available)
def test_product_not_available_by_stock(self):
""" If max orders have been made, the product is NOT available. """
product = ProductFactory(stock_amount=2)
for i in range(2):
opr = OrderProductRelationFactory(product=product)
order = opr.order
order.paid = True
order.save()
self.assertEqual(product.left_in_stock(), 0)
self.assertFalse(product.is_available())

11
src/utils/factories.py Normal file
View file

@ -0,0 +1,11 @@
import factory
from factory.django import DjangoModelFactory
class UserFactory(DjangoModelFactory):
class Meta:
model = 'auth.User'
username = factory.Faker('word')
email = factory.Faker('ascii_email')