Skip to content

Commit

Permalink
refactor: improve readability and apply style rules
Browse files Browse the repository at this point in the history
  • Loading branch information
huyenngn committed Oct 5, 2023
1 parent ac3c4b3 commit 91bb975
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
2 changes: 1 addition & 1 deletion capella2polarion/elements/api_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def patch_work_item(


def split_and_decode_diagram(diagram: str) -> tuple[str, bytes]:
"""Split the diagram into type and data and decode the data."""
"""Split the diagram into type and decoded data."""
prefix, encoded = diagram.split(";base64,")
return prefix.replace("data:image/", ""), b64.b64decode(encoded)

Expand Down
42 changes: 20 additions & 22 deletions tests/test_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

from __future__ import annotations

import base64
import io
import base64 as b64
import logging
import pathlib
import re
Expand All @@ -16,7 +15,6 @@
import polarion_rest_api_client as polarion_api
import pytest
from capellambse.model import common
from PIL import Image

from capella2polarion import elements
from capella2polarion.elements import (
Expand Down Expand Up @@ -79,53 +77,53 @@ class TestAPIHelper:
SVG_PATH = (
pathlib.Path(__file__).parent / "data" / "svg_diff" / "example.svg"
)
SVG_PREFIX = "data:image/svg+xml;base64,"

def encode_svg(self, params) -> str:
def encode_svg(self, params: dict[str, str]) -> str:
svg = self.SVG_PATH.read_text()
for key, value in params.items():
svg = re.sub(f"{key}=[\"'][^\"']*[\"']", f'{key}="{value}"', svg)
encoded = base64.b64encode(svg.encode("utf-8")).decode("utf-8")
return f"{self.SVG_PREFIX}{encoded}"
content_encoded = b64.b64encode(svg.encode("utf-8"))
image_data = b"data:image/svg+xml;base64," + content_encoded
src = image_data.decode()
return src

@pytest.mark.parametrize(
"changed_params,expected",
[
pytest.param(
({}, {}),
{},
False,
id="unchanged",
),
pytest.param(
({"fill": "red"}, {"fill": "blue"}),
{"fill": "red"},
True,
id="fill_changed",
),
pytest.param(
({"id": "test"}, {"id": "test2"}),
{"height": "50"},
True,
id="height_changed",
),
pytest.param(
{"id": "test2"},
False,
id="id_changed",
),
pytest.param(
(
{"id": "test", "fill": "blue"},
{"id": "test2", "fill": "red"},
),
{"id": "test2", "fill": "red"},
True,
id="id_and_fill_changed",
),
],
)
def test_image_diff(self, changed_params, expected):
old_params, new_params = changed_params
old_svg = self.encode_svg({})
new_svg = self.encode_svg(changed_params)

assert (
api_helper.has_visual_changes(
self.encode_svg(old_params),
self.encode_svg(new_params),
)
is expected
)
is_different = api_helper.has_visual_changes(old_svg, new_svg)

assert is_different is expected


class TestDiagramElements:
Expand Down

0 comments on commit 91bb975

Please sign in to comment.