def if_applicable_release_or_deprecate(
    existing_transformation_revision: Optional[TransformationRevision],
    updated_transformation_revision: TransformationRevision,
) -> TransformationRevision:
    if existing_transformation_revision is not None:
        if (
            existing_transformation_revision.state == State.DRAFT
            and updated_transformation_revision.state == State.RELEASED
        ):
            logger.info(
                "release transformation revision %s",
                existing_transformation_revision.id,
            )
            updated_transformation_revision.release()
            # prevent overwriting content during releasing
            updated_transformation_revision.content = (
                existing_transformation_revision.content
            )
        if (
            existing_transformation_revision.state == State.RELEASED
            and updated_transformation_revision.state == State.DISABLED
        ):
            logger.info(
                "deprecate transformation revision %s",
                existing_transformation_revision.id,
            )
            updated_transformation_revision = TransformationRevision(
                **existing_transformation_revision.dict()
            )
            updated_transformation_revision.deprecate()
            # prevent overwriting content during deprecating
            updated_transformation_revision.content = (
                existing_transformation_revision.content
            )
    return updated_transformation_revision
Ejemplo n.º 2
0
async def test_get_all_transformation_revisions_with_specified_state(
        async_test_client, clean_test_db_engine):
    with mock.patch(
            "hetdesrun.persistence.dbservice.revision.Session",
            sessionmaker(clean_test_db_engine),
    ):
        store_single_transformation_revision(
            TransformationRevision(**tr_json_component_1)  # DRAFT
        )
        store_single_transformation_revision(
            TransformationRevision(**tr_json_component_2)  # RELEASED
        )
        store_single_transformation_revision(
            TransformationRevision(**tr_json_workflow_1)  # DRAFT
        )
        tr_workflow_2 = TransformationRevision(**tr_json_workflow_2)
        tr_workflow_2.deprecate()
        store_single_transformation_revision(tr_workflow_2)
        async with async_test_client as ac:
            response_draft = await ac.get("/api/transformations/?state=DRAFT")
            response_released = await ac.get(
                "/api/transformations/?state=RELEASED")
            response_disabled = await ac.get(
                "/api/transformations/?state=DISABLED")
            response_foo = await ac.get("/api/transformations/?state=foo")

        assert response_draft.status_code == 200
        assert len(response_draft.json()) == 2
        assert response_draft.json()[0] == tr_json_component_1
        assert response_draft.json()[1] == tr_json_workflow_1
        assert response_released.status_code == 200
        assert len(response_released.json()) == 1
        assert response_released.json()[0] == tr_json_component_2
        assert response_disabled.status_code == 200
        assert len(response_disabled.json()) == 1
        assert response_disabled.json()[0]["id"] == tr_json_workflow_2["id"]
        assert response_disabled.json()[0]["state"] == "DISABLED"
        assert response_foo.status_code == 422
        assert ("not a valid enumeration member"
                in response_foo.json()["detail"][0]["msg"])