- ~60 new/extended test files across api, core, plugins, services, schemas: routes, superset clients, task_manager, lineage, git, translate, dashboard-testing, load-testing, migration, llm_analysis, scheduler, ssl - .coveragerc: enable branch coverage; exclude src/__tests__ (test files) and src/scripts (CLI/ops tools) from the denominator - bug fixes found while testing: * settings: PUT /settings/reports registered under duplicated prefix * schemas/lineage: FleetReportDTO missing run_status (route always 500) * dashboard_testing/baseline_inheritance: visual entry read wrong field * superset_client/_databases: logger extra name shadowed LogRecord attr * routes/datasets: _yaml_string_paths recursion without yield from * translate/sql_generator: restore explicit-type timestamp contract * baseline_catalog: remove unreachable dashboard_id fallback - conftest fixes: pytest_plugins to rootdir conftest (pytest 9), test filename collision, TMPDIR-safe integration fixtures
133 lines
5.1 KiB
Python
133 lines
5.1 KiB
Python
# #region Test.LoadTesting.Api [C:3] [TYPE Module] [SEMANTICS test,load-testing,api,validate]
|
|
# @BRIEF 040 US1 API contract tests (T024): profile validate returns cap/matrix/needs_context/blast radius.
|
|
# @TEST_EDGE: missing_environment -> 404 environment not found
|
|
# @TEST_EDGE: service_validation_error -> 422 LoadTestingValidationError surfaced
|
|
# @RELATION BINDS_TO -> [Api.LoadTesting.Routes]
|
|
from unittest.mock import patch
|
|
|
|
import pytest
|
|
from fastapi import HTTPException
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import Session
|
|
|
|
from src.api.routes.load_testing import validate_profile_endpoint
|
|
from src.models.mapping import Base, Environment
|
|
from src.schemas.load_testing import LoadTestingValidationError
|
|
|
|
|
|
def _validation_db() -> Session:
|
|
engine = create_engine("sqlite:///:memory:")
|
|
Base.metadata.create_all(bind=engine)
|
|
db = Session(bind=engine)
|
|
db.add_all(
|
|
[
|
|
Environment(id="env-dev", name="dev", url="https://dev.example", credentials_id="cred"),
|
|
Environment(id="env-x", name="x", url="https://x.example", credentials_id="cred"),
|
|
]
|
|
)
|
|
db.commit()
|
|
return db
|
|
|
|
|
|
def test_valid_profile_returns_cap_and_matrix() -> None:
|
|
body = {
|
|
"dashboard_id": 400,
|
|
"environment_id": "env-dev",
|
|
"stage": "dev",
|
|
"pool_size": 20,
|
|
"concurrency_requested": 5,
|
|
"execution_mode": "iterations",
|
|
"iterations": 10,
|
|
"axes": {"filters": [{"region": "north"}, {"region": "south"}], "viewport": [{"w": 1366}]},
|
|
"blast_radius_fingerprint": "fp1",
|
|
"blast_radius": {"fingerprint": "fp1", "dependent_dashboard_count": 2},
|
|
}
|
|
# Calling the endpoint directly: pass the Pydantic model + permission stub.
|
|
from src.api.routes.load_testing import ValidateInput
|
|
|
|
resp = validate_profile_endpoint(ValidateInput(**body), db=_validation_db(), _=None)
|
|
assert resp["valid"] is True
|
|
assert resp["cap"] == 5
|
|
assert resp["matrix"]["theoretical_variations"] == 2
|
|
assert resp["blast_radius_fingerprint"] == "fp1"
|
|
|
|
|
|
def test_rejected_pool_returns_invalid() -> None:
|
|
from src.api.routes.load_testing import ValidateInput
|
|
|
|
resp = validate_profile_endpoint(
|
|
ValidateInput(
|
|
dashboard_id=400, environment_id="env-x", stage="dev", pool_size=5,
|
|
concurrency_requested=3, execution_mode="iterations", iterations=5, axes={"filters": []},
|
|
),
|
|
db=_validation_db(), _=None,
|
|
)
|
|
assert resp["valid"] is False
|
|
assert any("no safe load slot" in e for e in resp["errors"])
|
|
|
|
|
|
def test_needs_context_surfaced() -> None:
|
|
from src.api.routes.load_testing import ValidateInput
|
|
|
|
resp = validate_profile_endpoint(
|
|
ValidateInput(
|
|
dashboard_id=400, environment_id="env-dev", stage="dev", pool_size=20,
|
|
concurrency_requested=2, execution_mode="iterations", iterations=5,
|
|
axes={"filters": [{"region": "mars"}]},
|
|
filter_metadata={"region": ["north", "south"]},
|
|
),
|
|
db=_validation_db(), _=None,
|
|
)
|
|
assert resp["valid"] is True
|
|
assert any("needs_context" in n for n in resp["needs_context"])
|
|
|
|
|
|
def test_validate_unknown_environment_404() -> None:
|
|
from src.api.routes.load_testing import ValidateInput
|
|
|
|
with pytest.raises(HTTPException) as exc_info:
|
|
validate_profile_endpoint(
|
|
ValidateInput(
|
|
dashboard_id=400, environment_id="env-missing", stage="dev", pool_size=20,
|
|
concurrency_requested=2, execution_mode="iterations", iterations=5, axes={"filters": []},
|
|
),
|
|
db=_validation_db(), _=None,
|
|
)
|
|
assert exc_info.value.status_code == 404
|
|
assert "environment not found" in exc_info.value.detail
|
|
|
|
|
|
def test_validate_service_validation_error_422() -> None:
|
|
from src.api.routes.load_testing import ValidateInput
|
|
|
|
def _boom(**_kwargs):
|
|
raise LoadTestingValidationError("closed axes rejected")
|
|
|
|
with patch("src.api.routes.load_testing.validate_profile", side_effect=_boom):
|
|
with pytest.raises(HTTPException) as exc_info:
|
|
validate_profile_endpoint(
|
|
ValidateInput(
|
|
dashboard_id=400, environment_id="env-dev", stage="dev", pool_size=20,
|
|
concurrency_requested=2, execution_mode="iterations", iterations=5, axes={"filters": []},
|
|
),
|
|
db=_validation_db(), _=None,
|
|
)
|
|
assert exc_info.value.status_code == 422
|
|
assert exc_info.value.detail == "closed axes rejected"
|
|
|
|
|
|
def test_validate_cap_clamped_surfaces_clamp_list() -> None:
|
|
from src.api.routes.load_testing import ValidateInput
|
|
|
|
resp = validate_profile_endpoint(
|
|
ValidateInput(
|
|
dashboard_id=400, environment_id="env-dev", stage="dev", pool_size=20,
|
|
concurrency_requested=30, execution_mode="iterations", iterations=5, axes={"filters": []},
|
|
),
|
|
db=_validation_db(), _=None,
|
|
)
|
|
assert resp["valid"] is True
|
|
assert resp["cap"] == 10 # DEV default cap, requested 30 clamped down
|
|
assert any("clamped" in c for c in resp["clamps"])
|
|
# #endregion Test.LoadTesting.Api
|