-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cv_engine.py
More file actions
112 lines (92 loc) · 3.44 KB
/
Copy pathtest_cv_engine.py
File metadata and controls
112 lines (92 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
"""
Tests for Friday CV Engine — background camera + object detection.
"""
import sys, os, json, unittest, threading, time
import cv2
import numpy as np
class DummyVideoCapture:
def __init__(self, camera_index=0, *args, **kwargs):
self.is_open = (camera_index != 999)
def isOpened(self):
return self.is_open
def read(self):
if not self.is_open:
return False, None
return True, np.zeros((480, 640, 3), dtype=np.uint8)
def release(self):
self.is_open = False
def set(self, *args, **kwargs):
return True
def get(self, *args, **kwargs):
return 0.0
cv2.VideoCapture = DummyVideoCapture
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from friday.cv_engine import (
cv_tool,
get_cv_status,
get_cv_frame_b64,
start_camera,
stop_camera,
_cv_state,
_cv_lock,
COCO_CLASSES,
)
class TestCVEngine(unittest.TestCase):
def test_coco_classes_loaded(self):
self.assertGreater(len(COCO_CLASSES), 50)
self.assertIn("person", COCO_CLASSES)
self.assertIn("car", COCO_CLASSES)
self.assertIn("dog", COCO_CLASSES)
def test_cv_tool_status_no_camera(self):
status = cv_tool("status")
self.assertIn("CV ENGINE", status)
self.assertIn("INACTIVE", status)
def test_cv_tool_context_no_camera(self):
ctx = cv_tool("context")
self.assertEqual(ctx, "")
def test_cv_tool_unknown_action(self):
result = cv_tool("bogus")
self.assertIn("[FAIL]", result)
def test_cv_tool_list_cameras(self):
"""Should not crash — may return fail if no cameras."""
result = cv_tool("list_cameras")
self.assertTrue(result.startswith("[OK]") or result.startswith("[FAIL]"))
def test_cv_state_init(self):
with _cv_lock:
self.assertIn("camera_active", _cv_state)
self.assertIn("last_capture", _cv_state)
def test_get_cv_status_no_camera(self):
status = get_cv_status()
self.assertFalse(status["camera_active"])
self.assertIn("camera_active", status)
self.assertIn("objects_found", status)
self.assertIn("detections", status)
self.assertIn("scene_description", status)
def test_camera_start_stop_no_crash(self):
"""Starting camera with invalid index should fail gracefully."""
result = start_camera(camera_index=999, interval=1.0)
self.assertTrue(result.startswith("[FAIL]"))
def test_cv_tool_start_stop(self):
"""Calling start then stop should work (even if camera unavailable)."""
result = cv_tool("start", camera_index=999)
self.assertTrue(result.startswith("[FAIL]"))
result = cv_tool("stop")
self.assertIn("[OK]", result)
def test_cv_tool_start_valid(self):
"""Start with camera_index=0 may succeed if camera exists."""
result = cv_tool("start", camera_index=0, interval=3.0)
# Either way, stop after
stop_camera()
self.assertTrue(result.startswith("[OK]") or result.startswith("[FAIL]"))
def test_stop_when_not_running(self):
result = stop_camera()
self.assertIn("[OK]", result)
def test_double_stop(self):
stop_camera()
result = stop_camera()
self.assertIn("[OK]", result)
def test_get_cv_frame_no_camera(self):
frame = get_cv_frame_b64()
self.assertIsNone(frame)
if __name__ == "__main__":
unittest.main()