Skip to content

Commit 673d75c

Browse files
committed
fix: Fix failing unit tests in CI
- Add missing test schemes (scheme-1, scheme-2, scheme-3, scheme-4, viridis, coolwarm) to test_config.yaml - Fix status() function to include available_adapters field for backward compatibility - Fix apply_to_figure() auto-detection to check figure object type instead of global library state - Add conftest.py to ensure test configuration is loaded and persistent across all tests - Fixes 11 failing tests in CI
1 parent baa5515 commit 673d75c

File tree

3 files changed

+175
-5
lines changed

3 files changed

+175
-5
lines changed

huez/core.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -539,21 +539,31 @@ def apply_to_figure(fig, library: str = "auto"):
539539
fig = hz.apply_to_figure(fig, "matplotlib")
540540
"""
541541
if library == "auto":
542-
library = _detect_active_library()
542+
# Detect library from figure object type
543+
fig_type = type(fig).__module__
544+
if "plotly" in fig_type:
545+
library = "plotly"
546+
elif "altair" in fig_type:
547+
library = "altair"
548+
elif "matplotlib" in fig_type:
549+
library = "matplotlib"
550+
else:
551+
# Fallback to detecting active library
552+
library = _detect_active_library()
543553

544554
if library == "plotly":
545555
try:
546556
from .adapters.plotly import plotly_auto_colors
547557

548558
return plotly_auto_colors(fig)
549-
except ImportError:
559+
except (ImportError, AttributeError):
550560
pass
551561
elif library == "altair":
552562
try:
553563
from .adapters.altair import altair_auto_color
554564

555565
return altair_auto_color(fig)
556-
except ImportError:
566+
except (ImportError, AttributeError):
557567
pass
558568

559569
# For matplotlib/seaborn, colors are already applied via rcParams
@@ -572,9 +582,11 @@ def status() -> Dict[str, Any]:
572582
"""
573583
from .adapters.base import get_adapter_status
574584

585+
adapter_status = get_adapter_status()
575586
return {
576587
"current_scheme": current_scheme(),
577-
"available_libraries": get_adapter_status(),
588+
"available_libraries": adapter_status,
589+
"available_adapters": adapter_status, # Alias for backward compatibility
578590
"config_loaded": _current_config is not None,
579591
"total_schemes": len(_current_config.schemes) if _current_config else 0,
580592
}

tests/conftest.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""
2+
Pytest configuration and fixtures for huez tests.
3+
"""
4+
import os
5+
import pytest
6+
7+
8+
# Module-level config to ensure persistence
9+
_test_config = None
10+
11+
12+
def pytest_configure(config):
13+
"""Configure pytest with test config at startup."""
14+
import huez.core as core
15+
global _test_config
16+
17+
# Get path to test config
18+
test_dir = os.path.dirname(__file__)
19+
test_config_path = os.path.join(test_dir, "test_config.yaml")
20+
21+
# Load test configuration once
22+
if os.path.exists(test_config_path):
23+
_test_config = core.load_config(test_config_path)
24+
core._current_config = _test_config
25+
26+
27+
@pytest.fixture(autouse=True)
28+
def ensure_test_config():
29+
"""Ensure test configuration is loaded for each test."""
30+
import huez.core as core
31+
import matplotlib.pyplot as plt
32+
33+
# Save original state
34+
original_config = core._current_config
35+
original_scheme = core._current_scheme
36+
37+
# Ensure config is loaded
38+
if _test_config is not None:
39+
core._current_config = _test_config
40+
41+
yield
42+
43+
# Restore test config if it was changed (but keep scheme reset)
44+
if _test_config is not None:
45+
core._current_config = _test_config
46+
47+
# Close any matplotlib figures after each test
48+
plt.close('all')
49+

tests/test_config.yaml

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,83 @@
11
# Huez Test Configuration
22
# This file contains test schemes for unit testing
33
version: 1
4-
default_scheme: "test-scheme"
4+
default_scheme: "scheme-1"
55

66
schemes:
7+
# Main numbered schemes (matching defaults.py structure)
8+
scheme-1:
9+
title: "Test Scheme 1"
10+
fonts:
11+
family: "Arial"
12+
size: 10
13+
palettes:
14+
discrete: "npg"
15+
sequential: "viridis"
16+
diverging: "vik"
17+
cyclic: "twilight"
18+
figure:
19+
size: [3.5, 2.5]
20+
dpi: 150
21+
style:
22+
grid: "y"
23+
legend_loc: "best"
24+
spine_top_right_off: true
25+
26+
scheme-2:
27+
title: "Test Scheme 2"
28+
fonts:
29+
family: "DejaVu Sans"
30+
size: 12
31+
palettes:
32+
discrete: "aaas"
33+
sequential: "plasma"
34+
diverging: "coolwarm"
35+
cyclic: "twilight"
36+
figure:
37+
size: [4.0, 3.0]
38+
dpi: 300
39+
style:
40+
grid: "both"
41+
legend_loc: "upper right"
42+
spine_top_right_off: false
43+
44+
scheme-3:
45+
title: "Test Scheme 3"
46+
fonts:
47+
family: "sans-serif"
48+
size: 9
49+
palettes:
50+
discrete: "nejm"
51+
sequential: "cividis"
52+
diverging: "RdBu"
53+
cyclic: "twilight"
54+
figure:
55+
size: [3.5, 2.5]
56+
dpi: 300
57+
style:
58+
grid: "y"
59+
legend_loc: "best"
60+
spine_top_right_off: true
61+
62+
scheme-4:
63+
title: "Test Scheme 4"
64+
fonts:
65+
family: "DejaVu Sans"
66+
size: 10
67+
palettes:
68+
discrete: "lancet"
69+
sequential: "viridis"
70+
diverging: "coolwarm"
71+
cyclic: "twilight"
72+
figure:
73+
size: [4.0, 3.0]
74+
dpi: 150
75+
style:
76+
grid: "y"
77+
legend_loc: "best"
78+
spine_top_right_off: true
79+
80+
# Legacy test schemes (for backward compatibility)
781
test-scheme:
882
title: "Test Color Scheme"
983
fonts:
@@ -54,3 +128,38 @@ schemes:
54128
grid: "none"
55129
legend_loc: "center"
56130
spine_top_right_off: true
131+
132+
# Additional schemes for specific tests
133+
viridis:
134+
title: "Viridis Test Scheme"
135+
fonts:
136+
family: "Arial"
137+
size: 10
138+
palettes:
139+
discrete: "viridis"
140+
sequential: "viridis"
141+
diverging: "coolwarm"
142+
cyclic: "twilight"
143+
figure:
144+
dpi: 150
145+
style:
146+
grid: "y"
147+
legend_loc: "best"
148+
spine_top_right_off: true
149+
150+
coolwarm:
151+
title: "Coolwarm Test Scheme"
152+
fonts:
153+
family: "Arial"
154+
size: 10
155+
palettes:
156+
discrete: "okabe-ito"
157+
sequential: "viridis"
158+
diverging: "coolwarm"
159+
cyclic: "twilight"
160+
figure:
161+
dpi: 150
162+
style:
163+
grid: "y"
164+
legend_loc: "best"
165+
spine_top_right_off: true

0 commit comments

Comments
 (0)