Spaces:
Sleeping
Sleeping
Deprotate commited on
Commit ·
2afbca6
1
Parent(s): 2344dcb
docker3
Browse files- main2.py +34 -9
- tests/test_main2.py +16 -0
main2.py
CHANGED
|
@@ -42,6 +42,17 @@ class TestHealthResponse(BaseSchema):
|
|
| 42 |
cases_count: int
|
| 43 |
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
class TestMessage(BaseSchema):
|
| 46 |
role: str
|
| 47 |
content: str
|
|
@@ -378,21 +389,25 @@ def create_main2_app(repository: JsonRepository | None = None) -> FastAPI:
|
|
| 378 |
app.state.repository = repository or JsonRepository(DATA_DIR)
|
| 379 |
app.state.test_state = create_test_state()
|
| 380 |
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
|
|
|
| 390 |
|
| 391 |
if api_key and folder_id:
|
| 392 |
app.state.case_service = YandexCaseService(api_key=api_key, folder_id=folder_id)
|
| 393 |
else:
|
| 394 |
app.state.case_service = None
|
| 395 |
-
app.state.service_error = "YANDEX_API_KEY and YANDEX_FOLDER_ID are required when
|
|
|
|
|
|
|
|
|
|
| 396 |
|
| 397 |
def get_repository() -> JsonRepository:
|
| 398 |
return app.state.repository
|
|
@@ -468,6 +483,16 @@ def create_main2_app(repository: JsonRepository | None = None) -> FastAPI:
|
|
| 468 |
cases_count=len(get_repository().list_cases()),
|
| 469 |
)
|
| 470 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 471 |
@app.get("/test/cases", response_model=list[BusinessCaseSummary])
|
| 472 |
def list_cases() -> list[BusinessCaseSummary]:
|
| 473 |
return [BusinessCaseSummary.model_validate(item) for item in get_repository().list_cases()]
|
|
|
|
| 42 |
cases_count: int
|
| 43 |
|
| 44 |
|
| 45 |
+
class TestModeSwitchRequest(BaseSchema):
|
| 46 |
+
mode: str = Field(pattern="^(mock|yandex)$")
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
class TestModeSwitchResponse(BaseSchema):
|
| 50 |
+
status: str
|
| 51 |
+
mode: str
|
| 52 |
+
ready: bool
|
| 53 |
+
error: str | None = None
|
| 54 |
+
|
| 55 |
+
|
| 56 |
class TestMessage(BaseSchema):
|
| 57 |
role: str
|
| 58 |
content: str
|
|
|
|
| 389 |
app.state.repository = repository or JsonRepository(DATA_DIR)
|
| 390 |
app.state.test_state = create_test_state()
|
| 391 |
|
| 392 |
+
def configure_mode(mode: str) -> None:
|
| 393 |
+
app.state.mode = mode
|
| 394 |
+
app.state.service_error = None
|
| 395 |
|
| 396 |
+
if mode == "mock":
|
| 397 |
+
app.state.case_service = MockCaseService()
|
| 398 |
+
return
|
| 399 |
+
|
| 400 |
+
api_key = os.getenv("YANDEX_API_KEY", "AQVN164qFPx2CKKmz9OreO_zs9s9FAn4mZ9qz69D")
|
| 401 |
+
folder_id = os.getenv("YANDEX_FOLDER_ID", "b1grlh2bqatdjmcl9tt0")
|
| 402 |
|
| 403 |
if api_key and folder_id:
|
| 404 |
app.state.case_service = YandexCaseService(api_key=api_key, folder_id=folder_id)
|
| 405 |
else:
|
| 406 |
app.state.case_service = None
|
| 407 |
+
app.state.service_error = "YANDEX_API_KEY and YANDEX_FOLDER_ID are required when mode=yandex"
|
| 408 |
+
|
| 409 |
+
use_mock = parse_bool_env("TEST_USE_MOCK", default=False)
|
| 410 |
+
configure_mode("mock" if use_mock else "yandex")
|
| 411 |
|
| 412 |
def get_repository() -> JsonRepository:
|
| 413 |
return app.state.repository
|
|
|
|
| 483 |
cases_count=len(get_repository().list_cases()),
|
| 484 |
)
|
| 485 |
|
| 486 |
+
@app.post("/test/mode", response_model=TestModeSwitchResponse)
|
| 487 |
+
def switch_mode(payload: TestModeSwitchRequest) -> TestModeSwitchResponse:
|
| 488 |
+
configure_mode(payload.mode)
|
| 489 |
+
return TestModeSwitchResponse(
|
| 490 |
+
status="ok" if app.state.case_service is not None else "misconfigured",
|
| 491 |
+
mode=app.state.mode,
|
| 492 |
+
ready=app.state.case_service is not None,
|
| 493 |
+
error=app.state.service_error,
|
| 494 |
+
)
|
| 495 |
+
|
| 496 |
@app.get("/test/cases", response_model=list[BusinessCaseSummary])
|
| 497 |
def list_cases() -> list[BusinessCaseSummary]:
|
| 498 |
return [BusinessCaseSummary.model_validate(item) for item in get_repository().list_cases()]
|
tests/test_main2.py
CHANGED
|
@@ -32,11 +32,27 @@ def test_health_and_openapi(client: TestClient) -> None:
|
|
| 32 |
openapi = client.get("/openapi.json")
|
| 33 |
assert openapi.status_code == 200
|
| 34 |
paths = openapi.json()["paths"]
|
|
|
|
| 35 |
assert "/test/case/start" in paths
|
| 36 |
assert "/test/cases/progress" in paths
|
| 37 |
assert all(path.startswith("/test") for path in paths)
|
| 38 |
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
def test_cases_list_and_detail(client: TestClient) -> None:
|
| 41 |
cases = client.get("/test/cases")
|
| 42 |
assert cases.status_code == 200
|
|
|
|
| 32 |
openapi = client.get("/openapi.json")
|
| 33 |
assert openapi.status_code == 200
|
| 34 |
paths = openapi.json()["paths"]
|
| 35 |
+
assert "/test/mode" in paths
|
| 36 |
assert "/test/case/start" in paths
|
| 37 |
assert "/test/cases/progress" in paths
|
| 38 |
assert all(path.startswith("/test") for path in paths)
|
| 39 |
|
| 40 |
|
| 41 |
+
def test_switch_mode_endpoint(client: TestClient) -> None:
|
| 42 |
+
to_yandex = client.post("/test/mode", json={"mode": "yandex"})
|
| 43 |
+
assert to_yandex.status_code == 200
|
| 44 |
+
assert to_yandex.json()["mode"] == "yandex"
|
| 45 |
+
|
| 46 |
+
health = client.get("/test/health")
|
| 47 |
+
assert health.status_code == 200
|
| 48 |
+
assert health.json()["mode"] == "yandex"
|
| 49 |
+
|
| 50 |
+
to_mock = client.post("/test/mode", json={"mode": "mock"})
|
| 51 |
+
assert to_mock.status_code == 200
|
| 52 |
+
assert to_mock.json()["mode"] == "mock"
|
| 53 |
+
assert to_mock.json()["ready"] is True
|
| 54 |
+
|
| 55 |
+
|
| 56 |
def test_cases_list_and_detail(client: TestClient) -> None:
|
| 57 |
cases = client.get("/test/cases")
|
| 58 |
assert cases.status_code == 200
|