Skip to content

Commit

Permalink
Add CRUD methods for TemplateCategories
Browse files Browse the repository at this point in the history
- Added template_category_id to the template_history table
- Adjusted model class name TemplateCategories -> TemplateCategory
- WIP: Added some basic tests for the TemplateCategory dao
  • Loading branch information
whabanks committed Jun 13, 2024
1 parent e7346fd commit 70334b0
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 17 deletions.
29 changes: 29 additions & 0 deletions app/dao/template_categories_dao.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import uuid

from flask import current_app

Check notice

Code scanning / CodeQL

Unused import Note

Import of 'current_app' is not used.
from sqlalchemy import asc

from app import db
from app.dao.dao_utils import transactional

Check notice

Code scanning / CodeQL

Unused import Note

Import of 'transactional' is not used.
from app.models import TemplateCategory


def dao_create_template_category(template_category: TemplateCategory):
template_category.id = uuid.uuid4()
db.session.add(template_category)


def dao_update_template_category(template_category: TemplateCategory):
db.session.add(template_category)


def dao_get_template_category_by_id(template_category_id):
return TemplateCategory.query.filter_by(id=template_category_id).one()


def dao_get_template_category_by_template_id(template_id):
return TemplateCategory.query.join(TemplateCategory.templates).filter_by(id=template_id).one()


def dao_get_all_template_categories():
return TemplateCategory.query.order_by(asc(TemplateCategory.name_en)).all()
7 changes: 3 additions & 4 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
convert_local_timezone_to_utc,
convert_utc_to_local_timezone,
)
from pkg_resources import declare_namespace
from sqlalchemy import CheckConstraint, Index, UniqueConstraint
from sqlalchemy.dialects.postgresql import JSON, JSONB, UUID
from sqlalchemy.ext.associationproxy import association_proxy
Expand Down Expand Up @@ -1033,7 +1032,8 @@ def get_users_with_permission(self):

PRECOMPILED_TEMPLATE_NAME = "Pre-compiled PDF"

class TemplateCategories(BaseModel):

class TemplateCategory(BaseModel):
__tablename__ = "template_categories"

id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
Expand Down Expand Up @@ -1196,8 +1196,7 @@ class Template(TemplateBase):

service = db.relationship("Service", backref="templates")
version = db.Column(db.Integer, default=0, nullable=False)
template_categories = db.relationship("TemplateCategories", backref="templates")

template_category = db.relationship("TemplateCategory", backref="templates")

folder = db.relationship(
"TemplateFolder",
Expand Down
20 changes: 7 additions & 13 deletions migrations/versions/0454_add_template_category.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@ def upgrade():
sa.Column("email_process_type", sa.String(length=255), nullable=False),
sa.Column("hidden", sa.Boolean(), nullable=False),
sa.UniqueConstraint("name_en"),
sa.UniqueConstraint("name_fr")
sa.UniqueConstraint("name_fr"),
)

op.add_column(
"templates",
sa.Column("template_category_id", postgresql.UUID(as_uuid=True), nullable=True)
)
op.add_column("templates", sa.Column("template_category_id", postgresql.UUID(as_uuid=True), nullable=True))
op.add_column("templates_history", sa.Column("template_category_id", postgresql.UUID(as_uuid=True), nullable=True))
op.create_index(
op.f("ix_template_category_id"),
"templates",
Expand All @@ -52,13 +50,7 @@ def upgrade():
["name_fr"],
unique=False,
)
op.create_foreign_key(
"fk_template_template_categories",
"templates",
"template_categories",
["template_category_id"],
["id"]
)
op.create_foreign_key("fk_template_template_categories", "templates", "template_categories", ["template_category_id"], ["id"])

# Insert the generic Low priority (bulk) category
# op.execute("""
Expand All @@ -67,10 +59,12 @@ def upgrade():
# """
# )


def downgrade():
op.drop_constraint("fk_template_template_categories", "templates", type_="foreignkey")
op.drop_index(op.f("ix_template_category_id"), table_name="templates")
op.drop_index(op.f("ix_template_categories_name_en"), table_name="template_categories")
op.drop_index(op.f("ix_template_categories_name_fr"), table_name="template_categories")
op.drop_column("templates", "template_category_id")
op.drop_table("template_categories")
op.drop_column("templates_history", "template_category_id")
op.drop_table("template_categories")
153 changes: 153 additions & 0 deletions tests/app/dao/test_template_categories_dao.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import pytest

Check notice

Code scanning / CodeQL

Unused import Note test

Import of 'pytest' is not used.

from app.dao.template_categories_dao import (
dao_create_template_category,
dao_get_all_template_categories,
dao_get_template_category_by_id,
dao_get_template_category_by_template_id,
dao_update_template_category,
)
from app.dao.templates_dao import dao_create_template
from app.models import BULK, NORMAL, PRIORITY, Template, TemplateCategory

Check notice

Code scanning / CodeQL

Unused import Note test

Import of 'PRIORITY' is not used.


def test_create_template_category(notify_db_session):
data = {
"name_en": "english",
"name_fr": "french",
"description_en": "english description",
"description_fr": "french description",
"sms_process_type": NORMAL,
"email_process_type": NORMAL,
"hidden": False,
}

template_category = TemplateCategory(**data)
dao_create_template_category(template_category)

assert TemplateCategory.query.count() == 1
assert len(dao_get_all_template_categories()) == 1


def test_update_template_category(notify_db_session):
data = {
"name_en": "english",
"name_fr": "french",
"description_en": "english description",
"description_fr": "french description",
"sms_process_type": NORMAL,
"email_process_type": NORMAL,
"hidden": False,
}

template_category = TemplateCategory(**data)
dao_create_template_category(template_category)

template_category.name_en = "new english"
template_category.name_fr = "new french"
template_category.description_en = "new english description"
template_category.description_fr = "new french description"
template_category.sms_process_type = BULK
template_category.email_process_type = BULK
template_category.hidden = True
dao_update_template_category(template_category)

assert TemplateCategory.query.count() == 1
assert len(dao_get_all_template_categories()) == 1
assert dao_get_all_template_categories()[0].name_en == "new english"
assert dao_get_all_template_categories()[0].name_fr == "new french"
assert dao_get_all_template_categories()[0].description_en == "new english description"
assert dao_get_all_template_categories()[0].description_fr == "new french description"
assert dao_get_all_template_categories()[0].sms_process_type == BULK
assert dao_get_all_template_categories()[0].email_process_type == BULK
assert dao_get_all_template_categories()[0].hidden == True
assert dao_get_all_template_categories()[0].id == template_category.id


def test_get_template_category_by_template_id(notify_db_session, sample_service, sample_user):
category_data = {
"name_en": "english",
"name_fr": "french",
"description_en": "english description",
"description_fr": "french description",
"sms_process_type": NORMAL,
"email_process_type": NORMAL,
"hidden": False,
}

template_category = TemplateCategory(**category_data)
dao_create_template_category(template_category)

template_data = {
"name": "Sample Template",
"template_type": "email",
"content": "Template content",
"service": sample_service,
"created_by": sample_user,
}

template = Template(**template_data)
template.template_category = template_category
dao_create_template(template)

assert dao_get_template_category_by_template_id(template.id) == template_category


def test_get_template_category_by_id(notify_db_session):
data = {
"name_en": "english",
"name_fr": "french",
"description_en": "english description",
"description_fr": "french description",
"sms_process_type": NORMAL,
"email_process_type": NORMAL,
"hidden": False,
}

template_category = TemplateCategory(**data)
dao_create_template_category(template_category)

assert dao_get_template_category_by_id(template_category.id) == template_category


def test_get_all_template_categories(notify_db_session):
data1 = {
"name_en": "english",
"name_fr": "french",
"description_en": "english description",
"description_fr": "french description",
"sms_process_type": NORMAL,
"email_process_type": NORMAL,
"hidden": False,
}

data2 = {
"name_en": "english2",
"name_fr": "french2",
"description_en": "english description2",
"description_fr": "french description2",
"sms_process_type": BULK,
"email_process_type": BULK,
"hidden": True,
}

template_category1 = TemplateCategory(**data1)
template_category2 = TemplateCategory(**data2)
dao_create_template_category(template_category1)
dao_create_template_category(template_category2)

assert len(dao_get_all_template_categories()) == 2
assert dao_get_all_template_categories()[0].name_en == "english"
assert dao_get_all_template_categories()[0].name_fr == "french"
assert dao_get_all_template_categories()[0].description_en == "english description"
assert dao_get_all_template_categories()[0].description_fr == "french description"
assert dao_get_all_template_categories()[0].sms_process_type == NORMAL
assert dao_get_all_template_categories()[0].email_process_type == NORMAL
assert dao_get_all_template_categories()[0].hidden == False
assert dao_get_all_template_categories()[1].name_en == "english2"
assert dao_get_all_template_categories()[1].name_fr == "french2"
assert dao_get_all_template_categories()[1].description_en == "english description2"
assert dao_get_all_template_categories()[1].description_fr == "french description2"
assert dao_get_all_template_categories()[1].sms_process_type == BULK
assert dao_get_all_template_categories()[1].email_process_type == BULK
assert dao_get_all_template_categories()[1].hidden == True

0 comments on commit 70334b0

Please sign in to comment.