"""Axes des comptages et des pourcentages : ils partent de 0 (1.2.2). Vues, likes, commentaires, likes du commentaire, taux et nombre de vidéos : un axe qui commence ailleurs qu'à 0 exagère les écarts. En logarithmique, l'échelle devient symlog de 0 à une borne ronde, graduée 0, 1, 10, 100... Les autres mesures (durée, âge, heure) gardent un axe ajusté aux données. """ import pytest from shortstats.web import charts, evolution, multi from .test_charts import fake_rows from .test_evolution import evo, preview, seed_snapshots # noqa: F401 (fixtures) from .test_multi_y import multi_rows from .test_web import app_client, login, users_file # noqa: F401 def test_zero_based_categories(): for key in ("views", "likes", "comments", "comment_likes", "like_rate", "comment_rate", "count", "views@6", "likes@18", "comment_rate@40"): assert charts.zero_based(key), key for key in ("duration", "publish_hour", "publish_date", "age", "title", None, ""): assert not charts.zero_based(key), key def test_quant_scale_linear(): assert charts.quant_scale("views@6", [120, 5000], False) == ({"zero": True}, {}) assert charts.quant_scale("like_rate", [1.5, 4.2], False) == ({"zero": True}, {}) assert charts.quant_scale("duration", [12, 58], False) == ({"zero": False}, {}) # barres : toujours depuis 0, quelle que soit la donnée assert charts.quant_scale("duration", [12, 58], False, zero_default=True) == ({"zero": True}, {}) def test_quant_scale_log_keeps_zero(): sc, axis = charts.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 = charts.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 = charts.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 = charts.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 = charts.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 charts.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 charts.quant_scale("duration", [0, 60], True)[0]["type"] == "symlog" # somme : borne haute laissée à Vega (les sommes dépassent les valeurs brutes) sc, axis = charts.quant_scale("views", [10, 900000], True, bounded=False) assert sc == {"type": "symlog", "domainMin": 0} and axis == {} # aucune valeur positive assert charts.quant_scale("comment_likes", [0, 0], True) == ({"type": "symlog", "domainMin": 0}, {}) def test_zero_bin(): assert charts.zero_bin("views", [300, 900, 5000], {"maxbins": 12}) == {"maxbins": 12, "extent": [0, 5000]} assert charts.zero_bin("duration", [12, 58], {"maxbins": 12}) == {"maxbins": 12} assert charts.zero_bin("views", [], {"maxbins": 12}) == {"maxbins": 12} def test_j7_single_axes(): cfg = charts.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 = charts.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 = charts.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 = charts.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 = charts.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 charts.log_ticks(1000, 38673) == [1000, 2000, 5000, 10000, 20000] assert charts.log_ticks(160, 900000) == [1000, 10000, 100000] assert charts.log_ticks(0, 50) == [0, 1, 2, 5, 10, 20, 50] assert charts.log_ticks(1000, 3500) == [1000, 2000, 3000] assert charts.log_ticks(5, 5) == [] and charts.log_ticks(-3, 0) == [] def test_zoom_x_follows_range_filter(): cfg = charts.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 = charts.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 = charts.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 = charts.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"] == {"orient": "right"} # logarithmique : graduations ajoutées sans perdre le côté de l'axe cfg = charts.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 def test_evolution_axes_from_zero(evo): agg = preview(evo, {"source": "evolution", "x": "step", "y": "views", "scale": "linear"}) assert agg["spec"]["encoding"]["y"]["scale"] == {"zero": True} log = preview(evo, {"source": "evolution", "x": "age", "y": "views", "scale": "log"}) y = log["spec"]["encoding"]["y"] assert y["scale"]["type"] == "symlog" and y["scale"]["domainMin"] == 0 and y["axis"]["values"][0] == 0 assert log["spec"]["encoding"]["x"]["scale"]["type"] == "log" # l'âge n'est pas un comptage box = preview(evo, {"source": "evolution", "x": "step", "y": "views", "type": "boxplot", "scale": "log"}) whiskers = box["spec"]["layer"][0]["encoding"]["y"] top = max(r["q95"] for r in box["spec"]["data"]["values"]) assert whiskers["scale"]["domainMin"] == 0 and whiskers["scale"]["domainMax"] >= top two = preview(evo, {"source": "evolution", "x": "age", "y": "views", "y_more": ["like_rate"], "scale": "linear"}) for layer in two["spec"].get("spec", two["spec"])["layer"]: assert layer["encoding"]["y"]["scale"] == {"zero": True} @pytest.mark.parametrize("key", sorted(evolution.Y_FIELDS)) def test_every_evolution_measure_is_zero_based(key): assert charts.zero_based(key)