Skip to content

Commit

Permalink
Add unit tests for helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
huong-li-nguyen committed Jan 23, 2025
1 parent 7cada8a commit 611e639
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
2 changes: 1 addition & 1 deletion vizro-core/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ exclude_lines = [
"if __name__ == .__main__.:",
"if TYPE_CHECKING:"
]
fail_under = 89
fail_under = 90
show_missing = true
skip_covered = true

Expand Down
59 changes: 59 additions & 0 deletions vizro-core/tests/unit/vizro/_themes/test_create_chart_template.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 611e639

Please sign in to comment.