From 896649790c11d83169bbda26fc1c02b7c6b755ee Mon Sep 17 00:00:00 2001 From: Rui Alves Date: Sun, 12 Jan 2025 20:08:00 +0000 Subject: [PATCH] Added unit tests for GET /stats Endpoint --- frigate/test/http_api/test_http_app.py | 26 ++++++++++++++++++++++++++ frigate/test/test_http.py | 21 --------------------- 2 files changed, 26 insertions(+), 21 deletions(-) create mode 100644 frigate/test/http_api/test_http_app.py diff --git a/frigate/test/http_api/test_http_app.py b/frigate/test/http_api/test_http_app.py new file mode 100644 index 000000000..e7785a9d7 --- /dev/null +++ b/frigate/test/http_api/test_http_app.py @@ -0,0 +1,26 @@ +from unittest.mock import Mock + +from fastapi.testclient import TestClient + +from frigate.models import Event, Recordings, ReviewSegment +from frigate.stats.emitter import StatsEmitter +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 /stats Endpoint ######################################################### + #################################################################################################################### + def test_stats_endpoint(self): + stats = Mock(spec=StatsEmitter) + stats.get_latest_stats.return_value = self.test_stats + app = super().create_app(stats) + + with TestClient(app) as client: + response = client.get("/stats") + response_json = response.json() + assert response_json == self.test_stats diff --git a/frigate/test/test_http.py b/frigate/test/test_http.py index 213794259..54c7b3ae9 100644 --- a/frigate/test/test_http.py +++ b/frigate/test/test_http.py @@ -2,7 +2,6 @@ import datetime import logging import os import unittest -from unittest.mock import Mock from fastapi.testclient import TestClient from peewee_migrate import Router @@ -13,7 +12,6 @@ from playhouse.sqliteq import SqliteQueueDatabase from frigate.api.fastapi_app import create_fastapi_app from frigate.config import FrigateConfig from frigate.models import Event, Recordings, Timeline -from frigate.stats.emitter import StatsEmitter from frigate.test.const import TEST_DB, TEST_DB_CLEANUPS @@ -381,25 +379,6 @@ class TestHttp(unittest.TestCase): assert recording assert recording[0]["id"] == id - def test_stats(self): - stats = Mock(spec=StatsEmitter) - stats.get_latest_stats.return_value = self.test_stats - app = create_fastapi_app( - FrigateConfig(**self.minimal_config), - self.db, - None, - None, - None, - None, - None, - stats, - None, - ) - - with TestClient(app) as client: - full_stats = client.get("/stats").json() - assert full_stats == self.test_stats - def _insert_mock_event( id: str,