Skip to content

Commit

Permalink
fix: tests after rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
micha91 committed Dec 21, 2023
1 parent 386ae7a commit 299d480
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 39 deletions.
4 changes: 1 addition & 3 deletions capella2polarion/c2polarion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -345,6 +345,4 @@ def patch_work_items(
self.client,
new_work_item,
old_work_item,
old_work_item.title,
"element",
)
22 changes: 7 additions & 15 deletions capella2polarion/elements/serialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down
98 changes: 77 additions & 21 deletions tests/test_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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"]
}
Expand All @@ -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]

Expand All @@ -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"]
}
Expand All @@ -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]

Expand All @@ -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
Expand All @@ -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",
Expand All @@ -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
Expand All @@ -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",
Expand All @@ -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]

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 299d480

Please sign in to comment.