From 6445effade37732bc51b67fc50ce0b32ea6c5f51 Mon Sep 17 00:00:00 2001 From: Kresten Laust Date: Fri, 27 Oct 2023 20:04:01 +0200 Subject: [PATCH] Using mocked time --- stregsystem/tests.py | 198 ++++++++++++++++++++++--------------------- 1 file changed, 100 insertions(+), 98 deletions(-) diff --git a/stregsystem/tests.py b/stregsystem/tests.py index a8a03619..80bb4447 100644 --- a/stregsystem/tests.py +++ b/stregsystem/tests.py @@ -695,34 +695,34 @@ def test_is_active_active(self): self.assertTrue(product.is_active()) def test_is_active_active_not_expired(self): - product = Product.objects.create( - active=True, price=100, deactivate_date=(timezone.now() + datetime.timedelta(hours=1)) - ) + with freeze_time(timezone.datetime(2018, 1, 1)) as frozen_time: + product = Product.objects.create( + active=True, price=100, deactivate_date=(timezone.now() + datetime.timedelta(hours=1)) + ) - self.assertTrue(product.is_active()) + self.assertTrue(product.is_active()) def test_is_active_active_expired(self): - product = Product.objects.create( - active=True, price=100, deactivate_date=(timezone.now() - datetime.timedelta(hours=1)) - ) + with freeze_time(timezone.datetime(2018, 1, 1)) as frozen_time: + product = Product.objects.create( + active=True, price=100, deactivate_date=(timezone.now() - datetime.timedelta(hours=1)) + ) - self.assertFalse(product.is_active()) + self.assertFalse(product.is_active()) def test_is_active_active_out_of_stock(self): - product = Product.objects.create( - active=True, price=100, quantity=1, start_date=datetime.date(year=2017, month=1, day=1) - ) - product.sale_set.create(price=100, member=self.jeff) + with freeze_time(timezone.datetime(2017, 1, 1)) as frozen_time: + product = Product.objects.create(active=True, price=100, quantity=1, start_date=timezone.now()) + product.sale_set.create(price=100, member=self.jeff) - self.assertFalse(product.is_active()) + self.assertFalse(product.is_active()) def test_is_active_active_in_stock(self): - product = Product.objects.create( - active=True, price=100, quantity=2, start_date=datetime.date(year=2017, month=1, day=1) - ) - product.sale_set.create(price=100, member=self.jeff) + with freeze_time(timezone.datetime(2017, 1, 1)) as frozen_time: + product = Product.objects.create(active=True, price=100, quantity=2, start_date=timezone.now()) + product.sale_set.create(price=100, member=self.jeff) - self.assertTrue(product.is_active()) + self.assertTrue(product.is_active()) def test_is_active_deactive(self): product = Product.objects.create( @@ -733,27 +733,26 @@ def test_is_active_deactive(self): self.assertFalse(product.is_active()) def test_is_active_deactive_expired(self): - product = Product.objects.create( - active=False, price=100, deactivate_date=(timezone.now() - datetime.timedelta(hours=1)) - ) + with freeze_time(timezone.datetime(2017, 1, 1)) as frozen_time: + product = Product.objects.create( + active=False, price=100, deactivate_date=(timezone.now() - datetime.timedelta(hours=1)) + ) - self.assertFalse(product.is_active()) + self.assertFalse(product.is_active()) def test_is_active_deactive_out_of_stock(self): - product = Product.objects.create( - active=False, price=100, quantity=1, start_date=datetime.date(year=2017, month=12, day=1) - ) - product.sale_set.create(price=100, member=self.jeff) + with freeze_time(timezone.datetime(2017, 12, 1)) as frozen_time: + product = Product.objects.create(active=False, price=100, quantity=1, start_date=timezone.now()) + product.sale_set.create(price=100, member=self.jeff) - self.assertFalse(product.is_active()) + self.assertFalse(product.is_active()) def test_is_active_deactive_in_stock(self): - product = Product.objects.create( - active=False, price=100, quantity=2, start_date=datetime.date(year=2017, month=12, day=1) - ) - product.sale_set.create(price=100, member=self.jeff) + with freeze_time(timezone.datetime(2017, 12, 1)) as frozen_time: + product = Product.objects.create(active=False, price=100, quantity=2, start_date=timezone.now()) + product.sale_set.create(price=100, member=self.jeff) - self.assertFalse(product.is_active()) + self.assertFalse(product.is_active()) class EventTests(TestCase): @@ -763,91 +762,94 @@ def setUp(self): ) def test_is_active_active_not_expired(self): - product = Product.objects.create( - active=True, - price=100, - ) - event = Event.objects.create( - name="testEvent", - active=True, - product=product, - time=timezone.now() + datetime.timedelta(hours=1), - ticket_start_threshold=timezone.now() - datetime.timedelta(hours=1), - ticket_end_threshold=timezone.now() + datetime.timedelta(hours=1), - ) + with freeze_time(timezone.datetime(2017, 1, 1)) as frozen_time: + product = Product.objects.create( + active=True, + price=100, + ) + event = Event.objects.create( + name="testEvent", + active=True, + product=product, + time=timezone.now() + datetime.timedelta(hours=1), + ticket_start_threshold=timezone.now() - datetime.timedelta(hours=1), + ticket_end_threshold=timezone.now() + datetime.timedelta(hours=1), + ) - self.assertTrue(event.is_active()) + self.assertTrue(event.is_active()) def test_is_active_active_expired_after(self): - product = Product.objects.create( - active=True, - price=100, - ) - event = Event.objects.create( - name="testEvent", - active=True, - product=product, - time=timezone.now() + datetime.timedelta(hours=1), - ticket_start_threshold=timezone.now() - datetime.timedelta(hours=10), - ticket_end_threshold=timezone.now() - datetime.timedelta(hours=1), - ) + with freeze_time(timezone.datetime(2017, 1, 1)) as frozen_time: + product = Product.objects.create( + active=True, + price=100, + ) + event = Event.objects.create( + name="testEvent", + active=True, + product=product, + time=timezone.now() + datetime.timedelta(hours=1), + ticket_start_threshold=timezone.now() - datetime.timedelta(hours=10), + ticket_end_threshold=timezone.now() - datetime.timedelta(hours=1), + ) - self.assertFalse(event.is_active()) + self.assertFalse(event.is_active()) def test_is_active_active_before_active(self): - product = Product.objects.create( - active=True, - price=100, - ) - event = Event.objects.create( - name="testEvent", - active=True, - product=product, - time=timezone.now() + datetime.timedelta(hours=1), - ticket_start_threshold=timezone.now() + datetime.timedelta(hours=2), - ticket_end_threshold=timezone.now() + datetime.timedelta(hours=5), - ) + with freeze_time(timezone.datetime(2017, 1, 1)) as frozen_time: + product = Product.objects.create( + active=True, + price=100, + ) + event = Event.objects.create( + name="testEvent", + active=True, + product=product, + time=timezone.now() + datetime.timedelta(hours=1), + ticket_start_threshold=timezone.now() + datetime.timedelta(hours=2), + ticket_end_threshold=timezone.now() + datetime.timedelta(hours=5), + ) - self.assertFalse(event.is_active()) + self.assertFalse(event.is_active()) def test_is_active_inactive(self): - product = Product.objects.create( - active=True, - price=100, - ) - event = Event.objects.create( - name="testEvent", - active=False, - product=product, - time=timezone.now() + datetime.timedelta(hours=1), - ticket_start_threshold=timezone.now() - datetime.timedelta(hours=10), - ticket_end_threshold=timezone.now() - datetime.timedelta(hours=1), - ) + with freeze_time(timezone.datetime(2017, 1, 1)) as frozen_time: + product = Product.objects.create( + active=True, + price=100, + ) + event = Event.objects.create( + name="testEvent", + active=False, + product=product, + time=timezone.now() + datetime.timedelta(hours=1), + ticket_start_threshold=timezone.now() - datetime.timedelta(hours=10), + ticket_end_threshold=timezone.now() - datetime.timedelta(hours=1), + ) - self.assertFalse(event.is_active()) + self.assertFalse(event.is_active()) def test_is_active_inactive_expired(self): - product = Product.objects.create( - active=False, price=100, deactivate_date=(timezone.now() - datetime.timedelta(hours=1)) - ) + with freeze_time(timezone.datetime(2017, 1, 1)) as frozen_time: + product = Product.objects.create( + active=False, price=100, deactivate_date=(timezone.now() - datetime.timedelta(hours=1)) + ) - self.assertFalse(product.is_active()) + self.assertFalse(product.is_active()) def test_is_active_deactive_out_of_stock(self): - product = Product.objects.create( - active=False, price=100, quantity=1, start_date=datetime.date(year=2017, month=12, day=1) - ) - product.sale_set.create(price=100, member=self.jeff) + with freeze_time(timezone.datetime(2017, 12, 1)) as frozen_time: + product = Product.objects.create(active=False, price=100, quantity=1, start_date=timezone.now()) + product.sale_set.create(price=100, member=self.jeff) - self.assertFalse(product.is_active()) + self.assertFalse(product.is_active()) def test_is_active_deactive_in_stock(self): - product = Product.objects.create( - active=False, price=100, quantity=2, start_date=datetime.date(year=2017, month=12, day=1) - ) - product.sale_set.create(price=100, member=self.jeff) + with freeze_time(timezone.datetime(2017, 12, 1)) as frozen_time: + product = Product.objects.create(active=False, price=100, quantity=2, start_date=timezone.now()) + product.sale_set.create(price=100, member=self.jeff) - self.assertFalse(product.is_active()) + self.assertFalse(product.is_active()) class SaleTests(TestCase):