it-architecture/backend/tests/test_backend.py

432 lines
18 KiB
Python

"""Backend behaviour: seeding, metrics, CRUD guards, and backup round-trips."""
from __future__ import annotations
from datetime import date, datetime
import pytest
from fastapi import HTTPException
from conftest import FakeRequest
from app.routers import backup as backup_api
from app.routers import content, flows
from app.schemas import BackupIn, FlowIn, FlowPatch, FlowTypeIn, LayerIn, SettingsIn, SystemIn, SystemPatch
from app.seed import FLOWS, SYSTEMS, seed
from app.services import metrics
from app.services.graph import build_graph
# ------------------------------------------------------------------ seeding
def test_seed_is_idempotent(db):
assert seed(db) is False
assert db.execute("SELECT COUNT(*) AS c FROM systems").fetchone()["c"] == len(SYSTEMS)
def test_every_flow_endpoint_resolves(db):
rows = db.execute("SELECT * FROM data_flows").fetchall()
keys = {r["key"] for r in db.execute("SELECT key FROM systems").fetchall()}
assert len(rows) == len(FLOWS)
for row in rows:
assert row["source_key"] in keys and row["target_key"] in keys
assert row["source_key"] != row["target_key"]
def test_tiers_are_contiguous_and_layered(db):
tiers = {r["tier"] for r in db.execute("SELECT tier FROM systems").fetchall()}
assert min(tiers) == 0 and tiers == set(range(min(tiers), max(tiers) + 1))
# ------------------------------------------------------------------ metrics
def test_history_backfills_only_flows_without_rows(db):
metrics.ensure_history(db)
first = db.execute("SELECT COUNT(*) AS c FROM flow_metrics").fetchone()["c"]
metrics.ensure_history(db)
assert db.execute("SELECT COUNT(*) AS c FROM flow_metrics").fetchone()["c"] == first
assert first > 0
# A newly added link gets its own history rather than rendering dead.
created = flows.create_flow(
FlowIn(source_key="pms", target_key="kingdee", flow_type_id="core", label="新增测试链路"),
db,
FakeRequest(),
)
metrics.ensure_history(db)
own = db.execute("SELECT COUNT(*) AS c FROM flow_metrics WHERE flow_id = ?", (created["id"],)).fetchone()["c"]
assert own == 24 * 30 + datetime.now().hour + 1
def test_bucket_generation_is_deterministic(db):
metrics.ensure_history(db)
flow = db.execute("SELECT * FROM data_flows WHERE enabled = 1 LIMIT 1").fetchone()
today = date.today()
a = metrics.compute_bucket(dict(flow), today, 10, "seed-x")
b = metrics.compute_bucket(dict(flow), today, 10, "seed-x")
assert a == b
assert metrics.compute_bucket(dict(flow), today, 3, "seed-x")["batches"] <= a["batches"] + 1
def test_live_tick_advances_current_hour(db):
metrics.ensure_history(db)
today = date.today().isoformat()
before = db.execute(
"SELECT COALESCE(SUM(records),0) AS r FROM flow_metrics WHERE day = ?", (today,)
).fetchone()["r"]
result = metrics.live_tick(db)
after = db.execute(
"SELECT COALESCE(SUM(records),0) AS r FROM flow_metrics WHERE day = ?", (today,)
).fetchone()["r"]
assert result["touched"] > 0
assert after > before
def test_totals_and_health_shape(db):
metrics.ensure_history(db)
totals = metrics.totals(db, 7)
assert totals["records"] > 0 and totals["avg_latency_ms"] > 0
health = metrics.flow_health(db, days=7)
assert health and all(h["health"] in {"healthy", "watch", "risk"} for h in health.values())
# ------------------------------------------------------------------ graph
def test_graph_uses_declared_tiers_as_ranks(db):
metrics.ensure_history(db)
graph = build_graph(db, days=7)
by_key = {n["key"]: n for n in graph["nodes"]}
assert by_key["wecom"]["rank"] == 0, "统一入口层必须留在最左列"
assert all(n["rank"] == n["tier"] for n in graph["nodes"])
assert {n["health"] for n in graph["nodes"]} <= {"healthy", "watch", "risk", "idle"}
def test_graph_edges_carry_animation_inputs(db):
metrics.ensure_history(db)
graph = build_graph(db, days=7)
assert len(graph["edges"]) == len(FLOWS)
for edge in graph["edges"]:
assert edge["color"] and edge["throughput_rps"] >= 0 and edge["payload_mb_per_hour"] >= 0
assert edge["health"]["state"] in {"healthy", "watch", "risk", "idle"}
def test_disabled_flows_are_hidden_by_default(db):
metrics.ensure_history(db)
target = db.execute("SELECT id FROM data_flows LIMIT 1").fetchone()["id"]
flows.update_flow(target, FlowPatch(enabled=False), db, FakeRequest())
assert len(build_graph(db, days=7)["edges"]) == len(FLOWS) - 1
assert len(build_graph(db, days=7, include_disabled=True)["edges"]) == len(FLOWS)
# ------------------------------------------------------------------ CRUD guards
def test_system_crud_roundtrip(db):
created = content.create_system(SystemIn(key="demo", name="演示系统", layer_id="base", tier=3), db, FakeRequest())
assert created["layer_name"] == "底座(基础平台)"
updated = content.update_system("demo", SystemPatch(status="dev", tech="Python"), db, FakeRequest())
assert updated["status"] == "dev" and updated["tech"] == "Python"
assert content.get_system("demo", db)["name"] == "演示系统"
assert content.delete_system("demo", db, FakeRequest())["ok"] is True
with pytest.raises(HTTPException) as exc:
content.get_system("demo", db)
assert exc.value.status_code == 404
def test_system_validation_rejects_unknown_layer(db):
with pytest.raises(HTTPException) as exc:
content.create_system(SystemIn(key="x1", name="X", layer_id="nope"), db, FakeRequest())
assert exc.value.status_code == 400
def test_layer_delete_blocked_while_in_use(db):
with pytest.raises(HTTPException) as exc:
content.delete_layer("base", db, FakeRequest())
assert exc.value.status_code == 409
def test_layer_delete_allowed_once_empty(db):
content.create_layer(LayerIn(id="tmp", name="临时层"), db, FakeRequest())
assert content.delete_layer("tmp", db, FakeRequest())["ok"] is True
def test_duplicate_layer_id_rejected(db):
content.create_layer(LayerIn(id="tmp2", name="临时层2"), db, FakeRequest())
with pytest.raises(HTTPException) as exc:
content.create_layer(LayerIn(id="tmp2", name="重名"), db, FakeRequest())
assert exc.value.status_code == 409
def test_flow_rejects_self_loop_and_missing_endpoints(db):
base = dict(flow_type_id="core", label="测试")
with pytest.raises(HTTPException) as exc:
flows.create_flow(FlowIn(source_key="oa", target_key="oa", **base), db, FakeRequest())
assert exc.value.status_code == 400
with pytest.raises(HTTPException):
flows.create_flow(FlowIn(source_key="oa", target_key="ghost", **base), db, FakeRequest())
def test_flow_duplicate_rejected(db):
payload = FlowIn(source_key="pms", target_key="kingdee", flow_type_id="core", label="唯一测试链路")
flows.create_flow(payload, db, FakeRequest())
with pytest.raises(HTTPException) as exc:
flows.create_flow(payload, db, FakeRequest())
assert exc.value.status_code == 409
def test_flow_patch_normalizes_peak_hours_and_enabled(db):
created = flows.create_flow(
FlowIn(source_key="pms", target_key="nas", flow_type_id="archive", label="测试归档",
peak_hours=[30, 9, 9, 8]),
db, FakeRequest(),
)
assert created["peak_hours"] == [8, 9]
patched = flows.update_flow(created["id"], FlowPatch(enabled=False, peak_hours=[25, 7]), db, FakeRequest())
assert patched["enabled"] is False and patched["peak_hours"] == [7]
def test_flow_type_delete_blocked_while_in_use(db):
with pytest.raises(HTTPException) as exc:
content.delete_flow_type("core", db, FakeRequest())
assert exc.value.status_code == 409
def test_flow_type_crud(db):
content.create_flow_type(FlowTypeIn(id="batch", name="批处理", line_style="dotted"), db, FakeRequest())
row = content.update_flow_type("batch", FlowTypeIn(id="batch", name="批处理2"), db, FakeRequest())
assert row["name"] == "批处理2"
assert content.delete_flow_type("batch", db, FakeRequest())["ok"] is True
def test_settings_patch_partial(db):
updated = content.patch_settings(SettingsIn(site_title="新标题"), db, FakeRequest())
assert updated["site_title"] == "新标题"
assert "animation_speed" in updated, "未提交的配置项必须保留"
def test_audit_records_editor_identity(db):
content.create_system(SystemIn(key="audited", name="审计系统"), db, FakeRequest())
logs = db.execute("SELECT * FROM audit_logs ORDER BY id DESC LIMIT 1").fetchone()
assert logs["actor"] == "测试员" and logs["action"] == "create"
# ------------------------------------------------------------------ backup
def test_backup_roundtrip_preserves_counts(db):
metrics.ensure_history(db)
payload = backup_api.export_backup(db)
assert payload["systems"] and payload["data_flows"]
imported = backup_api.import_backup(
BackupIn(**{**payload, "mode": "merge"}), db, FakeRequest()
)
assert imported["errors"] == []
assert imported["imported"]["systems"] == len(SYSTEMS)
# Merging the same ids must not duplicate rows.
assert db.execute("SELECT COUNT(*) AS c FROM data_flows").fetchone()["c"] == len(FLOWS)
# Metrics stay attached because the ids were preserved.
assert db.execute("SELECT COUNT(*) AS c FROM flow_metrics").fetchone()["c"] > 0
def test_backup_replace_wipes_and_restores(db):
payload = backup_api.export_backup(db)
payload["systems"] = [s for s in payload["systems"] if s["key"] in {"oa", "pms"}]
result = backup_api.import_backup(BackupIn(**{**payload, "mode": "replace"}), db, FakeRequest())
assert db.execute("SELECT COUNT(*) AS c FROM systems").fetchone()["c"] == 2
# Only the link whose endpoints both survived is restored; the rest are rejected
# rather than silently imported against missing systems.
surviving = db.execute("SELECT source_key, target_key FROM data_flows").fetchall()
assert [(r["source_key"], r["target_key"]) for r in surviving] == [("pms", "oa")]
assert any("端点系统不存在" in e for e in result["errors"])
# A replace wipes history too, so metrics are rebuilt from the restored cadence.
metrics.ensure_history(db)
assert db.execute("SELECT COUNT(*) AS c FROM flow_metrics").fetchone()["c"] == 24 * 30 + datetime.now().hour + 1
def test_backup_rejects_empty_payload(db):
with pytest.raises(HTTPException) as exc:
backup_api.import_backup(BackupIn(), db, FakeRequest())
assert exc.value.status_code == 400
# ------------------------------------------------------------------ threading
def test_request_connection_works_across_threadpool_threads(tmp_path, monkeypatch):
"""FastAPI resolves a sync dependency on one threadpool worker and runs the
endpoint body on another. The request-scoped connection is created in the
dependency, so it must tolerate being used from a different thread; sqlite's
default same-thread guard otherwise raises ProgrammingError on every request.
"""
import threading
monkeypatch.setenv("DB_PATH", str(tmp_path / "thread.db"))
from app import config
monkeypatch.setattr(config, "db_path", lambda: tmp_path / "thread.db")
from app.db import connect, init_db, get_db
from app.seed import seed
init_db()
bootstrap = connect()
seed(bootstrap)
bootstrap.close()
# The dependency yields the connection. Keep a reference to the generator itself,
# otherwise it is finalized immediately and closes the connection underneath us.
dependency = get_db()
connection = next(dependency)
errors: list[BaseException] = []
done = threading.Event()
# ...and the endpoint body consumes it on a foreign thread.
def worker():
try:
assert connection.execute("SELECT COUNT(*) AS c FROM systems").fetchone()["c"] == len(SYSTEMS)
except BaseException as exc: # noqa: BLE001 - surfaced below as a test failure
errors.append(exc)
finally:
done.set()
thread = threading.Thread(target=worker)
thread.start()
thread.join(timeout=10)
assert done.is_set(), "worker thread did not finish"
assert not errors, f"connection rejected on foreign thread: {errors!r}"
next(dependency, None)
# ------------------------------------------------------------------ actor header
def test_actor_header_is_percent_decoded():
"""HTTP headers cannot carry Chinese text, so the client percent-encodes the
editor name; the audit trail must still record the readable name."""
from app.deps import actor_from
class Headers:
def __init__(self, value):
self._value = value
def get(self, key, default=""):
return self._value if key.lower() in ("x-editor", "x-actor") else default
assert actor_from(type("R", (), {"headers": Headers("%E5%BA%B7%E6%96%87%E6%99%B6")})()) == "康文晶"
assert actor_from(type("R", (), {"headers": Headers("alice")})()) == "alice"
assert actor_from(type("R", (), {"headers": Headers("")})()) == "匿名"
# Malformed escapes decode leniently rather than raising.
assert actor_from(type("R", (), {"headers": Headers("%%%E4%B8%AD")})()) == "%%中"
# Invalid UTF-8 byte escapes fall back to the raw header instead of 500ing.
assert actor_from(type("R", (), {"headers": Headers("%FF%FE")})()) == "%FF%FE"
def test_impl_time_and_entry_flag_survive_backup_roundtrip(db):
metrics.ensure_history(db)
content.update_system(
"oa",
SystemPatch(impl_time="2024-06 上线", entry_enabled=False, url="https://oa.example.com"),
db,
FakeRequest(),
)
payload = backup_api.export_backup(db)
db.execute("UPDATE systems SET impl_time = '', entry_enabled = 1, url = '' WHERE key = 'oa'")
backup_api.import_backup(BackupIn(**{**payload, "mode": "merge"}), db, FakeRequest())
row = db.execute("SELECT impl_time, entry_enabled, url FROM systems WHERE key = 'oa'").fetchone()
assert (row["impl_time"], row["entry_enabled"], row["url"]) == ("2024-06 上线", 0, "https://oa.example.com")
def test_graph_nodes_expose_impl_time_and_entry_flag(db):
content.update_system("oa", SystemPatch(impl_time="2023 Q4 立项", entry_enabled=False), db, FakeRequest())
node = next(n for n in build_graph(db)["nodes"] if n["key"] == "oa")
assert node["impl_time"] == "2023 Q4 立项"
assert node["entry_enabled"] is False
def test_read_only_lock_blocks_service_level_writes(db):
"""The same switch guards the service functions, not only the HTTP layer."""
from app.deps import ensure_editable
from app.schemas import SettingsIn
content.patch_settings(SettingsIn(editing_enabled=False), db, FakeRequest())
with pytest.raises(HTTPException) as exc:
ensure_editable(db, "新增系统")
assert exc.value.status_code == 423 and "只读" in exc.value.detail
content.patch_settings(SettingsIn(editing_enabled=True), db, FakeRequest())
ensure_editable(db) # must not raise
def test_manual_layout_columns_are_added_to_older_databases(tmp_path, monkeypatch):
"""flow_x/flow_y arrived after the first release; old files must gain them in place."""
import sqlite3
from app import config
from app.db import init_db
path = tmp_path / "legacy.db"
legacy = sqlite3.connect(path)
legacy.executescript(
"""
CREATE TABLE systems (
id INTEGER PRIMARY KEY AUTOINCREMENT, key TEXT NOT NULL UNIQUE, name TEXT NOT NULL,
layer_id TEXT, dept TEXT NOT NULL DEFAULT '', status TEXT NOT NULL DEFAULT 'unknown',
tech TEXT NOT NULL DEFAULT '', url TEXT NOT NULL DEFAULT '', domain TEXT NOT NULL DEFAULT '',
owner TEXT NOT NULL DEFAULT '', description TEXT NOT NULL DEFAULT '',
tier INTEGER NOT NULL DEFAULT 2, sort_order INTEGER NOT NULL DEFAULT 0,
created_at TEXT NOT NULL DEFAULT '', updated_at TEXT NOT NULL DEFAULT ''
);
INSERT INTO systems (key, name) VALUES ('oa', 'OA');
"""
)
legacy.commit()
legacy.close()
monkeypatch.setenv("DB_PATH", str(path))
monkeypatch.setattr(config, "db_path", lambda: path)
init_db()
init_db() # migrating twice must not fail
conn = sqlite3.connect(path)
columns = {row[1] for row in conn.execute("PRAGMA table_info(systems)")}
assert {"flow_x", "flow_y", "impl_time", "entry_enabled"} <= columns
assert conn.execute("SELECT COUNT(*) FROM systems").fetchone()[0] == 1
conn.close()
def test_tier_label_covers_every_configured_column():
from app.services.graph import TIER_LABELS, tier_label
assert len(TIER_LABELS) == 4
assert tier_label(0) == "统一入口层"
assert tier_label(7).startswith("第 8 层")
def test_migrate_survives_losing_an_add_column_race(tmp_path, monkeypatch):
"""Two processes boot at once; the one that loses the ALTER must still start.
That is exactly what happened when both servers came up against the same file: the
losing connection still saw the column as missing, then hit `duplicate column name`.
"""
import sqlite3
path = tmp_path / "race.db"
monkeypatch.setenv("DB_PATH", str(path))
from app import config
monkeypatch.setattr(config, "db_path", lambda: path)
from app.db import init_db, migrate
init_db()
class Blind:
"""Connection view frozen before the other process added the columns."""
def __init__(self, conn: sqlite3.Connection) -> None:
self.conn = conn
def execute(self, sql: str):
if sql.startswith("PRAGMA table_info"):
return iter(())
return self.conn.execute(sql)
conn = sqlite3.connect(path)
migrate(Blind(conn)) # must not raise
conn.close()