Example #1
0
async def test_execute_latest_for_transformation_revision_no_revision_in_db(
        async_test_client, clean_test_db_engine):
    patched_session = sessionmaker(clean_test_db_engine)
    with mock.patch(
            "hetdesrun.persistence.dbservice.nesting.Session",
            patched_session,
    ):
        with mock.patch(
                "hetdesrun.persistence.dbservice.revision.Session",
                patched_session,
        ):
            tr_component_1 = TransformationRevision(**tr_json_component_1)
            tr_component_1.content = update_code(tr_component_1)

            exec_latest_by_group_id_input = ExecLatestByGroupIdInput(
                revision_group_id=tr_component_1.revision_group_id,
                wiring=tr_component_1.test_wiring,
            )

            async with async_test_client as ac:
                response = await ac.post(
                    "/api/transformations/execute-latest",
                    json=json.loads(exec_latest_by_group_id_input.json()),
                )

            assert response.status_code == 404
            assert (
                "no released transformation revisions with revision group id"
                in response.json()["detail"])
Example #2
0
async def test_get_all_transformation_revisions_with_specified_type_and_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
        )
        store_single_transformation_revision(
            TransformationRevision(**tr_json_workflow_2)  # DRAFT
        )

        async with async_test_client as ac:
            response_released_component = await ac.get(
                "/api/transformations/?type=COMPONENT&state=RELEASED")
            response_draft_workflow = await ac.get(
                "/api/transformations/?type=WORKFLOW&state=DRAFT")

        assert response_released_component.status_code == 200
        assert len(response_released_component.json()) == 1
        assert response_released_component.json()[0] == tr_json_component_2
        assert response_draft_workflow.status_code == 200
        assert len(response_draft_workflow.json()) == 2
        assert response_draft_workflow.json()[0] == tr_json_workflow_1
        assert response_draft_workflow.json()[1] == tr_json_workflow_2
def test_multiple_select(clean_test_db_engine):
    with mock.patch(
        "hetdesrun.persistence.dbservice.revision.Session",
        sessionmaker(clean_test_db_engine),
    ):
        tr_template_id = get_uuid_from_seed("object_template")
        tr_object_template = TransformationRevision(
            id=tr_template_id,
            revision_group_id=tr_template_id,
            name="Test",
            description="Test description",
            version_tag="1.0.0",
            category="Test category",
            state=State.DRAFT,
            type=Type.COMPONENT,
            content="code",
            io_interface=IOInterface(),
            test_wiring=WorkflowWiring(),
            documentation="",
        )

        tr_uuid_1 = get_uuid_from_seed("test_multiple_select_1")
        tr_object_1 = tr_object_template.copy()
        tr_object_1.id = tr_uuid_1
        tr_object_1.revision_group_id = tr_uuid_1
        store_single_transformation_revision(tr_object_1)

        tr_uuid_2 = get_uuid_from_seed("test_multiple_select_2")
        tr_object_2 = tr_object_1.copy()
        tr_object_2.id = tr_uuid_2
        tr_object_2.version_tag = "1.0.1"
        store_single_transformation_revision(tr_object_2)

        tr_object_3 = tr_object_template.copy()
        tr_uuid_3 = get_uuid_from_seed("test_multiple_select_3")
        tr_object_3.id = tr_uuid_3
        tr_object_3.revision_group_id = tr_uuid_3
        tr_object_3.release()
        store_single_transformation_revision(tr_object_3)

        results = select_multiple_transformation_revisions()
        assert len(results) == 3

        results = select_multiple_transformation_revisions(state=State.DRAFT)
        assert len(results) == 2

        results = select_multiple_transformation_revisions(state=State.RELEASED)
        assert len(results) == 1

        results = select_multiple_transformation_revisions(revision_group_id=tr_uuid_1)
        assert len(results) == 2

        results = select_multiple_transformation_revisions(type=Type.COMPONENT)
        assert len(results) == 3

        results = select_multiple_transformation_revisions(type=Type.WORKFLOW)
        assert len(results) == 0
async def test_execute_for_component_without_hetdesrun_imports(
    async_test_client, clean_test_db_engine
):
    with mock.patch(
        "hetdesrun.persistence.dbservice.revision.Session",
        sessionmaker(clean_test_db_engine),
    ):

        path = (
            "./tests/data/components/"
            "alerts-from-score_100_38f168ef-cb06-d89c-79b3-0cd823f32e9d"
            ".json"
        )
        component_tr_json = load_json(path)
        wiring_json = {
            "id": "38f168ef-cb06-d89c-79b3-0cd823f32e9d",
            "name": "STANDARD-WIRING",
            "inputWirings": [
                {
                    "id": "8c249f92-4b81-457e-9371-24204d6b373b",
                    "workflowInputName": "scores",
                    "adapterId": "direct_provisioning",
                    "filters": {
                        "value": (
                            "{\n"
                            '    "2020-01-03T08:20:03.000Z": 18.7,\n'
                            '    "2020-01-01T01:15:27.000Z": 42.2,\n'
                            '    "2020-01-03T08:20:04.000Z": 25.9\n'
                            "}"
                        )
                    },
                },
                {
                    "id": "0f0f97f7-1f5d-4f5d-be11-7c7b78d02129",
                    "workflowInputName": "threshold",
                    "adapterId": "direct_provisioning",
                    "filters": {"value": "30"},
                },
            ],
            "outputWirings": [],
        }

        tr = TransformationRevision(**component_tr_json)
        tr.content = update_code(tr)
        assert "COMPONENT_INFO" in tr.content

        store_single_transformation_revision(tr)

        async with async_test_client as ac:
            response = await ac.post(
                "/api/components/" + component_tr_json["id"] + "/execute",
                json=wiring_json,
            )

        assert response.status_code == 200
        assert "output_types_by_output_name" in response.json()
def test_deleting(clean_test_db_engine):
    with mock.patch(
        "hetdesrun.persistence.dbservice.revision.Session",
        sessionmaker(clean_test_db_engine),
    ):
        tr_draft_uuid = get_uuid_from_seed("draft")

        tr_draft_object = TransformationRevision(
            id=tr_draft_uuid,
            revision_group_id=tr_draft_uuid,
            name="Test",
            description="Test description",
            version_tag="1.0.0",
            category="Test category",
            state=State.DRAFT,
            type=Type.COMPONENT,
            content="code",
            io_interface=IOInterface(),
            test_wiring=WorkflowWiring(),
            documentation="",
        )

        tr_released_uuid = get_uuid_from_seed("released")

        tr_released_object = TransformationRevision(
            id=tr_released_uuid,
            revision_group_id=tr_released_uuid,
            name="Test",
            description="Test description",
            version_tag="1.0.0",
            category="Test category",
            released_timestamp="2021-12-24 00:00",
            state=State.RELEASED,
            type=Type.COMPONENT,
            content="code",
            io_interface=IOInterface(),
            test_wiring=WorkflowWiring(),
            documentation="",
        )

        store_single_transformation_revision(tr_draft_object)
        store_single_transformation_revision(tr_released_object)

        delete_single_transformation_revision(tr_draft_uuid)

        with pytest.raises(DBNotFoundError):
            read_single_transformation_revision(tr_draft_uuid)

        with pytest.raises(DBBadRequestError):
            delete_single_transformation_revision(tr_released_uuid)
Example #6
0
def update_tr(session: SQLAlchemySession,
              transformation_revision: TransformationRevision) -> None:
    try:
        db_model = transformation_revision.to_orm_model()
        session.execute(
            update(TransformationRevisionDBModel).where(
                TransformationRevisionDBModel.id == db_model.id).values(
                    revision_group_id=db_model.revision_group_id,
                    name=db_model.name,
                    description=db_model.description,
                    category=db_model.category,
                    version_tag=db_model.version_tag,
                    state=db_model.state,
                    type=db_model.type,
                    documentation=db_model.documentation,
                    workflow_content=db_model.workflow_content,
                    component_code=db_model.component_code,
                    io_interface=db_model.io_interface,
                    test_wiring=db_model.test_wiring,
                    released_timestamp=db_model.released_timestamp,
                    disabled_timestamp=db_model.disabled_timestamp,
                ))

    except IntegrityError as e:
        msg = (
            f"Integrity Error while trying to update "
            f"transformation revision with id {transformation_revision.id}.\n"
            f"Error was:\n{str(e)}")
        logger.error(msg)
        raise DBIntegrityError(msg) from e
Example #7
0
def test_function_header_multiple_inputs():
    component = TransformationRevision(
        io_interface=IOInterface(
            inputs=[
                IO(name="x", data_type=DataType.Float),
                IO(name="okay", data_type=DataType.Boolean),
            ],
            outputs=[IO(name="output", data_type=DataType.Float)],
        ),
        name="Test Component",
        description="A test component",
        category="Tests",
        id="c6eff22c-21c4-43c6-9ae1-b2bdfb944565",
        revision_group_id="c6eff22c-21c4-43c6-9ae1-b2bdfb944565",
        version_tag="1.0.0",
        state="DRAFT",
        type="COMPONENT",
        content="",
        test_wiring=[],
    )
    func_header = generate_function_header(component)
    assert "main(*, x, okay)" in func_header
    assert ("""
    "inputs": {
        "x": "FLOAT",
        "okay": "BOOLEAN",
    },
    """ in func_header)
    assert ("""
    "outputs": {
        "output": "FLOAT",
    },
    """ in func_header)
    assert '"version_tag": "1.0.0"' in func_header
async def create_transformation_revision(
    transformation_revision: TransformationRevision,
) -> TransformationRevision:
    """Store a transformation revision in the data base."""
    logger.info("create transformation revision %s", transformation_revision.id)

    if transformation_revision.type == Type.COMPONENT:
        logger.debug("transformation revision has type %s", Type.COMPONENT)
        transformation_revision.content = update_code(transformation_revision)
        logger.debug("generated code:\n%s", transformation_revision.content)

    try:
        store_single_transformation_revision(transformation_revision)
        logger.info("created transformation revision")
    except DBIntegrityError as e:
        raise HTTPException(status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e)) from e

    try:
        persisted_transformation_revision = read_single_transformation_revision(
            transformation_revision.id
        )
    except DBNotFoundError as e:
        raise HTTPException(status.HTTP_404_NOT_FOUND, detail=str(e)) from e

    logger.debug(persisted_transformation_revision.json())

    return persisted_transformation_revision
Example #9
0
async def test_tr_from_code_for_component_without_register_decorator(
    clean_test_db_engine, ):
    with mock.patch(
            "hetdesrun.persistence.dbservice.revision.Session",
            sessionmaker(clean_test_db_engine),
    ):
        path = os.path.join(
            "tests",
            "data",
            "components",
            "alerts-from-score_100_38f168ef-cb06-d89c-79b3-0cd823f32e9d.py",
        )
        with open(path) as f:
            code = f.read()

        tr_json = transformation_revision_from_python_code(code, path)

        tr = TransformationRevision(**tr_json)

        assert tr.name == "Alerts from Score"
        assert tr.category == "Anomaly Detection"
        assert "anomalous situations" in tr.description
        assert tr.version_tag == "1.0.0"
        assert str(tr.id) == "38f168ef-cb06-d89c-79b3-0cd823f32e9d"
        assert str(
            tr.revision_group_id) == "38f168ef-cb06-d89c-79b3-0cd823f32e9d"
        assert len(tr.io_interface.inputs) == 2
        assert len(tr.io_interface.outputs) == 1
        assert tr.type == "COMPONENT"
        assert "COMPONENT_INFO" in tr.content
def test_storing_and_receiving(clean_test_db_engine):
    with mock.patch(
        "hetdesrun.persistence.dbservice.revision.Session",
        sessionmaker(clean_test_db_engine),
    ):
        tr_uuid = get_uuid_from_seed("test_storing_and_receiving")

        tr_object = TransformationRevision(
            id=tr_uuid,
            revision_group_id=tr_uuid,
            name="Test",
            description="Test description",
            version_tag="1.0.0",
            category="Test category",
            state=State.DRAFT,
            type=Type.COMPONENT,
            content="code",
            io_interface=IOInterface(),
            test_wiring=WorkflowWiring(),
            documentation="",
        )

        store_single_transformation_revision(tr_object)

        received_tr_object = read_single_transformation_revision(tr_uuid)

        assert tr_object == received_tr_object

        # non-existent object
        wrong_tr_uuid = get_uuid_from_seed("wrong id")
        with pytest.raises(DBNotFoundError):
            received_tr_object = read_single_transformation_revision(wrong_tr_uuid)
def update_content(
    existing_transformation_revision: Optional[TransformationRevision],
    updated_transformation_revision: TransformationRevision,
) -> TransformationRevision:
    if updated_transformation_revision.type == Type.COMPONENT:
        updated_transformation_revision.content = update_code(
            updated_transformation_revision
        )
    elif existing_transformation_revision is not None:
        assert isinstance(
            existing_transformation_revision.content, WorkflowContent
        )  # hint for mypy

        existing_operator_ids: List[UUID] = []
        for operator in existing_transformation_revision.content.operators:
            existing_operator_ids.append(operator.id)

        assert isinstance(
            updated_transformation_revision.content, WorkflowContent
        )  # hint for mypy

        for operator in updated_transformation_revision.content.operators:
            if (
                operator.type == Type.WORKFLOW
                and operator.id not in existing_operator_ids
            ):
                operator.state = (
                    State.DISABLED
                    if contains_deprecated(operator.transformation_id)
                    else operator.state
                )
    return updated_transformation_revision
Example #12
0
def select_multiple_transformation_revisions(
    category: Optional[str] = None,
    revision_group_id: Optional[UUID] = None,
    type: Optional[Type] = None,
    state: Optional[State] = None,
) -> List[TransformationRevision]:
    """Filterable selection of transformation revisions from db"""
    with Session() as session, session.begin():
        selection = select(TransformationRevisionDBModel)

        if category is not None:
            selection = selection.where(
                TransformationRevisionDBModel.category == category)
        if revision_group_id is not None:
            selection = selection.where(TransformationRevisionDBModel.
                                        revision_group_id == revision_group_id)
        if type is not None:
            selection = selection.where(
                TransformationRevisionDBModel.type == type)
        if state is not None:
            selection = selection.where(
                TransformationRevisionDBModel.state == state)

        results = session.execute(selection).scalars().all()

        return [
            TransformationRevision.from_orm_model(result) for result in results
        ]
Example #13
0
 def to_transformation_revision(self,
                                documentation: str = ""
                                ) -> TransformationRevision:
     return TransformationRevision(
         id=self.id,
         revision_group_id=self.group_id,
         name=self.name,
         description=self.description,
         category=self.category,
         version_tag=self.tag,
         released_timestamp=datetime.now()
         if self.state == State.RELEASED else None,
         disabled_timestamp=datetime.now()
         if self.state == State.DISABLED else None,
         state=self.state,
         type=self.type,
         documentation=documentation,
         io_interface=IOInterface(
             inputs=[input.to_io() for input in self.inputs],
             outputs=[output.to_io() for output in self.outputs],
         ),
         content=self.code,
         test_wiring=self.wirings[0].to_wiring()
         if len(self.wirings) > 0 else WorkflowWiring(),
     )
Example #14
0
async def test_update_transformation_revision_with_invalid_name_workflow(
        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_workflow_2))

        tr_json_workflow_2_update_invalid_name = deepcopy(
            tr_json_workflow_2_update)
        tr_json_workflow_2_update_invalid_name["name"] = "'"

        async with async_test_client as ac:
            response = await ac.put(
                posix_urljoin("/api/transformations/",
                              str(get_uuid_from_seed("workflow 2"))),
                json=tr_json_workflow_2_update_invalid_name,
            )

        print(response.json())
        assert response.status_code == 422
        assert "string does not match regex" in response.json(
        )["detail"][0]["msg"]
        assert "name" in response.json()["detail"][0]["loc"]
Example #15
0
def test_tr_validator_content_type_correct():
    id = get_uuid_from_seed("test")

    combi = namedtuple("combi", "type content")
    incorrect_combis = (
        combi(type=Type.WORKFLOW, content="test"),
        combi(type=Type.COMPONENT, content=WorkflowContent()),
    )

    correct_combis = (
        combi(type=Type.WORKFLOW, content=WorkflowContent()),
        combi(type=Type.COMPONENT, content="test"),
    )

    for combi in incorrect_combis:
        with pytest.raises(ValidationError):
            TransformationRevision(
                id=id,
                revision_group_id=id,
                name="Test",
                description="Test description",
                version_tag="1.0.0",
                category="Test category",
                state=State.DRAFT,
                type=combi.type,
                content=combi.content,
                io_interface=IOInterface(),
                test_wiring=WorkflowWiring(),
                documentation="",
            )
    for combi in correct_combis:
        # no validation errors
        TransformationRevision(
            id=id,
            revision_group_id=id,
            name="Test",
            description="Test description",
            version_tag="1.0.0",
            category="Test category",
            state=State.DRAFT,
            type=combi.type,
            content=combi.content,
            io_interface=IOInterface(),
            test_wiring=WorkflowWiring(),
            documentation="",
        )
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
Example #17
0
def test_tr_validator_io_interface_fits_to_content():
    tr_json_empty_io_interface = deepcopy(tr_json_valid_released_example)
    tr_json_empty_io_interface["io_interface"]["inputs"] = []
    tr_json_empty_io_interface["io_interface"]["outputs"] = []
    tr_generated_io_interface = TransformationRevision(
        **tr_json_empty_io_interface)

    assert len(tr_generated_io_interface.content.inputs) == len(
        tr_json_valid_released_example["io_interface"]["inputs"])
Example #18
0
def import_transformation(
    tr_json: dict,
    path: str,
    strip_wirings: bool = False,
    directly_into_db: bool = False,
    update_component_code: bool = True,
) -> None:

    if strip_wirings:
        tr_json["test_wiring"] = {"input_wirings": [], "output_wirings": []}

    if directly_into_db:
        tr = TransformationRevision(**tr_json)
        logger.info(
            ("Update or create database entry"
             " for transformation revision %s of type %s\n"
             "in category %s with name %s"),
            str(tr.id),
            str(tr.type),
            tr.category,
            tr.name,
        )
        update_or_create_single_transformation_revision(tr)

    else:
        headers = get_auth_headers()

        response = requests.put(
            posix_urljoin(get_config().hd_backend_api_url, "transformations",
                          tr_json["id"]),
            params={
                "allow_overwrite_released": True,
                "update_component_code": update_component_code,
            },
            verify=get_config().hd_backend_verify_certs,
            json=tr_json,
            auth=get_backend_basic_auth()  # type: ignore
            if get_config().hd_backend_use_basic_auth else None,
            headers=headers,
        )
        logger.info(
            ("PUT transformation status code: %d"
             " for transformation revision %s of type %s\n"
             "in category %s with name %s"),
            response.status_code,
            tr_json["id"],
            tr_json["type"],
            tr_json["name"],
            tr_json["category"],
        )
        if response.status_code != 201:
            msg = (f"COULD NOT PUT {tr_json['type']} from path {path}\n."
                   f"Response status code {response.status_code}"
                   f"with response text:\n{response.text}")
            logger.error(msg)
Example #19
0
def add_tr(session: SQLAlchemySession,
           transformation_revision: TransformationRevision) -> None:
    try:
        db_model = transformation_revision.to_orm_model()
        session.add(db_model)
    except IntegrityError as e:
        msg = (
            f"Integrity Error while trying to store transformation revision "
            f"with id {transformation_revision.id}. Error was:\n{str(e)}")
        logger.error(msg)
        raise DBIntegrityError(msg) from e
Example #20
0
async def test_get_all_transformation_revisions_with_valid_db_entries(
        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))
        store_single_transformation_revision(
            TransformationRevision(**tr_json_component_2))
        store_single_transformation_revision(
            TransformationRevision(**tr_json_workflow_1))
        store_single_transformation_revision(
            TransformationRevision(**tr_json_workflow_2))
        async with async_test_client as ac:
            response = await ac.get("/api/transformations/")

        assert response.status_code == 200
        assert response.json()[0] == tr_json_component_1
        assert response.json()[1] == tr_json_component_2
        assert response.json()[2] == tr_json_workflow_1
        assert response.json()[3] == tr_json_workflow_2
Example #21
0
async def test_execute_for_transformation_revision(async_test_client,
                                                   clean_test_db_engine):
    patched_session = sessionmaker(clean_test_db_engine)
    with mock.patch(
            "hetdesrun.persistence.dbservice.nesting.Session",
            patched_session,
    ):
        with mock.patch(
                "hetdesrun.persistence.dbservice.revision.Session",
                patched_session,
        ):
            tr_component_1 = TransformationRevision(**tr_json_component_1)
            tr_component_1.content = update_code(tr_component_1)
            store_single_transformation_revision(tr_component_1)
            tr_workflow_2 = TransformationRevision(**tr_json_workflow_2_update)

            store_single_transformation_revision(tr_workflow_2)

            update_or_create_nesting(tr_workflow_2)

            exec_by_id_input = ExecByIdInput(
                id=tr_workflow_2.id,
                wiring=tr_workflow_2.test_wiring,
                job_id=UUID("1270547c-b224-461d-9387-e9d9d465bbe1"),
            )

            async with async_test_client as ac:
                response = await ac.post(
                    "/api/transformations/execute",
                    json=json.loads(exec_by_id_input.json()),
                )

            assert response.status_code == 200
            resp_data = response.json()
            assert "output_types_by_output_name" in resp_data
            assert "job_id" in resp_data
            assert UUID(resp_data["job_id"]) == UUID(
                "1270547c-b224-461d-9387-e9d9d465bbe1")
Example #22
0
def component_creator(identifier: str) -> TransformationRevision:
    tr_component = TransformationRevision(
        id=get_uuid_from_seed("component " + identifier),
        revision_group_id=get_uuid_from_seed("group of component " +
                                             identifier),
        name=identifier,
        type="COMPONENT",
        state="DRAFT",
        version_tag="1.0.0",
        io_interface=IOInterface(),
        content="code",
        test_wiring=WorkflowWiring(),
    )
    return tr_component
Example #23
0
def workflow_creator(identifier: str) -> TransformationRevision:
    tr_workflow = TransformationRevision(
        id=get_uuid_from_seed("workflow " + identifier),
        revision_group_id=get_uuid_from_seed("group of workflow " +
                                             identifier),
        name=identifier,
        type="WORKFLOW",
        state="DRAFT",
        version_tag="1.0.0",
        io_interface=IOInterface(),
        content=WorkflowContent(),
        test_wiring=WorkflowWiring(),
    )
    return tr_workflow
Example #24
0
def test_tr_validator_disabled_requires_released_timestamp():
    tr_json_disabled_no_released_timestamp = deepcopy(
        tr_json_valid_released_example)
    tr_json_disabled_no_released_timestamp[
        "disabled_timestamp"] = tr_json_disabled_no_released_timestamp[
            "released_timestamp"]
    tr_json_disabled_no_released_timestamp["state"] = "DISABLED"
    tr_json_disabled_no_released_timestamp["released_timestamp"] = None
    tr_set_released_timestamp = TransformationRevision(
        **tr_json_disabled_no_released_timestamp)

    assert tr_set_released_timestamp.released_timestamp is not None
    assert (tr_set_released_timestamp.released_timestamp ==
            tr_set_released_timestamp.disabled_timestamp)
Example #25
0
async def test_get_all_transformation_revisions_with_specified_type(
        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
        )
        store_single_transformation_revision(
            TransformationRevision(**tr_json_workflow_2)  # DRAFT
        )

        async with async_test_client as ac:
            response_component = await ac.get(
                "/api/transformations/?type=COMPONENT")
            response_workflow = await ac.get(
                "/api/transformations/?type=WORKFLOW")
            response_foo = await ac.get("/api/transformations/?type=foo")

        assert response_component.status_code == 200
        assert len(response_component.json()) == 2
        assert response_component.json()[0] == tr_json_component_1
        assert response_component.json()[1] == tr_json_component_2
        assert response_workflow.status_code == 200
        assert len(response_workflow.json()) == 2
        assert response_workflow.json()[0] == tr_json_workflow_1
        assert response_workflow.json()[1] == tr_json_workflow_2
        assert response_foo.status_code == 422
        assert ("not a valid enumeration member"
                in response_foo.json()["detail"][0]["msg"])
def test_get_latest_revision_id(clean_test_db_engine):
    with mock.patch(
        "hetdesrun.persistence.dbservice.revision.Session",
        sessionmaker(clean_test_db_engine),
    ):
        tr_template_id = get_uuid_from_seed("object_template")
        tr_object_template = TransformationRevision(
            id=get_uuid_from_seed("test_get_latest_revision_0"),
            revision_group_id=tr_template_id,
            name="Test",
            description="Test description",
            version_tag="1.0.0",
            category="Test category",
            state=State.DRAFT,
            type=Type.COMPONENT,
            content="code",
            io_interface=IOInterface(),
            test_wiring=WorkflowWiring(),
            documentation="",
        )

        tr_object_1 = tr_object_template.copy()
        tr_object_1.id = get_uuid_from_seed("test_get_latest_revision_1")
        tr_object_1.version_tag = "1.0.1"
        tr_object_1.release()
        store_single_transformation_revision(tr_object_1)

        tr_object_2 = tr_object_template.copy()
        tr_object_2.id = get_uuid_from_seed("test_get_latest_revision_2")
        tr_object_2.version_tag = "1.0.2"
        tr_object_2.release()
        store_single_transformation_revision(tr_object_2)

        assert get_latest_revision_id(tr_template_id) == get_uuid_from_seed(
            "test_get_latest_revision_2"
        )
Example #27
0
async def test_get_transformation_revision_by_id_with_workflow(
        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_workflow_1))

        async with async_test_client as ac:
            response = await ac.get(
                posix_urljoin("/api/transformations/",
                              str(get_uuid_from_seed("workflow 1"))))
        assert response.status_code == 200
        assert response.json() == tr_json_workflow_1
Example #28
0
def select_tr_by_id(session: SQLAlchemySession,
                    id: UUID,
                    log_error: bool = True) -> TransformationRevision:

    result = session.execute(
        select(TransformationRevisionDBModel).where(
            TransformationRevisionDBModel.id == id)).scalar_one_or_none()

    if result is None:
        msg = f"Found no transformation revision in database with id {id}"
        if log_error:
            logger.error(msg)
        raise DBNotFoundError(msg)

    return TransformationRevision.from_orm_model(result)
Example #29
0
def test_tr_nonemptyvalidstr_regex_validator_fancy_characters():
    id = get_uuid_from_seed("test")
    TransformationRevision(
        id=id,
        revision_group_id=id,
        name="bößä",
        description="中文, español, Çok teşekkürler",
        version_tag="(-_-) /  =.= & +_+",
        category="ไทย",
        state=State.DRAFT,
        type=Type.COMPONENT,
        content="test",
        io_interface=IOInterface(),
        test_wiring=WorkflowWiring(),
        documentation="",
    )
Example #30
0
def test_tr_validstr_regex_validator_empty():
    id = get_uuid_from_seed("test")
    TransformationRevision(
        id=id,
        revision_group_id=id,
        name="Test",
        description="",
        version_tag="1.0.0",
        category="Test category",
        state=State.DRAFT,
        type=Type.COMPONENT,
        content="test",
        io_interface=IOInterface(),
        test_wiring=WorkflowWiring(),
        documentation="",
    )