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.
This commit is contained in:
120
backend/tests/models/test_email_template.py
Normal file
120
backend/tests/models/test_email_template.py
Normal file
@@ -0,0 +1,120 @@
|
||||
# tests/models/test_email_template.py
|
||||
import uuid
|
||||
|
||||
import sqlalchemy
|
||||
|
||||
from app.models.email_template import EmailTemplate, TemplateType
|
||||
|
||||
|
||||
def test_create_email_template(db_session, mock_user):
|
||||
# Arrange
|
||||
email_template = EmailTemplate(
|
||||
id=uuid.uuid4(),
|
||||
name="Reminder Template",
|
||||
description="A template for sending reminders.",
|
||||
template_type=TemplateType.REMINDER,
|
||||
subject="Reminder: {{event_name}} is happening soon!",
|
||||
html_content="<h1>Reminder: {{event_name}}</h1><p>Don't miss it!</p>",
|
||||
text_content="Reminder: {{event_name}}. Don't miss it!",
|
||||
variables={"event_name": "Birthday Party"},
|
||||
event_id=None, # Global template
|
||||
created_by=mock_user.id,
|
||||
is_active=True,
|
||||
is_system=False,
|
||||
)
|
||||
db_session.add(email_template)
|
||||
|
||||
# Act
|
||||
db_session.commit()
|
||||
created_template = db_session.query(EmailTemplate).filter_by(id=email_template.id).first()
|
||||
|
||||
# Assert
|
||||
assert created_template is not None
|
||||
assert created_template.name == "Reminder Template"
|
||||
assert created_template.description == "A template for sending reminders."
|
||||
assert created_template.template_type == TemplateType.REMINDER
|
||||
assert created_template.subject == "Reminder: {{event_name}} is happening soon!"
|
||||
assert created_template.html_content == "<h1>Reminder: {{event_name}}</h1><p>Don't miss it!</p>"
|
||||
assert created_template.text_content == "Reminder: {{event_name}}. Don't miss it!"
|
||||
assert created_template.variables == {"event_name": "Birthday Party"}
|
||||
assert created_template.created_by == mock_user.id
|
||||
assert created_template.is_active is True
|
||||
assert created_template.is_system is False
|
||||
|
||||
|
||||
def test_render_email_template(email_template_fixture):
|
||||
# Arrange
|
||||
context = {"event_name": "Emma's 1st Birthday"}
|
||||
|
||||
# Act
|
||||
rendered_subject, rendered_html, rendered_text = email_template_fixture.render(context)
|
||||
|
||||
# Assert
|
||||
assert rendered_subject == "You're invited to Emma's 1st Birthday!"
|
||||
assert rendered_html == "<h1>Welcome to Emma's 1st Birthday</h1><p>We look forward to seeing you!</p>"
|
||||
assert rendered_text == "Welcome to Emma's 1st Birthday. We look forward to seeing you!"
|
||||
|
||||
|
||||
def test_unique_event_template_name_constraint(db_session, email_template_fixture, mock_user):
|
||||
# Arrange: Ensure both templates share the same event_id
|
||||
event_id = uuid.uuid4()
|
||||
email_template_fixture.event_id = event_id # Set the event_id for the existing fixture
|
||||
db_session.commit()
|
||||
|
||||
# Attempt to create another template with the same name for the same event
|
||||
duplicate_template = EmailTemplate(
|
||||
id=uuid.uuid4(),
|
||||
name=email_template_fixture.name,
|
||||
description="Another template with the same name.",
|
||||
template_type=TemplateType.INVITATION,
|
||||
subject="Duplicate Subject",
|
||||
html_content="<p>Duplicate Content</p>",
|
||||
text_content="Duplicate Content",
|
||||
variables={},
|
||||
event_id=event_id, # Use the same event_id
|
||||
created_by=mock_user.id,
|
||||
is_active=True,
|
||||
is_system=False,
|
||||
)
|
||||
db_session.add(duplicate_template)
|
||||
|
||||
# Act & Assert
|
||||
try:
|
||||
db_session.commit()
|
||||
assert False, "Expected IntegrityError due to unique constraint on event-specific template names."
|
||||
except sqlalchemy.exc.IntegrityError as e:
|
||||
# Validate that the constraint error relates to `uq_event_template_name`
|
||||
assert "uq_event_template_name" in str(e.orig) or "email_templates.event_id, email_templates.name" in str(
|
||||
e.orig)
|
||||
|
||||
|
||||
|
||||
|
||||
def test_template_is_active_field(email_template_fixture):
|
||||
# Assert
|
||||
assert email_template_fixture.is_active is True
|
||||
|
||||
# Deactivate the template
|
||||
email_template_fixture.is_active = False
|
||||
|
||||
# Assert
|
||||
assert email_template_fixture.is_active is False
|
||||
|
||||
|
||||
def test_template_is_system_field(email_template_fixture):
|
||||
# Assert
|
||||
assert email_template_fixture.is_system is False
|
||||
|
||||
# Promote to system template
|
||||
email_template_fixture.is_system = True
|
||||
|
||||
# Assert
|
||||
assert email_template_fixture.is_system is True
|
||||
|
||||
|
||||
def test_repr_method(email_template_fixture):
|
||||
# Act
|
||||
repr_output = repr(email_template_fixture)
|
||||
|
||||
# Assert
|
||||
assert repr_output == f"<EmailTemplate {email_template_fixture.name} ({email_template_fixture.template_type.value})>"
|
||||
Reference in New Issue
Block a user