112 lines
4.5 KiB
Python
112 lines
4.5 KiB
Python
"""Séries par chaîne : couleurs, petits diagrammes, sans découpage."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import itertools
|
|
import json
|
|
|
|
import pytest
|
|
|
|
from shortstats.web import chart_catalog, chart_config
|
|
from shortstats.web.chart_catalog import FIELDS
|
|
from shortstats.web.chart_config import validate_config
|
|
from shortstats.web.charts import build
|
|
|
|
from ..outils.lignes import ALL_X, ALL_Y, A, fake_rows
|
|
|
|
NAMES = ["Chaîne principale", "Chaîne secondaire", "Troisième"]
|
|
|
|
|
|
def rows_with_channels(cfg, n=42, names=NAMES[:2]):
|
|
return [row + (names[i % len(names)],) for i, row in enumerate(fake_rows(cfg, n))]
|
|
|
|
|
|
@pytest.mark.parametrize("ctype", ["scatter", "line", "bar", "histogram"])
|
|
def test_color_split_when_color_is_free(ctype):
|
|
x, y = {"scatter": ("views", "likes"), "line": ("publish_date", "views"),
|
|
"bar": ("weekday", "views"), "histogram": ("views", "count")}[ctype]
|
|
cfg = validate_config({"x": x, "y": y, "type": ctype})
|
|
out = build(cfg, rows_with_channels(cfg), NAMES)
|
|
enc = out["spec"]["encoding"]
|
|
assert out["resolved"]["split"] == "color"
|
|
assert enc["color"]["field"] == "c" and enc["color"]["title"] == "Chaîne"
|
|
assert enc["color"]["scale"]["domain"] == NAMES
|
|
assert enc["color"]["legend"]["values"] == NAMES[:2] # la 3e chaîne n'a pas de données ici
|
|
assert {r["c"] for r in out["spec"]["data"]["values"]} == set(NAMES[:2])
|
|
if ctype == "bar":
|
|
assert enc["xOffset"]["field"] == "c" # barres groupées par chaîne
|
|
assert "une couleur par chaîne" in out["explanation"]
|
|
|
|
|
|
@pytest.mark.parametrize("raw", [
|
|
{"x": "weekday", "y": "views", "type": "boxplot"},
|
|
{"x": "weekday", "y": "publish_hour", "type": "heatmap"},
|
|
{"x": "views", "y": "likes", "type": "scatter", "color": "weekday"},
|
|
{"x": "weekday", "y": "views", "type": "bar", "color": "publish_hour"},
|
|
{"x": "views@56", "y": "theme_rate@56", "type": "scatter"}, # couleur : les thèmes
|
|
{"x": "weekday", "y": "theme_rate@56", "type": "bar"},
|
|
])
|
|
def test_facet_split_when_color_taken_or_unsupported(raw):
|
|
cfg = validate_config(raw)
|
|
out = build(cfg, rows_with_channels(cfg), NAMES)
|
|
spec = out["spec"]
|
|
assert out["resolved"]["split"] == "facet"
|
|
assert spec["facet"]["row"]["field"] == "ch"
|
|
assert isinstance(spec["spec"]["width"], int) and "autosize" not in spec
|
|
assert "un diagramme par chaîne" in out["explanation"]
|
|
|
|
|
|
@pytest.mark.parametrize("raw", [
|
|
{"x": "views", "y": "likes", "split_channels": False},
|
|
{"x": "channel", "y": "views"},
|
|
{"x": "views", "y": "likes", "color": "channel"},
|
|
])
|
|
def test_no_split(raw):
|
|
cfg = validate_config(raw)
|
|
out = build(cfg, rows_with_channels(cfg), NAMES)
|
|
assert out["resolved"]["split"] is None and "facet" not in out["spec"]
|
|
|
|
|
|
def test_single_channel_does_not_split():
|
|
cfg = validate_config({"x": "views", "y": "likes", "channels": [A]})
|
|
out = build(cfg, rows_with_channels(cfg, names=NAMES[:1]), NAMES)
|
|
assert out["resolved"]["split"] is None and "color" not in out["spec"]["encoding"]
|
|
assert "1 chaîne(s) sélectionnée(s)" in out["explanation"]
|
|
|
|
|
|
def test_many_channels_use_small_multiples():
|
|
names = [f"Chaîne {i}" for i in range(9)]
|
|
cfg = validate_config({"x": "views", "y": "likes"})
|
|
out = build(cfg, rows_with_channels(cfg, n=45, names=names), names)
|
|
assert out["resolved"]["split"] == "facet"
|
|
|
|
|
|
def test_titles_ranking_is_colored_not_grouped():
|
|
cfg = validate_config({"x": "title", "y": "views", "type": "bar"})
|
|
out = build(cfg, rows_with_channels(cfg), NAMES)
|
|
enc = out["spec"]["encoding"]
|
|
assert out["resolved"]["split"] == "color" and "xOffset" not in enc
|
|
|
|
|
|
def test_scatter_tooltip_names_channel():
|
|
cfg = validate_config({"x": "views", "y": "likes", "color": "weekday"})
|
|
out = build(cfg, rows_with_channels(cfg), NAMES)
|
|
tips = out["spec"]["spec"]["encoding"]["tooltip"]
|
|
assert any(t["field"] == "ch" for t in tips)
|
|
|
|
|
|
def test_without_channel_column_still_builds():
|
|
cfg = validate_config({"x": "views", "y": "likes"})
|
|
out = build(cfg, fake_rows(cfg))
|
|
assert out["spec"] is not None and out["resolved"]["split"] is None
|
|
|
|
|
|
@pytest.mark.parametrize("x,y,ctype", [
|
|
(x, y, t) for x, y, t in itertools.product(ALL_X, ALL_Y, chart_catalog.CHART_TYPES)
|
|
if chart_config.incompatibility(t, FIELDS[x].kind, chart_catalog.field(y).kind) is None
|
|
])
|
|
def test_every_combination_with_two_channels_builds(x, y, ctype):
|
|
cfg = validate_config({"x": x, "y": y, "type": ctype})
|
|
out = build(cfg, rows_with_channels(cfg), NAMES)
|
|
assert out["spec"] is not None
|
|
json.dumps(out["spec"])
|