From 611e63983ca424a34aea05cb2fbda7a395667eed Mon Sep 17 00:00:00 2001 From: huong-li-nguyen Date: Thu, 23 Jan 2025 19:36:59 +0100 Subject: [PATCH] Add unit tests for helper functions --- vizro-core/pyproject.toml | 2 +- .../_themes/test_create_chart_template.py | 59 +++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 vizro-core/tests/unit/vizro/_themes/test_create_chart_template.py diff --git a/vizro-core/pyproject.toml b/vizro-core/pyproject.toml index d40da7dc6..391927197 100644 --- a/vizro-core/pyproject.toml +++ b/vizro-core/pyproject.toml @@ -54,7 +54,7 @@ exclude_lines = [ "if __name__ == .__main__.:", "if TYPE_CHECKING:" ] -fail_under = 89 +fail_under = 90 show_missing = true skip_covered = true diff --git a/vizro-core/tests/unit/vizro/_themes/test_create_chart_template.py b/vizro-core/tests/unit/vizro/_themes/test_create_chart_template.py new file mode 100644 index 000000000..8baa9ef05 --- /dev/null +++ b/vizro-core/tests/unit/vizro/_themes/test_create_chart_template.py @@ -0,0 +1,59 @@ +import pytest +from pathlib import Path +from unittest.mock import mock_open, patch + +from vizro._themes.create_chart_templates import _extract_last_two_occurrences, extract_bs_variables_from_css_file + +@pytest.fixture +def css_content(): + css_content = """ + :root, [data-bs-theme=light] { + --bs-primary: #007bff; + --bs-tertiary-color: #adb5bd; + } + :root, [data-bs-theme=dark] { + --bs-primary: #375a7f; + --bs-secondary: #6c757d; + + } + [data-bs-theme=light] { + --bs-primary: #976fd1; + --bs-secondary: #444fff; + } + """ + + return css_content + +@pytest.mark.parametrize( + "variable, expected", + [ + ("--bs-primary", ("#375a7f", "#976fd1")), + ("--bs-secondary", ("#6c757d", "#444fff")), + ("--bs-tertiary", (None, None)), + ], + ) +def test_extract_last_two_occurrences(variable, css_content, expected): + result_dark, result_light = _extract_last_two_occurrences(variable, css_content) + assert (result_dark, result_light) == expected + + + +def test_extract_bs_variables_from_css_file(css_content): + variables = ["--bs-primary", "--bs-secondary", "--bs-tertiary"] + expected_dark = { + "BS-PRIMARY": "#375a7f", + "BS-SECONDARY": "#6c757d", + "BS-TERTIARY": None, + } + expected_light = { + "BS-PRIMARY": "#976fd1", + "BS-SECONDARY": "#444fff", + "BS-TERTIARY": None, + } + + mock_path = Path("/fake/path/to/css/vizro-bootstrap.css") + with patch("builtins.open", mock_open(read_data=css_content)): + result_dark, result_light = extract_bs_variables_from_css_file(variables, mock_path) + + assert result_dark == expected_dark + assert result_light == expected_light