Skip to content

Commit

Permalink
Simplify unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
huong-li-nguyen committed Jan 24, 2025
1 parent 3559bda commit 3152df2
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 16 deletions.
1 change: 1 addition & 0 deletions vizro-core/src/vizro/_themes/_colors.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Color sequences by Vizro."""


def get_colors():
return {
"DIVERGING_PURPLE_ORANGE": [
Expand Down
14 changes: 6 additions & 8 deletions vizro-core/src/vizro/_themes/generate_plotly_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,31 @@
from vizro._themes._common_template import create_template_common

THEMES_FOLDER = Path(__file__).parent
CSS_FILE = THEMES_FOLDER.parent / "static" / "css" / "vizro-bootstrap.min.css"
CSS_PATH = THEMES_FOLDER.parent / "static/css/vizro-bootstrap.min.css"
VARIABLES = ["--bs-primary", "--bs-secondary", "--bs-tertiary-color", "--bs-border-color", "--bs-body-bg"]


def _extract_last_two_occurrences(variable: str, content: str) -> tuple[Optional[str], Optional[str]]:
def _extract_last_two_occurrences(variable: str, css_content: str) -> tuple[Optional[str], Optional[str]]:
"""Extracts the last two occurrences of a variable from the CSS content.
Within the `vizro-bootstrap.min.css` file, variables appear multiple times: initially from the default Bootstrap
values, followed by the dark theme, and lastly the light theme. We are interested in the final two occurrences,
as these represent the values for our dark and light themes.
"""
matches = re.findall(rf"{variable}:\s*([^;]+);", content)
matches = re.findall(rf"{variable}:\s*([^;]+);", css_content)
if len(matches) >= 2: # noqa: PLR2004
return matches[-2].strip(), matches[-1].strip()

return None, None


def extract_bs_variables_from_css_file(
variables: list[str], css_file_path: Path
def extract_bs_variables_from_css(
variables: list[str], css_content: str
) -> tuple[dict[str, Optional[str]], dict[str, Optional[str]]]:
"""Extract the last two occurrences for each variable in the CSS file."""
extracted_dark = {}
extracted_light = {}

css_content = css_file_path.read_text()

for variable in variables:
dark_value, light_value = _extract_last_two_occurrences(variable, css_content)
cleaned_variable = variable.replace("--", "").upper()
Expand Down Expand Up @@ -117,7 +115,7 @@ def generate_json_template(extracted_values: dict[str, Optional[str]]):


if __name__ == "__main__":
extracted_dark, extracted_light = extract_bs_variables_from_css_file(VARIABLES, CSS_FILE)
extracted_dark, extracted_light = extract_bs_variables_from_css(VARIABLES, CSS_PATH.read_text())
template_dark = generate_json_template(extracted_dark)
template_light = generate_json_template(extracted_light)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
from pathlib import Path
from unittest.mock import mock_open, patch

import pytest

from vizro._themes.generate_plotly_templates import _extract_last_two_occurrences, extract_bs_variables_from_css_file
from vizro._themes.generate_plotly_templates import _extract_last_two_occurrences, extract_bs_variables_from_css


@pytest.fixture
Expand Down Expand Up @@ -41,7 +38,6 @@ def test_extract_last_two_occurrences(variable, css_content, 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",
Expand All @@ -53,9 +49,9 @@ def test_extract_bs_variables_from_css_file(css_content):
"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)
result_dark, result_light = extract_bs_variables_from_css(
["--bs-primary", "--bs-secondary", "--bs-tertiary"], css_content
)

assert result_dark == expected_dark
assert result_light == expected_light

0 comments on commit 3152df2

Please sign in to comment.