Files
eventspace/backend/tests/test_notification_log.py
Felipe Cardoso c15c55d691 Add tests for EmailTemplate and NotificationLog models
Introduced comprehensive unit tests for EmailTemplate and NotificationLog, including creation, field validation, and custom methods. Added relevant test fixtures to support these models and expanded `conftest.py` for reusability. Validates functionality and maintains model integrity.
2025-02-28 15:16:23 +01:00

105 lines
3.9 KiB
Python

# tests/models/test_notification_log.py
import uuid
from datetime import datetime, timezone
from app.models.notification_log import NotificationLog, NotificationType, NotificationStatus
def test_create_notification_log(db_session, email_template_fixture, guest_fixture):
# Arrange
notification_log = NotificationLog(
id=uuid.uuid4(),
notification_type=NotificationType.SMS,
status=NotificationStatus.QUEUED,
subject="Event Reminder",
content_preview="Don't forget the event!",
template_id=email_template_fixture.id,
event_id=guest_fixture.event_id,
guest_id=guest_fixture.id,
recipient="+1234567890",
notification_metadata={"provider": "Twilio", "priority": "High"},
)
db_session.add(notification_log)
# Act
db_session.commit()
created_log = db_session.query(NotificationLog).filter_by(id=notification_log.id).first()
# Assert
assert created_log is not None
assert created_log.notification_type == NotificationType.SMS
assert created_log.status == NotificationStatus.QUEUED
assert created_log.subject == "Event Reminder"
assert created_log.content_preview == "Don't forget the event!"
assert created_log.template_id == email_template_fixture.id
assert created_log.event_id == guest_fixture.event_id
assert created_log.guest_id == guest_fixture.id
assert created_log.recipient == "+1234567890"
assert created_log.notification_metadata == {"provider": "Twilio", "priority": "High"}
def test_mark_sent(notification_log_fixture):
# Act
notification_log_fixture.mark_sent()
# Assert
assert notification_log_fixture.status == NotificationStatus.SENT
assert notification_log_fixture.sent_at is not None
assert notification_log_fixture.sent_at < datetime.now(timezone.utc)
def test_mark_delivered(notification_log_fixture):
# Act
notification_log_fixture.mark_delivered()
# Assert
assert notification_log_fixture.status == NotificationStatus.DELIVERED
assert notification_log_fixture.delivered_at is not None
assert notification_log_fixture.delivered_at < datetime.now(timezone.utc)
def test_mark_opened(notification_log_fixture):
# Act
notification_log_fixture.mark_opened()
# Assert
assert notification_log_fixture.status == NotificationStatus.OPENED
assert notification_log_fixture.opened_at is not None
assert notification_log_fixture.opened_at < datetime.now(timezone.utc)
def test_mark_failed(notification_log_fixture):
# Act
notification_log_fixture.mark_failed("Undeliverable address")
# Assert
assert notification_log_fixture.status == NotificationStatus.FAILED
assert notification_log_fixture.error_message == "Undeliverable address"
assert notification_log_fixture.retry_count == 1
def test_can_retry_property(notification_log_fixture):
# Arrange
notification_log_fixture.retry_count = 2
notification_log_fixture.status = NotificationStatus.FAILED # Ensure initial FAILED status
# Act: Call mark_failed and inspect retry_count/state
notification_log_fixture.mark_failed("Temporary issue")
# Assert: Still retryable since retry_count < 3
assert notification_log_fixture.retry_count == 3 # Ensure retry_count incremented
assert notification_log_fixture.status == NotificationStatus.FAILED # Ensure status is 'FAILED'
assert notification_log_fixture.can_retry is False # Now retries are exhausted
# Reset retry count and revalidate
notification_log_fixture.retry_count = 1
can_retry = notification_log_fixture.can_retry
assert can_retry is True # Should be retryable with retry_count = 1
def test_repr_method(notification_log_fixture):
# Act
repr_output = repr(notification_log_fixture)
# Assert
assert repr_output == f"<NotificationLog {notification_log_fixture.notification_type.value} to {notification_log_fixture.recipient} status={notification_log_fixture.status.value}>"