Skip to content

Commit

Permalink
fix: Apply changes from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
ewuerger committed Aug 20, 2024
1 parent a194776 commit c2fe9cc
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 86 deletions.
10 changes: 5 additions & 5 deletions capella2polarion/converters/converter_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,12 @@ def _filter_links(
available_links = []
for link in links:
cappela_attr = link.capella_attr.split(".")[0]
is_description_reference = (
cappela_attr == DESCRIPTION_REFERENCE_SERIALIZER
)
if (
cappela_attr.endswith(DESCRIPTION_REFERENCE_SERIALIZER)
or (
cappela_attr.endswith(DIAGRAM_ELEMENTS_SERIALIZER)
and c_class == diagram.Diagram
)
is_description_reference
or (is_description_reference and c_class == diagram.Diagram)
or hasattr(c_class, cappela_attr)
):
available_links.append(link)
Expand Down
31 changes: 17 additions & 14 deletions capella2polarion/converters/link_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,8 @@ def create_links_for_work_item(
new_links: list[polarion_api.WorkItemLink] = []
link_errors: list[str] = []
for link_config in converter_data.type_config.links:
assert (role_id := link_config.polarion_role) is not None
attr_id = link_config.capella_attr or ""
serializer = self.serializers.get(attr_id)
serializer = self.serializers.get(link_config.capella_attr)
role_id = link_config.polarion_role
if self.role_prefix:
role_id = f"{self.role_prefix}_{role_id}"
try:
Expand All @@ -72,7 +71,7 @@ def create_links_for_work_item(
serializer(obj, work_item.id, role_id, {})
)
else:
refs = _resolve_attribute(obj, attr_id)
refs = _resolve_attribute(obj, link_config.capella_attr)
new: cabc.Iterable[str]
if isinstance(refs, common.ElementList):
new = refs.by_uuid # type: ignore[assignment]
Expand All @@ -87,9 +86,14 @@ def create_links_for_work_item(
self._create(work_item.id, role_id, new, {})
)
except Exception as error:
request_text = f"Requested attribute: {attr_id}"
error_text = f"{type(error).__name__} {str(error)}"
link_errors.extend([request_text, error_text, "--------"])
link_errors.extend(
[
f"Requested attribute: {link_config.capella_attr}",
error_text,
"--------",
]
)

if link_errors:
for link_error in link_errors:
Expand Down Expand Up @@ -227,10 +231,7 @@ def create_grouped_link_fields(
config = link_config
break

role_id = role
if self.role_prefix:
role_id = role.removeprefix(f"{self.role_prefix}_")

role_id = self._remove_prefix(role)
self._create_link_fields(
work_item, role_id, grouped_links, config=config
)
Expand Down Expand Up @@ -315,12 +316,14 @@ def create_grouped_back_link_fields(
List of links referencing work_item as secondary
"""
for role, grouped_links in _group_by("role", links).items():
role_id = role
if self.role_prefix:
role_id = role.removeprefix(f"{self.role_prefix}_")

role_id = self._remove_prefix(role)
self._create_link_fields(work_item, role_id, grouped_links, True)

def _remove_prefix(self, role: str) -> str:
if self.role_prefix:
return role.removeprefix(f"{self.role_prefix}_")
return role


def _group_by(
attr: str,
Expand Down
136 changes: 69 additions & 67 deletions tests/test_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,55 @@
DIAGRAM_CONFIG = converter_config.CapellaTypeConfig("diagram", "diagram")


class GroupedLinksBaseObject(t.TypedDict):
link_serializer: link_converter.LinkSerializer
work_items: dict[str, data_models.CapellaWorkItem]
back_links: dict[str, list[polarion_api.WorkItemLink]]
reverse_polarion_id_map: dict[str, str]
config: converter_config.CapellaTypeConfig


# pylint: disable=redefined-outer-name
@pytest.fixture()
def grouped_links_base_object(
base_object: BaseObjectContainer,
dummy_work_items: dict[str, data_models.CapellaWorkItem],
) -> GroupedLinksBaseObject:
reverse_polarion_id_map = {v: k for k, v in POLARION_ID_MAP.items()}
back_links: dict[str, list[polarion_api.WorkItemLink]] = {}
config = converter_config.CapellaTypeConfig(
"fakeModelObject",
links=[
converter_config.LinkConfig(
capella_attr="attribute", polarion_role="attribute"
)
],
)
mock_model = mock.MagicMock()
fake_2 = FakeModelObject("uuid2", "Fake 2")
fake_1 = FakeModelObject("uuid1", "Fake 1")
fake_0 = FakeModelObject("uuid0", "Fake 0", attribute=[fake_1, fake_2])
fake_1.attribute = [fake_0, fake_2]
mock_model.by_uuid.side_effect = lambda uuid: {
"uuid0": fake_0,
"uuid1": fake_1,
"uuid2": fake_2,
}[uuid]
link_serializer = link_converter.LinkSerializer(
base_object.pw.polarion_data_repo,
base_object.mc.converter_session,
base_object.pw.polarion_params.project_id,
mock_model,
)
return {
"link_serializer": link_serializer,
"work_items": dummy_work_items,
"back_links": back_links,
"reverse_polarion_id_map": reverse_polarion_id_map,
"config": config,
}


class TestDiagramElements:
@staticmethod
@pytest.fixture
Expand Down Expand Up @@ -1251,51 +1300,27 @@ def test_grouped_links_attributes_with_includes(

@staticmethod
def test_maintain_reverse_grouped_links_attributes(
base_object: BaseObjectContainer,
dummy_work_items: dict[str, data_models.CapellaWorkItem],
grouped_links_base_object: GroupedLinksBaseObject,
):
reverse_polarion_id_map = {v: k for k, v in POLARION_ID_MAP.items()}
back_links: dict[str, list[polarion_api.WorkItemLink]] = {}
config = converter_config.CapellaTypeConfig(
"fakeModelObject",
links=[
converter_config.LinkConfig(
capella_attr="attribute", polarion_role="attribute"
)
],
)
mock_model = mock.MagicMock()
fake_2 = FakeModelObject("uuid2", "Fake 2")
fake_1 = FakeModelObject("uuid1", "Fake 1")
fake_0 = FakeModelObject("uuid0", "Fake 0", attribute=[fake_1, fake_2])
fake_1.attribute = [fake_0, fake_2]
mock_model.by_uuid.side_effect = lambda uuid: {
"uuid0": fake_0,
"uuid1": fake_1,
"uuid2": fake_2,
}[uuid]
link_serializer = link_converter.LinkSerializer(
base_object.pw.polarion_data_repo,
base_object.mc.converter_session,
base_object.pw.polarion_params.project_id,
mock_model,
)
link_serializer = grouped_links_base_object["link_serializer"]
dummy_work_items = grouped_links_base_object["work_items"]
reverse_polarion_id_map = grouped_links_base_object[
"reverse_polarion_id_map"
]
back_links = grouped_links_base_object["back_links"]
config = grouped_links_base_object["config"]

for work_item in dummy_work_items.values():
converter_data = data_session.ConverterData(
"test", config, [], work_item
)
link_serializer.create_grouped_link_fields(
converter_data, back_links
)

for work_item_id, links in back_links.items():
work_item = dummy_work_items[reverse_polarion_id_map[work_item_id]]
link_serializer.create_grouped_back_link_fields(work_item, links)
del dummy_work_items["uuid0"].additional_attributes["uuid_capella"]
del dummy_work_items["uuid1"].additional_attributes["uuid_capella"]
del dummy_work_items["uuid2"].additional_attributes["uuid_capella"]
del dummy_work_items["uuid0"].additional_attributes["attribute"]
del dummy_work_items["uuid1"].additional_attributes["attribute"]

assert (
dummy_work_items["uuid0"].additional_attributes.pop(
"attribute_reverse"
Expand All @@ -1314,52 +1339,29 @@ def test_maintain_reverse_grouped_links_attributes(
)["value"]
== HTML_LINK_2["attribute_reverse"]
)
assert dummy_work_items["uuid0"].additional_attributes == {}
assert dummy_work_items["uuid1"].additional_attributes == {}
assert dummy_work_items["uuid2"].additional_attributes == {}

@staticmethod
def test_maintain_reverse_grouped_links_attributes_with_role_prefix(
base_object: BaseObjectContainer,
dummy_work_items: dict[str, data_models.CapellaWorkItem],
grouped_links_base_object: GroupedLinksBaseObject,
):
reverse_polarion_id_map = {v: k for k, v in POLARION_ID_MAP.items()}
back_links: dict[str, list[polarion_api.WorkItemLink]] = {}
config = converter_config.CapellaTypeConfig(
"fakeModelObject",
links=[
converter_config.LinkConfig(
capella_attr="attribute", polarion_role="attribute"
)
],
)
mock_model = mock.MagicMock()
fake_2 = FakeModelObject("uuid2", "Fake 2")
fake_1 = FakeModelObject("uuid1", "Fake 1")
fake_0 = FakeModelObject("uuid0", "Fake 0", attribute=[fake_1, fake_2])
fake_1.attribute = [fake_0, fake_2]
mock_model.by_uuid.side_effect = lambda uuid: {
"uuid0": fake_0,
"uuid1": fake_1,
"uuid2": fake_2,
}[uuid]
link_serializer = grouped_links_base_object["link_serializer"]
dummy_work_items = grouped_links_base_object["work_items"]
reverse_polarion_id_map = grouped_links_base_object[
"reverse_polarion_id_map"
]
back_links = grouped_links_base_object["back_links"]
config = grouped_links_base_object["config"]
for link in dummy_work_items["uuid0"].linked_work_items:
link.role = f"_C2P_{link.role}"
link_serializer = link_converter.LinkSerializer(
base_object.pw.polarion_data_repo,
base_object.mc.converter_session,
base_object.pw.polarion_params.project_id,
mock_model,
role_prefix="_C2P",
)
link_serializer.role_prefix = "_C2P"

for work_item in dummy_work_items.values():
converter_data = data_session.ConverterData(
"test", config, [], work_item
)
link_serializer.create_grouped_link_fields(
converter_data, back_links
)

for work_item_id, links in back_links.items():
work_item = dummy_work_items[reverse_polarion_id_map[work_item_id]]
link_serializer.create_grouped_back_link_fields(work_item, links)
Expand Down

0 comments on commit c2fe9cc

Please sign in to comment.