|
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,68 @@ def get_test_run(job: JobExecution = resolve_job("view", JobExecution.job_key == |
63 | 74 | ) |
64 | 75 |
|
65 | 76 |
|
| 77 | +def _to_item(row: TestRunResultRow) -> TestResultItem: |
| 78 | + """Map a DB-valued result row to the API item, normalizing enum casing.""" |
| 79 | + return TestResultItem( |
| 80 | + test_definition_id=row.test_definition_id, |
| 81 | + test_type=row.test_type, |
| 82 | + schema_name=row.schema_name, |
| 83 | + table_name=row.table_name, |
| 84 | + column_names=row.column_names, |
| 85 | + result_status=RESULT_STATUS_FROM_DB.get(row.status), |
| 86 | + result_measure=row.result_measure, |
| 87 | + threshold_value=row.threshold_value, |
| 88 | + result_message=row.message, |
| 89 | + test_time=row.test_time, |
| 90 | + disposition=DISPOSITION_FROM_DB[DbDisposition(row.disposition)] if row.disposition else Disposition.no_decision, |
| 91 | + ) |
| 92 | + |
| 93 | + |
| 94 | +@router.get( |
| 95 | + "/test-runs/{job_id}/results", |
| 96 | + response_model=TestResultListResponse, |
| 97 | +) |
| 98 | +def list_test_run_results( |
| 99 | + job: JobExecution = resolve_job("view", JobExecution.job_key == JobKey.run_tests), # noqa: B008 |
| 100 | + status: ResultStatus | None = Query(default=None), # noqa: B008 |
| 101 | + table_name: str | None = Query(default=None), |
| 102 | + column_name: str | None = Query(default=None), |
| 103 | + test_type: str | None = Query(default=None), |
| 104 | + disposition: Disposition | None = Query(default=None), # noqa: B008 |
| 105 | + page: int = Query(default=1, ge=1), |
| 106 | + limit: int = Query(default=20, ge=1, le=100), |
| 107 | +): |
| 108 | + """List individual results for a test run. |
| 109 | +
|
| 110 | + Omitting ``disposition`` returns active results — confirmed and no_decision |
| 111 | + (excludes dismissed and muted); pass an explicit value to filter to one state. |
| 112 | + """ |
| 113 | + clauses = [] |
| 114 | + if status: |
| 115 | + clauses.append(TestResult.status == RESULT_STATUS_TO_DB[status]) |
| 116 | + if table_name: |
| 117 | + clauses.append(TestResult.table_name == table_name) |
| 118 | + if column_name: |
| 119 | + clauses.append(TestResult.column_names == column_name) |
| 120 | + if test_type: |
| 121 | + clauses.append(TestResult.test_type == test_type) |
| 122 | + if disposition is None: |
| 123 | + # Active: confirmed plus no_decision (NULL). Dismissed/muted excluded. |
| 124 | + clauses.append(or_(TestResult.disposition.is_(None), TestResult.disposition == DbDisposition.CONFIRMED.value)) |
| 125 | + elif disposition == Disposition.no_decision: |
| 126 | + clauses.append(TestResult.disposition.is_(None)) |
| 127 | + else: |
| 128 | + clauses.append(TestResult.disposition == DISPOSITION_TO_DB[disposition].value) |
| 129 | + |
| 130 | + items, total = TestResult.list_for_run(job.id, *clauses, page=page, limit=limit) |
| 131 | + return TestResultListResponse( |
| 132 | + items=[_to_item(row) for row in items], |
| 133 | + page=page, |
| 134 | + limit=limit, |
| 135 | + total=total, |
| 136 | + ) |
| 137 | + |
| 138 | + |
66 | 139 | @router.get( |
67 | 140 | "/profiling-runs/{job_id}", |
68 | 141 | response_model=ProfilingRunResponse, |
|
0 commit comments