From e365a4e3ea0f2da378bb35a55c60ddb76a1e5d51 Mon Sep 17 00:00:00 2001 From: ewuerger Date: Thu, 12 Dec 2024 17:16:25 +0100 Subject: [PATCH] test: Improve tests --- tests/test_elements.py | 16 ++-- tests/test_workitem_attachments.py | 129 +++++++++++------------------ 2 files changed, 57 insertions(+), 88 deletions(-) diff --git a/tests/test_elements.py b/tests/test_elements.py index a0f5424..08672d6 100644 --- a/tests/test_elements.py +++ b/tests/test_elements.py @@ -50,6 +50,7 @@ TEST_ACTOR_UUID = "08e02248-504d-4ed8-a295-c7682a614f66" TEST_PHYS_COMP = "b9f9a83c-fb02-44f7-9123-9d86326de5f1" TEST_PHYS_NODE = "8a6d68c8-ac3d-4654-a07e-ada7adeed09f" +TEST_PHYS_FNC = "11906f7b-3ae9-4343-b998-95b170be2e2b" TEST_SCENARIO = "afdaa095-e2cd-4230-b5d3-6cb771a90f51" TEST_CAP_REAL = "b80b3141-a7fc-48c7-84b2-1467dcef5fce" TEST_CONSTRAINT = "95cbd4af-7224-43fe-98cb-f13dda540b8e" @@ -1861,7 +1862,6 @@ def test_generic_work_item( @staticmethod def test_add_context_diagram(model: capellambse.MelodyModel): - uuid = "11906f7b-3ae9-4343-b998-95b170be2e2b" type_config = converter_config.CapellaTypeConfig( "test", "add_context_diagram", [] ) @@ -1869,16 +1869,16 @@ def test_add_context_diagram(model: capellambse.MelodyModel): model, polarion_repo.PolarionDataRepository(), { - uuid: data_session.ConverterData( + TEST_PHYS_FNC: data_session.ConverterData( "pa", type_config, - model.by_uuid(uuid), + model.by_uuid(TEST_PHYS_FNC), ) }, True, ) - work_item = serializer.serialize(uuid) + work_item = serializer.serialize(TEST_PHYS_FNC) assert work_item is not None assert "context_diagram" in work_item.additional_attributes @@ -2154,7 +2154,6 @@ def test_read_config_tree_view_with_params( with mock.patch.object( context.ContextDiagram, "render" ) as wrapped_render: - wis = serializer.serialize_all() _ = wis[0].attachments[0].content_bytes @@ -2204,7 +2203,6 @@ def test_read_config_links(caplog: pytest.LogCaptureFixture): @staticmethod def test_add_context_diagram_with_caption(model: capellambse.MelodyModel): - uuid = "11906f7b-3ae9-4343-b998-95b170be2e2b" type_config = converter_config.CapellaTypeConfig( "test", "add_context_diagram", [] ) @@ -2212,17 +2210,17 @@ def test_add_context_diagram_with_caption(model: capellambse.MelodyModel): model, polarion_repo.PolarionDataRepository(), { - uuid: data_session.ConverterData( + TEST_PHYS_FNC: data_session.ConverterData( "pa", type_config, - model.by_uuid(uuid), + model.by_uuid(TEST_PHYS_FNC), ) }, True, generate_figure_captions=True, ) - work_item = serializer.serialize(uuid) + work_item = serializer.serialize(TEST_PHYS_FNC) assert work_item is not None assert "context_diagram" in work_item.additional_attributes diff --git a/tests/test_workitem_attachments.py b/tests/test_workitem_attachments.py index 5642c5c..0b0dbf2 100644 --- a/tests/test_workitem_attachments.py +++ b/tests/test_workitem_attachments.py @@ -3,6 +3,7 @@ import base64 import hashlib import json +import typing as t from unittest import mock import cairosvg @@ -21,10 +22,10 @@ # pylint: disable=relative-beyond-top-level, useless-suppression from .conftest import TEST_DIAGRAM_CACHE -from .test_elements import TEST_DIAG_DESCR +from .test_elements import TEST_DIAG_DESCR, TEST_PHYS_FNC DIAGRAM_WI_CHECKSUM = ( - "76fc1f7e4b73891488de7e47de8ef75fc24e85fc3cdde80661503201e70b1733" + "1239ced17306dc92213cd1b729e3652a1cbc9b07997683ed36033a9d05adcb75" ) WI_CONTEXT_DIAGRAM_CHECKSUM = ( "0ed1417e8e4717524bc91162dcf8633afca686e93f8b036d0bc48d81f0444f56" @@ -62,6 +63,20 @@ ) +@pytest.fixture +def converter( + model: capellambse.MelodyModel, +) -> model_converter.ModelConverter: + diag = model.diagrams.by_uuid(TEST_DIAG_UUID) + converter = model_converter.ModelConverter(model, "TEST") + converter.converter_session[TEST_DIAG_UUID] = data_session.ConverterData( + model_converter.get_layer_name(diag), + converter_config.CapellaTypeConfig("diagram", "diagram", []), + diag, + ) + return converter + + @pytest.fixture def worker(monkeypatch: pytest.MonkeyPatch): mock_api_client = mock.MagicMock(spec=polarion_api.PolarionClient) @@ -97,17 +112,11 @@ def set_attachment_ids(attachments: list[polarion_api.WorkItemAttachment]): counter += 1 -def test_diagram_no_attachments(model: capellambse.MelodyModel): - diag = model.diagrams.by_uuid(TEST_DIAG_UUID) - converter = model_converter.ModelConverter(model, "TEST") - converter.converter_session[TEST_DIAG_UUID] = data_session.ConverterData( - model_converter.get_layer_name(diag), - converter_config.CapellaTypeConfig("diagram", "diagram", []), - diag, - ) - +def test_diagram_no_attachments(converter: model_converter.ModelConverter): converter.generate_work_items( - polarion_repo.PolarionDataRepository(), False, False + polarion_repo.PolarionDataRepository(), + generate_links=False, + generate_attachments=False, ) work_item = converter.converter_session[TEST_DIAG_UUID].work_item @@ -115,17 +124,11 @@ def test_diagram_no_attachments(model: capellambse.MelodyModel): assert work_item.attachments == [] -def test_diagram_has_attachments(model: capellambse.MelodyModel): - diag = model.diagrams.by_uuid(TEST_DIAG_UUID) - converter = model_converter.ModelConverter(model, "TEST") - converter.converter_session[TEST_DIAG_UUID] = data_session.ConverterData( - model_converter.get_layer_name(diag), - converter_config.CapellaTypeConfig("diagram", "diagram", []), - diag, - ) - +def test_diagram_has_attachments(converter: model_converter.ModelConverter): converter.generate_work_items( - polarion_repo.PolarionDataRepository(), False, True + polarion_repo.PolarionDataRepository(), + generate_links=False, + generate_attachments=True, ) work_item = converter.converter_session[TEST_DIAG_UUID].work_item @@ -135,11 +138,9 @@ def test_diagram_has_attachments(model: capellambse.MelodyModel): # pylint: disable=redefined-outer-name def test_diagram_attachments_new( - model: capellambse.MelodyModel, + converter: model_converter.ModelConverter, worker: polarion_worker.CapellaPolarionWorker, ): - diag = model.diagrams.by_uuid(TEST_DIAG_UUID) - converter = model_converter.ModelConverter(model, "TEST") worker.polarion_data_repo = polarion_repo.PolarionDataRepository( [data_model.CapellaWorkItem(WORKITEM_ID, uuid_capella=TEST_DIAG_UUID)] ) @@ -150,11 +151,6 @@ def test_diagram_attachments_new( worker.project_client.work_items.attachments.create.side_effect = ( set_attachment_ids ) - converter.converter_session[TEST_DIAG_UUID] = data_session.ConverterData( - model_converter.get_layer_name(diag), - converter_config.CapellaTypeConfig("diagram", "diagram", []), - diag, - ) converter.generate_work_items(worker.polarion_data_repo, False, True) worker.compare_and_update_work_item( @@ -187,11 +183,9 @@ def test_diagram_attachments_new( # pylint: disable=redefined-outer-name def test_new_diagram( - model: capellambse.MelodyModel, + converter: model_converter.ModelConverter, worker: polarion_worker.CapellaPolarionWorker, ): - diag = model.diagrams.by_uuid(TEST_DIAG_UUID) - converter = model_converter.ModelConverter(model, "TEST") checksum = json.dumps({"__C2P__WORK_ITEM": DIAGRAM_WI_CHECKSUM}) worker.polarion_data_repo = polarion_repo.PolarionDataRepository( [ @@ -209,11 +203,6 @@ def test_new_diagram( worker.project_client.work_items.attachments.create.side_effect = ( set_attachment_ids ) - converter.converter_session[TEST_DIAG_UUID] = data_session.ConverterData( - model_converter.get_layer_name(diag), - converter_config.CapellaTypeConfig("diagram", "diagram", []), - diag, - ) converter.generate_work_items(worker.polarion_data_repo, False, True) worker.compare_and_update_work_item( @@ -233,11 +222,9 @@ def test_new_diagram( def test_diagram_attachments_updated( - model: capellambse.MelodyModel, + converter: model_converter.ModelConverter, worker: polarion_worker.CapellaPolarionWorker, ): - diag = model.diagrams.by_uuid(TEST_DIAG_UUID) - converter = model_converter.ModelConverter(model, "TEST") worker.polarion_data_repo = polarion_repo.PolarionDataRepository( [data_model.CapellaWorkItem(WORKITEM_ID, uuid_capella=TEST_DIAG_UUID)] ) @@ -266,11 +253,6 @@ def test_diagram_attachments_updated( worker.project_client.work_items.attachments.get_all.return_value = ( existing_attachments ) - converter.converter_session[TEST_DIAG_UUID] = data_session.ConverterData( - model_converter.get_layer_name(diag), - converter_config.CapellaTypeConfig("diagram", "diagram", []), - diag, - ) converter.generate_work_items(worker.polarion_data_repo, False, True) worker.compare_and_update_work_item( @@ -293,11 +275,9 @@ def test_diagram_attachments_updated( def test_diagram_attachments_unchanged_work_item_changed( - model: capellambse.MelodyModel, + converter: model_converter.ModelConverter, worker: polarion_worker.CapellaPolarionWorker, ): - diag = model.diagrams.by_uuid(TEST_DIAG_UUID) - converter = model_converter.ModelConverter(model, "TEST") diagram_work_item = data_model.CapellaWorkItem( WORKITEM_ID, uuid_capella=TEST_DIAG_UUID, @@ -330,11 +310,6 @@ def test_diagram_attachments_unchanged_work_item_changed( worker.project_client.work_items.attachments.get_all.return_value = ( diagram_work_item.attachments ) - converter.converter_session[TEST_DIAG_UUID] = data_session.ConverterData( - model_converter.get_layer_name(diag), - converter_config.CapellaTypeConfig("diagram", "diagram", []), - diag, - ) converter.generate_work_items(worker.polarion_data_repo, False, True) worker.compare_and_update_work_item( @@ -358,11 +333,9 @@ def test_diagram_attachments_unchanged_work_item_changed( def test_diagram_attachments_fully_unchanged( - model: capellambse.MelodyModel, + converter: model_converter.ModelConverter, worker: polarion_worker.CapellaPolarionWorker, ): - diag = model.diagrams.by_uuid(TEST_DIAG_UUID) - converter = model_converter.ModelConverter(model, "TEST") worker.polarion_data_repo = polarion_repo.PolarionDataRepository( [ data_model.CapellaWorkItem( @@ -372,11 +345,6 @@ def test_diagram_attachments_fully_unchanged( ) ] ) - converter.converter_session[TEST_DIAG_UUID] = data_session.ConverterData( - model_converter.get_layer_name(diag), - converter_config.CapellaTypeConfig("diagram", "diagram", []), - diag, - ) converter.generate_work_items(worker.polarion_data_repo, False, True) worker.compare_and_update_work_item( @@ -393,16 +361,15 @@ def test_add_context_diagram( model: capellambse.MelodyModel, worker: polarion_worker.CapellaPolarionWorker, ): - uuid = "11906f7b-3ae9-4343-b998-95b170be2e2b" converter = model_converter.ModelConverter(model, "TEST") worker.polarion_data_repo = polarion_repo.PolarionDataRepository( - [data_model.CapellaWorkItem(WORKITEM_ID, uuid_capella=uuid)] + [data_model.CapellaWorkItem(WORKITEM_ID, uuid_capella=TEST_PHYS_FNC)] ) - converter.converter_session[uuid] = data_session.ConverterData( - "", + converter.converter_session[TEST_PHYS_FNC] = data_session.ConverterData( + "pa", converter_config.CapellaTypeConfig("test", "add_context_diagram", []), - model.by_uuid(uuid), + model.by_uuid(TEST_PHYS_FNC), ) worker.project_client.work_items.attachments.create = mock.MagicMock() @@ -412,7 +379,9 @@ def test_add_context_diagram( converter.generate_work_items(worker.polarion_data_repo, False, True) - worker.compare_and_update_work_item(converter.converter_session[uuid]) + worker.compare_and_update_work_item( + converter.converter_session[TEST_PHYS_FNC] + ) assert worker.project_client.work_items.update.call_count == 1 assert worker.project_client.work_items.attachments.create.call_count == 1 @@ -445,13 +414,12 @@ def test_update_context_diagram_no_changes( model: capellambse.MelodyModel, worker: polarion_worker.CapellaPolarionWorker, ): - uuid = "11906f7b-3ae9-4343-b998-95b170be2e2b" converter = model_converter.ModelConverter(model, "TEST") worker.polarion_data_repo = polarion_repo.PolarionDataRepository( [ data_model.CapellaWorkItem( WORKITEM_ID, - uuid_capella=uuid, + uuid_capella=TEST_PHYS_FNC, checksum=json.dumps( { "__C2P__WORK_ITEM": WI_CONTEXT_DIAGRAM_CHECKSUM, @@ -462,15 +430,17 @@ def test_update_context_diagram_no_changes( ] ) - converter.converter_session[uuid] = data_session.ConverterData( - "", + converter.converter_session[TEST_PHYS_FNC] = data_session.ConverterData( + "pa", converter_config.CapellaTypeConfig("test", "add_context_diagram", []), - model.by_uuid(uuid), + model.by_uuid(TEST_PHYS_FNC), ) with mock.patch.object(context.ContextDiagram, "render") as wrapped_render: converter.generate_work_items(worker.polarion_data_repo, False, True) - worker.compare_and_update_work_item(converter.converter_session[uuid]) + worker.compare_and_update_work_item( + converter.converter_session[TEST_PHYS_FNC] + ) assert worker.project_client.work_items.update.call_count == 0 assert worker.project_client.work_items.attachments.update.call_count == 0 @@ -481,13 +451,12 @@ def test_update_context_diagram_with_changes( model: capellambse.MelodyModel, worker: polarion_worker.CapellaPolarionWorker, ): - uuid = "11906f7b-3ae9-4343-b998-95b170be2e2b" converter = model_converter.ModelConverter(model, "TEST") worker.polarion_data_repo = polarion_repo.PolarionDataRepository( [ data_model.CapellaWorkItem( WORKITEM_ID, - uuid_capella=uuid, + uuid_capella=TEST_PHYS_FNC, checksum=json.dumps( { "__C2P__WORK_ITEM": WI_CONTEXT_DIAGRAM_CHECKSUM, @@ -498,10 +467,10 @@ def test_update_context_diagram_with_changes( ] ) - converter.converter_session[uuid] = data_session.ConverterData( + converter.converter_session[TEST_PHYS_FNC] = data_session.ConverterData( "", converter_config.CapellaTypeConfig("test", "add_context_diagram", []), - model.by_uuid(uuid), + model.by_uuid(TEST_PHYS_FNC), ) worker.project_client.work_items.attachments.get_all.return_value = [ polarion_api.WorkItemAttachment( @@ -532,7 +501,9 @@ def test_update_context_diagram_with_changes( 'width="100" height="100">' ) converter.generate_work_items(worker.polarion_data_repo, False, True) - worker.compare_and_update_work_item(converter.converter_session[uuid]) + worker.compare_and_update_work_item( + converter.converter_session[TEST_PHYS_FNC] + ) assert worker.project_client.work_items.update.call_count == 1 assert worker.project_client.work_items.attachments.update.call_count == 2