Test stats

This commit is contained in:
Nick Mowen 2022-06-16 06:47:51 -06:00
parent 59c253b8df
commit f7f29437d3

View File

@ -3,6 +3,7 @@ import json
import logging import logging
import os import os
import unittest import unittest
from unittest.mock import patch
from peewee_migrate import Router from peewee_migrate import Router
from playhouse.sqlite_ext import SqliteExtDatabase from playhouse.sqlite_ext import SqliteExtDatabase
@ -18,16 +19,16 @@ from frigate.test.const import TEST_DB
class TestHttp(unittest.TestCase): class TestHttp(unittest.TestCase):
def setUp(self): def setUp(self):
# setup clean database for each test run
migrate_db = SqliteExtDatabase("test.db") migrate_db = SqliteExtDatabase("test.db")
del logging.getLogger("peewee_migrate").handlers[:] del logging.getLogger("peewee_migrate").handlers[:]
router = Router(migrate_db) router = Router(migrate_db)
router.run() router.run()
migrate_db.close() migrate_db.close()
self.db = SqliteQueueDatabase(TEST_DB) self.db = SqliteQueueDatabase(TEST_DB)
models = [Event, Recordings] models = [Event, Recordings]
self.db.bind(models) self.db.bind(models)
self.minimal_config = { self.minimal_config = {
"mqtt": {"host": "mqtt"}, "mqtt": {"host": "mqtt"},
"cameras": { "cameras": {
@ -46,60 +47,6 @@ class TestHttp(unittest.TestCase):
}, },
} }
self.test_stats = { self.test_stats = {
"detection_fps": 13.7,
"detectors": {
"cpu1": {
"detection_start": 0.0,
"inference_speed": 91.43,
"pid": 42,
},
"cpu2": {
"detection_start": 0.0,
"inference_speed": 84.99,
"pid": 44,
},
},
"front_door": {
"camera_fps": 4.1,
"capture_pid": 53,
"detection_fps": 6.0,
"pid": 52,
"process_fps": 4.0,
"skipped_fps": 0.0,
},
"service": {
"storage": {
"/dev/shm": {
"free": 50.5,
"mount_type": "tmpfs",
"total": 67.1,
"used": 16.6,
},
"/media/frigate/clips": {
"free": 42429.9,
"mount_type": "ext4",
"total": 244529.7,
"used": 189607.0,
},
"/media/frigate/recordings": {
"free": 42429.9,
"mount_type": "ext4",
"total": 244529.7,
"used": 189607.0,
},
"/tmp/cache": {
"free": 976.8,
"mount_type": "tmpfs",
"total": 1000.0,
"used": 23.2,
},
},
"uptime": 101113,
"version": "0.8.4-09a4d6d",
"latest_version": "0.10.1",
},
}
self.all_stats = {
"detection_fps": 13.7, "detection_fps": 13.7,
"detectors": { "detectors": {
"cpu1": { "cpu1": {
@ -331,6 +278,21 @@ class TestHttp(unittest.TestCase):
assert recording assert recording
assert recording[0]["id"] == id assert recording[0]["id"] == id
@patch("frigate.http.stats_snapshot")
def test_stats(self, mock_stats):
app = create_app(
FrigateConfig(**self.minimal_config).runtime_config,
self.db,
None,
None,
None,
)
mock_stats.return_value = self.test_stats
with app.test_client() as client:
stats = client.get("/stats").json
assert stats == self.test_stats
def _insert_mock_event(id: str) -> Event: def _insert_mock_event(id: str) -> Event:
"""Inserts a basic event model with a given id.""" """Inserts a basic event model with a given id."""