-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api.py
More file actions
70 lines (49 loc) · 2.24 KB
/
test_api.py
File metadata and controls
70 lines (49 loc) · 2.24 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
"""Tests for the FastAPI scaffold (`/api/v1/health`, `/api/v1/echo`)."""
from __future__ import annotations
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from fastapi.testclient import TestClient
def test_health_returns_ok_with_version(client: TestClient) -> None:
response = client.get("/api/v1/health")
assert response.status_code == 200
body = response.json()
assert body["status"] == "ok"
assert isinstance(body["version"], str)
assert body["version"] # non-empty
def test_health_response_rejects_unknown_keys() -> None:
"""The HealthResponse contract is StrictModel — extra keys raise."""
from pydantic import ValidationError
from src.models.health import HealthResponse
try:
HealthResponse(status="ok", version="0.1.0", extra="boom") # type: ignore[call-arg]
except ValidationError as exc:
assert "extra" in str(exc)
else:
msg = "expected ValidationError on unknown key"
raise AssertionError(msg)
def test_echo_returns_message(client: TestClient) -> None:
response = client.get("/api/v1/echo", params={"msg": "hi"})
assert response.status_code == 200
assert response.json() == {"echoed": "hi"}
def test_echo_requires_msg_query_param(client: TestClient) -> None:
response = client.get("/api/v1/echo")
assert response.status_code == 422 # FastAPI's missing-required-query default
def test_session_store_create_and_get() -> None:
"""Sessions store roundtrip — pattern test (not exercised by an endpoint)."""
from src.api.sessions import SessionStore
store = SessionStore()
info = store.create()
assert info.message_count == 0
fetched = store.get(str(info.session_id))
assert fetched is not None
assert fetched.session_id == info.session_id
store.set_messages(str(info.session_id), [{"role": "user", "content": "hi"}])
again = store.get(str(info.session_id))
assert again is not None
assert again.message_count == 1
def test_session_store_get_unknown_returns_none() -> None:
from src.api.sessions import SessionStore
store = SessionStore()
assert store.get("does-not-exist") is None
assert store.get_messages("does-not-exist") is None
assert store.exists("does-not-exist") is False