cmsmanage/reservations/tests/test_Reservation.py

117 lines
3.7 KiB
Python
Raw Permalink Normal View History

from datetime import datetime, timedelta
from django.test import TestCase
from django.utils import timezone
from reservations.models import Reservation, Resource
class ReservationRangesTestCase(TestCase):
def setUp(self):
tz = timezone.get_current_timezone()
test_resource = Resource.objects.create(
name="test resource",
min_length=timedelta(minutes=15),
max_length=timedelta(hours=4),
)
test_reservation = Reservation.objects.create(
start=datetime(2021, 12, 8, 12, 00, tzinfo=tz),
end=datetime(2021, 12, 8, 14, 0, tzinfo=tz),
)
test_reservation.resources.add(test_resource)
def test_after(self):
tz = timezone.get_current_timezone()
self.assertTrue(
Reservation.objects.filter_after(datetime(2021, 12, 8, 11, 0, tzinfo=tz))
)
self.assertTrue(
Reservation.objects.filter_after(datetime(2021, 12, 8, 12, 0, tzinfo=tz))
)
self.assertTrue(
Reservation.objects.filter_after(datetime(2021, 12, 8, 13, 0, tzinfo=tz))
)
self.assertFalse(
Reservation.objects.filter_after(datetime(2021, 12, 8, 14, 00, tzinfo=tz))
)
self.assertFalse(
Reservation.objects.filter_after(datetime(2021, 12, 8, 15, 00, tzinfo=tz))
)
def test_before(self):
tz = timezone.get_current_timezone()
self.assertTrue(
Reservation.objects.filter_before(datetime(2021, 12, 8, 13, 0, tzinfo=tz))
)
self.assertTrue(
Reservation.objects.filter_before(datetime(2021, 12, 8, 14, 0, tzinfo=tz))
)
self.assertTrue(
Reservation.objects.filter_before(datetime(2021, 12, 8, 15, 0, tzinfo=tz))
)
self.assertFalse(
Reservation.objects.filter_before(datetime(2021, 12, 8, 11, 00, tzinfo=tz))
)
self.assertFalse(
Reservation.objects.filter_before(datetime(2021, 12, 8, 12, 00, tzinfo=tz))
)
def test_between(self):
tz = timezone.get_current_timezone()
# contained (reservation entirely inside both start and end)
self.assertTrue(
Reservation.objects.filter_between(
datetime(2021, 12, 8, 11, 0, tzinfo=tz),
datetime(2021, 12, 8, 15, 0, tzinfo=tz),
)
)
# overlapping edges
self.assertTrue(
Reservation.objects.filter_between(
datetime(2021, 12, 8, 11, 0, tzinfo=tz),
datetime(2021, 12, 8, 13, 0, tzinfo=tz),
)
)
self.assertTrue(
Reservation.objects.filter_between(
datetime(2021, 12, 8, 13, 0, tzinfo=tz),
datetime(2021, 12, 8, 15, 0, tzinfo=tz),
)
)
# containing (reservation contains both start and end)
self.assertTrue(
Reservation.objects.filter_between(
datetime(2021, 12, 8, 12, 30, tzinfo=tz),
datetime(2021, 12, 8, 13, 30, tzinfo=tz),
)
)
# on boundries
self.assertFalse(
Reservation.objects.filter_between(
datetime(2021, 12, 8, 11, 0, tzinfo=tz),
datetime(2021, 12, 8, 12, 0, tzinfo=tz),
)
)
self.assertFalse(
Reservation.objects.filter_between(
datetime(2021, 12, 8, 14, 0, tzinfo=tz),
datetime(2021, 12, 8, 15, 0, tzinfo=tz),
)
)
# outside
self.assertFalse(
Reservation.objects.filter_between(
datetime(2021, 12, 8, 15, 0, tzinfo=tz),
datetime(2021, 12, 8, 17, 0, tzinfo=tz),
)
)