Skip to content

Commit

Permalink
Add tests for method of entry
Browse files Browse the repository at this point in the history
  • Loading branch information
matti-lamppu committed Jan 13, 2025
1 parent a3c23dd commit 5cc707a
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 1 deletion.
5 changes: 4 additions & 1 deletion tests/factories/reservation_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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: [])
Expand Down
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5cc707a

Please sign in to comment.