Public Access
mirror of
https://github.com/xlorepdarkhelm/numinar-coding-project.git
synced 2026-09-11 02:23:22 -04:00
141 lines
4.9 KiB
Python
141 lines
4.9 KiB
Python
"""Unit tests for booking validation helpers in backend.services.bookings."""
|
|
|
|
from datetime import datetime
|
|
from datetime import timedelta
|
|
from datetime import timezone
|
|
from unittest.mock import MagicMock
|
|
|
|
import pytest
|
|
|
|
from backend.src.backend.services.bookings import validate_new_booking_max_future
|
|
from backend.src.backend.services.bookings import validate_new_booking_time_constraints
|
|
from backend.src.backend.services.bookings import validate_time_constraints
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"start_offset,end_offset,should_raise",
|
|
[
|
|
(1, 2, False), # valid
|
|
(-2, -1, True), # start/end in past
|
|
(2, 1, True), # start after end
|
|
],
|
|
)
|
|
@pytest.mark.parametrize("mock_logger", ["backend.services.bookings"], indirect=True)
|
|
def test_validate_new_booking_time_constraints_param(
|
|
mock_logger: MagicMock, start_offset: int, end_offset: int, should_raise: bool
|
|
) -> None:
|
|
"""Test validate_new_booking_time_constraints with various offsets and expected errors.
|
|
|
|
Args:
|
|
mock_logger: The mock logger instance.
|
|
start_offset: Hours to offset start time from now.
|
|
end_offset: Hours to offset end time from now.
|
|
should_raise: Whether a ValueError is expected.
|
|
"""
|
|
now = datetime.now(timezone.utc)
|
|
start = now + timedelta(hours=start_offset)
|
|
end = now + timedelta(hours=end_offset)
|
|
if should_raise:
|
|
with pytest.raises(ValueError):
|
|
validate_new_booking_time_constraints(start, end, now)
|
|
else:
|
|
validate_new_booking_time_constraints(start, end, now)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"start_days,end_days,max_months,should_raise",
|
|
[
|
|
(10, 11, 12, False), # valid
|
|
(400, 401, 12, True), # too far in future
|
|
],
|
|
)
|
|
@pytest.mark.parametrize("mock_logger", ["backend.services.bookings"], indirect=True)
|
|
def test_validate_new_booking_max_future_param(
|
|
mock_logger: MagicMock,
|
|
start_days: int,
|
|
end_days: int,
|
|
max_months: int,
|
|
should_raise: bool,
|
|
) -> None:
|
|
"""Test validate_new_booking_max_future with various days and max_months.
|
|
|
|
Args:
|
|
mock_logger: The mock logger instance.
|
|
start_days: Days to offset start time from now.
|
|
end_days: Days to offset end time from now.
|
|
max_months: Maximum months allowed in future.
|
|
should_raise: Whether a ValueError is expected.
|
|
"""
|
|
now = datetime.now(timezone.utc)
|
|
start = now + timedelta(days=start_days)
|
|
end = now + timedelta(days=end_days)
|
|
if should_raise:
|
|
with pytest.raises(ValueError):
|
|
validate_new_booking_max_future(start, end, now, max_months)
|
|
else:
|
|
validate_new_booking_max_future(start, end, now, max_months)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"start_offset,end_offset,max_months,should_raise",
|
|
[
|
|
(1, 2, 12, False), # valid
|
|
(-2, -1, 12, True), # start/end in past
|
|
],
|
|
)
|
|
@pytest.mark.parametrize("mock_logger", ["backend.services.bookings"], indirect=True)
|
|
def test_validate_time_constraints_param(
|
|
mock_logger: MagicMock,
|
|
start_offset: int,
|
|
end_offset: int,
|
|
max_months: int,
|
|
should_raise: bool,
|
|
) -> None:
|
|
"""Test validate_time_constraints with various offsets and max_months.
|
|
|
|
Args:
|
|
mock_logger: The mock logger instance.
|
|
start_offset: Hours to offset start time from now.
|
|
end_offset: Hours to offset end time from now.
|
|
max_months: Maximum months allowed in future.
|
|
should_raise: Whether a ValueError is expected.
|
|
"""
|
|
now = datetime.now(timezone.utc)
|
|
new_start = now + timedelta(hours=start_offset)
|
|
new_end = now + timedelta(hours=end_offset)
|
|
if should_raise:
|
|
with pytest.raises(ValueError):
|
|
validate_time_constraints(new_start, new_end, now, max_months)
|
|
else:
|
|
validate_time_constraints(new_start, new_end, now, max_months)
|
|
|
|
|
|
@pytest.mark.parametrize("mock_logger", ["backend.services.bookings"], indirect=True)
|
|
def test_validate_time_constraints_start_after_end(mock_logger: MagicMock) -> None:
|
|
"""Test that start time after end time raises ValueError in validate_time_constraints.
|
|
|
|
Args:
|
|
mock_logger: The mock logger instance.
|
|
"""
|
|
now = datetime.now(timezone.utc)
|
|
new_start = now + timedelta(hours=2)
|
|
new_end = now + timedelta(hours=1)
|
|
max_months = 12
|
|
with pytest.raises(ValueError):
|
|
validate_time_constraints(new_start, new_end, now, max_months)
|
|
|
|
|
|
@pytest.mark.parametrize("mock_logger", ["backend.services.bookings"], indirect=True)
|
|
def test_validate_time_constraints_too_far(mock_logger: MagicMock) -> None:
|
|
"""Test that booking too far in the future raises ValueError in validate_time_constraints.
|
|
|
|
Args:
|
|
mock_logger: The mock logger instance.
|
|
"""
|
|
now = datetime.now(timezone.utc)
|
|
new_start = now + timedelta(days=400)
|
|
new_end = now + timedelta(days=401)
|
|
max_months = 12
|
|
with pytest.raises(ValueError):
|
|
validate_time_constraints(new_start, new_end, now, max_months)
|