Add validation for display_order and EventMedia tests
Introduce a `validate_display_order` method to ensure a default value of 0 for `display_order`. Extend testing infrastructure with fixtures and unit tests for `EventMedia` to validate properties, metadata, and default behaviors.
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
from enum import Enum
|
from enum import Enum
|
||||||
from sqlalchemy import Column, String, Integer, Boolean, ForeignKey, JSON, Enum as SQLEnum
|
from sqlalchemy import Column, String, Integer, Boolean, ForeignKey, JSON, Enum as SQLEnum
|
||||||
from sqlalchemy.dialects.postgresql import UUID
|
from sqlalchemy.dialects.postgresql import UUID
|
||||||
from sqlalchemy.orm import relationship
|
from sqlalchemy.orm import relationship, validates
|
||||||
from .base import Base, TimestampMixin, UUIDMixin
|
from .base import Base, TimestampMixin, UUIDMixin
|
||||||
|
|
||||||
|
|
||||||
@@ -48,6 +48,11 @@ class EventMedia(Base, UUIDMixin, TimestampMixin):
|
|||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f"<EventMedia {self.original_filename} ({self.media_type}) for event={self.event_id}>"
|
return f"<EventMedia {self.original_filename} ({self.media_type}) for event={self.event_id}>"
|
||||||
|
|
||||||
|
@validates('display_order')
|
||||||
|
def validate_display_order(self, key, value):
|
||||||
|
return value if value is not None else 0
|
||||||
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_image(self):
|
def is_image(self):
|
||||||
return self.media_type == MediaType.IMAGE
|
return self.media_type == MediaType.IMAGE
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import pytest
|
|||||||
import uuid
|
import uuid
|
||||||
from app.models.user import User
|
from app.models.user import User
|
||||||
from app.utils.test_utils import setup_test_db, teardown_test_db
|
from app.utils.test_utils import setup_test_db, teardown_test_db
|
||||||
|
from app.models import GiftItem, GiftStatus, GiftPriority, RSVP, RSVPStatus, EventMedia, MediaType, MediaPurpose
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="function")
|
@pytest.fixture(scope="function")
|
||||||
@@ -39,3 +40,74 @@ def mock_user(db_session):
|
|||||||
db_session.add(mock_user)
|
db_session.add(mock_user)
|
||||||
db_session.commit()
|
db_session.commit()
|
||||||
return mock_user
|
return mock_user
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def gift_item_fixture(db_session, mock_user):
|
||||||
|
"""
|
||||||
|
Fixture to create and return a default GiftItem instance.
|
||||||
|
The event_id, added_by, and other necessary attributes are predefined.
|
||||||
|
"""
|
||||||
|
gift_item = GiftItem(
|
||||||
|
id=uuid.uuid4(),
|
||||||
|
event_id=uuid.uuid4(),
|
||||||
|
added_by=mock_user.id,
|
||||||
|
name="Default Gift",
|
||||||
|
description="Default gift description.",
|
||||||
|
price=100.00,
|
||||||
|
currency="USD",
|
||||||
|
quantity_requested=5,
|
||||||
|
quantity_received=0,
|
||||||
|
status=GiftStatus.AVAILABLE,
|
||||||
|
priority=GiftPriority.MEDIUM,
|
||||||
|
is_visible=True
|
||||||
|
)
|
||||||
|
db_session.add(gift_item)
|
||||||
|
db_session.commit()
|
||||||
|
return gift_item
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def rsvp_fixture(db_session, mock_user):
|
||||||
|
"""
|
||||||
|
Fixture to create and return a default RSVP instance.
|
||||||
|
The event_id and guest_id fields are predefined for testing.
|
||||||
|
"""
|
||||||
|
rsvp = RSVP(
|
||||||
|
id=uuid.uuid4(),
|
||||||
|
event_id=uuid.uuid4(),
|
||||||
|
guest_id=uuid.uuid4(),
|
||||||
|
status=RSVPStatus.ATTENDING,
|
||||||
|
number_of_guests=2,
|
||||||
|
response_message="Looking forward to the event!",
|
||||||
|
dietary_requirements="Vegetarian",
|
||||||
|
)
|
||||||
|
db_session.add(rsvp)
|
||||||
|
db_session.commit()
|
||||||
|
return rsvp
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def event_media_fixture(db_session, mock_user):
|
||||||
|
"""
|
||||||
|
Fixture to create and return a default EventMedia instance.
|
||||||
|
"""
|
||||||
|
event_media = EventMedia(
|
||||||
|
id=uuid.uuid4(),
|
||||||
|
event_id=uuid.uuid4(),
|
||||||
|
uploaded_by=mock_user.id,
|
||||||
|
file_path="uploads/sample.jpg",
|
||||||
|
original_filename="sample.jpg",
|
||||||
|
media_type=MediaType.IMAGE,
|
||||||
|
content_type="image/jpeg",
|
||||||
|
file_size=2048,
|
||||||
|
purpose=MediaPurpose.GALLERY,
|
||||||
|
is_public=True,
|
||||||
|
display_order=1,
|
||||||
|
title="Sample Image",
|
||||||
|
description="A sample image for testing.",
|
||||||
|
media_metadata={"resolution": "1920x1080"},
|
||||||
|
)
|
||||||
|
db_session.add(event_media)
|
||||||
|
db_session.commit()
|
||||||
|
return event_media
|
||||||
|
|||||||
104
backend/tests/models/test_event_media.py
Normal file
104
backend/tests/models/test_event_media.py
Normal file
@@ -0,0 +1,104 @@
|
|||||||
|
# tests/models/test_event_media.py
|
||||||
|
import uuid
|
||||||
|
from app.models.event_media import EventMedia, MediaType, MediaPurpose
|
||||||
|
|
||||||
|
|
||||||
|
def test_create_event_media(db_session, mock_user):
|
||||||
|
# Arrange
|
||||||
|
event_media = EventMedia(
|
||||||
|
id=uuid.uuid4(),
|
||||||
|
event_id=uuid.uuid4(),
|
||||||
|
uploaded_by=mock_user.id,
|
||||||
|
file_path="uploads/test_image.jpg",
|
||||||
|
original_filename="test_image.jpg",
|
||||||
|
media_type=MediaType.IMAGE,
|
||||||
|
content_type="image/jpeg",
|
||||||
|
file_size=1024,
|
||||||
|
purpose=MediaPurpose.BANNER,
|
||||||
|
is_public=False,
|
||||||
|
display_order=0,
|
||||||
|
title="Test Image",
|
||||||
|
description="This is a test image.",
|
||||||
|
media_metadata={"resolution": "1024x768"},
|
||||||
|
)
|
||||||
|
db_session.add(event_media)
|
||||||
|
|
||||||
|
# Act
|
||||||
|
db_session.commit()
|
||||||
|
created_media = db_session.query(EventMedia).filter_by(id=event_media.id).first()
|
||||||
|
|
||||||
|
# Assert
|
||||||
|
assert created_media is not None
|
||||||
|
assert created_media.file_path == "uploads/test_image.jpg"
|
||||||
|
assert created_media.original_filename == "test_image.jpg"
|
||||||
|
assert created_media.media_type == MediaType.IMAGE
|
||||||
|
assert created_media.file_size == 1024
|
||||||
|
assert created_media.purpose == MediaPurpose.BANNER
|
||||||
|
assert created_media.is_public is False
|
||||||
|
assert created_media.media_metadata == {"resolution": "1024x768"}
|
||||||
|
|
||||||
|
|
||||||
|
def test_is_image_property(event_media_fixture):
|
||||||
|
# Act
|
||||||
|
is_image = event_media_fixture.is_image
|
||||||
|
|
||||||
|
# Assert
|
||||||
|
assert is_image is True # Fixture sets media_type to MediaType.IMAGE
|
||||||
|
|
||||||
|
# Update media type to VIDEO and re-check
|
||||||
|
event_media_fixture.media_type = MediaType.VIDEO
|
||||||
|
assert event_media_fixture.is_image is False
|
||||||
|
|
||||||
|
|
||||||
|
def test_is_video_property(event_media_fixture):
|
||||||
|
# Act
|
||||||
|
is_video = event_media_fixture.is_video
|
||||||
|
|
||||||
|
# Assert
|
||||||
|
assert is_video is False # Fixture sets media_type to MediaType.IMAGE
|
||||||
|
|
||||||
|
# Update media type to VIDEO and re-check
|
||||||
|
event_media_fixture.media_type = MediaType.VIDEO
|
||||||
|
assert event_media_fixture.is_video is True
|
||||||
|
|
||||||
|
|
||||||
|
def test_is_document_property(event_media_fixture):
|
||||||
|
# Act
|
||||||
|
is_document = event_media_fixture.is_document
|
||||||
|
|
||||||
|
# Assert
|
||||||
|
assert is_document is False # Fixture sets media_type to MediaType.IMAGE
|
||||||
|
|
||||||
|
# Update media type to DOCUMENT and re-check
|
||||||
|
event_media_fixture.media_type = MediaType.DOCUMENT
|
||||||
|
assert event_media_fixture.is_document is True
|
||||||
|
|
||||||
|
|
||||||
|
def test_url_property(event_media_fixture):
|
||||||
|
# Act
|
||||||
|
url = event_media_fixture.url
|
||||||
|
|
||||||
|
# Assert
|
||||||
|
# Since the `url` method is not implemented, it should currently return None
|
||||||
|
assert url is None # Implement this logic if further changes are made
|
||||||
|
|
||||||
|
|
||||||
|
def test_default_display_order(event_media_fixture):
|
||||||
|
# Arrange
|
||||||
|
event_media_fixture.display_order = None
|
||||||
|
|
||||||
|
# Assert default value
|
||||||
|
# Ensure display_order defaults to 0
|
||||||
|
assert event_media_fixture.display_order == 0
|
||||||
|
|
||||||
|
|
||||||
|
def test_media_metadata(event_media_fixture):
|
||||||
|
# Act
|
||||||
|
metadata = event_media_fixture.media_metadata
|
||||||
|
|
||||||
|
# Assert
|
||||||
|
assert metadata == {"resolution": "1920x1080"}
|
||||||
|
|
||||||
|
# Update and check
|
||||||
|
event_media_fixture.media_metadata = {"resolution": "1280x720"}
|
||||||
|
assert event_media_fixture.media_metadata == {"resolution": "1280x720"}
|
||||||
Reference in New Issue
Block a user