From 1083c0772fe9c9c6bca5c36a1664443b967d546f Mon Sep 17 00:00:00 2001 From: Khari Jarrett Date: Mon, 15 Jul 2024 10:47:16 -0400 Subject: [PATCH] validate test --- Makefile | 2 +- src/motion_detector.py | 2 ++ tests/test_motiondetector.py | 26 ++++++++++++++++++++++---- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 416e972..cf6e874 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -build: +setup: ./build.sh test: diff --git a/src/motion_detector.py b/src/motion_detector.py index 2da5326..2bd3eae 100644 --- a/src/motion_detector.py +++ b/src/motion_detector.py @@ -56,6 +56,8 @@ def new_service( @classmethod def validate_config(cls, config: ServiceConfig) -> Sequence[str]: source_cam = config.attributes.fields["cam_name"].string_value + if source_cam == "": + raise Exception("Source camera must be provided as 'cam_name'") min_boxsize = config.attributes.fields["min_box_size"].number_value if min_boxsize < 0: raise Exception("Minimum bounding box size should be a positive integer") diff --git a/tests/test_motiondetector.py b/tests/test_motiondetector.py index 5ed2d2a..f1bd7c9 100644 --- a/tests/test_motiondetector.py +++ b/tests/test_motiondetector.py @@ -3,13 +3,23 @@ from PIL import Image from unittest.mock import MagicMock, patch from viam.components.camera import Camera +from viam.proto.app.robot import ComponentConfig +from google.protobuf.struct_pb2 import Struct from viam.services.vision import CaptureAllResult, Classification, Detection -from typing import List +from typing import List, Mapping, Any import pytest import cv2 import numpy as np +def make_component_config(dictionary: Mapping[str, Any]) -> ComponentConfig: + struct = Struct() + struct.update(dictionary=dictionary) + return ComponentConfig(attributes=struct) + + + + class TestMotionDetector: def getMD(self): @@ -19,10 +29,18 @@ def getMD(self): md.cam_name = "test" md.camera = FakeCamera("test") return md + + + def test_validate(self): + md = self.getMD() + empty_config = make_component_config({}) + config =make_component_config({ + "cam_name": "test" + }) + with pytest.raises(Exception): + response = md.validate_config(config=empty_config) + response = md.validate_config(config=config) - def test_blah(self): - x = "blah" - assert "h" in x def test_classifications(self): img1 = Image.open("tests/img1.jpg")