Files
eventspace/backend/tests/schemas/test_events.py
Felipe Cardoso 4a9a37f507
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 CRUD operations and tests for Event model
This commit introduces a new CRUDEvent class to manage event-related database operations, including retrieval, creation, updating, and deletion of events. It includes corresponding unit tests to ensure the correctness of these functionalities, updates event schemas for enhanced validation, and refines timezone handling for event dates and deadlines.
2025-03-05 14:58:15 +01:00

96 lines
2.9 KiB
Python

import pytest
from datetime import datetime, time, timedelta, timezone
from uuid import uuid4, UUID
from app.schemas.events import EventCreate, EventUpdate, EventResponse
def test_valid_event_create():
event_date = datetime.now(tz=timezone.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(tz=timezone.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(tz=timezone.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(tz=timezone.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(tz=timezone.utc),
"updated_at": datetime.now(tz=timezone.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(tz=timezone.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(tz=timezone.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
)