telemetry_daily gana columna `seconds` (migración idempotente ALTER ADD COLUMN
IF NOT EXISTS para el Postgres ya desplegado; create_all para SQLite fresco). El
POST acepta seconds_by_module y lo agrega por clave (tope 86400/clave/envío); el
summary suma y devuelve seconds por módulo/vista. Las claves ya eran genéricas,
así que Aprende ('aprende', 'aprende:<tema>') y las vistas ('today', '__other__')
entran sin cambios de esquema. Tests actualizados.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
59 lines
2.1 KiB
Python
59 lines
2.1 KiB
Python
"""Tests de telemetría anónima: el POST agrega contadores por día/módulo y el
|
|
summary los devuelve. SQLite en memoria. Sin identificador de usuario."""
|
|
import os
|
|
|
|
os.environ["DATABASE_URL"] = "sqlite://"
|
|
|
|
from datetime import date # noqa: E402
|
|
|
|
from fastapi.testclient import TestClient # noqa: E402
|
|
|
|
from main import app # noqa: E402
|
|
|
|
client = TestClient(app)
|
|
|
|
|
|
def _today() -> str:
|
|
return date.today().isoformat()
|
|
|
|
|
|
def test_telemetria_agrega_y_resume():
|
|
day = _today()
|
|
# Dos envíos de "instalaciones" distintas el mismo día.
|
|
r1 = client.post("/telemetry", json={
|
|
"day": day,
|
|
"app_version": "2.27.0",
|
|
"active_modules": ["fasting", "weight", "alcohol"],
|
|
"opens_by_module": {"alcohol": 3, "weight": 1},
|
|
"seconds_by_module": {"alcohol": 120, "aprende:sueno": 45},
|
|
})
|
|
assert r1.status_code < 300, r1.text
|
|
r2 = client.post("/telemetry", json={
|
|
"day": day,
|
|
"active_modules": ["fasting", "alcohol"],
|
|
"opens_by_module": {"alcohol": 2},
|
|
"seconds_by_module": {"alcohol": 60},
|
|
})
|
|
assert r2.status_code < 300, r2.text
|
|
|
|
summary = client.get("/telemetry/summary").json()
|
|
assert summary["submissions"] == 2 # dos envíos ese día
|
|
by_module = {m["module"]: m for m in summary["modules"]}
|
|
# alcohol: activo en 2 envíos, abierto 3+2 = 5 veces, 120+60 = 180 s.
|
|
assert by_module["alcohol"]["activeInstallDays"] == 2
|
|
assert by_module["alcohol"]["opens"] == 5
|
|
assert by_module["alcohol"]["seconds"] == 180
|
|
# Una subsección de Aprende con tiempo, sin ser módulo activo.
|
|
assert by_module["aprende:sueno"]["seconds"] == 45
|
|
assert by_module["aprende:sueno"]["opens"] == 0
|
|
# fasting: activo en 2 envíos, 0 aperturas.
|
|
assert by_module["fasting"]["activeInstallDays"] == 2
|
|
assert by_module["fasting"]["opens"] == 0
|
|
# No se filtra ningún identificador ni contenido: solo módulos.
|
|
assert "__submissions__" not in by_module
|
|
|
|
|
|
def test_day_invalido_rechazado():
|
|
r = client.post("/telemetry", json={"day": "hoy"})
|
|
assert r.status_code == 400
|