130 lines
4.2 KiB
Python
130 lines
4.2 KiB
Python
"""Real SQL adapter coverage for the read-only HTTP boundary."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import importlib
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
from fastapi.testclient import TestClient
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import sessionmaker
|
|
from sqlalchemy.pool import StaticPool
|
|
|
|
from ploop.api.app import create_app
|
|
from ploop.infra.db.control_plane_queries import SqlAlchemyControlPlaneReadPort
|
|
from ploop.infra.db.models import ArtifactPO, Base, DatasetVersionPO, RunPO
|
|
|
|
DATASET_ID = "dataset-sqlite-1"
|
|
|
|
|
|
def test_bootstrap_without_database_exposes_only_liveness(monkeypatch) -> None:
|
|
"""Catch bootstrap configurations that initialize a store when no URL was supplied."""
|
|
monkeypatch.delenv("PLOOP_DATABASE_URL", raising=False)
|
|
sys.modules.pop("ploop.bootstrap.api", None)
|
|
|
|
bootstrap = importlib.import_module("ploop.bootstrap.api")
|
|
client = TestClient(bootstrap.app)
|
|
|
|
assert client.get("/health").status_code == 200
|
|
assert client.get("/api/v1/datasets").status_code == 503
|
|
|
|
|
|
def test_sqlite_api_never_serializes_private_artifact_or_storage_key() -> None:
|
|
"""Catch an API mapping that leaks private ArtifactPO fields through the SQL port."""
|
|
engine = create_engine(
|
|
"sqlite+pysqlite://",
|
|
connect_args={"check_same_thread": False},
|
|
poolclass=StaticPool,
|
|
)
|
|
Base.metadata.create_all(engine)
|
|
session_factory = sessionmaker(engine)
|
|
try:
|
|
with session_factory() as session:
|
|
_seed_sqlite(session)
|
|
session.commit()
|
|
|
|
response = TestClient(create_app(SqlAlchemyControlPlaneReadPort(session_factory))).get(
|
|
f"/api/v1/datasets/{DATASET_ID}"
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
assert response.json()["data"]["public_artifacts"] == [
|
|
{
|
|
"id": "artifact-public",
|
|
"role": "public_request",
|
|
"digest": "4" * 64,
|
|
"size_bytes": 42,
|
|
}
|
|
]
|
|
assert "private-gold" not in response.text
|
|
assert "storage_key" not in response.text
|
|
finally:
|
|
engine.dispose()
|
|
|
|
|
|
def test_exported_openapi_has_no_restricted_field_names() -> None:
|
|
"""Catch a generated HTTP contract that publishes private persistence field names."""
|
|
schema_path = Path(__file__).resolve().parents[2] / "openapi.json"
|
|
schema = json.loads(schema_path.read_text(encoding="utf-8"))
|
|
|
|
assert "private_gold" not in json.dumps(schema, sort_keys=True)
|
|
assert "storage_key" not in json.dumps(schema, sort_keys=True)
|
|
|
|
|
|
def _seed_sqlite(session) -> None:
|
|
session.add(
|
|
DatasetVersionPO(
|
|
id=DATASET_ID,
|
|
dataset_key="sqlite-synthetic",
|
|
language="en",
|
|
source_digest="1" * 64,
|
|
public_requests_digest="2" * 64,
|
|
private_gold_digest="3" * 64,
|
|
case_count=1,
|
|
lineage_dataset_version_id=None,
|
|
manifest_digest="5" * 64,
|
|
manifest_verified=True,
|
|
status="published",
|
|
)
|
|
)
|
|
session.add_all(
|
|
[
|
|
ArtifactPO(
|
|
id="artifact-public",
|
|
storage_key=f"datasets/{DATASET_ID}/public-requests.jsonl",
|
|
role="public_request",
|
|
access_class="public",
|
|
digest="4" * 64,
|
|
size_bytes=42,
|
|
sealed=True,
|
|
),
|
|
ArtifactPO(
|
|
id="artifact-private",
|
|
storage_key=f"datasets/{DATASET_ID}/private-gold.jsonl",
|
|
role="private_gold",
|
|
access_class="private",
|
|
digest="6" * 64,
|
|
size_bytes=99,
|
|
sealed=True,
|
|
),
|
|
]
|
|
)
|
|
session.add(
|
|
RunPO(
|
|
id="run-sqlite-1",
|
|
dataset_version_id=DATASET_ID,
|
|
sut_version_id="sut-sqlite-1",
|
|
evaluator_version="private-evaluator",
|
|
run_config={"top_k": 3},
|
|
case_ids=["private-case"],
|
|
shard_count=1,
|
|
terminal_shard_count=0,
|
|
failed_shard_count=0,
|
|
canonical_trace_sealed=False,
|
|
phase="queued",
|
|
status="active",
|
|
)
|
|
)
|