87 lines
2.7 KiB
Python
87 lines
2.7 KiB
Python
from __future__ import annotations
|
|
|
|
import sqlite3
|
|
from pathlib import Path
|
|
|
|
from app.models import (
|
|
DependencySignals,
|
|
RepositoryMetrics,
|
|
ScanReport,
|
|
)
|
|
from app.models import (
|
|
TestSignals as SignalsModel,
|
|
)
|
|
from app.scanner import scan_repository
|
|
from app.store import SCHEMA_VERSION, ScanStore
|
|
|
|
|
|
def test_v1_scan_schema_is_migrated_and_legacy_history_is_cataloged(tmp_path: Path) -> None:
|
|
database = tmp_path / "history.db"
|
|
report = ScanReport(
|
|
id="legacy-scan",
|
|
repository_path=str((tmp_path / "repo").resolve()),
|
|
repository_name="repo",
|
|
scanned_at="2026-09-12T00:00:00+00:00",
|
|
duration_ms=1,
|
|
metrics=RepositoryMetrics(),
|
|
languages={},
|
|
category_scores={},
|
|
health_score=100,
|
|
release_readiness="READY",
|
|
findings=[],
|
|
tests=SignalsModel(),
|
|
dependencies=DependencySignals(),
|
|
)
|
|
connection = sqlite3.connect(database)
|
|
connection.execute(
|
|
"""
|
|
CREATE TABLE scans (
|
|
id TEXT PRIMARY KEY,
|
|
repository_path TEXT NOT NULL,
|
|
repository_name TEXT NOT NULL,
|
|
scanned_at TEXT NOT NULL,
|
|
health_score INTEGER NOT NULL,
|
|
report_json TEXT NOT NULL
|
|
)
|
|
"""
|
|
)
|
|
connection.execute(
|
|
"INSERT INTO scans VALUES (?, ?, ?, ?, ?, ?)",
|
|
(report.id, report.repository_path, report.repository_name, report.scanned_at.isoformat(), 100, report.model_dump_json()),
|
|
)
|
|
connection.commit()
|
|
connection.close()
|
|
|
|
store = ScanStore(str(database))
|
|
|
|
assert store.get_state("missing") is None
|
|
assert store.repository_count() == 1
|
|
migrated = store.get("legacy-scan")
|
|
assert migrated is not None
|
|
assert migrated.source.provider == "local"
|
|
assert migrated.source.repository_id is not None
|
|
assert store.list_repositories()[0].last_scan_id == "legacy-scan"
|
|
connection = sqlite3.connect(database)
|
|
assert connection.execute("PRAGMA user_version").fetchone()[0] == SCHEMA_VERSION
|
|
connection.close()
|
|
|
|
|
|
def test_schema_upgrade_creates_a_timestamped_pre_migration_backup(tmp_path: Path) -> None:
|
|
database = tmp_path / "history.db"
|
|
repository = tmp_path / "repo"
|
|
repository.mkdir()
|
|
(repository / "README.md").write_text("# Demo\n", encoding="utf-8")
|
|
(repository / ".gitignore").write_text("*.db\n", encoding="utf-8")
|
|
|
|
original = ScanStore(str(database))
|
|
saved = original.save(scan_repository(str(repository)))
|
|
connection = sqlite3.connect(database)
|
|
connection.execute("PRAGMA user_version = 3")
|
|
connection.commit()
|
|
connection.close()
|
|
|
|
migrated = ScanStore(str(database))
|
|
|
|
assert list(tmp_path.glob("history.db.pre-v3-*.bak"))
|
|
assert migrated.get(saved.id) is not None
|