Skip to content

Commit 043b11d

Browse files
committed
Use more structured approach to testing
Why the old assertions were brittle: - They asserted giant exact HTML blobs (including whitespace, token spans, and exact Vega-Lite schema URL string). - Those details are affected by upstream tooling changes: - Sphinx/Pygments changed tokenized output (<span class="w"> spacing, etc.) - Altair schema string changed from v5.20.1 to v6.1.0 - So tests failed even though core behavior (plot rendered, options respected) was still correct. New approach: 1. Parse HTML and assert DOM structure (order/sections/classes) instead of raw substrings. 2. Regex-match the embedded spec/actions blocks (strict on keys/values, loose on whitespace/order). 3. Keep one small “golden” snippet for critical template output, not entire page chunks.
1 parent f9bbe0e commit 043b11d

File tree

1 file changed

+44
-11
lines changed

1 file changed

+44
-11
lines changed

tests/test_altairplot.py

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Tests are inspired by the test suite of sphinx itself
22
from __future__ import annotations
33

4+
import json
45
import re
56
from typing import TYPE_CHECKING, cast
67

@@ -79,27 +80,59 @@ def test_altairplotdirective(app: Sphinx) -> None:
7980
assert result.count(VEGA_JS_URL_DEFAULT)
8081
assert SCHEMA_URL in result
8182

83+
def extract_embed_values(
84+
plot_id: int,
85+
) -> tuple[dict[str, object], dict[str, object], str | None]:
86+
div_id = f"index-rst-altair-plot-{plot_id}"
87+
match = re.search(
88+
rf'<div id="{div_id}"(?: class="([^"]+)")?>\s*<script>.*?'
89+
rf"var spec = (\{{.*?\}});\s*"
90+
rf"var opt = (\{{.*?\}});\s*"
91+
rf"vegaEmbed\('#{div_id}', spec, opt\)",
92+
result,
93+
re.DOTALL,
94+
)
95+
assert match is not None
96+
class_name = match.group(1)
97+
spec = json.loads(match.group(2))
98+
opt = json.loads(match.group(3))
99+
return spec, opt, class_name
100+
101+
for plot_id in (1, 2, 4, 5, 6, 7):
102+
spec, opt, _ = extract_embed_values(plot_id)
103+
assert spec["$schema"] == SCHEMA_URL
104+
assert opt["mode"] == "vega-lite"
105+
assert opt["renderer"] == "canvas"
106+
82107
assert 'id="index-rst-altair-source-0"' in result
83108
assert '<div id="index-rst-altair-plot-0"' not in result
84109

85110
assert 'id="index-rst-altair-source-1"' in result
86-
assert 'id="index-rst-altair-plot-1"' in result
87-
assert '"actions": {"editor": true, "source": true, "export": true}' in result
88-
89-
assert '<div id="index-rst-altair-plot-2">' in result
90-
assert '</div><div class="highlight-python notranslate">' in result
111+
_, plot_1_opt, _ = extract_embed_values(1)
112+
assert plot_1_opt["actions"] == {"editor": True, "source": True, "export": True}
113+
114+
code_below_section = re.search(
115+
r'<section id="code-below-plot">.*?</section>', result, re.DOTALL
116+
)
117+
assert code_below_section is not None
118+
assert re.search(
119+
r'<div id="index-rst-altair-plot-2">.*?</div><div class="highlight-python notranslate">',
120+
code_below_section.group(0),
121+
re.DOTALL,
122+
)
91123

92124
assert 'id="index-rst-altair-source-3"' in result
93125
assert "Data({" in result
94126

95-
assert '<div id="index-rst-altair-plot-4"' in result
127+
extract_embed_values(4)
96128
assert 'id="index-rst-altair-source-4"' not in result
97129

98130
assert "Click to show code" in result
99-
assert '<div id="index-rst-altair-plot-5"' in result
131+
assert re.search(r"<details>.*?Click to show code.*?</details>", result, re.DOTALL)
132+
extract_embed_values(5)
100133

101-
assert '<div id="index-rst-altair-plot-6"' in result
102-
assert '"actions": {"editor": true, "source": false, "export": false}' in result
134+
_, plot_6_opt, _ = extract_embed_values(6)
135+
assert plot_6_opt["actions"] == {"editor": True, "source": False, "export": False}
103136

104-
assert result.count('class="test-class"') == 1
105-
assert '<div id="index-rst-altair-plot-7" class="test-class">' in result
137+
_, _, plot_7_class = extract_embed_values(7)
138+
assert plot_7_class == "test-class"

0 commit comments

Comments
 (0)