146 lines
4.9 KiB
Python
146 lines
4.9 KiB
Python
"""Real SQL adapter coverage for the read-only HTTP boundary."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
import subprocess
|
|
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_liveness_without_creating_an_engine() -> None:
|
|
"""Catch bootstrap configurations that initialize a store when no URL was supplied."""
|
|
result = subprocess.run(
|
|
[
|
|
sys.executable,
|
|
"-c",
|
|
"from unittest.mock import patch\n"
|
|
"from fastapi.testclient import TestClient\n"
|
|
"with patch('sqlalchemy.create_engine', "
|
|
"side_effect=AssertionError('unconfigured bootstrap created an engine')):\n"
|
|
" from ploop.bootstrap.api import app\n"
|
|
" with TestClient(app) as client:\n"
|
|
" assert client.get('/health').status_code == 200\n"
|
|
" assert client.get('/api/v1/datasets').status_code == 503\n"
|
|
"print('unconfigured bootstrap did not create an engine')\n",
|
|
],
|
|
env={name: value for name, value in os.environ.items() if name != "PLOOP_DATABASE_URL"},
|
|
capture_output=True,
|
|
text=True,
|
|
check=False,
|
|
timeout=15,
|
|
)
|
|
|
|
assert result.returncode == 0, result.stdout + result.stderr
|
|
assert "unconfigured bootstrap did not create an engine" in result.stdout
|
|
|
|
|
|
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",
|
|
)
|
|
)
|