From b04e7235d9ae91c1c32fc41685b50bd060d4829d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=AD=C3=B0ir=20Valberg=20Gu=C3=B0mundsson?= Date: Fri, 29 Mar 2019 11:19:31 +0100 Subject: [PATCH] Adding a test for the OrderProductRelationForm. --- src/shop/tests.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/shop/tests.py b/src/shop/tests.py index 7cf03842..3cd492d0 100644 --- a/src/shop/tests.py +++ b/src/shop/tests.py @@ -1,8 +1,10 @@ +from django.core.exceptions import ValidationError from django.test import TestCase from django.utils import timezone from psycopg2.extras import DateTimeTZRange +from shop.forms import OrderProductRelationForm from .factories import ( ProductFactory, OrderProductRelationFactory, @@ -81,3 +83,32 @@ class ProductAvailabilityTest(TestCase): # The factory defines the timeframe as now and 31 days forward. self.assertTrue(product.is_time_available) self.assertTrue(product.is_available()) + + +class TestOrderProductRelationForm(TestCase): + + def test_clean_quantity_succeeds_when_stock_not_exceeded(self): + product = ProductFactory(stock_amount=2) + + # Mark an order as paid/reserved by setting open to None + opr1 = OrderProductRelationFactory(product=product, quantity=1) + opr1.order.open = None + opr1.order.save() + + opr2 = OrderProductRelationFactory(product=product, quantity=1) + + form = OrderProductRelationForm(instance=opr2) + self.assertTrue(form.is_valid) + + def test_clean_quantity_fails_when_stock_exceeded(self): + product = ProductFactory(stock_amount=2) + # Mark an order as paid/reserved by setting open to None + opr1 = OrderProductRelationFactory(product=product, quantity=1) + opr1.order.open = None + opr1.order.save() + + # There should only be 1 product left, since we just reserved 1 + opr2 = OrderProductRelationFactory(product=product, quantity=2) + + form = OrderProductRelationForm(instance=opr2) + self.assertFalse(form.is_valid())