diff --git a/frigate/config/camera/motion_path.py b/frigate/config/camera/motion_path.py index fd63a2742..5093dda76 100644 --- a/frigate/config/camera/motion_path.py +++ b/frigate/config/camera/motion_path.py @@ -1,6 +1,6 @@ from typing import Any, Optional, Union -from pydantic import Field, field_serializer +from pydantic import Field, field_serializer, validator from ..base import FrigateBaseModel @@ -25,3 +25,11 @@ class MotionPathConfig(FrigateBaseModel): @field_serializer("raw_mask", when_used="json") def serialize_raw_mask(self, value: Any, info): return None + + @validator('max_history') + def max_history_range(cls, v): + if v < 2: + raise ValueError('max_history must be >= 2') + if v > 100: + raise ValueError('max_history must be <= 100') + return v diff --git a/frigate/test/test_motion_path_config.py b/frigate/test/test_motion_path_config.py new file mode 100644 index 000000000..24feb0bb8 --- /dev/null +++ b/frigate/test/test_motion_path_config.py @@ -0,0 +1,62 @@ +import unittest + +from pydantic import ValidationError + +from frigate.config.camera.motion_path import MotionPathConfig + + +class TestMotionPathConfig(unittest.TestCase): + def setUp(self): + self.default_config = { + "enabled": True, + "max_history": 10, + "mask": None, + "raw_mask": None, + } + + def test_default_motion_path_config(self): + motion_path_config = MotionPathConfig(**self.default_config) + assert motion_path_config.enabled is True + assert motion_path_config.max_history == 10 + assert motion_path_config.mask is None + assert motion_path_config.raw_mask is None + + def test_valid_max_history(self): + config = self.default_config.copy() + config["max_history"] = 50 + motion_path_config = MotionPathConfig(**config) + assert motion_path_config.max_history == 50 + + def test_invalid_max_history_below_minimum(self): + config = self.default_config.copy() + config["max_history"] = 1 + with self.assertRaises(ValidationError): + MotionPathConfig(**config) + + def test_invalid_max_history_above_maximum(self): + config = self.default_config.copy() + config["max_history"] = 101 + with self.assertRaises(ValidationError): + MotionPathConfig(**config) + + def test_serialize_mask(self): + config = self.default_config.copy() + config["raw_mask"] = "test_mask" + motion_path_config = MotionPathConfig(**config) + assert ( + motion_path_config.serialize_mask(motion_path_config.mask, None) + == "test_mask" + ) + + def test_serialize_raw_mask(self): + config = self.default_config.copy() + config["raw_mask"] = "test_mask" + motion_path_config = MotionPathConfig(**config) + assert ( + motion_path_config.serialize_raw_mask(motion_path_config.raw_mask, None) + is None + ) + + +if __name__ == "__main__": + unittest.main(verbosity=2)