Fix admin import chain and tests

This commit is contained in:
root
2026-06-06 20:59:23 +00:00
parent fcc22063ed
commit 3548733019
15 changed files with 1818 additions and 1193 deletions
+73
View File
@@ -0,0 +1,73 @@
from backend.database import db
from backend.main import app
from fastapi.testclient import TestClient
client = TestClient(app)
def setup_function() -> None:
db._cases.clear() # type: ignore[attr-defined]
def test_admin_cases_paginates_and_sorts_newest_first() -> None:
db.create_case(age=10, gender='male', status='new')
db.create_case(age=20, gender='female', status='new')
third = db.create_case(age=30, gender='male', status='new')
response = client.get('/api/v1/admin/cases?page=1&page_size=2')
assert response.status_code == 200
payload = response.json()
assert payload['total'] == 3
assert payload['page'] == 1
assert payload['page_size'] == 2
assert [item['id'] for item in payload['items']] == [third.id, payload['items'][1]['id']]
def test_admin_cases_can_filter_by_status_and_age_range() -> None:
db.create_case(age=14, gender='male', status='new')
analyzed_young = db.create_case(age=19, gender='female', status='analyzed')
db.create_case(age=24, gender='male', status='analyzed')
db.create_case(age=29, gender='female', status='closed')
response = client.get('/api/v1/admin/cases?status=analyzed&age_min=18&age_max=21')
assert response.status_code == 200
payload = response.json()
assert payload['total'] == 1
assert [item['id'] for item in payload['items']] == [analyzed_young.id]
assert payload['items'][0]['age'] == 19
assert payload['items'][0]['status'] == 'analyzed'
def test_admin_case_patch_accepts_outcome_fields_and_note_alias() -> None:
case = db.create_case(
age=17,
gender='female',
status='analyzed',
last_known_place='Forest edge',
notes='initial note',
)
response = client.patch(
f'/api/v1/admin/cases/{case.id}',
json={
'found_alive': True,
'found_distance_km': 3.5,
'found_lat': 54.1,
'found_lon': 37.2,
'who_found': 'Search team 4',
'note': 'Found near the logging road',
},
)
assert response.status_code == 200
payload = response.json()
assert payload['found_alive'] is True
assert payload['found_distance_km'] == 3.5
assert payload['found_lat'] == 54.1
assert payload['found_lon'] == 37.2
assert payload['who_found'] == 'Search team 4'
assert payload['notes'] == 'Found near the logging road'
assert payload['last_known_place'] == 'Forest edge'