Skip to content

Commit

Permalink
Using mocked time
Browse files Browse the repository at this point in the history
  • Loading branch information
krestenlaust committed Oct 27, 2023
1 parent c415303 commit 6445eff
Showing 1 changed file with 100 additions and 98 deletions.
198 changes: 100 additions & 98 deletions stregsystem/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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):
Expand All @@ -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):
Expand Down

0 comments on commit 6445eff

Please sign in to comment.