Added unit tests for GET /stats Endpoint

This commit is contained in:
Rui Alves 2025-01-12 20:08:00 +00:00
parent bd746cdaf7
commit 896649790c
2 changed files with 26 additions and 21 deletions

View File

@ -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

View File

@ -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,