shortstats/tests/unitaires/test_dimensions.py
ploush 81ef12a9cd
Some checks failed
build / test (push) Failing after 2m48s
build / images (collector) (push) Has been skipped
build / images (web) (push) Has been skipped
upgrade 1.4.0
2026-11-29 20:43:50 +01:00

51 lines
2.5 KiB
Python

"""Dimensions des diagrammes définies une seule fois (chart_layout.py) : la spec les
utilise, et la page les reçoit pour réserver la place des axes et de la légende."""
from __future__ import annotations
import html
import json
import re
from shortstats.web import chart_config, chart_layout, charts
from shortstats.web.templating import HERE, make_templates
from ..outils.lignes import fake_rows, multi_rows
RENDER = (HERE / "static" / "js" / "render.js").read_text()
def test_page_receives_the_layout():
base = (HERE / "templates" / "base.html").read_text()
assert '<body data-layout="{{ layout_json }}">' in base
t = make_templates("/static/x")
attr = html.escape(t.env.globals["layout_json"], quote=True) # comme Jinja l'écrit dans l'attribut
assert json.loads(html.unescape(attr)) == chart_layout.page_layout()
layout = chart_layout.page_layout()
assert layout["facet"]["legend"] == chart_layout.LEGEND_LABEL_LIMIT + chart_layout.LEGEND_SYMBOL
assert layout["facet"]["axis_left_categories"] > chart_layout.AXIS_LABEL_LIMIT
def test_render_reads_every_dimension_from_the_layout():
assert "const LAYOUT = JSON.parse(document.body.dataset.layout);" in RENDER
assert "labelLimit: LAYOUT.axis_label_limit" in RENDER and "labelLimit: LAYOUT.legend_label_limit" in RENDER
used = set(re.findall(r"\b(?:LAYOUT|F|P)\.(\w+)", RENDER))
layout = chart_layout.page_layout()
known = set(layout) | set(layout["facet"]) | set(layout["fill"])
assert used <= known, used - known
# plus aucune de ces dimensions écrite en dur dans le dessin
code = "\n".join(line for line in RENDER.splitlines() if not line.strip().startswith("//"))
for value in (220, 200, 240, 250, 320, 520):
assert not re.search(rf"[^\w.]{value}\b(?!\))", code.replace("}, 200);", "")), value
def test_specs_use_the_same_dimensions():
cfg = chart_config.validate_config({"x": "publish_date", "y": "views", "split_channels": False})
spec = charts.build(cfg, fake_rows(cfg))["spec"]
assert spec["height"] == chart_layout.DEFAULT_HEIGHT and spec["width"] == "container"
cfg = chart_config.validate_config({"x": "views", "y": "likes", "type": "scatter", "split_channels": True})
rows = multi_rows(cfg, names=[f"Chaîne {i}" for i in range(10)])
out = charts.build(cfg, rows, [f"Chaîne {i}" for i in range(10)])
assert "facet" in out["spec"] # 10 chaînes : un petit diagramme par chaîne
assert out["spec"]["spec"]["width"] == chart_layout.FACET_WIDTH
assert out["spec"]["spacing"] == chart_layout.FACET_SPACING