Added test for review endpoint (severity filter)

This commit is contained in:
Rui Alves 2024-11-20 14:10:18 +00:00
parent edabe94947
commit 3b61d3c4f1
2 changed files with 44 additions and 14 deletions

View File

@ -148,6 +148,7 @@ class BaseTestHttp(unittest.TestCase):
id: str,
start_time: datetime.datetime = datetime.datetime.now().timestamp(),
end_time: datetime.datetime = datetime.datetime.now().timestamp() + 20,
severity:SeverityEnum = SeverityEnum.alert,
) -> Event:
"""Inserts a basic event model with a given id."""
return ReviewSegment.insert(
@ -156,7 +157,7 @@ class BaseTestHttp(unittest.TestCase):
start_time=start_time,
end_time=end_time,
has_been_reviewed=False,
severity=SeverityEnum.alert,
severity=severity,
thumb_path=False,
data={},
).execute()

View File

@ -3,19 +3,20 @@ import datetime
from fastapi.testclient import TestClient
from frigate.models import Event, ReviewSegment
from frigate.review.maintainer import SeverityEnum
from frigate.test.http_api.base_http_test import BaseTestHttp
class TestHttpReview(BaseTestHttp):
def setUp(self):
super().setUp([Event, ReviewSegment])
self.app = super().create_app()
# Does not return any data point since the end time (before parameter) is not passed and the review segment end_time is 2 seconds from now
def test_get_review_no_filters_no_matches(self):
app = super().create_app()
now = datetime.datetime.now().timestamp()
with TestClient(app) as client:
with TestClient(self.app) as client:
super().insert_mock_review_segment("123456.random", now, now + 2)
reviews_response = client.get("/review")
assert reviews_response.status_code == 200
@ -23,10 +24,9 @@ class TestHttpReview(BaseTestHttp):
assert len(reviews_in_response) == 0
def test_get_review_no_filters(self):
app = super().create_app()
now = datetime.datetime.now().timestamp()
with TestClient(app) as client:
with TestClient(self.app) as client:
super().insert_mock_review_segment("123456.random", now - 2, now - 1)
reviews_response = client.get("/review")
assert reviews_response.status_code == 200
@ -34,10 +34,9 @@ class TestHttpReview(BaseTestHttp):
assert len(reviews_in_response) == 1
def test_get_review_with_time_filter_no_matches(self):
app = super().create_app()
now = datetime.datetime.now().timestamp()
with TestClient(app) as client:
with TestClient(self.app) as client:
id = "123456.random"
super().insert_mock_review_segment(id, now, now + 2)
params = {
@ -50,10 +49,9 @@ class TestHttpReview(BaseTestHttp):
assert len(reviews_in_response) == 0
def test_get_review_with_time_filter(self):
app = super().create_app()
now = datetime.datetime.now().timestamp()
with TestClient(app) as client:
with TestClient(self.app) as client:
id = "123456.random"
super().insert_mock_review_segment(id, now, now + 2)
params = {
@ -67,10 +65,9 @@ class TestHttpReview(BaseTestHttp):
assert reviews_in_response[0]["id"] == id
def test_get_review_with_limit_filter(self):
app = super().create_app()
now = datetime.datetime.now().timestamp()
with TestClient(app) as client:
with TestClient(self.app) as client:
id = "123456.random"
id2 = "654321.random"
super().insert_mock_review_segment(id, now, now + 2)
@ -86,11 +83,43 @@ class TestHttpReview(BaseTestHttp):
assert len(reviews_in_response) == 1
assert reviews_in_response[0]["id"] == id2
def test_get_review_with_all_filters(self):
app = super().create_app()
def test_get_review_with_severity_filters_no_matches(self):
now = datetime.datetime.now().timestamp()
with TestClient(app) as client:
with TestClient(self.app) as client:
id = "123456.random"
super().insert_mock_review_segment(id, now, now + 2, SeverityEnum.detection)
params = {
"severity": "detection",
"after": now - 1,
"before": now + 3,
}
reviews_response = client.get("/review", params=params)
assert reviews_response.status_code == 200
reviews_in_response = reviews_response.json()
assert len(reviews_in_response) == 1
assert reviews_in_response[0]["id"] == id
def test_get_review_with_severity_filters(self):
now = datetime.datetime.now().timestamp()
with TestClient(self.app) as client:
id = "123456.random"
super().insert_mock_review_segment(id, now, now + 2, SeverityEnum.detection)
params = {
"severity": "alert",
"after": now - 1,
"before": now + 3,
}
reviews_response = client.get("/review", params=params)
assert reviews_response.status_code == 200
reviews_in_response = reviews_response.json()
assert len(reviews_in_response) == 0
def test_get_review_with_all_filters(self):
now = datetime.datetime.now().timestamp()
with TestClient(self.app) as client:
id = "123456.random"
super().insert_mock_review_segment(id, now, now + 2)
params = {