158 lines
7.5 KiB
Python
158 lines
7.5 KiB
Python
"""Diagrammes à 7 j : types, choix automatique, échelles, axes du temps."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import itertools
|
|
import json
|
|
|
|
import pytest
|
|
|
|
from shortstats.web import chart_catalog, chart_config, charts, highlight
|
|
from shortstats.web.chart_catalog import FIELDS, ChartError
|
|
from shortstats.web.chart_config import choose_type, validate_config
|
|
from shortstats.web.charts import build
|
|
|
|
from ..outils.lignes import ALL_X, ALL_Y, fake_rows, multi_rows
|
|
|
|
|
|
@pytest.mark.parametrize("x,y", list(itertools.product(ALL_X, ALL_Y)))
|
|
def test_every_pair_builds_in_auto_mode(x, y):
|
|
cfg = validate_config({"x": x, "y": y})
|
|
out = build(cfg, fake_rows(cfg))
|
|
assert out["spec"] is not None
|
|
json.dumps(out["spec"]) # sérialisable
|
|
assert out["resolved"]["type"] in chart_catalog.CHART_TYPES
|
|
# le type automatique doit toujours être compatible avec les données
|
|
assert chart_config.incompatibility(out["resolved"]["type"], FIELDS[x].kind, chart_catalog.field(y).kind) is None
|
|
|
|
|
|
@pytest.mark.parametrize("x,y,ctype", [
|
|
(x, y, t) for x in ALL_X for y in ALL_Y for t in chart_catalog.CHART_TYPES if t != "auto"
|
|
])
|
|
def test_every_compatible_type_builds(x, y, ctype):
|
|
reason = chart_config.incompatibility(ctype, FIELDS[x].kind, chart_catalog.field(y).kind)
|
|
if reason:
|
|
with pytest.raises(ChartError):
|
|
validate_config({"x": x, "y": y, "type": ctype})
|
|
return
|
|
for color in (None, "channel", "views"):
|
|
cfg = validate_config({"x": x, "y": y, "type": ctype, "color": color})
|
|
out = build(cfg, fake_rows(cfg))
|
|
assert out["spec"]["mark"]
|
|
|
|
|
|
@pytest.mark.parametrize("xk,yk,median,expected", [
|
|
("Q", "Q", None, "scatter"),
|
|
("T", "Q", None, "line"),
|
|
("O", "Q", 10, "boxplot"),
|
|
("N", "Q", 2, "bar"),
|
|
("Q", "N", 8, "boxplot"),
|
|
("Q", "C", None, "histogram"),
|
|
("T", "C", None, "bar"),
|
|
("O", "C", None, "bar"),
|
|
("O", "N", None, "heatmap"),
|
|
("I", "Q", None, "bar"),
|
|
("T", "T", None, "scatter"),
|
|
])
|
|
def test_auto_choice(xk, yk, median, expected):
|
|
assert choose_type(xk, yk, median)[0] == expected
|
|
|
|
|
|
def test_log_scale_auto_for_wide_spread():
|
|
cfg = validate_config({"x": "views", "y": "likes"})
|
|
rows = [(f"v{i}", "t", 10 ** (i % 6), i + 1) for i in range(30)]
|
|
out = build(cfg, rows)
|
|
# Les vues sont un comptage : l'échelle logarithmique garde le 0 (symlog).
|
|
assert out["spec"]["encoding"]["x"]["scale"] == {"type": "symlog", "domainMin": 0, "domainMax": 100000}
|
|
assert out["spec"]["encoding"]["x"]["axis"]["values"] == [0, 1, 10, 100, 1000, 10000, 100000]
|
|
assert "x" in out["resolved"]["log_axes"]
|
|
|
|
|
|
def test_symlog_when_zero_present():
|
|
cfg = validate_config({"x": "views", "y": "comments", "scale": "log"})
|
|
rows = [(f"v{i}", "t", 100 * (i + 1), i) for i in range(10)] # y contient 0
|
|
out = build(cfg, rows)
|
|
assert out["spec"]["encoding"]["y"]["scale"]["type"] == "symlog"
|
|
|
|
|
|
def test_bar_never_log():
|
|
cfg = validate_config({"x": "weekday", "y": "views", "type": "bar", "scale": "log"})
|
|
out = build(cfg, fake_rows(cfg))
|
|
assert out["spec"]["encoding"]["y"]["scale"] == {"zero": True}
|
|
assert "non applicable" in out["explanation"]
|
|
|
|
|
|
def test_titles_limited_to_top():
|
|
cfg = validate_config({"x": "title", "y": "views"})
|
|
out = build(cfg, fake_rows(cfg, n=80))
|
|
values = out["spec"]["data"]["values"]
|
|
assert len(values) == chart_catalog.TOP_TITLES
|
|
assert values[0]["y"] == max(v["y"] for v in values)
|
|
|
|
|
|
def test_nulls_excluded_and_counted():
|
|
cfg = validate_config({"x": "views", "y": "likes"})
|
|
rows = [("a", "t", 10, None), ("b", "t", 20, 3), ("c", "t", None, 4), ("d", "t", 5, 1)]
|
|
out = build(cfg, rows)
|
|
assert out["n"] == 2 and out["excluded"] == 2
|
|
|
|
|
|
def test_empty_data():
|
|
out = build(validate_config({"x": "views", "y": "likes"}), [])
|
|
assert out["spec"] is None and "Aucune" in out["explanation"]
|
|
|
|
|
|
def test_weekday_sorted_monday_first():
|
|
cfg = validate_config({"x": "weekday", "y": "views", "type": "bar"})
|
|
out = build(cfg, fake_rows(cfg))
|
|
assert out["spec"]["encoding"]["x"]["sort"][0] == "Lundi"
|
|
|
|
|
|
def test_publish_date_groups_on_time_axis():
|
|
for ctype in ("bar", "heatmap"):
|
|
cfg = chart_config.validate_config({"x": "publish_date", "y": "views" if ctype != "heatmap" else "weekday",
|
|
"type": ctype, "split_channels": False})
|
|
x = charts.build(cfg, fake_rows(cfg, 80))["spec"]["encoding"]["x"]
|
|
assert x["type"] == "temporal" and x["timeUnit"]["unit"] in {"yearmonthdate", "yearweek", "yearmonth"}
|
|
assert x["scale"] == {"type": "utc"}, ctype
|
|
|
|
|
|
def test_line_over_date_bars_is_centered():
|
|
cfg = chart_config.validate_config({"x": "publish_date", "y": "views", "y_more": ["likes"], "type": "bar",
|
|
"type2": "line", "split_channels": False})
|
|
bars, line = charts.build(cfg, multi_rows(cfg), None, None)["spec"]["layer"]
|
|
assert "bandPosition" not in bars["encoding"]["x"] and line["encoding"]["x"]["bandPosition"] == 0.5
|
|
|
|
|
|
def test_every_hour_and_day_on_the_axis():
|
|
for x, domain in (("publish_hour", list(range(24))), ("weekday", chart_catalog.WEEKDAYS)):
|
|
for ctype in ("bar", "line", "boxplot", "scatter"):
|
|
cfg = chart_config.validate_config({"x": x, "y": "views", "type": ctype, "split_channels": False})
|
|
rows = [r for r in fake_rows(cfg) if r[2] not in (3, "Mercredi")] # une heure, un jour vides
|
|
assert charts.build(cfg, rows)["spec"]["encoding"]["x"]["scale"]["domain"] == domain, (x, ctype)
|
|
cfg = chart_config.validate_config({"x": "views", "y": "publish_hour", "type": "bar", "split_channels": False})
|
|
assert charts.build(cfg, fake_rows(cfg))["spec"]["encoding"]["y"]["scale"]["domain"] == list(range(24))
|
|
cfg = chart_config.validate_config({"x": "publish_hour", "y": "weekday", "type": "heatmap", "split_channels": False})
|
|
enc = charts.build(cfg, fake_rows(cfg))["spec"]["encoding"]
|
|
assert enc["x"]["scale"]["domain"] == list(range(24)) and enc["y"]["scale"]["domain"] == chart_catalog.WEEKDAYS
|
|
|
|
|
|
def test_count_line_drops_to_zero_on_empty_hours():
|
|
cfg = chart_config.validate_config({"x": "publish_hour", "y": "count", "type": "line", "color": "weekday",
|
|
"split_channels": False})
|
|
spec = charts.build(cfg, fake_rows(cfg))["spec"]
|
|
count, impute = spec["transform"]
|
|
assert count == {"aggregate": [{"op": "count", "as": "n_videos"}], "groupby": ["x", "c"]}
|
|
assert impute == {"impute": "n_videos", "key": "x", "keyvals": list(range(24)), "groupby": ["c"], "value": 0}
|
|
assert spec["encoding"]["y"]["field"] == "n_videos" and "aggregate" not in spec["encoding"]["y"]
|
|
# mesure (vues) : pas de comptage, une heure vide reste sans valeur
|
|
cfg = chart_config.validate_config({"x": "publish_hour", "y": "views", "type": "line", "split_channels": False})
|
|
assert "transform" not in charts.build(cfg, fake_rows(cfg))["spec"]
|
|
# vidéo mise en avant sur un comptage : un trait à son heure, filtrée avant le comptage
|
|
cfg = chart_config.validate_config({"x": "weekday", "y": "count", "type": "line", "split_channels": False})
|
|
res = charts.build(cfg, fake_rows(cfg))
|
|
highlight.decorate(res, [{"id": "vid00000001", "t": "Short n°1", "x": "Mardi", "y": 1, "hl": 1, "hn": "S"}],
|
|
"point", videos=[{"label": "S", "shape": "diamond", "dash": "solid"}])
|
|
main, hl = res["spec"]["layer"]
|
|
assert main["transform"][0] == {"filter": highlight.MAIN} and "impute" in main["transform"][2]
|
|
assert hl["mark"]["type"] == "rule" and "y" not in hl["encoding"]
|