mirror of
https://github.com/blakeblackshear/frigate.git
synced 2025-02-05 00:15:51 +01:00
Added unit tests for GET /events Endpoint
This commit is contained in:
parent
896649790c
commit
3aaf6713ae
@ -126,6 +126,10 @@ class BaseTestHttp(unittest.TestCase):
|
|||||||
id: str,
|
id: str,
|
||||||
start_time: float = datetime.datetime.now().timestamp(),
|
start_time: float = datetime.datetime.now().timestamp(),
|
||||||
end_time: float = datetime.datetime.now().timestamp() + 20,
|
end_time: float = datetime.datetime.now().timestamp() + 20,
|
||||||
|
has_clip: bool = True,
|
||||||
|
top_score: int = 100,
|
||||||
|
score: int = 0,
|
||||||
|
data: Json = {},
|
||||||
) -> Event:
|
) -> Event:
|
||||||
"""Inserts a basic event model with a given id."""
|
"""Inserts a basic event model with a given id."""
|
||||||
return Event.insert(
|
return Event.insert(
|
||||||
@ -134,15 +138,17 @@ class BaseTestHttp(unittest.TestCase):
|
|||||||
camera="front_door",
|
camera="front_door",
|
||||||
start_time=start_time,
|
start_time=start_time,
|
||||||
end_time=end_time,
|
end_time=end_time,
|
||||||
top_score=100,
|
top_score=top_score,
|
||||||
|
score=score,
|
||||||
false_positive=False,
|
false_positive=False,
|
||||||
zones=list(),
|
zones=list(),
|
||||||
thumbnail="",
|
thumbnail="",
|
||||||
region=[],
|
region=[],
|
||||||
box=[],
|
box=[],
|
||||||
area=0,
|
area=0,
|
||||||
has_clip=True,
|
has_clip=has_clip,
|
||||||
has_snapshot=True,
|
has_snapshot=True,
|
||||||
|
data=data,
|
||||||
).execute()
|
).execute()
|
||||||
|
|
||||||
def insert_mock_review_segment(
|
def insert_mock_review_segment(
|
||||||
|
137
frigate/test/http_api/test_http_event.py
Normal file
137
frigate/test/http_api/test_http_event.py
Normal file
@ -0,0 +1,137 @@
|
|||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
|
from frigate.models import Event, Recordings, ReviewSegment
|
||||||
|
from frigate.test.http_api.base_http_test import BaseTestHttp
|
||||||
|
|
||||||
|
|
||||||
|
class TestHttpApp(BaseTestHttp):
|
||||||
|
def setUp(self):
|
||||||
|
super().setUp([Event, Recordings, ReviewSegment])
|
||||||
|
self.app = super().create_app()
|
||||||
|
|
||||||
|
####################################################################################################################
|
||||||
|
################################### GET /events Endpoint #########################################################
|
||||||
|
####################################################################################################################
|
||||||
|
def test_get_event_list_no_events(self):
|
||||||
|
with TestClient(self.app) as client:
|
||||||
|
events = client.get("/events").json()
|
||||||
|
assert len(events) == 0
|
||||||
|
|
||||||
|
def test_get_event_list_no_match_event_id(self):
|
||||||
|
id = "123456.random"
|
||||||
|
with TestClient(self.app) as client:
|
||||||
|
super().insert_mock_event(id)
|
||||||
|
events = client.get("/events", params={"event_id": "abc"}).json()
|
||||||
|
assert len(events) == 0
|
||||||
|
|
||||||
|
def test_get_event_list_match_event_id(self):
|
||||||
|
id = "123456.random"
|
||||||
|
with TestClient(self.app) as client:
|
||||||
|
super().insert_mock_event(id)
|
||||||
|
events = client.get("/events", params={"event_id": id}).json()
|
||||||
|
assert len(events) == 1
|
||||||
|
assert events[0]["id"] == id
|
||||||
|
|
||||||
|
def test_get_event_list_match_length(self):
|
||||||
|
now = int(datetime.now().timestamp())
|
||||||
|
|
||||||
|
id = "123456.random"
|
||||||
|
with TestClient(self.app) as client:
|
||||||
|
super().insert_mock_event(id, now, now + 1)
|
||||||
|
events = client.get(
|
||||||
|
"/events", params={"max_length": 1, "min_length": 1}
|
||||||
|
).json()
|
||||||
|
assert len(events) == 1
|
||||||
|
assert events[0]["id"] == id
|
||||||
|
|
||||||
|
def test_get_event_list_no_match_max_length(self):
|
||||||
|
now = int(datetime.now().timestamp())
|
||||||
|
|
||||||
|
with TestClient(self.app) as client:
|
||||||
|
id = "123456.random"
|
||||||
|
super().insert_mock_event(id, now, now + 2)
|
||||||
|
events = client.get("/events", params={"max_length": 1}).json()
|
||||||
|
assert len(events) == 0
|
||||||
|
|
||||||
|
def test_get_event_list_no_match_min_length(self):
|
||||||
|
now = int(datetime.now().timestamp())
|
||||||
|
|
||||||
|
with TestClient(self.app) as client:
|
||||||
|
id = "123456.random"
|
||||||
|
super().insert_mock_event(id, now, now + 2)
|
||||||
|
events = client.get("/events", params={"min_length": 3}).json()
|
||||||
|
assert len(events) == 0
|
||||||
|
|
||||||
|
def test_get_event_list_limit(self):
|
||||||
|
id = "123456.random"
|
||||||
|
id2 = "54321.random"
|
||||||
|
|
||||||
|
with TestClient(self.app) as client:
|
||||||
|
super().insert_mock_event(id)
|
||||||
|
events = client.get("/events").json()
|
||||||
|
assert len(events) == 1
|
||||||
|
assert events[0]["id"] == id
|
||||||
|
|
||||||
|
super().insert_mock_event(id2)
|
||||||
|
events = client.get("/events").json()
|
||||||
|
assert len(events) == 2
|
||||||
|
|
||||||
|
events = client.get("/events", params={"limit": 1}).json()
|
||||||
|
assert len(events) == 1
|
||||||
|
assert events[0]["id"] == id
|
||||||
|
|
||||||
|
events = client.get("/events", params={"limit": 3}).json()
|
||||||
|
assert len(events) == 2
|
||||||
|
|
||||||
|
def test_get_event_list_no_match_has_clip(self):
|
||||||
|
now = int(datetime.now().timestamp())
|
||||||
|
|
||||||
|
with TestClient(self.app) as client:
|
||||||
|
id = "123456.random"
|
||||||
|
super().insert_mock_event(id, now, now + 2)
|
||||||
|
events = client.get("/events", params={"has_clip": 0}).json()
|
||||||
|
assert len(events) == 0
|
||||||
|
|
||||||
|
def test_get_event_list_has_clip(self):
|
||||||
|
with TestClient(self.app) as client:
|
||||||
|
id = "123456.random"
|
||||||
|
super().insert_mock_event(id, has_clip=True)
|
||||||
|
events = client.get("/events", params={"has_clip": 1}).json()
|
||||||
|
assert len(events) == 1
|
||||||
|
assert events[0]["id"] == id
|
||||||
|
|
||||||
|
def test_get_event_list_sort_score(self):
|
||||||
|
with TestClient(self.app) as client:
|
||||||
|
id = "123456.random"
|
||||||
|
id2 = "54321.random"
|
||||||
|
super().insert_mock_event(id, top_score=37, score=37, data={"score": 50})
|
||||||
|
super().insert_mock_event(id2, top_score=47, score=47, data={"score": 20})
|
||||||
|
events = client.get("/events", params={"sort": "score_asc"}).json()
|
||||||
|
assert len(events) == 2
|
||||||
|
assert events[0]["id"] == id2
|
||||||
|
assert events[1]["id"] == id
|
||||||
|
|
||||||
|
events = client.get("/events", params={"sort": "score_des"}).json()
|
||||||
|
assert len(events) == 2
|
||||||
|
assert events[0]["id"] == id
|
||||||
|
assert events[1]["id"] == id2
|
||||||
|
|
||||||
|
def test_get_event_list_sort_start_time(self):
|
||||||
|
now = int(datetime.now().timestamp())
|
||||||
|
|
||||||
|
with TestClient(self.app) as client:
|
||||||
|
id = "123456.random"
|
||||||
|
id2 = "54321.random"
|
||||||
|
super().insert_mock_event(id, start_time=now+3)
|
||||||
|
super().insert_mock_event(id2, start_time=now)
|
||||||
|
events = client.get("/events", params={"sort": "date_asc"}).json()
|
||||||
|
assert len(events) == 2
|
||||||
|
assert events[0]["id"] == id2
|
||||||
|
assert events[1]["id"] == id
|
||||||
|
|
||||||
|
events = client.get("/events", params={"sort": "date_desc"}).json()
|
||||||
|
assert len(events) == 2
|
||||||
|
assert events[0]["id"] == id
|
||||||
|
assert events[1]["id"] == id2
|
Loading…
Reference in New Issue
Block a user