155 lines
8.2 KiB
Python
155 lines
8.2 KiB
Python
"""Plusieurs ordonnées, deux axes, un type par catégorie."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
|
|
import pytest
|
|
|
|
from shortstats.web import chart_catalog, chart_config, chart_view, charts, multi
|
|
|
|
from ..outils.lignes import multi_rows
|
|
from ..outils.specs import DUMP, _dumped, body_of, layer_of
|
|
|
|
TYPES = ("line", "bar", "scatter")
|
|
|
|
SPEC_X = ["publish_date", "views", "weekday", "theme", "publish_hour", "title", "channel"]
|
|
|
|
THEME_YS = ["theme_rate@56", "theme_rate@6"]
|
|
|
|
|
|
@pytest.mark.parametrize("x,ys,ctype,individual,split", [
|
|
(x, ys, t, ind, sp)
|
|
for x in SPEC_X
|
|
for ys in ((["views@6", "views@8"], ["views@6", "views@8", "likes@6"], ["views", "duration"]) if x != "theme"
|
|
else (THEME_YS, THEME_YS + ["theme_comments@56"]))
|
|
for t in ("auto", "scatter", "line", "bar")
|
|
for ind in (False, True) for sp in (False, True)
|
|
if chart_config.multi_incompatibility(t, chart_catalog.field(x).kind) is None or t == "auto"
|
|
])
|
|
def test_every_multi_combination_builds(x, ys, ctype, individual, split):
|
|
cfg = chart_config.validate_config({"x": x, "y": ys[0], "y_more": ys[1:], "type": ctype, "split_channels": split})
|
|
out = charts.build(cfg, multi_rows(cfg), ["Chaîne A", "Chaîne B"], chart_view.View(individual=individual))
|
|
spec = out["spec"]
|
|
assert spec is not None
|
|
body = spec.get("spec", spec)
|
|
cats = {chart_catalog.y_category(k) for k in ys}
|
|
assert len(body["layer"]) == len(cats)
|
|
if len(cats) == 2:
|
|
assert body["resolve"] == {"scale": {"y": "independent"}}
|
|
assert body["layer"][1]["encoding"]["y"]["axis"]["orient"] == "right"
|
|
assert "axe de droite" in out["explanation"]
|
|
assert ("facet" in spec) == (split and x != "channel")
|
|
assert {r["s"] for r in spec["data"]["values"]} <= {chart_catalog.field(k).label for k in ys}
|
|
json.dumps(spec)
|
|
if DUMP:
|
|
_dumped.append({"name": f"{x}/{ys}/{ctype}/{individual}/{split}", "spec": spec})
|
|
|
|
|
|
def test_series_keep_their_own_values():
|
|
cfg = chart_config.validate_config({"x": "publish_date", "y": "views@6", "y_more": ["likes@6"]})
|
|
rows = [("v1", "A", "2026-10-01", 100, "Ch", None), ("v2", "B", "2026-10-02", None, "Ch", 7),
|
|
("v3", "C", "2026-10-03", None, "Ch", None)]
|
|
out = charts.build(cfg, rows, ["Ch"])
|
|
recs = out["spec"]["data"]["values"]
|
|
assert [(r["id"], r["s"], r["y"]) for r in recs] == [("v1", "Vues à 6 h", 100), ("v2", "Likes à 6 h", 7)]
|
|
assert out["n"] == 2 and out["excluded"] == 1
|
|
|
|
|
|
@pytest.mark.parametrize("x,t1,t2,individual,split", [
|
|
(x, t1, t2, ind, sp)
|
|
for x in SPEC_X if x != "theme"
|
|
for t1 in ("auto", *TYPES) for t2 in TYPES
|
|
for ind in (False, True) for sp in (False, True)
|
|
if (t1 == "auto" or chart_config.multi_incompatibility(t1, chart_catalog.field(x).kind) is None)
|
|
and chart_config.multi_incompatibility(t2, chart_catalog.field(x).kind) is None
|
|
])
|
|
def test_every_mixed_combination_builds(x, t1, t2, individual, split):
|
|
cfg = chart_config.validate_config({"x": x, "y": "views@6", "y_more": ["views@8", "likes@6"], "type": t1,
|
|
"type2": t2, "split_channels": split})
|
|
out = charts.build(cfg, multi_rows(cfg), ["Chaîne A", "Chaîne B"], chart_view.View(individual=individual))
|
|
spec = out["spec"]
|
|
body = body_of(spec)
|
|
types = out["resolved"]["types"]
|
|
if not individual or not out["resolved"]["individual_available"]:
|
|
assert types[1] == t2
|
|
marks = {"line": "line", "bar": "bar", "scatter": "point"}
|
|
for k in (0, 1):
|
|
layer = layer_of(body, k)
|
|
assert layer["mark"]["type"] == marks[types[k]]
|
|
y = layer["encoding"]["y"]
|
|
assert ("aggregate" in y) == (types[k] in {"line", "bar"})
|
|
assert y["axis"]["orient"] == ("left" if k == 0 else "right")
|
|
assert layer_of(body, 1)["encoding"]["y"]["axis"]["titleAngle"] == 270
|
|
# barres sous les autres marques ; seules les séries en barres se partagent un groupe
|
|
kinds = [layer["mark"]["type"] for layer in body["layer"]]
|
|
assert kinds == sorted(kinds, key=lambda t: t != "bar")
|
|
bar_series = [s for k, t in enumerate(types) if t == "bar"
|
|
for s in (["Vues à 6 h", "Vues à 8 h"] if k == 0 else ["Likes à 6 h"])]
|
|
for layer in body["layer"]:
|
|
offset = layer["encoding"].get("xOffset")
|
|
if layer["mark"]["type"] == "bar" and len(bar_series) > 1:
|
|
assert offset["scale"]["domain"] == bar_series
|
|
else:
|
|
assert offset is None
|
|
# curseur vertical sur chaque axe ; barres : l'axe part de 0, seul le haut se règle
|
|
assert [(a["index"], bool(a.get("fixed_min"))) for a in out["resolved"]["y_axes"]] == \
|
|
[(k, t == "bar") for k, t in enumerate(types)]
|
|
assert "axe de droite" in out["explanation"]
|
|
json.dumps(spec)
|
|
if DUMP:
|
|
_dumped.append({"name": f"{x}/{t1}/{t2}/{individual}/{split}", "spec": spec})
|
|
|
|
|
|
def test_bar_and_line_by_date():
|
|
cfg = chart_config.validate_config({"x": "publish_date", "y": "views@6", "y_more": ["likes@6"], "type": "bar",
|
|
"type2": "line", "split_channels": False})
|
|
out = charts.build(cfg, multi_rows(cfg), None, None)
|
|
bars, line = out["spec"]["layer"]
|
|
assert bars["mark"]["type"] == "bar" and line["mark"]["type"] == "line"
|
|
# même abscisse (groupes par période, axe du temps : 1.3.2) : la courbe passe au milieu de chaque barre
|
|
assert bars["encoding"]["x"]["type"] == "temporal" and "timeUnit" in bars["encoding"]["x"]
|
|
assert line["encoding"]["x"] == {**bars["encoding"]["x"], "bandPosition": 0.5}
|
|
assert "xOffset" not in bars["encoding"] # une seule série en barres : toute la largeur
|
|
assert "axe de gauche : vues (barres) ; axe de droite : likes (courbe en pointillés)" in out["explanation"]
|
|
assert out["resolved"]["types"] == ["bar", "line"] and out["resolved"]["agg"] == "mean"
|
|
|
|
|
|
def test_line_left_bar_right_draws_bars_first():
|
|
cfg = chart_config.validate_config({"x": "weekday", "y": "views@6", "y_more": ["likes@6"], "type": "line",
|
|
"type2": "bar", "split_channels": False})
|
|
body = charts.build(cfg, multi_rows(cfg), None, None)["spec"]
|
|
assert [layer["mark"]["type"] for layer in body["layer"]] == ["bar", "line"]
|
|
bars = layer_of(body, 1)
|
|
assert bars["encoding"]["y"]["axis"] == multi.RIGHT_AXIS # axe de droite malgré l'ordre de dessin
|
|
assert layer_of(body, 0)["encoding"]["y"]["axis"] == {"orient": "left"}
|
|
|
|
|
|
def test_scatter_keeps_exact_dates_next_to_a_line():
|
|
cfg = chart_config.validate_config({"x": "publish_date", "y": "views@6", "y_more": ["likes@6"], "type": "line",
|
|
"type2": "scatter", "split_channels": False})
|
|
out = charts.build(cfg, multi_rows(cfg), None, None)
|
|
line, points = layer_of(out["spec"], 0), layer_of(out["spec"], 1)
|
|
assert "timeUnit" in line["encoding"]["x"] and "timeUnit" not in points["encoding"]["x"]
|
|
assert points["encoding"]["tooltip"][0] == {"field": "t", "title": "Titre"}
|
|
assert "tooltip" not in line["encoding"] and "aggregate" not in points["encoding"]["y"]
|
|
assert "axe de gauche : vues (courbe) ; axe de droite : likes (carrés)" in out["explanation"]
|
|
|
|
|
|
def test_individual_turns_aggregates_into_points():
|
|
cfg = chart_config.validate_config({"x": "weekday", "y": "views@6", "y_more": ["likes@6"], "type": "bar",
|
|
"type2": "line", "split_channels": False})
|
|
out = charts.build(cfg, multi_rows(cfg), None, chart_view.View(individual=True))
|
|
assert out["resolved"]["types"] == ["scatter", "scatter"] and out["resolved"]["individual_available"]
|
|
assert "chaque vidéo" in out["explanation"]
|
|
|
|
|
|
def test_same_type_unchanged():
|
|
"""Sans type propre à droite : même rendu qu'en 1.2.2 (titre de droite mis à part)."""
|
|
cfg = chart_config.validate_config({"x": "publish_date", "y": "views@6", "y_more": ["likes@6"], "type": "line",
|
|
"split_channels": False})
|
|
out = charts.build(cfg, multi_rows(cfg), None, None)
|
|
assert [layer["mark"]["type"] for layer in out["spec"]["layer"]] == ["line", "line"]
|
|
assert out["spec"]["layer"][1]["mark"]["strokeDash"] == [6, 4]
|
|
assert "axe de droite : likes (pointillés), échelles différentes" in out["explanation"]
|
|
assert out["resolved"]["types"] == ["line", "line"]
|