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)
async def test_update_transformation_revision_from_component_dto( async_test_client, clean_test_db_engine ): with mock.patch( "hetdesrun.persistence.dbservice.revision.Session", sessionmaker(clean_test_db_engine), ): store_single_transformation_revision( ComponentRevisionFrontendDto( **dto_json_component_1 ).to_transformation_revision() ) async with async_test_client as ac: response = await ac.put( "/api/components/" + str(get_uuid_from_seed("component 1")), json=dto_json_component_1_update, ) assert response.status_code == 201 assert response.json()["name"] == "new name" assert response.json()["category"] == "Test" assert response.json()["inputs"][0]["id"] == str( get_uuid_from_seed("new input") ) print(response.json()["code"]) assert "new_input" in response.json()["code"]
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
def input_json_with_wiring_with_input(): json_with_wiring = deepcopy(base_workflow_json) json_with_wiring["code_modules"][1][ "code" ] = 'from hetdesrun.component.registration import register\nfrom hetdesrun.datatypes import DataType\nfrom hetdesrun import logger\n# add your own imports here\n\n\n# ***** DO NOT EDIT LINES BELOW *****\n# These lines may be overwritten if input/output changes.\n@register(\n inputs={"inp": DataType.Float}, outputs={"c": DataType.Float}\n)\nasync def main(*, inp):\n """entrypoint function for this component"""\n logger.info("TEST")\n # ***** DO NOT EDIT LINES ABOVE *****\n # write your function code here.\n pass\n return {"c": inp}' json_with_wiring["code_modules"][1]["uuid"] = str( get_uuid_from_seed("value_giver_module") ) json_with_wiring["components"][1]["inputs"] = [ { "name": "inp", "type": "FLOAT", "id": str(get_uuid_from_seed("inp_in_my_const_giver")), } ] json_with_wiring["components"][1]["code_module_uuid"] = str( str(get_uuid_from_seed("value_giver_module")) ) json_with_wiring["workflow"]["inputs"] = [ { "name": "val_inp", "id": str(get_uuid_from_seed("input_inp_in_1001")), "type": "FLOAT", "name_in_subnode": "inp", "id_of_sub_node": str(get_uuid_from_seed("1001")), } ] json_with_wiring["workflow_wiring"] = { "input_wirings": [ { "workflow_input_name": "val_inp", "adapter_id": 1, "ref_id": "TEST-ID", "filters": {"value": 32}, } ], "output_wirings": [ { "workflow_output_name": "z", "adapter_id": 1, "ref_id": "TEST-ID", } ], } return json_with_wiring
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)
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
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
async def test_publish_transformation_revision_from_workflow_dto( async_test_client, clean_test_db_engine): with mock.patch( "hetdesrun.persistence.dbservice.revision.Session", sessionmaker(clean_test_db_engine), ): store_single_transformation_revision( WorkflowRevisionFrontendDto(**dto_json_workflow_2_publishable). to_transformation_revision()) dto_json_workflow_2_publish = deepcopy(dto_json_workflow_2_publishable) dto_json_workflow_2_publish["state"] = "RELEASED" # print("json inputs",dto_json_workflow_2_publish["inputs"]) # print("json outputs",dto_json_workflow_2_publish["outputs"]) print() async with async_test_client as ac: response = await ac.put( "/api/workflows/" + str(get_uuid_from_seed("workflow 2")), json=dto_json_workflow_2_publish, ) assert response.status_code == 201 assert response.json()["state"] == "RELEASED" assert response.json()["name"] != "new name"
async def test_deprecate_transformation_revision_from_component_dto( async_test_client, clean_test_db_engine ): with mock.patch( "hetdesrun.persistence.dbservice.revision.Session", sessionmaker(clean_test_db_engine), ): store_single_transformation_revision( ComponentRevisionFrontendDto( **dto_json_component_2 ).to_transformation_revision() ) async with async_test_client as ac: response = await ac.put( "/api/components/" + str(get_uuid_from_seed("component 2")), json=dto_json_component_2_deprecate, ) assert response.status_code == 201 assert response.json()["state"] == "DISABLED" assert response.json()["name"] != "new name" assert response.json()["category"] != "Test" assert len(response.json()["inputs"]) == 0 assert "new comment" not in response.json()["code"] assert "disabled_timestamp" in response.json()["code"] assert "released_timestamp" in response.json()["code"]
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"]
async def test_execute_for_workflow_dto(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, ): component_dto = ComponentRevisionFrontendDto( **dto_json_component_1) tr_component = component_dto.to_transformation_revision() tr_component.content = update_code(tr_component) store_single_transformation_revision(tr_component) tr_workflow_2 = WorkflowRevisionFrontendDto( **dto_json_workflow_2_update).to_transformation_revision() tr_workflow_2.content.inputs[0].name = "wf_input" tr_workflow_2.content.outputs[0].name = "wf_output" tr_workflow_2.content.links.append( Link( start=Vertex( operator=None, connector=Connector.from_io( tr_workflow_2.content.inputs[0]), ), end=Vertex( operator=tr_workflow_2.content.operators[0].id, connector=tr_workflow_2.content.operators[0].inputs[0], ), )) tr_workflow_2.content.links.append( Link( start=Vertex( operator=tr_workflow_2.content.operators[0].id, connector=tr_workflow_2.content.operators[0]. outputs[0], ), end=Vertex( operator=None, connector=Connector.from_io( tr_workflow_2.content.outputs[0]), ), )) tr_workflow_2.io_interface.inputs[0].name = "wf_input" tr_workflow_2.io_interface.outputs[0].name = "wf_output" store_single_transformation_revision(tr_workflow_2) update_or_create_nesting(tr_workflow_2) async with async_test_client as ac: response = await ac.post( "/api/workflows/" + str(get_uuid_from_seed("workflow 2")) + "/execute", json=dto_json_wiring, ) assert response.status_code == 200 assert "output_types_by_output_name" in response.json()
async def test_update_transformation_revision_from_non_existing_component_dto( async_test_client, clean_test_db_engine): with mock.patch( "hetdesrun.persistence.dbservice.revision.Session", sessionmaker(clean_test_db_engine), ): async with async_test_client as ac: response = await ac.put( "/api/base-items/" + str(get_uuid_from_seed("component 1")), json=tr_dto_json_component_1_update, ) assert response.status_code == 201 assert response.json()["name"] == "new name" assert response.json()["category"] == "Test" assert response.json()["inputs"][0]["id"] == str( get_uuid_from_seed("new input"))
async def test_update_transformation_revision_from_non_existing_workflow_dto( async_test_client, clean_test_db_engine): with mock.patch( "hetdesrun.persistence.dbservice.revision.Session", sessionmaker(clean_test_db_engine), ): async with async_test_client as ac: response = await ac.put( "/api/workflows/" + str(get_uuid_from_seed("workflow 2")), json=dto_json_workflow_2_update, ) assert response.status_code == 201 assert response.json()["operators"][0]["id"] == str( get_uuid_from_seed("operator")) assert "name" not in response.json()["inputs"][0] assert "name" not in response.json()["outputs"][0]
async def test_update_transformation_revision_with_non_existing_workflow( async_test_client, clean_test_db_engine): with mock.patch( "hetdesrun.persistence.dbservice.revision.Session", sessionmaker(clean_test_db_engine), ): 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, ) workflow_tr_in_db = read_single_transformation_revision( get_uuid_from_seed("workflow 2")) assert response.status_code == 201 assert response.json()["name"] == "new name" assert len(workflow_tr_in_db.content.links) == 2
async def test_get_documentation_of_inexistent_component( async_test_client, clean_test_db_engine): with mock.patch( "hetdesrun.persistence.dbservice.revision.Session", sessionmaker(clean_test_db_engine), ): async with async_test_client as ac: response = await ac.get( "/api/documentations/" + str(get_uuid_from_seed("inexistent component"))) assert response.status_code == 404 assert "Found no" in response.json()["detail"]
async def test_get_transformation_revision_by_id_with_inexistent_workflow( async_test_client, clean_test_db_engine): with mock.patch( "hetdesrun.persistence.dbservice.revision.Session", sessionmaker(clean_test_db_engine), ): async with async_test_client as ac: response = await ac.get( "/api/base-items/" + str(get_uuid_from_seed("inexistent workflow"))) assert response.status_code == 404 assert "Found no" in response.json()["detail"]
async def test_get_documentation(async_test_client, clean_test_db_engine): with mock.patch( "hetdesrun.persistence.dbservice.revision.Session", sessionmaker(clean_test_db_engine), ): store_single_transformation_revision(component_tr_1) async with async_test_client as ac: response = await ac.get("/api/documentations/" + str(get_uuid_from_seed("component 1"))) assert response.status_code == 200 assert response.json()["document"] == "documentation"
async def test_update_documentation_of_component_dto(async_test_client, clean_test_db_engine): with mock.patch( "hetdesrun.persistence.dbservice.revision.Session", sessionmaker(clean_test_db_engine), ): store_single_transformation_revision(component_tr_1) new_documentation = DocumentationFrontendDto( id=get_uuid_from_seed("component 1"), document="new documentation") json_of_new_documentation = json.loads( new_documentation.json(by_alias=True)) async with async_test_client as ac: response = await ac.put( "/api/documentations/" + str(get_uuid_from_seed("component 1")), json=json_of_new_documentation, ) assert response.status_code == 201 assert response.json() == json_of_new_documentation
async def test_delete_transformation_revision_from_workflow_dto( async_test_client, clean_test_db_engine): with mock.patch( "hetdesrun.persistence.dbservice.revision.Session", sessionmaker(clean_test_db_engine), ): store_single_transformation_revision( WorkflowRevisionFrontendDto( **dto_json_workflow_2).to_transformation_revision()) async with async_test_client as ac: response = await ac.delete( "/api/workflows/" + str(get_uuid_from_seed("workflow 2")), ) assert response.status_code == 204
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" )
async def test_get_transformation_revision_by_id_with_component( 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)) async with async_test_client as ac: response = await ac.get( posix_urljoin("/api/transformations/", str(get_uuid_from_seed("component 1")))) assert response.status_code == 200 assert response.json() == tr_json_component_1
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( TransformationRevisionFrontendDto( **tr_dto_json_workflow_1).to_transformation_revision()) async with async_test_client as ac: response = await ac.get("/api/base-items/" + str(get_uuid_from_seed("workflow 1"))) assert response.status_code == 200 assert response.json() == tr_dto_json_workflow_1
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="", )
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="", )
def test_tr_shortnonemptyvalidstr_validator_max_characters(): id = get_uuid_from_seed("test") with pytest.raises(ValidationError): TransformationRevision( id=id, revision_group_id=id, name="Name", description="Test description", version_tag="1.0.0.0.0.0.0.0.0.0.0", category="Test category", state=State.DRAFT, type=Type.COMPONENT, content="test", io_interface=IOInterface(), test_wiring=WorkflowWiring(), documentation="", )
async def test_update_transformation_revision_from_released_component_dto( async_test_client, clean_test_db_engine): with mock.patch( "hetdesrun.persistence.dbservice.revision.Session", sessionmaker(clean_test_db_engine), ): store_single_transformation_revision( TransformationRevisionFrontendDto( **tr_dto_json_component_2).to_transformation_revision()) async with async_test_client as ac: response = await ac.put( "/api/base-items/" + str(get_uuid_from_seed("component 2")), json=tr_dto_json_component_2_update, ) assert response.status_code == 403
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="", )
async def test_deprecate_transformation_revision_from_workflow_dto( async_test_client, clean_test_db_engine): with mock.patch( "hetdesrun.persistence.dbservice.revision.Session", sessionmaker(clean_test_db_engine), ): store_single_transformation_revision( WorkflowRevisionFrontendDto( **dto_json_workflow_1).to_transformation_revision()) async with async_test_client as ac: response = await ac.put( "/api/workflows/" + str(get_uuid_from_seed("workflow 1")), json=dto_json_workflow_1_deprecate, ) assert response.status_code == 201 assert response.json()["state"] == "DISABLED" assert response.json()["name"] != "new name"
async def test_get_component_revision_by_id_with_valid_component( async_test_client, clean_test_db_engine ): with mock.patch( "hetdesrun.persistence.dbservice.revision.Session", sessionmaker(clean_test_db_engine), ): store_single_transformation_revision( ComponentRevisionFrontendDto( **dto_json_component_1 ).to_transformation_revision() ) async with async_test_client as ac: response = await ac.get( "/api/components/" + str(get_uuid_from_seed("component 1")) ) assert response.status_code == 200 assert response.json() == dto_json_component_1
async def test_update_transformation_revision_with_released_component_and_allow_overwrite_flag( 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_2)) async with async_test_client as ac: response = await ac.put( posix_urljoin("/api/transformations/", str(get_uuid_from_seed("component 2"))) + "?allow_overwrite_released=true", json=tr_json_component_2_update, ) assert response.status_code == 201 assert response.json()["name"] == "new name" assert response.json()["category"] == "Test"