From e112f708aee8b774ea8d04ff19841a2f789dea98 Mon Sep 17 00:00:00 2001 From: Omar Selo Date: Mon, 4 Dec 2023 13:51:07 +0300 Subject: [PATCH] Return artefact status from api (#65) * Return artefact status from api * Replace null artefact status with 'UNDECIDED' * Bug fixes --- ...d4333_make_artefact_status_not_nullable.py | 43 +++++++++++++++++++ .../controllers/artefacts/models.py | 1 + backend/test_observer/data_access/models.py | 2 +- .../test_observer/data_access/models_enums.py | 1 + .../controllers/artefacts/test_artefacts.py | 11 ++++- backend/tests/helpers.py | 1 + 6 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 backend/migrations/versions/2023_12_04_0820-8317277d4333_make_artefact_status_not_nullable.py diff --git a/backend/migrations/versions/2023_12_04_0820-8317277d4333_make_artefact_status_not_nullable.py b/backend/migrations/versions/2023_12_04_0820-8317277d4333_make_artefact_status_not_nullable.py new file mode 100644 index 00000000..deaa8440 --- /dev/null +++ b/backend/migrations/versions/2023_12_04_0820-8317277d4333_make_artefact_status_not_nullable.py @@ -0,0 +1,43 @@ +"""Make artefact status not nullable + +Revision ID: 8317277d4333 +Revises: 8d8643821588 +Create Date: 2023-12-04 08:20:23.131927+00:00 + +""" +from alembic import op + +# revision identifiers, used by Alembic. +revision = "8317277d4333" +down_revision = "8d8643821588" +branch_labels = None +depends_on = None + + +def upgrade() -> None: + op.execute("ALTER TYPE artefact_status_enum RENAME TO artefact_status_enum_old") + op.execute( + "CREATE TYPE artefact_status_enum AS " + "ENUM('APPROVED', 'MARKED_AS_FAILED', 'UNDECIDED')" + ) + op.execute( + "ALTER TABLE artefact ALTER COLUMN status TYPE " + "artefact_status_enum USING status::text::artefact_status_enum" + ) + op.execute("DROP TYPE artefact_status_enum_old") + op.execute("UPDATE artefact SET status = 'UNDECIDED' WHERE status IS NULL") + op.execute("ALTER TABLE artefact ALTER COLUMN status SET NOT NULL") + + +def downgrade() -> None: + op.execute("ALTER TYPE artefact_status_enum RENAME TO artefact_status_enum_old") + op.execute( + "CREATE TYPE artefact_status_enum AS ENUM('APPROVED', 'MARKED_AS_FAILED')" + ) + op.execute("ALTER TABLE artefact ALTER COLUMN status DROP NOT NULL") + op.execute("UPDATE artefact SET status = NULL WHERE status = 'UNDECIDED'") + op.execute( + "ALTER TABLE artefact ALTER COLUMN status TYPE " + "artefact_status_enum USING status::text::artefact_status_enum" + ) + op.execute("DROP TYPE artefact_status_enum_old") diff --git a/backend/test_observer/controllers/artefacts/models.py b/backend/test_observer/controllers/artefacts/models.py index ee334aec..72707641 100644 --- a/backend/test_observer/controllers/artefacts/models.py +++ b/backend/test_observer/controllers/artefacts/models.py @@ -33,6 +33,7 @@ class ArtefactDTO(BaseModel): series: str | None repo: str | None stage: str = Field(validation_alias=AliasPath("stage", "name")) + status: ArtefactStatus class EnvironmentDTO(BaseModel): diff --git a/backend/test_observer/data_access/models.py b/backend/test_observer/data_access/models.py index 5d515359..be165388 100644 --- a/backend/test_observer/data_access/models.py +++ b/backend/test_observer/data_access/models.py @@ -106,7 +106,7 @@ class Artefact(Base): ) # Default fields due_date: Mapped[date | None] - status: Mapped[ArtefactStatus | None] + status: Mapped[ArtefactStatus] = mapped_column(default=ArtefactStatus.UNDECIDED) __table_args__ = ( UniqueConstraint("name", "version", "track", name="unique_snap"), diff --git a/backend/test_observer/data_access/models_enums.py b/backend/test_observer/data_access/models_enums.py index 76848d8a..0e32c3c9 100644 --- a/backend/test_observer/data_access/models_enums.py +++ b/backend/test_observer/data_access/models_enums.py @@ -38,3 +38,4 @@ class TestExecutionStatus(Enum): class ArtefactStatus(str, Enum): APPROVED = "APPROVED" MARKED_AS_FAILED = "MARKED_AS_FAILED" + UNDECIDED = "UNDECIDED" diff --git a/backend/tests/controllers/artefacts/test_artefacts.py b/backend/tests/controllers/artefacts/test_artefacts.py index 1c4648af..c51ec513 100644 --- a/backend/tests/controllers/artefacts/test_artefacts.py +++ b/backend/tests/controllers/artefacts/test_artefacts.py @@ -29,7 +29,12 @@ def test_get_latest_artefacts_by_family(db_session: Session, test_client: TestClient): """Should only get latest artefacts and only ones that belong to given family""" - relevant_artefact = create_artefact(db_session, "edge", version="2") + relevant_artefact = create_artefact( + db_session, + "edge", + version="2", + status=ArtefactStatus.MARKED_AS_FAILED, + ) old_timestamp = relevant_artefact.created_at - timedelta(days=1) create_artefact(db_session, "edge", created_at=old_timestamp, version="1") @@ -48,13 +53,14 @@ def test_get_latest_artefacts_by_family(db_session: Session, test_client: TestCl "series": relevant_artefact.series, "repo": relevant_artefact.repo, "stage": relevant_artefact.stage.name, + "status": relevant_artefact.status, } ] def test_get_artefact(db_session: Session, test_client: TestClient): """Should be able to fetch an existing artefact""" - artefact = create_artefact(db_session, "edge") + artefact = create_artefact(db_session, "edge", status=ArtefactStatus.APPROVED) response = test_client.get(f"/v1/artefacts/{artefact.id}") @@ -68,6 +74,7 @@ def test_get_artefact(db_session: Session, test_client: TestClient): "series": artefact.series, "repo": artefact.repo, "stage": artefact.stage.name, + "status": artefact.status, } diff --git a/backend/tests/helpers.py b/backend/tests/helpers.py index 51cd21a5..5e5188e2 100644 --- a/backend/tests/helpers.py +++ b/backend/tests/helpers.py @@ -20,6 +20,7 @@ def create_artefact(db_session: Session, stage_name: str, **kwargs) -> Artefact: series=kwargs.get("series", "jammy" if family == FamilyName.DEB else None), repo=kwargs.get("repo", "main" if family == FamilyName.DEB else None), created_at=kwargs.get("created_at", datetime.utcnow()), + status=kwargs.get("status"), ) db_session.add(artefact) db_session.commit()