From db7355e5a0d9120fd4fbb04b94b2547904d73e66 Mon Sep 17 00:00:00 2001 From: u237713 Date: Thu, 6 Feb 2025 10:05:29 +0100 Subject: [PATCH] chore: update with repo template Add test comments and fix float values in get_px_conversion_ratio --- app/svg_utils.py | 2 +- tests/test_svg_utils.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/app/svg_utils.py b/app/svg_utils.py index 58a0aba..0a153f7 100644 --- a/app/svg_utils.py +++ b/app/svg_utils.py @@ -192,4 +192,4 @@ def convert_to_px(value: str | None, unit: str | None) -> int | None: def get_px_conversion_ratio(unit: str | None) -> float: - return {"px": 1, "pt": 4 / 3, "in": 96, "cm": 96 / 2.54, "mm": 96 / 2.54 * 10, "pc": 16}.get(unit, 1) if unit else 1 + return {"px": 1.0, "pt": 4 / 3, "in": 96.0, "cm": 96 / 2.54, "mm": 96 / 2.54 * 10, "pc": 16.0}.get(unit, 1.0) if unit else 1.0 diff --git a/tests/test_svg_utils.py b/tests/test_svg_utils.py index 432fab5..238a507 100644 --- a/tests/test_svg_utils.py +++ b/tests/test_svg_utils.py @@ -37,14 +37,17 @@ def inner(): @setup_env_variables def test_process_svg(): + # Invalid utf-8 string 123ABC==, return same content html = '"' content = process_svg(html) assert content == html + # image/png is not extracted, return the same value html = '"' content = process_svg(html) assert content == html + # svg image type extracted and converted using test script os.environ["CHROMIUM_EXECUTABLE_PATH"] = test_script_path os.environ["SET_WRITE_OUTPUT"] = "true" html = '"' @@ -55,37 +58,46 @@ def test_process_svg(): @setup_env_variables def test_get_svg_content(): + # image/png not svg image type, return None content = get_svg_content("image/png", "123ABC==") assert content is None + # 0x00 in base64 decoded content (b' (b''), return decoded svg content content = get_svg_content("image/svg+xml", "PHN2ZyBoZWlnaHQ9IjIwMHB4IiB3aWR0aD0iMTAwcHgiPC9zdmc+") assert content == r'' + # Invalid base64 string, return None content = get_svg_content("image/svg+xml", "PHN2ZyBoZWlnaHQ9IjIwMHB4IiB3aWR0aD0iMTAwcHgiPC9zdmc¨") assert content is None @setup_env_variables def test_replace_svg_with_png(): + # Invalid svg tag attributes, return same content svg_content = r'' width, height = extract_svg_dimensions_as_px(svg_content) assert width == 100 and height == 200 + # Return None values for incorrectly formatted svg tag attributes svg_content = r'