From 299d4809df512ad7dd7f2b92fdf82c02614d5b73 Mon Sep 17 00:00:00 2001 From: Michael Harbarth Date: Thu, 21 Dec 2023 20:01:34 +0100 Subject: [PATCH] fix: tests after rebase --- capella2polarion/c2polarion.py | 4 +- capella2polarion/elements/serialize.py | 22 ++---- tests/test_elements.py | 98 ++++++++++++++++++++------ 3 files changed, 85 insertions(+), 39 deletions(-) diff --git a/capella2polarion/c2polarion.py b/capella2polarion/c2polarion.py index 9e443fdb..f2b56bf8 100644 --- a/capella2polarion/c2polarion.py +++ b/capella2polarion/c2polarion.py @@ -322,11 +322,11 @@ def patch_work_items( links = element.create_links( obj, self.polarion_id_map, + self.polarion_work_item_map, descr_references, self.polarion_params.project_id, model, link_roles, - TYPES_POL2CAPELLA, ) work_item: serialize.CapellaWorkItem = new_work_items[uuid] work_item.linked_work_items = links @@ -345,6 +345,4 @@ def patch_work_items( self.client, new_work_item, old_work_item, - old_work_item.title, - "element", ) diff --git a/capella2polarion/elements/serialize.py b/capella2polarion/elements/serialize.py index 9898623a..a33ec72d 100644 --- a/capella2polarion/elements/serialize.py +++ b/capella2polarion/elements/serialize.py @@ -236,7 +236,8 @@ def _sanitize_description( ) -> tuple[list[str], markupsafe.Markup]: referenced_uuids: list[str] = [] replaced_markup = RE_DESCR_LINK_PATTERN.sub( - lambda match: self.replace_markup(match, referenced_uuids, 2), descr + lambda match: self.replace_markup(match, referenced_uuids, 2), + descr, ) def repair_images(node: etree._Element) -> None: @@ -280,14 +281,14 @@ def replace_markup( """ uuid = match.group(1) try: - ctx["MODEL"].by_uuid(uuid) + self.model.by_uuid(uuid) except KeyError: logger.error("Found link to non-existing model element: %r", uuid) return strike_through(match.group(default_group)) if pid := self.polarion_id_map.get(uuid): - referenced_uuids.append(uuid) - return POLARION_WORK_ITEM_URL.format(pid=pid) - logger.warning("Found reference to non-existing work item: %r", uuid) + referenced_uuids.append(uuid) + return POLARION_WORK_ITEM_URL.format(pid=pid) + logger.warning("Found reference to non-existing work item: %r", uuid) return match.group(default_group) def include_pre_and_post_condition( @@ -351,17 +352,8 @@ def _include_actor_in_type(self, obj: cs.Component) -> CapellaWorkItem: work_item.type = helpers.resolve_element_type(xtype) return work_item - def _include_nature_in_type(self, obj: pa.PhysicalComponent) -> CapellaWorkItem: - """Return attributes for a ``PhysicalComponent``.""" - work_item = self._include_actor_in_type(obj) - xtype = work_item.type - if obj.nature is not None: - # pylint: disable-next=attribute-defined-outside-init - work_item.type = f"{xtype}{obj.nature.name.capitalize()}" - return work_item - def _include_nature_in_type( - self, obj: pa.PhysicalComponent, ctx: dict[str, t.Any] + self, obj: pa.PhysicalComponent ) -> CapellaWorkItem: """Return attributes for a ``PhysicalComponent``.""" work_item = self._include_actor_in_type(obj) diff --git a/tests/test_elements.py b/tests/test_elements.py index 218ca9e7..c778cc38 100644 --- a/tests/test_elements.py +++ b/tests/test_elements.py @@ -187,18 +187,12 @@ def test_create_diagrams(base_object: BaseObjectContainer): ) assert len(new_work_items) == 1 work_item = new_work_items[TEST_DIAG_UUID] - work_item.calculate_checksum() assert isinstance(work_item, serialize.CapellaWorkItem) - assert { - "id": work_item.id, - "status": work_item.status, - "description_type": work_item.description_type, - "title": work_item.title, - "type": work_item.type, - "additional_attributes": work_item.additional_attributes, - } == TEST_SER_DIAGRAM - assert isinstance(work_item.description, str) - assert work_item.description.startswith(TEST_DIAG_DESCR) + description = work_item.description + work_item.description = None + assert work_item == serialize.CapellaWorkItem(**TEST_SER_DIAGRAM) + assert isinstance(description, str) + assert description.startswith(TEST_DIAG_DESCR) @staticmethod def test_create_diagrams_filters_non_diagram_elements( @@ -265,7 +259,10 @@ def write(self, text: str): pass work_item = serialize.CapellaWorkItem( - id="Obj-1", uuid_capella="uuid1", status="open" + id="Obj-1", + uuid_capella="uuid1", + status="open", + type="fakeModelObject", ) c2p_cli = Capella2PolarionCli( debug=True, @@ -349,6 +346,16 @@ def test_create_work_items( def test_create_links_custom_resolver(base_object: BaseObjectContainer): obj = base_object.pw.elements["FakeModelObject"][1] base_object.pw.polarion_id_map["uuid2"] = "Obj-2" + base_object.pw.polarion_work_item_map[ + "uuid2" + ] = serialize.CapellaWorkItem( + id="Obj-2", + uuid_capella="uuid2", + type="fakeModelObject", + description_type="text/html", + description=markupsafe.Markup(""), + status="open", + ) base_object.c2pcli.synchronize_config_roles = { "FakeModelObject": ["description_reference"] } @@ -362,11 +369,11 @@ def test_create_links_custom_resolver(base_object: BaseObjectContainer): links = element.create_links( obj, base_object.pw.polarion_id_map, + base_object.pw.polarion_work_item_map, description_reference, base_object.pw.polarion_params.project_id, base_object.c2pcli.capella_model, base_object.c2pcli.synchronize_config_roles, - TYPES_POL2CAPELLA, ) assert links == [expected] @@ -375,11 +382,33 @@ def test_create_links_custom_exchanges_resolver( base_object: BaseObjectContainer, ): function_uuid = "ceffa011-7b66-4b3c-9885-8e075e312ffa" + uuid = "1a414995-f4cd-488c-8152-486e459fb9de" + obj = base_object.c2pcli.capella_model.by_uuid(function_uuid) + base_object.pw.polarion_id_map[function_uuid] = "Obj-1" - base_object.pw.polarion_id_map[ - "1a414995-f4cd-488c-8152-486e459fb9de" - ] = "Obj-2" + base_object.pw.polarion_work_item_map[ + function_uuid + ] = serialize.CapellaWorkItem( + id="Obj-1", + uuid_capella=function_uuid, + type=type(obj).__name__, + description_type="text/html", + description=markupsafe.Markup(""), + status="open", + ) + base_object.pw.polarion_id_map[uuid] = "Obj-2" + base_object.pw.polarion_work_item_map[ + uuid + ] = serialize.CapellaWorkItem( + id="Obj-2", + uuid_capella=uuid, + type="functionalExchange", + description_type="text/html", + description=markupsafe.Markup(""), + status="open", + ) + base_object.c2pcli.synchronize_config_roles = { "SystemFunction": ["input_exchanges"] } @@ -392,11 +421,11 @@ def test_create_links_custom_exchanges_resolver( links = element.create_links( obj, base_object.pw.polarion_id_map, + base_object.pw.polarion_work_item_map, {}, base_object.pw.polarion_params.project_id, base_object.c2pcli.capella_model, base_object.c2pcli.synchronize_config_roles, - TYPES_POL2CAPELLA, ) assert links == [expected] @@ -414,11 +443,11 @@ def test_create_links_missing_attribute( links = element.create_links( obj, base_object.pw.polarion_id_map, + base_object.pw.polarion_work_item_map, {}, base_object.pw.polarion_params.project_id, base_object.c2pcli.capella_model, base_object.c2pcli.synchronize_config_roles, - TYPES_POL2CAPELLA, ) assert not links assert caplog.messages[0] == expected @@ -440,6 +469,18 @@ def test_create_links_from_ElementList(base_object: BaseObjectContainer): base_object.pw.polarion_id_map |= { f"uuid{i}": f"Obj-{i}" for i in range(4, 7) } + base_object.pw.polarion_work_item_map |= { + f"uuid{i}": serialize.CapellaWorkItem( + id=f"Obj-{i}", + uuid_capella=f"uuid{i}", + type="fakeModelObject", + description_type="text/html", + description=markupsafe.Markup(""), + status="open", + ) + for i in range(4, 7) + } + expected_link = polarion_api.WorkItemLink( "Obj-6", "Obj-5", @@ -455,11 +496,11 @@ def test_create_links_from_ElementList(base_object: BaseObjectContainer): links = element.create_links( obj, base_object.pw.polarion_id_map, + base_object.pw.polarion_work_item_map, {}, base_object.pw.polarion_params.project_id, base_object.c2pcli.capella_model, base_object.c2pcli.synchronize_config_roles, - TYPES_POL2CAPELLA, ) # type: ignore[arg-type] assert expected_link in links @@ -471,6 +512,17 @@ def test_create_link_from_single_attribute( ): obj = base_object.pw.elements["FakeModelObject"][1] base_object.pw.polarion_id_map["uuid2"] = "Obj-2" + base_object.pw.polarion_work_item_map[ + "uuid2" + ] = serialize.CapellaWorkItem( + id="Obj-2", + uuid_capella="uuid2", + type="fakeModelObject", + description_type="text/html", + description=markupsafe.Markup(""), + status="open", + ) + expected = polarion_api.WorkItemLink( "Obj-2", "Obj-1", @@ -480,11 +532,11 @@ def test_create_link_from_single_attribute( links = element.create_links( obj, base_object.pw.polarion_id_map, + base_object.pw.polarion_work_item_map, {}, base_object.pw.polarion_params.project_id, base_object.c2pcli.capella_model, base_object.c2pcli.synchronize_config_roles, - TYPES_POL2CAPELLA, ) assert links == [expected] @@ -560,6 +612,7 @@ def test_update_work_items_filters_work_items_with_same_checksum( uuid_capella="uuid1", status="open", checksum=TEST_WI_CHECKSUM, + type="fakeModelObject", ) work_items: dict[str, serialize.CapellaWorkItem] = {} base_object.pw.patch_work_items( @@ -624,7 +677,10 @@ def test_update_links(base_object: BaseObjectContainer): base_object.pw.polarion_work_item_map[ "uuid2" ] = serialize.CapellaWorkItem( - id="Obj-2", uuid_capella="uuid2", status="open" + id="Obj-2", + uuid_capella="uuid2", + status="open", + type="fakeModelObject", ) work_items = { "uuid1": serialize.CapellaWorkItem(