Add unit tests for RSVP model
This commit introduces comprehensive unit tests for the RSVP model, including creation, status updates, attendance checks, unique constraints, and dietary requirements handling. These tests ensure the RSVP model
This commit is contained in:
106
backend/tests/models/test_rsvp.py
Normal file
106
backend/tests/models/test_rsvp.py
Normal file
@@ -0,0 +1,106 @@
|
||||
# tests/models/test_rsvp.py
|
||||
import uuid
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from app.models.rsvp import RSVP, RSVPStatus
|
||||
|
||||
|
||||
def test_create_rsvp(db_session):
|
||||
# Arrange
|
||||
rsvp = RSVP(
|
||||
id=uuid.uuid4(),
|
||||
event_id=uuid.uuid4(),
|
||||
guest_id=uuid.uuid4(),
|
||||
status=RSVPStatus.NOT_ATTENDING,
|
||||
number_of_guests=1,
|
||||
response_message="Sorry, can't make it.",
|
||||
dietary_requirements="None",
|
||||
)
|
||||
db_session.add(rsvp)
|
||||
|
||||
# Act
|
||||
db_session.commit()
|
||||
created_rsvp = db_session.query(RSVP).filter_by(id=rsvp.id).first()
|
||||
|
||||
# Assert
|
||||
assert created_rsvp is not None
|
||||
assert created_rsvp.status == RSVPStatus.NOT_ATTENDING
|
||||
assert created_rsvp.number_of_guests == 1
|
||||
assert created_rsvp.dietary_requirements == "None"
|
||||
|
||||
|
||||
def test_update_rsvp_status(rsvp_fixture, db_session):
|
||||
# Act
|
||||
rsvp_fixture.update_status(new_status=RSVPStatus.MAYBE, guests_count=3)
|
||||
db_session.commit()
|
||||
|
||||
# Assert
|
||||
assert rsvp_fixture.status == RSVPStatus.MAYBE
|
||||
assert rsvp_fixture.number_of_guests == 3
|
||||
|
||||
# Check last_updated is approximately now
|
||||
assert rsvp_fixture.last_updated >= datetime.now(timezone.utc) - timedelta(seconds=1)
|
||||
|
||||
|
||||
def test_is_attending(rsvp_fixture):
|
||||
# Act
|
||||
is_attending = rsvp_fixture.is_attending
|
||||
|
||||
# Assert
|
||||
assert is_attending is True # By default RSVPStatus.ATTENDING is set in the fixture
|
||||
|
||||
# Update RSVP to NOT_ATTENDING and verify
|
||||
rsvp_fixture.status = RSVPStatus.NOT_ATTENDING
|
||||
assert rsvp_fixture.is_attending is False
|
||||
|
||||
|
||||
def test_unique_rsvp_constraint(db_session):
|
||||
# Arrange
|
||||
event_id = uuid.uuid4()
|
||||
guest_id = uuid.uuid4()
|
||||
|
||||
rsvp1 = RSVP(
|
||||
id=uuid.uuid4(),
|
||||
event_id=event_id,
|
||||
guest_id=guest_id,
|
||||
status=RSVPStatus.ATTENDING,
|
||||
number_of_guests=2,
|
||||
)
|
||||
db_session.add(rsvp1)
|
||||
db_session.commit()
|
||||
|
||||
# Attempt to create another RSVP for the same event and guest
|
||||
rsvp2 = RSVP(
|
||||
id=uuid.uuid4(),
|
||||
event_id=event_id,
|
||||
guest_id=guest_id,
|
||||
status=RSVPStatus.NOT_ATTENDING,
|
||||
number_of_guests=1,
|
||||
)
|
||||
db_session.add(rsvp2)
|
||||
|
||||
# Act and Assert
|
||||
try:
|
||||
db_session.commit()
|
||||
# If no exception is raised, the test should fail
|
||||
assert False, "Expected IntegrityError for duplicate RSVP"
|
||||
except Exception as e:
|
||||
# Check the exception to confirm it matches SQLite's UNIQUE constraint error
|
||||
error_message = str(e)
|
||||
if "sqlite3.IntegrityError" in error_message:
|
||||
assert "UNIQUE constraint failed: rsvps.event_id, rsvps.guest_id" in error_message
|
||||
else:
|
||||
# For other databases (e.g., PostgreSQL), the constraint name should appear
|
||||
assert "uq_event_guest_rsvp" in error_message
|
||||
|
||||
|
||||
|
||||
def test_dietary_requirements(rsvp_fixture):
|
||||
# Act
|
||||
dietary = rsvp_fixture.dietary_requirements
|
||||
|
||||
# Assert
|
||||
assert dietary == "Vegetarian"
|
||||
|
||||
# Update and re-check
|
||||
rsvp_fixture.dietary_requirements = "No preference"
|
||||
assert rsvp_fixture.dietary_requirements == "No preference"
|
||||
Reference in New Issue
Block a user