From fa8ffa6a180705b5982ef90e48c7fdffe7f3a5e6 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 | 222 ++++++++++------------------- 2 files changed, 79 insertions(+), 159 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..a639e1c 100644 --- a/tests/test_workitem_attachments.py +++ b/tests/test_workitem_attachments.py @@ -21,39 +21,31 @@ # 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" -) +DIAGRAM_WI_CHECKSUM = "1239ced17306dc92213cd1b729e3652a1cbc9b07997683ed36033a9d05adcb75" WI_CONTEXT_DIAGRAM_CHECKSUM = ( - "0ed1417e8e4717524bc91162dcf8633afca686e93f8b036d0bc48d81f0444f56" + "69d8b2ca4e690ccaf70fff16d2e42bbf2ecf434307d49e020d2138160ba35cb2" ) CONTEXT_DIAGRAM_CHECKSUM = ( - "2b86192f2f65353512e1b4af0e652577d0ca3d0cf8595f5dcfba7d52bcb6d702" + "572cb7ba53bcde56638a119fafc1304af294467d8c851f4b2cc35ce2f5d231eb" ) TEST_DIAG_UUID = "_APOQ0QPhEeynfbzU12yy7w" WORKITEM_ID = "TEST-ID" -with open( - TEST_DIAGRAM_CACHE / f"{TEST_DIAG_UUID}.svg", "r", encoding="utf8" -) as f: +with open(TEST_DIAGRAM_CACHE / f"{TEST_DIAG_UUID}.svg", "r", encoding="utf8") as f: diagram_svg = f.read() wia_dict = { "work_item_id": WORKITEM_ID, "title": "Diagram", - "content_bytes": base64.b64encode(cairosvg.svg2png(diagram_svg)).decode( - "utf8" - ), + "content_bytes": base64.b64encode(cairosvg.svg2png(diagram_svg)).decode("utf8"), "mime_type": "image/png", "file_name": "__C2P__diagram.png", } -DIAGRAM_PNG_CHECKSUM = hashlib.sha256( - json.dumps(wia_dict).encode("utf8") -).hexdigest() +DIAGRAM_PNG_CHECKSUM = hashlib.sha256(json.dumps(wia_dict).encode("utf8")).hexdigest() DIAGRAM_CHECKSUM = json.dumps( { "__C2P__WORK_ITEM": DIAGRAM_WI_CHECKSUM, @@ -62,6 +54,21 @@ ) +# pylint: disable=redefined-outer-name +@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 +104,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 +116,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,31 +130,20 @@ 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)] ) - worker.project_client.work_items.get.return_value = ( - data_model.CapellaWorkItem(WORKITEM_ID, uuid_capella=TEST_DIAG_UUID) + worker.project_client.work_items.get.return_value = data_model.CapellaWorkItem( + WORKITEM_ID, uuid_capella=TEST_DIAG_UUID ) worker.project_client.work_items.attachments = mock.MagicMock() - 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, - ) + worker.project_client.work_items.attachments.create.side_effect = set_attachment_ids converter.generate_work_items(worker.polarion_data_repo, False, True) - worker.compare_and_update_work_item( - converter.converter_session[TEST_DIAG_UUID] - ) + worker.compare_and_update_work_item(converter.converter_session[TEST_DIAG_UUID]) assert worker.project_client.work_items.update.call_count == 1 assert worker.project_client.work_items.attachments.create.call_count == 1 @@ -172,10 +156,7 @@ def test_diagram_attachments_new( ) assert len(created_attachments) == 2 assert created_attachments[0].title == created_attachments[1].title - assert ( - created_attachments[0].file_name[:3] - == created_attachments[0].file_name[:3] - ) + assert created_attachments[0].file_name[:3] == created_attachments[0].file_name[:3] assert work_item.description.value == TEST_DIAG_DESCR.format( title="Diagram", attachment_id="1-__C2P__diagram.svg", @@ -187,11 +168,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( [ @@ -200,25 +179,14 @@ def test_new_diagram( ) ] ) - worker.project_client.work_items.get.return_value = ( - data_model.CapellaWorkItem( - WORKITEM_ID, uuid_capella=TEST_DIAG_UUID, checksum=checksum - ) + worker.project_client.work_items.get.return_value = data_model.CapellaWorkItem( + WORKITEM_ID, uuid_capella=TEST_DIAG_UUID, checksum=checksum ) worker.project_client.work_items.attachments.create = mock.MagicMock() - 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, - ) + worker.project_client.work_items.attachments.create.side_effect = set_attachment_ids converter.generate_work_items(worker.polarion_data_repo, False, True) - worker.compare_and_update_work_item( - converter.converter_session[TEST_DIAG_UUID] - ) + worker.compare_and_update_work_item(converter.converter_session[TEST_DIAG_UUID]) assert worker.project_client.work_items.update.call_count == 1 assert worker.project_client.work_items.attachments.create.call_count == 1 @@ -233,11 +201,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)] ) @@ -255,27 +221,18 @@ def test_diagram_attachments_updated( file_name="__C2P__diagram.png", ), ] - worker.project_client.work_items.get.return_value = ( - data_model.CapellaWorkItem( - WORKITEM_ID, - uuid_capella=TEST_DIAG_UUID, - attachments=existing_attachments, - ) + worker.project_client.work_items.get.return_value = data_model.CapellaWorkItem( + WORKITEM_ID, + uuid_capella=TEST_DIAG_UUID, + attachments=existing_attachments, ) worker.project_client.work_items.attachments.get_all = mock.MagicMock() 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( - converter.converter_session[TEST_DIAG_UUID] - ) + worker.compare_and_update_work_item(converter.converter_session[TEST_DIAG_UUID]) assert worker.project_client.work_items.update.call_count == 1 assert worker.project_client.work_items.attachments.create.call_count == 0 @@ -293,11 +250,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,16 +285,9 @@ 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( - converter.converter_session[TEST_DIAG_UUID] - ) + worker.compare_and_update_work_item(converter.converter_session[TEST_DIAG_UUID]) assert worker.project_client.work_items.get.call_count == 1 assert worker.project_client.work_items.update.call_count == 1 @@ -358,11 +306,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,16 +318,9 @@ 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( - converter.converter_session[TEST_DIAG_UUID] - ) + worker.compare_and_update_work_item(converter.converter_session[TEST_DIAG_UUID]) assert worker.project_client.work_items.update.call_count == 0 assert worker.project_client.work_items.attachments.create.call_count == 0 @@ -393,26 +332,23 @@ 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() - worker.project_client.work_items.attachments.create.side_effect = ( - set_attachment_ids - ) + worker.project_client.work_items.attachments.create.side_effect = set_attachment_ids 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 @@ -426,10 +362,7 @@ def test_add_context_diagram( assert len(created_attachments) == 2 assert created_attachments[0].title == created_attachments[1].title - assert ( - created_attachments[0].file_name[:3] - == created_attachments[0].file_name[:3] - ) + assert created_attachments[0].file_name[:3] == created_attachments[0].file_name[:3] assert str( work_item.additional_attributes["context_diagram"]["value"] @@ -445,13 +378,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 +394,15 @@ 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 +413,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 +429,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( @@ -522,17 +453,14 @@ def test_update_context_diagram_with_changes( ), ] # read the content manually on update as it be read in the client - worker.project_client.work_items.attachments.update.side_effect = ( - read_content - ) + worker.project_client.work_items.attachments.update.side_effect = read_content with mock.patch.object(context.ContextDiagram, "render") as wrapped_render: wrapped_render.return_value = ( - '' + '' ) 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 @@ -590,9 +518,7 @@ def test_diagram_delete_attachments( converter.generate_work_items(worker.polarion_data_repo, False, True) - worker.compare_and_update_work_item( - converter.converter_session[TEST_DIAG_UUID] - ) + worker.compare_and_update_work_item(converter.converter_session[TEST_DIAG_UUID]) assert worker.project_client.work_items.update.call_count == 1 assert worker.project_client.work_items.attachments.create.call_count == 0 @@ -626,13 +552,9 @@ def test_attached_image_in_description_with_caption( ) worker.project_client.work_items.attachments.create = mock.MagicMock() - worker.project_client.work_items.attachments.create.side_effect = ( - set_attachment_ids - ) + worker.project_client.work_items.attachments.create.side_effect = set_attachment_ids - converter.generate_work_items( - worker.polarion_data_repo, False, True, False, True - ) + converter.generate_work_items(worker.polarion_data_repo, False, True, False, True) worker.compare_and_update_work_item(converter.converter_session[uuid]) assert worker.project_client.work_items.update.call_count == 1