Fix test_http to use TestClient

This commit is contained in:
Rui Alves 2024-09-22 19:25:29 +01:00
parent cdc267cc36
commit be01c9ec76
4 changed files with 42 additions and 52 deletions

View File

@ -520,7 +520,7 @@ def get_labels(camera: str = ""):
@router.get("/sub_labels") @router.get("/sub_labels")
def get_sub_labels(split_joined: int): def get_sub_labels(split_joined: Optional[int] = None):
try: try:
events = Event.select(Event.sub_label).distinct() events = Event.select(Event.sub_label).distinct()
except Exception: except Exception:

View File

@ -6,7 +6,7 @@ from pydantic import BaseModel, Field
class EventsSubLabelBody(BaseModel): class EventsSubLabelBody(BaseModel):
subLabel: str subLabel: str
subLabelScore: float subLabelScore: Optional[float] = None
class EventsDescriptionBody(BaseModel): class EventsDescriptionBody(BaseModel):

View File

@ -854,17 +854,7 @@ def set_sub_label(
new_sub_label = body.subLabel new_sub_label = body.subLabel
new_score = body.subLabelScore new_score = body.subLabelScore
if new_sub_label is None: # TODO: Rui Move this validation to the EventsSubLabelBody. Str between 0 and 100 chars
return JSONResponse(
content=(
{
"success": False,
"message": "A sub label must be supplied",
}
),
status_code=400,
)
if new_sub_label and len(new_sub_label) > 100: if new_sub_label and len(new_sub_label) > 100:
return JSONResponse( return JSONResponse(
content=( content=(
@ -877,6 +867,7 @@ def set_sub_label(
status_code=400, status_code=400,
) )
# TODO: Rui Move this validation to the EventsSubLabelBody. Str between 0 and 100 chars
if new_score is not None and (new_score > 1.0 or new_score < 0): if new_score is not None and (new_score > 1.0 or new_score < 0):
return JSONResponse( return JSONResponse(
content=( content=(

View File

@ -1,5 +1,4 @@
import datetime import datetime
import json
import logging import logging
import os import os
import unittest import unittest
@ -130,25 +129,24 @@ class TestHttp(unittest.TestCase):
with TestClient(app) as client: with TestClient(app) as client:
_insert_mock_event(id) _insert_mock_event(id)
# TODO: Rui. All the tests are now broken since there's no .json in the FastAPI client.get/post requests events = client.get("/events").json()
events = client.get("/events").json
assert events assert events
assert len(events) == 1 assert len(events) == 1
assert events[0]["id"] == id assert events[0]["id"] == id
_insert_mock_event(id2) _insert_mock_event(id2)
events = client.get("/events").json events = client.get("/events").json()
assert events assert events
assert len(events) == 2 assert len(events) == 2
events = client.get( events = client.get(
"/events", "/events",
query_string={"limit": 1}, params={"limit": 1},
).json ).json()
assert events assert events
assert len(events) == 1 assert len(events) == 1
events = client.get( events = client.get(
"/events", "/events",
query_string={"has_clip": 0}, params={"has_clip": 0},
).json ).json()
assert not events assert not events
def test_get_good_event(self): def test_get_good_event(self):
@ -167,7 +165,7 @@ class TestHttp(unittest.TestCase):
with TestClient(app) as client: with TestClient(app) as client:
_insert_mock_event(id) _insert_mock_event(id)
event = client.get(f"/events/{id}").json event = client.get(f"/events/{id}").json()
assert event assert event
assert event["id"] == id assert event["id"] == id
@ -190,9 +188,9 @@ class TestHttp(unittest.TestCase):
with TestClient(app) as client: with TestClient(app) as client:
_insert_mock_event(id) _insert_mock_event(id)
event = client.get(f"/events/{bad_id}").json event_response = client.get(f"/events/{bad_id}")
assert event_response.status_code == 404
assert not event assert event_response.json() == "Event not found"
def test_delete_event(self): def test_delete_event(self):
app = create_fastapi_app( app = create_fastapi_app(
@ -210,11 +208,11 @@ class TestHttp(unittest.TestCase):
with TestClient(app) as client: with TestClient(app) as client:
_insert_mock_event(id) _insert_mock_event(id)
event = client.get(f"/events/{id}").json event = client.get(f"/events/{id}").json()
assert event assert event
assert event["id"] == id assert event["id"] == id
client.delete(f"/events/{id}") client.delete(f"/events/{id}")
event = client.get(f"/events/{id}").json event = client.get(f"/events/{id}").json()
assert not event assert not event
def test_event_retention(self): def test_event_retention(self):
@ -234,12 +232,12 @@ class TestHttp(unittest.TestCase):
with TestClient(app) as client: with TestClient(app) as client:
_insert_mock_event(id) _insert_mock_event(id)
client.post(f"/events/{id}/retain") client.post(f"/events/{id}/retain")
event = client.get(f"/events/{id}").json event = client.get(f"/events/{id}").json()
assert event assert event
assert event["id"] == id assert event["id"] == id
assert event["retain_indefinitely"] is True assert event["retain_indefinitely"] is True
client.delete(f"/events/{id}/retain") client.delete(f"/events/{id}/retain")
event = client.get(f"/events/{id}").json event = client.get(f"/events/{id}").json()
assert event assert event
assert event["id"] == id assert event["id"] == id
assert event["retain_indefinitely"] is False assert event["retain_indefinitely"] is False
@ -265,21 +263,21 @@ class TestHttp(unittest.TestCase):
_insert_mock_event(morning_id, morning) _insert_mock_event(morning_id, morning)
_insert_mock_event(evening_id, evening) _insert_mock_event(evening_id, evening)
# both events come back # both events come back
events = client.get("/events").json events = client.get("/events").json()
assert events assert events
assert len(events) == 2 assert len(events) == 2
# morning event is excluded # morning event is excluded
events = client.get( events = client.get(
"/events", "/events",
query_string={"time_range": "07:00,24:00"}, params={"time_range": "07:00,24:00"},
).json ).json()
assert events assert events
# assert len(events) == 1 # assert len(events) == 1
# evening event is excluded # evening event is excluded
events = client.get( events = client.get(
"/events", "/events",
query_string={"time_range": "00:00,18:00"}, params={"time_range": "00:00,18:00"},
).json ).json()
assert events assert events
assert len(events) == 1 assert len(events) == 1
@ -300,21 +298,21 @@ class TestHttp(unittest.TestCase):
with TestClient(app) as client: with TestClient(app) as client:
_insert_mock_event(id) _insert_mock_event(id)
client.post( new_sub_label_response = client.post(
f"/events/{id}/sub_label", f"/events/{id}/sub_label",
data=json.dumps({"subLabel": sub_label}), json={"subLabel": sub_label},
content_type="application/json",
) )
event = client.get(f"/events/{id}").json assert new_sub_label_response.status_code == 200
event = client.get(f"/events/{id}").json()
assert event assert event
assert event["id"] == id assert event["id"] == id
assert event["sub_label"] == sub_label assert event["sub_label"] == sub_label
client.post( empty_sub_label_response = client.post(
f"/events/{id}/sub_label", f"/events/{id}/sub_label",
data=json.dumps({"subLabel": ""}), json={"subLabel": ""},
content_type="application/json",
) )
event = client.get(f"/events/{id}").json assert empty_sub_label_response.status_code == 200
event = client.get(f"/events/{id}").json()
assert event assert event
assert event["id"] == id assert event["id"] == id
assert event["sub_label"] == "" assert event["sub_label"] == ""
@ -338,10 +336,9 @@ class TestHttp(unittest.TestCase):
_insert_mock_event(id) _insert_mock_event(id)
client.post( client.post(
f"/events/{id}/sub_label", f"/events/{id}/sub_label",
data=json.dumps({"subLabel": sub_label}), json={"subLabel": sub_label},
content_type="application/json",
) )
sub_labels = client.get("/sub_labels").json sub_labels = client.get("/sub_labels").json()
assert sub_labels assert sub_labels
assert sub_labels == [sub_label] assert sub_labels == [sub_label]
@ -359,13 +356,15 @@ class TestHttp(unittest.TestCase):
) )
with TestClient(app) as client: with TestClient(app) as client:
config = client.get("/config").json config = client.get("/config").json()
assert config assert config
assert config["cameras"]["front_door"] assert config["cameras"]["front_door"]
def test_recordings(self): def test_recordings(self):
app = create_fastapi_app( app = create_fastapi_app(
FrigateConfig(**self.minimal_config).runtime_config(), FrigateConfig(**self.minimal_config),
self.db,
None,
None, None,
None, None,
None, None,
@ -375,9 +374,9 @@ class TestHttp(unittest.TestCase):
) )
id = "123456.random" id = "123456.random"
_insert_mock_recording(id)
with TestClient(app) as client: with TestClient(app) as client:
response = client.get("/media/camera/front_door/recordings") _insert_mock_recording(id)
response = client.get("/front_door/recordings")
assert response.status_code == 200 assert response.status_code == 200
recording = response.json() recording = response.json()
assert recording assert recording
@ -399,7 +398,7 @@ class TestHttp(unittest.TestCase):
) )
with TestClient(app) as client: with TestClient(app) as client:
full_stats = client.get("/stats").json full_stats = client.get("/stats").json()
assert full_stats == self.test_stats assert full_stats == self.test_stats
@ -432,8 +431,8 @@ def _insert_mock_recording(id: str) -> Event:
id=id, id=id,
camera="front_door", camera="front_door",
path=f"/recordings/{id}", path=f"/recordings/{id}",
start_time=datetime.datetime.now().timestamp() - 50, start_time=datetime.datetime.now().timestamp() - 60,
end_time=datetime.datetime.now().timestamp() - 60, end_time=datetime.datetime.now().timestamp() - 50,
duration=10, duration=10,
motion=True, motion=True,
objects=True, objects=True,