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 28, 2023
1 parent 5d6d103 commit 0926c32
Showing 1 changed file with 128 additions and 31 deletions.
159 changes: 128 additions & 31 deletions stregsystem/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
PayTransaction,
Product,
Room,
Event,
Sale,
StregForbudError,
active_str,
Expand Down Expand Up @@ -694,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 @@ -732,27 +733,123 @@ 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)
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())


class EventTests(TestCase):
def setUp(self):
self.jeff = Member.objects.create(
username="Jeff",
)
product.sale_set.create(price=100, member=self.jeff)

self.assertFalse(product.is_active())
def test_is_active_active_not_expired(self):
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())

def test_is_active_active_expired_after(self):
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())

def test_is_active_active_before_active(self):
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())

def test_is_active_inactive(self):
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())

def test_is_active_inactive_expired(self):
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())

def test_is_active_deactive_out_of_stock(self):
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())

def test_is_active_deactive_in_stock(self):
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())


class SaleTests(TestCase):
Expand Down

0 comments on commit 0926c32

Please sign in to comment.