Files
eventspace/backend/tests/schemas/test_events.py
Felipe Cardoso 0b6f47a602
All checks were successful
Build and Push Docker Images / changes (push) Successful in 4s
Build and Push Docker Images / build-backend (push) Successful in 50s
Build and Push Docker Images / build-frontend (push) Has been skipped
Add event schema models and corresponding test cases
Introduce Pydantic models for event creation, updates, and responses, including validation for fields such as timezone, event date, and RSVP deadline. Add comprehensive pytest test cases to ensure correct behavior and data validation. This provides a robust foundation for event-related functionalities.
2025-03-05 12:48:25 +01:00

97 lines
2.9 KiB
Python

import pytest
from datetime import datetime, time, timedelta
from uuid import uuid4, UUID
from zoneinfo import ZoneInfo
from app.schemas.events import EventCreate, EventUpdate, EventResponse
def test_valid_event_create():
event_date = datetime.now(ZoneInfo('UTC')) + timedelta(days=1)
event_data = {
"title": "Emma's First Birthday",
"slug": "emmas-first-birthday",
"description": "Join us for Emma's first birthday celebration!",
"event_date": event_date,
"timezone": "America/New_York",
"location_name": "Central Park",
"is_public": False,
"access_code": "EMMA2024"
}
event = EventCreate(**event_data)
assert event.title == "Emma's First Birthday"
assert event.slug == "emmas-first-birthday"
assert event.timezone == "America/New_York"
def test_invalid_event_create():
event_date = datetime.now(ZoneInfo('UTC')) - timedelta(days=1)
with pytest.raises(ValueError):
EventCreate(
title="Past Event",
slug="past-event",
event_date=event_date,
timezone="America/New_York"
)
def test_invalid_timezone():
with pytest.raises(ValueError):
EventCreate(
title="Test Event",
slug="test-event",
event_date=datetime.now(ZoneInfo('UTC')) + timedelta(days=1),
timezone="Invalid/Timezone"
)
def test_event_update_partial():
update_data = {
"title": "Updated Title",
"description": "Updated description"
}
event_update = EventUpdate(**update_data)
assert event_update.title == "Updated Title"
assert event_update.description == "Updated description"
def test_event_response():
event_date = datetime.now(ZoneInfo('UTC')) + timedelta(days=1)
event_data = {
"id": uuid4(),
"title": "Test Event",
"slug": "test-event",
"event_date": event_date,
"timezone": "UTC",
"created_by": uuid4(),
"created_at": datetime.now(ZoneInfo('UTC')),
"updated_at": datetime.now(ZoneInfo('UTC'))
}
event_response = EventResponse(**event_data)
assert event_response.title == "Test Event"
assert isinstance(event_response.id, UUID)
def test_invalid_slug_format():
event_date = datetime.now(ZoneInfo('UTC')) + timedelta(days=1)
with pytest.raises(ValueError):
EventCreate(
title="Test Event",
slug="Invalid Slug!", # Invalid slug format
event_date=event_date,
timezone="UTC"
)
def test_rsvp_deadline_validation():
event_date = datetime.now(ZoneInfo('UTC')) + timedelta(days=10)
invalid_deadline = event_date + timedelta(days=1)
with pytest.raises(ValueError):
EventCreate(
title="Test Event",
slug="test-event",
event_date=event_date,
timezone="UTC",
rsvp_deadline=invalid_deadline
)