-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtest_definitions.py
More file actions
71 lines (61 loc) · 2.28 KB
/
test_definitions.py
File metadata and controls
71 lines (61 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""API v1 — test definition export and import."""
from fastapi import APIRouter, Depends, HTTPException, Query
from testgen.api import test_definition_service
from testgen.api.deps import db_session, resolve_test_suite
from testgen.api.schemas import (
ErrorDetail,
ErrorResponse,
ExportDocument,
ImportMode,
ImportRequest,
ImportResponse,
ImportStrictError,
Origin,
)
from testgen.common.models.test_suite import TestSuite
_error_responses = {
404: {"model": ErrorResponse, "description": "Not found"},
}
router = APIRouter(
tags=["Test Definitions"],
dependencies=[Depends(db_session)],
responses=_error_responses,
)
@router.get(
"/test-suites/{test_suite_id}/test-definition-export",
response_model=ExportDocument,
response_model_exclude_defaults=True,
)
def export_test_definitions(
test_suite: TestSuite = resolve_test_suite("view"), # noqa: B008
origin: Origin = Query(default=Origin.both), # noqa: B008
table_name: str | None = Query(default=None),
test_type: str | None = Query(default=None),
) -> ExportDocument:
"""Export test definitions from a test suite as a portable JSON document."""
return test_definition_service.export_definitions(test_suite, origin, table_name, test_type)
@router.post(
"/test-suites/{test_suite_id}/test-definition-import",
response_model=ImportResponse,
responses={
400: {"model": ImportStrictError, "description": "Invalid request or strict validation failed"},
},
)
def import_test_definitions(
body: ImportRequest,
test_suite: TestSuite = resolve_test_suite("edit"), # noqa: B008
) -> ImportResponse:
"""Import test definitions into a test suite from a portable JSON document."""
result = test_definition_service.import_definitions(test_suite, body.config, body.payload)
if body.config.mode == ImportMode.apply_strict and result.summary.skipped > 0:
raise HTTPException(
status_code=400,
detail=ImportStrictError(
errors=[ErrorDetail(
code="strict_validation_failed",
detail=f"{result.summary.skipped} test definition(s) would be skipped",
)],
import_result=result,
).model_dump(mode="json"),
)
return result