|
1 | 1 | """API v1 — test run and profiling run retrieval.""" |
2 | 2 |
|
3 | | -from fastapi import APIRouter, Depends |
4 | | -from sqlalchemy import select |
| 3 | +from fastapi import APIRouter, Depends, Query |
| 4 | +from sqlalchemy import or_, select |
5 | 5 |
|
6 | 6 | from testgen.api.deps import db_session, resolve_job |
| 7 | +from testgen.api.enums import ( |
| 8 | + DISPOSITION_FROM_DB, |
| 9 | + DISPOSITION_TO_DB, |
| 10 | + RESULT_STATUS_FROM_DB, |
| 11 | + RESULT_STATUS_TO_DB, |
| 12 | + Disposition, |
| 13 | + ResultStatus, |
| 14 | +) |
7 | 15 | from testgen.api.schemas import ( |
8 | 16 | ErrorResponse, |
9 | 17 | IssueCounts, |
10 | 18 | ProfilingRunResponse, |
11 | 19 | ProfilingRunResult, |
12 | 20 | ResultCounts, |
| 21 | + TestResultItem, |
| 22 | + TestResultListResponse, |
13 | 23 | TestRunResponse, |
14 | 24 | TestRunResult, |
15 | 25 | ) |
| 26 | +from testgen.common.enums import Disposition as DbDisposition |
16 | 27 | from testgen.common.enums import JobKey |
17 | 28 | from testgen.common.models import get_current_session |
18 | 29 | from testgen.common.models.hygiene_issue import HygieneIssue |
19 | 30 | from testgen.common.models.job_execution import JobExecution |
20 | 31 | from testgen.common.models.profiling_run import ProfilingRun |
21 | | -from testgen.common.models.test_result import TestResult |
| 32 | +from testgen.common.models.test_result import TestResult, TestRunResultRow |
22 | 33 | from testgen.common.models.test_run import TestRun |
23 | 34 | from testgen.common.models.test_suite import TestSuite |
24 | 35 |
|
@@ -63,6 +74,82 @@ def get_test_run(job: JobExecution = resolve_job("view", JobExecution.job_key == |
63 | 74 | ) |
64 | 75 |
|
65 | 76 |
|
| 77 | +def _disposition_from_db(value: str | None) -> Disposition: |
| 78 | + """Map a stored ``disposition`` to the API enum, degrading unknown values to ``no_decision``. |
| 79 | +
|
| 80 | + A NULL or unmapped value resolves to ``no_decision`` rather than raising, so a single odd |
| 81 | + row never fails serialization of the whole results page. |
| 82 | + """ |
| 83 | + if not value: |
| 84 | + return Disposition.no_decision |
| 85 | + try: |
| 86 | + return DISPOSITION_FROM_DB[DbDisposition(value)] |
| 87 | + except (ValueError, KeyError): |
| 88 | + return Disposition.no_decision |
| 89 | + |
| 90 | + |
| 91 | +def _to_item(row: TestRunResultRow) -> TestResultItem: |
| 92 | + """Map a DB-valued result row to the API item, normalizing enum casing.""" |
| 93 | + return TestResultItem( |
| 94 | + test_definition_id=row.test_definition_id, |
| 95 | + test_type=row.test_type, |
| 96 | + schema_name=row.schema_name, |
| 97 | + table_name=row.table_name, |
| 98 | + column_names=row.column_names, |
| 99 | + result_status=RESULT_STATUS_FROM_DB.get(row.status), |
| 100 | + result_measure=row.result_measure, |
| 101 | + threshold_value=row.threshold_value, |
| 102 | + result_message=row.message, |
| 103 | + test_time=row.test_time, |
| 104 | + disposition=_disposition_from_db(row.disposition), |
| 105 | + ) |
| 106 | + |
| 107 | + |
| 108 | +@router.get( |
| 109 | + "/test-runs/{job_id}/results", |
| 110 | + response_model=TestResultListResponse, |
| 111 | +) |
| 112 | +def list_test_run_results( |
| 113 | + job: JobExecution = resolve_job("view", JobExecution.job_key == JobKey.run_tests), # noqa: B008 |
| 114 | + status: ResultStatus | None = Query(default=None), # noqa: B008 |
| 115 | + table_name: str | None = Query(default=None), |
| 116 | + column_name: str | None = Query(default=None), |
| 117 | + test_type: str | None = Query(default=None), |
| 118 | + disposition: Disposition | None = Query(default=None), # noqa: B008 |
| 119 | + page: int = Query(default=1, ge=1), |
| 120 | + limit: int = Query(default=20, ge=1, le=100), |
| 121 | +): |
| 122 | + """List individual results for a test run. |
| 123 | +
|
| 124 | + Omitting ``disposition`` returns active results — confirmed and no_decision |
| 125 | + (excludes dismissed and muted); pass an explicit value to filter to one state. |
| 126 | + """ |
| 127 | + clauses = [] |
| 128 | + if status: |
| 129 | + clauses.append(TestResult.status == RESULT_STATUS_TO_DB[status]) |
| 130 | + if table_name: |
| 131 | + clauses.append(TestResult.table_name == table_name) |
| 132 | + if column_name: |
| 133 | + clauses.append(TestResult.column_names == column_name) |
| 134 | + if test_type: |
| 135 | + clauses.append(TestResult.test_type == test_type) |
| 136 | + if disposition is None: |
| 137 | + # Active: confirmed plus no_decision (NULL). Dismissed/muted excluded. |
| 138 | + clauses.append(or_(TestResult.disposition.is_(None), TestResult.disposition == DbDisposition.CONFIRMED.value)) |
| 139 | + elif disposition == Disposition.no_decision: |
| 140 | + clauses.append(TestResult.disposition.is_(None)) |
| 141 | + else: |
| 142 | + clauses.append(TestResult.disposition == DISPOSITION_TO_DB[disposition].value) |
| 143 | + |
| 144 | + items, total = TestResult.list_for_run(job.id, *clauses, page=page, limit=limit) |
| 145 | + return TestResultListResponse( |
| 146 | + items=[_to_item(row) for row in items], |
| 147 | + page=page, |
| 148 | + limit=limit, |
| 149 | + total=total, |
| 150 | + ) |
| 151 | + |
| 152 | + |
66 | 153 | @router.get( |
67 | 154 | "/profiling-runs/{job_id}", |
68 | 155 | response_model=ProfilingRunResponse, |
|
0 commit comments