Skip to content

Commit

Permalink
merge: Don't crash if there are no diagrams in the model
Browse files Browse the repository at this point in the history
  • Loading branch information
Wuestengecko committed Oct 25, 2023
2 parents ee3d152 + 1c19b83 commit a3dfe6b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
13 changes: 3 additions & 10 deletions capellambse/aird/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,8 @@ def enumerate_diagrams(
model
The MelodyLoader instance
"""
raw_views = model.xpath2(C.XP_VIEWS)
views: list[tuple[pathlib.PurePosixPath, etree._Element, str]] = []
if len(raw_views) == 0:
raise ValueError("Invalid XML: No viewpoints found")

# Extract the views' names
for i in raw_views:
for i in model.xpath2(C.XP_VIEWS):
viewname = helpers.xpath_fetch_unique(
"./viewpoint", i[1], "viewpoint description"
)
Expand All @@ -75,17 +70,15 @@ def enumerate_diagrams(
views.append((*i, urllib.parse.unquote(viewname or "")))

if not views:
raise ValueError("No viewpoints found")
C.LOGGER.debug("No viewpoints found in the model")
return

descriptors: list[tuple[pathlib.PurePosixPath, etree._Element, str]] = []
for view in views:
descriptors += [
(view[0], d, view[2]) for d in C.XP_DESCRIPTORS(view[1])
]

if len(descriptors) == 0:
raise ValueError("Invalid XML: No diagrams found")

for descriptor in descriptors:
name = uid = None
try:
Expand Down
5 changes: 4 additions & 1 deletion capellambse/extensions/metrics/composer.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,10 @@ def draw_labeled_bar(
) -> str:
"""Construct a bar with label and icon (SVG string)."""
total = sum(segments)
normalized_segments = [x / total for x in segments]
if total:
normalized_segments = [x / total for x in segments]
else:
normalized_segments = [0 for _ in segments]
return (
draw_icon(x, y)
+ draw_text(x + 5, y + 3, f"{total}")
Expand Down
9 changes: 9 additions & 0 deletions tests/test_aird_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
import capellambse
from capellambse import aird, diagram, loader

EMPTY_MODEL = pathlib.Path(__file__).parent.joinpath(
"data/decl/empty_project_52/empty_project_52.aird"
)


class TestAIRDBasicFunctionality:
test_model = (
Expand Down Expand Up @@ -81,6 +85,11 @@ def test_python_code_representation_matches_expected_output(
actual = repr(diagram_under_test)
assert actual == expected

def test_enumerate_diagrams_doesnt_crash_if_there_are_no_diagrams(self):
model = loader.MelodyLoader(EMPTY_MODEL)
for _ in aird.enumerate_diagrams(model):
pass


def test_airdparser_msm_produces_valid_json_without_error(
model: capellambse.MelodyModel,
Expand Down

0 comments on commit a3dfe6b

Please sign in to comment.