generated from ministryofjustice/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
119 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
"""case_tracker | ||
Revision ID: 117a6ec2770b | ||
Revises: da58faadfe41 | ||
Create Date: 2024-10-16 08:59:53.139922 | ||
""" | ||
|
||
from typing import Sequence, Union | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
import sqlmodel | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = "117a6ec2770b" | ||
down_revision: Union[str, None] = "da58faadfe41" | ||
branch_labels: Union[str, Sequence[str], None] = None | ||
depends_on: Union[str, Sequence[str], None] = None | ||
|
||
|
||
def upgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table( | ||
"case_tracker", | ||
sa.Column("created_at", sa.DateTime(), nullable=False), | ||
sa.Column("updated_at", sa.DateTime(), nullable=False), | ||
sa.Column("id", sa.Uuid(), nullable=False), | ||
sa.Column("case_id", sa.Uuid(), nullable=False), | ||
sa.Column("gtm_anon_id", sqlmodel.sql.sqltypes.AutoString(), nullable=False), | ||
sa.Column("journey", sa.JSON(), nullable=False), | ||
sa.ForeignKeyConstraint( | ||
["case_id"], | ||
["cases.id"], | ||
), | ||
sa.PrimaryKeyConstraint("id"), | ||
) | ||
op.create_index( | ||
op.f("ix_case_tracker_case_id"), "case_tracker", ["case_id"], unique=False | ||
) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_index(op.f("ix_case_tracker_case_id"), table_name="case_tracker") | ||
op.drop_table("case_tracker") | ||
# ### end Alembic commands ### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from uuid import UUID | ||
from sqlmodel import SQLModel, Field, JSON, Relationship | ||
from app.models.base import TableModelMixin | ||
|
||
|
||
class CaseTrackerBase(SQLModel): | ||
case_id: UUID = Field(foreign_key="cases.id", index=True) | ||
gtm_anon_id: str = Field() | ||
journey: dict = Field(sa_type=JSON) | ||
|
||
|
||
class CaseTracker(CaseTrackerBase, TableModelMixin, table=True): | ||
__tablename__ = "case_tracker" | ||
# This allows for linking the case tracker back to the case, this allows us to address case tracker | ||
# directly by using the `Case.case_tracker` syntax, rather than searching for each eligibility outcome | ||
# using its ID. | ||
case: "Case" = Relationship(back_populates="case_tracker") # noqa: F821 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from app.models.case_tracker import CaseTracker | ||
from app.models.cases import Case, CaseTypes | ||
import uuid | ||
from sqlmodel import Session, select | ||
|
||
|
||
def create_case_tracker(case_id=None): | ||
return CaseTracker( | ||
case_id=case_id or uuid.uuid4(), | ||
gtm_anon_id=str(uuid.uuid4()), | ||
journey={ | ||
"source": "access", | ||
"scope": "access", | ||
"means": "chs", | ||
"last_app": "ccq", | ||
}, | ||
) | ||
|
||
|
||
def test_create_case_tracker(session: Session): | ||
case_tracker = create_case_tracker() | ||
session.add(case_tracker) | ||
session.commit() | ||
assert session.exec(select(CaseTracker)).all() == [case_tracker] | ||
|
||
|
||
def test_case_tracker_journey_dict(session: Session): | ||
case_tracker = create_case_tracker() | ||
session.add(case_tracker) | ||
session.commit() | ||
expected_journey = case_tracker.journey | ||
|
||
case_tracker = session.exec(select(CaseTracker)).first() | ||
assert type(case_tracker.journey) is dict | ||
assert case_tracker.journey == expected_journey | ||
|
||
|
||
def test_cascade_delete(session: Session): | ||
"""If a case is deleted the case_tracker attached to said case should also be deleted.""" | ||
case = Case(case_type=CaseTypes.CLA) | ||
case_tracker = create_case_tracker(case_id=case.id) | ||
session.add(case) | ||
session.add(case_tracker) | ||
session.commit() | ||
assert session.exec(select(CaseTracker)).all() == [case_tracker] | ||
|
||
session.delete(case) | ||
session.commit() | ||
assert session.exec(select(CaseTracker)).all() == [] |