From 3b61d3c4f1b24aa6c49fdc2e3d75848e49eba8be Mon Sep 17 00:00:00 2001 From: Rui Alves Date: Wed, 20 Nov 2024 14:10:18 +0000 Subject: [PATCH] Added test for review endpoint (severity filter) --- frigate/test/http_api/base_http_test.py | 3 +- frigate/test/http_api/test_http_review.py | 55 +++++++++++++++++------ 2 files changed, 44 insertions(+), 14 deletions(-) diff --git a/frigate/test/http_api/base_http_test.py b/frigate/test/http_api/base_http_test.py index 013785692..ee4de1ec0 100644 --- a/frigate/test/http_api/base_http_test.py +++ b/frigate/test/http_api/base_http_test.py @@ -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() diff --git a/frigate/test/http_api/test_http_review.py b/frigate/test/http_api/test_http_review.py index 19e1f26f8..57c92934c 100644 --- a/frigate/test/http_api/test_http_review.py +++ b/frigate/test/http_api/test_http_review.py @@ -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 = {