From cce41cbc7bb6fc44a4ced771139696cf29465a40 Mon Sep 17 00:00:00 2001 From: Rui Alves Date: Sun, 12 Jan 2025 14:19:55 +0000 Subject: [PATCH] Started unit tests for GET /review/activity/motion Endpoint --- frigate/test/http_api/base_http_test.py | 2 + frigate/test/http_api/test_http_review.py | 102 ++++++++++++++++++++++ 2 files changed, 104 insertions(+) diff --git a/frigate/test/http_api/base_http_test.py b/frigate/test/http_api/base_http_test.py index e7a1d03e8..fb42e7101 100644 --- a/frigate/test/http_api/base_http_test.py +++ b/frigate/test/http_api/base_http_test.py @@ -168,6 +168,7 @@ class BaseTestHttp(unittest.TestCase): id: str, start_time: float = datetime.datetime.now().timestamp(), end_time: float = datetime.datetime.now().timestamp() + 20, + motion: int = 0, ) -> Event: """Inserts a recording model with a given id.""" return Recordings.insert( @@ -177,4 +178,5 @@ class BaseTestHttp(unittest.TestCase): start_time=start_time, end_time=end_time, duration=end_time - start_time, + motion=motion, ).execute() diff --git a/frigate/test/http_api/test_http_review.py b/frigate/test/http_api/test_http_review.py index 11bd33495..993eaf1d1 100644 --- a/frigate/test/http_api/test_http_review.py +++ b/frigate/test/http_api/test_http_review.py @@ -569,3 +569,105 @@ class TestHttpReview(BaseTestHttp): recording_ids_in_db_after = self._get_recordings(ids) assert len(review_ids_in_db_after) == 0 assert len(recording_ids_in_db_after) == 0 + + #################################################################################################################### + ################################### GET /review/activity/motion Endpoint ######################################## + #################################################################################################################### + def test_review_activity_motion_no_data_for_time_range(self): + now = datetime.now().timestamp() + + with TestClient(self.app) as client: + params = { + "after": now, + "before": now + 3, + } + response = client.get("/review/activity/motion", params=params) + assert response.status_code == 200 + response_json = response.json() + assert len(response_json) == 0 + + def test_review_activity_motion(self): + now = int(datetime.now().timestamp()) + + with TestClient(self.app) as client: + id = "123456.random" + id2 = "123451.random" + + one_m = int((datetime.now() + timedelta(minutes=1)).timestamp()) + + super().insert_mock_recording(id, now + 1, now + 2, motion=101) + super().insert_mock_recording(id2, one_m + 1, one_m + 2, motion=200) + params = { + "after": now, + "before": one_m + 3, + "scale": 1, + } + response = client.get("/review/activity/motion", params=params) + assert response.status_code == 200 + response_json = response.json() + assert len(response_json) == 61 + self.assertDictEqual( + {"motion": 50.5, "camera": "front_door", "start_time": now + 1}, + response_json[0], + ) + for item in response_json[1:-1]: + print(item) + self.assertDictEqual( + {"motion": 0.0, "camera": "", "start_time": item["start_time"]}, + item, + ) + self.assertDictEqual( + {"motion": 100.0, "camera": "front_door", "start_time": one_m + 1}, + response_json[len(response_json) - 1], + ) + + #################################################################################################################### + ################################### GET /review/activity/motion Endpoint ######################################## + #################################################################################################################### + def test_review_activity_motion_no_data_for_time_range(self): + now = datetime.now().timestamp() + + with TestClient(self.app) as client: + params = { + "after": now, + "before": now + 3, + } + response = client.get("/review/activity/motion", params=params) + assert response.status_code == 200 + response_json = response.json() + assert len(response_json) == 0 + + def test_review_activity_motion(self): + now = int(datetime.now().timestamp()) + + with TestClient(self.app) as client: + id = "123456.random" + id2 = "123451.random" + + one_m = int((datetime.now() + timedelta(minutes=1)).timestamp()) + + super().insert_mock_recording(id, now + 1, now + 2, motion=101) + super().insert_mock_recording(id2, one_m + 1, one_m + 2, motion=200) + params = { + "after": now, + "before": one_m + 3, + "scale": 1, + } + response = client.get("/review/activity/motion", params=params) + assert response.status_code == 200 + response_json = response.json() + assert len(response_json) == 61 + self.assertDictEqual( + {"motion": 50.5, "camera": "front_door", "start_time": now + 1}, + response_json[0], + ) + for item in response_json[1:-1]: + print(item) + self.assertDictEqual( + {"motion": 0.0, "camera": "", "start_time": item["start_time"]}, + item, + ) + self.assertDictEqual( + {"motion": 100.0, "camera": "front_door", "start_time": one_m + 1}, + response_json[len(response_json) - 1], + )