371 lines
21 KiB
Python
371 lines
21 KiB
Python
"""Axes : zoom vertical et horizontal, départ à zéro, échelles log, axe de l'âge."""
|
||
|
||
from __future__ import annotations
|
||
|
||
import json
|
||
import math
|
||
|
||
import pytest
|
||
|
||
from shortstats.web import (
|
||
chart_aggregate,
|
||
chart_catalog,
|
||
chart_config,
|
||
chart_view,
|
||
chart_zoom,
|
||
charts,
|
||
evolution,
|
||
multi,
|
||
)
|
||
|
||
from ..outils.lignes import fake_rows, multi_rows
|
||
from ..outils.specs import body_of
|
||
|
||
|
||
def view(*axes, **kw):
|
||
return chart_view.parse_view({"y_axes": list(axes), **kw}, "j7")
|
||
|
||
|
||
def test_parse_view():
|
||
assert chart_view.parse_view({}, "j7").y_axes == ()
|
||
assert view({"min": 10, "max": 500}).y_axes == ((10.0, 500.0),)
|
||
assert view({"min": 500, "max": 10}).y_axes == ((10.0, 500.0),) # inversées
|
||
assert view(None, {"max": 7}).y_axes == (None, (None, 7.0))
|
||
assert view({"min": None, "max": ""}).y_axes == (None,)
|
||
for bad in ([{}, {}, {}], "x", [{"min": "a"}], [{"min": True}], [{"min": float("inf")}],
|
||
[{"min": 1, "pas": 2}], [3]):
|
||
with pytest.raises(chart_catalog.ChartError):
|
||
chart_view.parse_view({"y_axes": bad}, "j7")
|
||
|
||
|
||
def test_axis_info():
|
||
info = chart_zoom.y_axis_info("Vues", [120, 5000], {"zero": True})
|
||
assert info == {"index": 0, "label": "Vues", "side": None, "min": 0, "max": 5000, "integer": True,
|
||
"log": False}
|
||
assert chart_zoom.y_axis_info("Durée", [12, 58], {"zero": False})["min"] == 12
|
||
assert chart_zoom.y_axis_info("Vues", [3, 900], {"type": "symlog", "domainMin": 0})["log"] is True
|
||
assert chart_zoom.y_axis_info("Durée", [5, 60], {"type": "log"})["min"] == 5
|
||
# comptage sans échelle explicite (nombre de vidéos) : zéro par défaut de Vega-Lite
|
||
assert chart_zoom.y_axis_info("Nombre", [2, 9], None)["min"] == 0
|
||
rate = chart_zoom.y_axis_info("Taux", [1.25, 4.5], {"zero": True})
|
||
assert rate["integer"] is False and rate["max"] == 4.5
|
||
assert chart_zoom.y_axis_info("Vues", [], {"zero": True}) is None
|
||
assert chart_zoom.y_axis_info("Durée", [7, 7], {"zero": False}) is None
|
||
|
||
|
||
def test_zoom_y():
|
||
info = chart_zoom.y_axis_info("Vues", [120, 900000], {"type": "symlog", "domainMin": 0, "domainMax": 1000000})
|
||
ydef = {"scale": {"type": "symlog", "domainMin": 0, "domainMax": 1000000}, "axis": {"values": [0, 1, 10]}}
|
||
assert chart_zoom.zoom_y(ydef, info, (100.0, 50000.0)) == (100, 50000) # min et max choisis
|
||
assert ydef["scale"] == {"type": "symlog", "domain": [100, 50000]}
|
||
assert ydef["axis"] == {"values": [100, 200, 500, 1000, 2000, 5000, 10000, 20000, 50000]}
|
||
# bornée par les données ; plage entière ou vide : axe inchangé
|
||
ydef = {"scale": {"zero": True}}
|
||
lin = chart_zoom.y_axis_info("Vues", [0, 800], {"zero": True})
|
||
assert chart_zoom.zoom_y(ydef, lin, (-50.0, 5000.0)) is None and ydef == {"scale": {"zero": True}}
|
||
assert chart_zoom.zoom_y(ydef, lin, (900.0, 1000.0)) is None
|
||
assert chart_zoom.zoom_y(ydef, lin, (None, 400.0)) == (None, 400)
|
||
assert ydef == {"scale": {"domain": [0, 400]}} # linéaire : pas de graduations imposées
|
||
# seul le bas change : le haut reste rond (borne de l'échelle ou arrondi)
|
||
ydef = {"scale": {"type": "symlog", "domainMin": 0, "domainMax": 1000000}}
|
||
assert chart_zoom.zoom_y(ydef, info, (1000.0, 900000.0)) == (1000, None)
|
||
assert ydef["scale"]["domain"] == [1000, 1000000] and ydef["axis"]["values"][-1] == 1000000
|
||
ydef = {"scale": {"zero": True}}
|
||
big = chart_zoom.y_axis_info("Vues", [0, 328273], {"zero": True})
|
||
assert chart_zoom.zoom_y(ydef, big, (1000.0, None)) == (1000, None)
|
||
assert ydef["scale"]["domain"] == [1000, 350000]
|
||
assert chart_zoom.zoom_note((1000, None), big) == "axe vertical à partir de 1\u00a0000 (ne part plus de 0)"
|
||
assert chart_zoom.zoom_note((None, 50), big, "de droite") == "axe de droite limité à 50"
|
||
assert chart_zoom.round_top(None, 12, 58) == 60 and chart_zoom.round_top("symlog", 50, 1546) == 2000
|
||
assert chart_zoom.zoom_y({"scale": {}}, None, (1.0, 2.0)) is None
|
||
assert chart_zoom.zoom_y({"scale": {}}, lin, None) is None
|
||
|
||
|
||
def test_single_scatter_zoom():
|
||
cfg = chart_config.validate_config({"x": "views@6", "y": "likes", "type": "scatter", "scale": "log"})
|
||
rows = fake_rows(cfg)
|
||
full = charts.build(cfg, rows)
|
||
assert full["resolved"]["y_axes"][0]["min"] == 0 and "clip" not in full["spec"]["mark"]
|
||
out = charts.build(cfg, rows, None, view({"min": 1000, "max": 4000}))
|
||
y = out["spec"]["encoding"]["y"]
|
||
assert y["scale"] == {"type": "symlog", "domain": [1000, 4000]}
|
||
assert y["axis"]["values"] == [1000, 2000, 3000, 4000]
|
||
assert out["spec"]["mark"]["clip"] is True
|
||
assert "axe vertical resserré de 1 000 à 4 000 (ne part plus de 0)" in out["explanation"]
|
||
# rien n'est retiré : mêmes vidéos, mêmes valeurs
|
||
assert out["spec"]["data"]["values"] == full["spec"]["data"]["values"]
|
||
# bornes du curseur indépendantes du zoom
|
||
assert out["resolved"]["y_axes"] == full["resolved"]["y_axes"]
|
||
|
||
|
||
def test_single_types():
|
||
def axes(raw, rows=None):
|
||
cfg = chart_config.validate_config(raw)
|
||
return charts.build(cfg, rows or fake_rows(cfg))["resolved"]["y_axes"]
|
||
assert axes({"x": "weekday", "y": "views", "type": "boxplot"})[0]["label"] == "Vues à 7 j"
|
||
assert axes({"x": "views", "y": "weekday", "type": "boxplot"}) == [] # boîtes horizontales
|
||
bars = axes({"x": "weekday", "y": "views", "type": "bar"}) # barres : seul le haut se règle
|
||
assert bars[0]["fixed_min"] is True and bars[0]["min"] == 0
|
||
assert axes({"x": "views", "y": "count", "type": "histogram"}) == []
|
||
assert axes({"x": "weekday", "y": "publish_hour", "type": "heatmap"}) == []
|
||
assert axes({"x": "views", "y": "publish_date", "type": "scatter"}) == [] # axe vertical de dates
|
||
count = axes({"x": "publish_date", "y": "count", "type": "line"})
|
||
assert count[0]["label"] == "Nombre de vidéos" and count[0]["min"] == 0
|
||
|
||
|
||
def test_line_extent_follows_aggregation():
|
||
cfg = chart_config.validate_config({"x": "weekday", "y": "views", "type": "line", "agg": "sum"})
|
||
rows = fake_rows(cfg)
|
||
info = charts.build(cfg, rows)["resolved"]["y_axes"][0]
|
||
sums: dict = {}
|
||
for r in rows:
|
||
sums[r[2]] = sums.get(r[2], 0) + r[3]
|
||
assert info["max"] == max(sums.values())
|
||
cfg = chart_config.validate_config({"x": "weekday", "y": "views", "type": "line", "agg": "max"})
|
||
assert charts.build(cfg, rows)["resolved"]["y_axes"][0]["max"] == max(r[3] for r in rows)
|
||
|
||
|
||
def test_x_group():
|
||
rec = lambda d: {"x": d} # noqa: E731
|
||
week = charts.x_group({"timeUnit": {"unit": "yearweek", "utc": True}})
|
||
assert week(rec("2026-07-05")) == week(rec("2026-07-11")) # dimanche au samedi
|
||
assert week(rec("2026-07-04")) != week(rec("2026-07-05"))
|
||
assert week(rec("2026-12-31")) != week(rec("2027-01-01")) # coupée au 1er janvier
|
||
month = charts.x_group({"timeUnit": {"unit": "yearmonth"}})
|
||
assert month(rec("2026-07-01")) == month(rec("2026-07-31T10:00:00+00:00"))
|
||
assert charts.x_group({"timeUnit": {"unit": "yearmonthdate"}})(rec("2026-07-01T09:00:00")) == "2026-07-01"
|
||
assert charts.x_group({"field": "x"})(rec(4)) == 4
|
||
assert chart_aggregate.group_values([{"x": 1, "y": 2}, {"x": 1, "y": 4}, {"x": 2, "y": None}],
|
||
lambda r: r["x"], "mean") == [3]
|
||
|
||
|
||
def test_multi_two_axes():
|
||
cfg = chart_config.validate_config({"x": "publish_date", "y": "views@6", "y_more": ["likes@6"],
|
||
"type": "scatter", "scale": "log"})
|
||
rows = multi_rows(cfg)
|
||
full = charts.build(cfg, rows, ["Chaîne A", "Chaîne B"], None)
|
||
left, right = full["resolved"]["y_axes"]
|
||
assert (left["index"], left["side"], left["label"]) == (0, "left", "Vues")
|
||
assert (right["index"], right["side"], right["label"]) == (1, "right", "Likes")
|
||
out = charts.build(cfg, rows, ["Chaîne A", "Chaîne B"], view(None, {"min": 100, "max": 1000}))
|
||
ly, ry = (layer["encoding"]["y"] for layer in body_of(out["spec"])["layer"])
|
||
assert "domain" not in ly["scale"] and ly["axis"]["orient"] == "left"
|
||
assert ry["scale"]["domain"] == [100, 1000]
|
||
assert ry["axis"] == {**multi.RIGHT_AXIS, "values": list(range(100, 1001, 100))} # une décade : 1 à 9
|
||
marks = [layer["mark"] for layer in body_of(out["spec"])["layer"]]
|
||
assert "clip" not in marks[0] and marks[1]["clip"] is True
|
||
assert "axe de droite resserré de 100 à 1 000" in out["explanation"]
|
||
# une seule catégorie : un seul axe, sans côté
|
||
cfg = chart_config.validate_config({"x": "publish_date", "y": "views@6", "y_more": ["views@8"], "type": "line"})
|
||
one = charts.build(cfg, multi_rows(cfg), None, None)["resolved"]["y_axes"]
|
||
assert len(one) == 1 and one[0]["side"] is None
|
||
cfg = chart_config.validate_config({"x": "weekday", "y": "views@6", "y_more": ["likes@6"], "type": "bar"})
|
||
bars = charts.build(cfg, multi_rows(cfg), None, None)["resolved"]["y_axes"]
|
||
assert [(a["side"], a.get("fixed_min")) for a in bars] == [("left", True), ("right", True)]
|
||
|
||
|
||
def test_parse_view_evolution():
|
||
v = chart_view.parse_view({"filters": {"views": {"min": 10, "max": 5}, "weekday": {"in": ["Lundi"]},
|
||
"publish_date": {"min": "2026-09-01"}}, "individual": True}, "j7")
|
||
assert v.filters["views"] == ("range", 5, 10) and v.filters["weekday"] == ("set", ("Lundi",))
|
||
assert v.individual and str(v.filters["publish_date"][1]) == "2026-09-01"
|
||
# filtre inconnu (diagramme modifié depuis ses filtres par défaut) : ignoré
|
||
assert chart_view.parse_view({"filters": {"inconnu": {"min": 1}}}, "j7").filters == {}
|
||
for bad in [{"filters": {"views": {"min": "x"}}},
|
||
{"filters": {"publish_date": {"min": "pas une date"}}}, {"individual": "oui"},
|
||
{"autre": 1}, {"filters": {"weekday": {"in": "Lundi"}}}]:
|
||
with pytest.raises(chart_catalog.ChartError):
|
||
chart_view.parse_view(bad, "j7")
|
||
assert chart_view.parse_view({"filters": {"age": {"min": 1, "max": 12}}}, "evolution").filters["age"] == ("range", 1, 12)
|
||
assert chart_view.parse_view({"filters": {"comment_likes": {"min": 1}}}, "evolution").filters == {}
|
||
|
||
|
||
def test_zero_based_categories():
|
||
for key in ("views", "likes", "comments", "like_rate", "comment_rate", "count",
|
||
"views@6", "likes@18", "comment_rate@40", "comments_read@56", "theme_comments@6", "theme_rate@56",
|
||
"theme_rate", "comments_read"):
|
||
assert chart_zoom.zero_based(key), key
|
||
for key in ("duration", "publish_hour", "publish_date", "age", "title", None, ""):
|
||
assert not chart_zoom.zero_based(key), key
|
||
|
||
|
||
def test_quant_scale_linear():
|
||
assert chart_zoom.quant_scale("views@6", [120, 5000], False) == ({"zero": True}, {})
|
||
assert chart_zoom.quant_scale("like_rate", [1.5, 4.2], False) == ({"zero": True}, {})
|
||
assert chart_zoom.quant_scale("duration", [12, 58], False) == ({"zero": False}, {})
|
||
# barres : toujours depuis 0, quelle que soit la donnée
|
||
assert chart_zoom.quant_scale("duration", [12, 58], False, zero_default=True) == ({"zero": True}, {})
|
||
|
||
|
||
def test_quant_scale_log_keeps_zero():
|
||
sc, axis = chart_zoom.quant_scale("views@6", [120, 5000, 900000], True)
|
||
assert sc == {"type": "symlog", "domainMin": 0, "domainMax": 1000000}
|
||
assert axis == {"values": [0, 1, 10, 100, 1000, 10000, 100000, 1000000]}
|
||
# borne ronde 1, 2 ou 5 × 10^k
|
||
sc, axis = chart_zoom.quant_scale("views", [0, 40, 1100000], True)
|
||
assert sc["domainMax"] == 2000000 and axis["values"][-2:] == [1000000, 2000000]
|
||
# taux en % : graduations sous 1 et constante symlog adaptée
|
||
sc, axis = chart_zoom.quant_scale("like_rate", [0.05, 3.2, 8.4], True)
|
||
assert sc == {"type": "symlog", "domainMin": 0, "domainMax": 10, "constant": 0.01}
|
||
assert axis == {"values": [0, 0.01, 0.1, 1, 10]}
|
||
# une ou deux décades : graduations 1, 2, 5
|
||
sc, axis = chart_zoom.quant_scale("like_rate", [2.1, 4.6], True)
|
||
assert sc == {"type": "symlog", "domainMin": 0, "domainMax": 5} and axis == {"values": [0, 1, 2, 5]}
|
||
sc, axis = chart_zoom.quant_scale("comments", [3, 70], True)
|
||
assert axis == {"values": [0, 1, 2, 5, 10, 20, 50, 100]}
|
||
# mesure non comptée, toute positive : vrai logarithme
|
||
assert chart_zoom.quant_scale("duration", [5, 60], True) == ({"type": "log"}, {})
|
||
# mesure non comptée avec un 0 : symlog quand même (le log de 0 n'existe pas)
|
||
assert chart_zoom.quant_scale("duration", [0, 60], True)[0]["type"] == "symlog"
|
||
# somme : borne haute laissée à Vega (les sommes dépassent les valeurs brutes)
|
||
sc, axis = chart_zoom.quant_scale("views", [10, 900000], True, bounded=False)
|
||
assert sc == {"type": "symlog", "domainMin": 0} and axis == {}
|
||
# aucune valeur positive
|
||
assert chart_zoom.quant_scale("theme_comments@56", [0, 0], True) == ({"type": "symlog", "domainMin": 0}, {})
|
||
|
||
|
||
def test_zero_bin():
|
||
assert chart_zoom.zero_bin("views", [300, 900, 5000], {"maxbins": 12}) == {"maxbins": 12, "extent": [0, 5000]}
|
||
assert chart_zoom.zero_bin("duration", [12, 58], {"maxbins": 12}) == {"maxbins": 12}
|
||
assert chart_zoom.zero_bin("views", [], {"maxbins": 12}) == {"maxbins": 12}
|
||
|
||
|
||
def test_j7_single_axes():
|
||
cfg = chart_config.validate_config({"x": "views@6", "y": "likes", "type": "scatter", "scale": "linear"})
|
||
enc = charts.build(cfg, fake_rows(cfg))["spec"]["encoding"]
|
||
assert enc["x"]["scale"] == {"zero": True} and enc["y"]["scale"] == {"zero": True}
|
||
cfg = chart_config.validate_config({"x": "duration", "y": "like_rate", "type": "scatter", "scale": "linear"})
|
||
enc = charts.build(cfg, fake_rows(cfg))["spec"]["encoding"]
|
||
assert enc["x"]["scale"] == {"zero": False} and enc["y"]["scale"] == {"zero": True}
|
||
# courbe agrégée par date : l'ordonnée (vues) part de 0
|
||
cfg = chart_config.validate_config({"x": "publish_date", "y": "views", "type": "line"})
|
||
enc = charts.build(cfg, fake_rows(cfg))["spec"]["encoding"]
|
||
assert enc["y"]["scale"]["zero"] is True
|
||
# histogramme : les classes partent de 0
|
||
cfg = chart_config.validate_config({"x": "views", "y": "count", "type": "histogram"})
|
||
enc = charts.build(cfg, fake_rows(cfg))["spec"]["encoding"]
|
||
assert enc["x"]["bin"]["extent"][0] == 0
|
||
|
||
|
||
def test_j7_log_line_sum_unbounded():
|
||
cfg = chart_config.validate_config({"x": "publish_date", "y": "views", "type": "line", "agg": "sum",
|
||
"scale": "log"})
|
||
y = charts.build(cfg, fake_rows(cfg))["spec"]["encoding"]["y"]
|
||
assert y["scale"]["type"] == "symlog" and "domainMax" not in y["scale"]
|
||
|
||
|
||
def test_log_ticks():
|
||
assert chart_zoom.log_ticks(1000, 38673) == [1000, 2000, 5000, 10000, 20000]
|
||
assert chart_zoom.log_ticks(160, 900000) == [1000, 10000, 100000]
|
||
assert chart_zoom.log_ticks(0, 50) == [0, 1, 2, 5, 10, 20, 50]
|
||
assert chart_zoom.log_ticks(1000, 3500) == [1000, 2000, 3000]
|
||
assert chart_zoom.log_ticks(5, 5) == [] and chart_zoom.log_ticks(-3, 0) == []
|
||
|
||
|
||
def test_zoom_x_follows_range_filter():
|
||
cfg = chart_config.validate_config({"x": "views@6", "y": "likes", "type": "scatter", "scale": "log"})
|
||
rows = fake_rows(cfg)
|
||
full = charts.build(cfg, rows)["spec"]["encoding"]["x"]
|
||
assert full["scale"]["domainMin"] == 0 and "values" in full["axis"]
|
||
view = chart_view.View(filters={"views@6": ("range", 1000, 3000)})
|
||
x = charts.build(cfg, rows, None, view)["spec"]["encoding"]["x"]
|
||
assert x["scale"]["domain"] == [1000, 3000]
|
||
assert not {"zero", "domainMin", "domainMax"} & set(x["scale"])
|
||
assert x["axis"]["values"] == [1000, 2000, 3000] # graduations lisibles dans la plage
|
||
# plage réduite à une seule valeur : pas de domaine dégénéré
|
||
one = chart_view.View(filters={"views@6": ("range", 2192, 2192)})
|
||
x = charts.build(cfg, rows, None, one)["spec"]["encoding"]["x"]
|
||
assert "domain" not in x["scale"]
|
||
|
||
|
||
def test_multi_axes_from_zero():
|
||
cfg = chart_config.validate_config({"x": "publish_date", "y": "views@6", "y_more": ["views@8", "duration"],
|
||
"type": "scatter", "scale": "linear"})
|
||
spec = charts.build(cfg, multi_rows(cfg), ["Chaîne A", "Chaîne B"], None)["spec"]
|
||
body = spec.get("spec", spec) # un petit diagramme par chaîne
|
||
left, right = (layer["encoding"]["y"] for layer in body["layer"])
|
||
assert left["scale"] == {"zero": True} and left["axis"] == {"orient": "left"}
|
||
assert right["scale"] == {"zero": False} and right["axis"] == multi.RIGHT_AXIS
|
||
# logarithmique : graduations ajoutées sans perdre le côté de l'axe
|
||
cfg = chart_config.validate_config({"x": "publish_date", "y": "views@6", "y_more": ["likes@6"],
|
||
"type": "scatter", "scale": "log"})
|
||
spec = charts.build(cfg, multi_rows(cfg), ["Chaîne A", "Chaîne B"], None)["spec"]
|
||
body = spec.get("spec", spec) # un petit diagramme par chaîne
|
||
for k, layer in enumerate(body["layer"]):
|
||
y = layer["encoding"]["y"]
|
||
assert y["scale"]["type"] == "symlog" and y["scale"]["domainMin"] == 0
|
||
assert y["axis"]["orient"] == ("left" if k == 0 else "right") and y["axis"]["values"][0] == 0
|
||
sc, axis, log = multi.y_scale([5, 900000], "line", "log", "views", "sum")
|
||
assert log and "domainMax" not in sc
|
||
|
||
|
||
@pytest.mark.parametrize("key", sorted(evolution.Y_FIELDS))
|
||
def test_every_evolution_measure_is_zero_based(key):
|
||
assert chart_zoom.zero_based(key)
|
||
|
||
|
||
def test_bar_axis_keeps_zero():
|
||
cfg = chart_config.validate_config({"x": "weekday", "y": "views", "type": "bar", "scale": "linear"})
|
||
rows = fake_rows(cfg)
|
||
full = charts.build(cfg, rows)
|
||
info = full["resolved"]["y_axes"][0]
|
||
assert info["fixed_min"] and info["min"] == 0
|
||
out = charts.build(cfg, rows, None, chart_view.View(y_axes=((50, info["max"] / 2),)))
|
||
y = out["spec"]["encoding"]["y"]
|
||
assert y["scale"]["domain"][0] == 0 and y["scale"]["domain"][1] == chart_zoom.clean_num(info["max"] / 2)
|
||
assert out["spec"]["mark"]["clip"] is True
|
||
# barres et courbe : chaque axe a son curseur, celui des barres part de 0
|
||
cfg = chart_config.validate_config({"x": "weekday", "y": "views@6", "y_more": ["likes@6"], "type": "bar",
|
||
"type2": "line", "split_channels": False})
|
||
two = charts.build(cfg, multi_rows(cfg), None, chart_view.View(y_axes=((10, 100), (10, 100))))
|
||
left = next(layer for layer in two["spec"]["layer"] if layer["mark"]["type"] == "bar")
|
||
right = next(layer for layer in two["spec"]["layer"] if layer["mark"]["type"] == "line")
|
||
assert left["encoding"]["y"]["scale"]["domain"] == [0, 100]
|
||
assert right["encoding"]["y"]["scale"]["domain"] == [10, 100]
|
||
|
||
|
||
def test_age_axis_linear_and_regular():
|
||
for lo, hi in [(1, 24), (1, 36), (1, 168), (1, 720), (24, 8766), (1, 26298), (730.5, 26298), (5, 5)]:
|
||
scale, axis = evolution.age_axis(lo, hi)
|
||
assert scale["type"] == "linear" and scale["nice"] is False
|
||
d0, d1 = scale["domain"]
|
||
assert d0 <= lo and d1 >= hi
|
||
ticks = axis["values"]
|
||
assert 2 <= len(ticks) <= evolution.AGE_MAX_TICKS + 1, (lo, hi, ticks)
|
||
gaps = {round(b - a, 6) for a, b in zip(ticks, ticks[1:])}
|
||
assert len(gaps) == 1, (lo, hi, ticks) # graduations régulières
|
||
assert all(d0 - 1e-6 <= t <= d1 + 1e-6 for t in ticks)
|
||
assert evolution.age_axis(1, 24)[0]["domain"][0] == 0 # depuis la sortie
|
||
assert evolution.age_axis(8766, 26298)[0]["domain"][0] == 8766 # plage tardive : au 1er relevé
|
||
|
||
|
||
def test_age_axis_labels():
|
||
def labels(lo, hi):
|
||
_, axis = evolution.age_axis(lo, hi)
|
||
return dict(zip(axis["values"], json.loads(axis["labelExpr"].split("[indexof(")[0])))
|
||
assert labels(1, 24)[12] == "12 h"
|
||
assert labels(1, 36)[30] == "1 j 6 h"
|
||
assert labels(1, 168)[48] == "2 j"
|
||
assert labels(1, 26298)[8766] == "12 mois" and labels(1, 26298)[0] == "0"
|
||
assert labels(1, 80000)[17532] == "2 ans" and labels(1, 80000)[8766] == "1 an"
|
||
assert labels(1, 4000)[2191.5] == "3 mois"
|
||
|
||
|
||
def test_step_cells_follow_durations():
|
||
hours = [1, 2, 3, 24, 27, 30, 168, 336]
|
||
recs = [{"x": float(h), "c": c} for h in hours for c in ("A", "B")]
|
||
evolution.step_cells(recs, "c")
|
||
by = {(r["x"], r["c"]): r for r in recs}
|
||
for h in hours:
|
||
a, b = by[(h, "A")], by[(h, "B")]
|
||
assert a["x0"] < a["x1"] <= b["x0"] < b["x1"] # côte à côte, A puis B
|
||
assert math.isclose(a["x1"] - a["x0"], b["x1"] - b["x0"])
|
||
# largeur proportionnelle à la durée couverte par l'étape ; jamais de chevauchement
|
||
width = {h: by[(h, "B")]["x1"] - by[(h, "A")]["x0"] for h in hours}
|
||
assert width[336] > width[27] > width[2]
|
||
edges = sorted((r["x0"], r["x1"]) for r in recs)
|
||
assert all(e1 <= f0 + 1e-9 for (_, e1), (f0, _) in zip(edges, edges[1:]))
|
||
# ordre imposé (jours de la semaine) et lignes concernées seulement
|
||
recs = [{"x": 1.0, "c": "Mardi"}, {"x": 1.0, "c": "Lundi"}, {"x": 2.0, "c": "Lundi", "skip": True}]
|
||
evolution.step_cells(recs, "c", chart_catalog.WEEKDAYS, only=lambda r: not r.get("skip"))
|
||
assert recs[1]["x0"] < recs[0]["x0"] and "x0" not in recs[2]
|