123 lines
5.3 KiB
Python
123 lines
5.3 KiB
Python
"""Diagrammes : choix des chaînes et une série distincte par chaîne."""
|
|
|
|
import itertools
|
|
import json
|
|
|
|
import pytest
|
|
|
|
from shortstats.web import charts
|
|
from shortstats.web.charts import ChartError, FIELDS, build, validate_config
|
|
from .test_charts import ALL_X, ALL_Y, fake_rows
|
|
|
|
A, B, C = "UC" + "a" * 22, "UC" + "b" * 22, "UC" + "c" * 22
|
|
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))]
|
|
|
|
|
|
def test_validate_channels():
|
|
cfg = validate_config({"x": "views", "y": "likes", "channels": [A, B, A], "split_channels": False})
|
|
assert cfg.channels == (A, B) and cfg.split_channels is False
|
|
assert cfg.to_dict()["channels"] == [A, B]
|
|
json.dumps(cfg.to_dict())
|
|
assert validate_config({"x": "views", "y": "likes"}).split_channels is True
|
|
for bad in (["@handle"], "UC" + "a" * 22, [1], ["UC" + "a" * 21]):
|
|
with pytest.raises(ChartError, match="chaînes"):
|
|
validate_config({"x": "views", "y": "likes", "channels": bad})
|
|
with pytest.raises(ChartError, match="maximum"):
|
|
validate_config({"x": "views", "y": "likes", "channels": [f"UC{i:022d}" for i in range(51)]})
|
|
with pytest.raises(ChartError, match="split_channels"):
|
|
validate_config({"x": "views", "y": "likes", "split_channels": "oui"})
|
|
|
|
|
|
@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, charts.CHART_TYPES)
|
|
if charts.incompatibility(t, FIELDS[x].kind, charts.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"])
|