From 91bb97549fccd5134a7da7bd092091529f64553e Mon Sep 17 00:00:00 2001 From: huyenngn Date: Thu, 5 Oct 2023 16:36:12 +0200 Subject: [PATCH] refactor: improve readability and apply style rules --- capella2polarion/elements/api_helper.py | 2 +- tests/test_elements.py | 42 ++++++++++++------------- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/capella2polarion/elements/api_helper.py b/capella2polarion/elements/api_helper.py index b6295b29..72f8dc95 100644 --- a/capella2polarion/elements/api_helper.py +++ b/capella2polarion/elements/api_helper.py @@ -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) diff --git a/tests/test_elements.py b/tests/test_elements.py index 802cb9ad..ab1a5e75 100644 --- a/tests/test_elements.py +++ b/tests/test_elements.py @@ -3,8 +3,7 @@ from __future__ import annotations -import base64 -import io +import base64 as b64 import logging import pathlib import re @@ -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 ( @@ -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: