Skip to content

Commit 666bd99

Browse files
committed
Fix tests given new import.
1 parent 0e315dd commit 666bd99

File tree

2 files changed

+35
-31
lines changed

2 files changed

+35
-31
lines changed

src/py/tests/test_page_generator.py

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -175,32 +175,38 @@ def st_mathjax(dir_path: Path):
175175
# Test default combinations
176176
@pytest.mark.order(1)
177177
async def test_defaults_no_plotly_available():
178-
"""Test defaults when plotly package is not available."""
179-
if not find_spec("plotly"):
180-
raise ImportError("Tests must be run with plotly installed to function")
181-
182-
old_path = sys.path
183-
sys.path = sys.path[:1]
184-
if find_spec("plotly"):
185-
raise RuntimeError(
186-
"Plotly cannot be imported during this test, "
187-
"as this tests default behavior while trying to import plotly. "
188-
"The best solution is to make sure this test always runs first, "
189-
"or if you really need to, run it separately and then skip it "
190-
"in the main group.",
191-
)
192-
193-
# Test no imports (plotly not available)
194-
no_imports = PageGenerator().generate_index()
195-
scripts, _encodings = get_scripts_from_html(no_imports)
196-
197-
# Should have mathjax, plotly default, and kaleido_scopes
198-
assert len(scripts) == 3 # noqa: PLR2004
199-
assert scripts[0] == DEFAULT_MATHJAX
200-
assert scripts[1] == DEFAULT_PLOTLY
201-
assert scripts[2].endswith("kaleido_scopes.js")
178+
"""
179+
Test defaults when plotly package is not available.
202180
203-
sys.path = old_path
181+
When we generate_index(), if we don't have plotly in path, we use a CDN.
182+
"""
183+
_old_path = sys.path
184+
try:
185+
sys.path = []
186+
_plotly_mo = sys.modules.pop("plotly", None)
187+
188+
if find_spec("plotly"):
189+
raise RuntimeError(
190+
"Plotly cannot be imported during this test, "
191+
"as this tests default behavior while trying to import plotly. "
192+
"The best solution is to make sure this test always runs first, "
193+
"or if you really need to, run it separately and then skip it "
194+
"in the main group.",
195+
)
196+
197+
# Test no imports (plotly not available)
198+
no_imports = PageGenerator().generate_index()
199+
scripts, _encodings = get_scripts_from_html(no_imports)
200+
201+
# Should have mathjax, plotly default, and kaleido_scopes
202+
assert len(scripts) == 3 # noqa: PLR2004
203+
assert scripts[0] == DEFAULT_MATHJAX
204+
assert scripts[1] == DEFAULT_PLOTLY
205+
assert scripts[2].endswith("kaleido_scopes.js")
206+
finally:
207+
sys.path = _old_path
208+
if _plotly_mo:
209+
sys.modules.update({"plotly": _plotly_mo})
204210

205211

206212
async def test_defaults_with_plotly_available():

src/py/uv.lock

Lines changed: 4 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)