diff --git a/tests/factories/reservation_unit.py b/tests/factories/reservation_unit.py index 7691e09ca8..e357743ded 100644 --- a/tests/factories/reservation_unit.py +++ b/tests/factories/reservation_unit.py @@ -9,7 +9,7 @@ from factory import LazyAttribute from factory.fuzzy import FuzzyInteger -from tilavarauspalvelu.enums import AuthenticationType, ReservationKind, ReservationStartInterval +from tilavarauspalvelu.enums import AuthenticationType, MethodOfEntry, ReservationKind, ReservationStartInterval from tilavarauspalvelu.models import ReservationUnit from utils.date_utils import local_start_of_day from utils.utils import as_p_tags @@ -93,6 +93,8 @@ class Meta: min_reservation_duration = None buffer_time_before = factory.LazyFunction(datetime.timedelta) buffer_time_after = factory.LazyFunction(datetime.timedelta) + method_of_entry_start_date = None + method_of_entry_end_date = None # Booleans is_draft = False @@ -108,6 +110,7 @@ class Meta: authentication = AuthenticationType.WEAK.value reservation_start_interval = ReservationStartInterval.INTERVAL_15_MINUTES.value reservation_kind = ReservationKind.DIRECT_AND_SEASON.value + method_of_entry = MethodOfEntry.OPEN_ACCESS.value # Lists search_terms = LazyAttribute(lambda i: []) diff --git a/tests/test_actions/test_reservation_unit_actions/test_method_of_entry_at.py b/tests/test_actions/test_reservation_unit_actions/test_method_of_entry_at.py new file mode 100644 index 0000000000..1164c3ea6c --- /dev/null +++ b/tests/test_actions/test_reservation_unit_actions/test_method_of_entry_at.py @@ -0,0 +1,117 @@ +from __future__ import annotations + +import datetime + +import freezegun +import pytest + +from tilavarauspalvelu.enums import MethodOfEntry +from utils.date_utils import local_datetime + +from tests.factories import ReservationUnitFactory + +pytestmark = [ + pytest.mark.django_db, +] + + +@freezegun.freeze_time("2025-01-01") +@pytest.mark.parametrize( + "method_of_entry", + [ + MethodOfEntry.OPEN_ACCESS, + MethodOfEntry.KEYLESS, + MethodOfEntry.WITH_KEY, + ], +) +def test_reservation_unit__method_of_entry_at__no_bounds(method_of_entry): + now = local_datetime() + past = now - datetime.timedelta(days=1) + future = now + datetime.timedelta(days=1) + + reservation_unit = ReservationUnitFactory.create(method_of_entry=method_of_entry) + + assert reservation_unit.actions.get_method_of_entry_at(past) == method_of_entry + assert reservation_unit.actions.get_method_of_entry_at(now) == method_of_entry + assert reservation_unit.actions.get_method_of_entry_at(future) == method_of_entry + + assert reservation_unit.current_method_of_entry == method_of_entry + + +@freezegun.freeze_time("2025-01-01") +@pytest.mark.parametrize( + "method_of_entry", + [ + MethodOfEntry.OPEN_ACCESS, + MethodOfEntry.KEYLESS, + MethodOfEntry.WITH_KEY, + ], +) +def test_reservation_unit__method_of_entry_at__starts(method_of_entry): + now = local_datetime() + past = now - datetime.timedelta(days=1) + future = now + datetime.timedelta(days=1) + + reservation_unit = ReservationUnitFactory.create( + method_of_entry=method_of_entry, + method_of_entry_start_date=future.date(), + ) + + assert reservation_unit.actions.get_method_of_entry_at(past) == MethodOfEntry.OPEN_ACCESS + assert reservation_unit.actions.get_method_of_entry_at(now) == MethodOfEntry.OPEN_ACCESS + assert reservation_unit.actions.get_method_of_entry_at(future) == method_of_entry + + assert reservation_unit.current_method_of_entry == MethodOfEntry.OPEN_ACCESS + + +@freezegun.freeze_time("2025-01-01") +@pytest.mark.parametrize( + "method_of_entry", + [ + MethodOfEntry.OPEN_ACCESS, + MethodOfEntry.KEYLESS, + MethodOfEntry.WITH_KEY, + ], +) +def test_reservation_unit__method_of_entry_at__ends(method_of_entry): + now = local_datetime() + past = now - datetime.timedelta(days=1) + future = now + datetime.timedelta(days=1) + + reservation_unit = ReservationUnitFactory.create( + method_of_entry=method_of_entry, + method_of_entry_end_date=now.date(), + ) + + assert reservation_unit.actions.get_method_of_entry_at(past) == method_of_entry + assert reservation_unit.actions.get_method_of_entry_at(now) == method_of_entry + assert reservation_unit.actions.get_method_of_entry_at(future) == MethodOfEntry.OPEN_ACCESS + + assert reservation_unit.current_method_of_entry == method_of_entry + + +@freezegun.freeze_time("2025-01-01") +@pytest.mark.parametrize( + "method_of_entry", + [ + MethodOfEntry.OPEN_ACCESS, + MethodOfEntry.KEYLESS, + MethodOfEntry.WITH_KEY, + ], +) +def test_reservation_unit__method_of_entry_at__period(method_of_entry): + now = local_datetime() + past = now - datetime.timedelta(days=1) + future = now + datetime.timedelta(days=1) + + reservation_unit = ReservationUnitFactory.create( + method_of_entry=method_of_entry, + method_of_entry_start_date=now.date(), + method_of_entry_end_date=now.date(), + ) + + assert reservation_unit.actions.get_method_of_entry_at(past) == MethodOfEntry.OPEN_ACCESS + assert reservation_unit.actions.get_method_of_entry_at(now) == method_of_entry + assert reservation_unit.actions.get_method_of_entry_at(future) == MethodOfEntry.OPEN_ACCESS + + assert reservation_unit.current_method_of_entry == method_of_entry