-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_progress.py
More file actions
356 lines (274 loc) · 11.1 KB
/
test_progress.py
File metadata and controls
356 lines (274 loc) · 11.1 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# ------------------------------------------------------------------------
# Trackers
# Copyright (c) 2026 Roboflow. All Rights Reserved.
# Licensed under the Apache License, Version 2.0 [see LICENSE for details]
# ------------------------------------------------------------------------
from __future__ import annotations
import time
from collections.abc import Callable
from io import StringIO
from pathlib import Path
from unittest.mock import MagicMock, patch
import cv2
import numpy as np
import pytest
from rich.console import Console
from trackers.scripts.progress import (
_classify_source,
_format_time,
_SourceInfo,
_TrackingProgress,
)
FRAME_WIDTH = 64
FRAME_HEIGHT = 64
FRAME_SIZE = (FRAME_WIDTH, FRAME_HEIGHT)
@pytest.fixture
def video_factory(tmp_path: Path) -> Callable[[int], Path]:
"""Create a small test video with *n* frames."""
def _create(n_frames: int) -> Path:
video_path = tmp_path / f"video_{n_frames}.mp4"
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
writer = cv2.VideoWriter(str(video_path), fourcc, 25.0, FRAME_SIZE)
for _ in range(n_frames):
writer.write(np.zeros((FRAME_HEIGHT, FRAME_WIDTH, 3), dtype=np.uint8))
writer.release()
return video_path
return _create
@pytest.fixture
def image_directory_factory(tmp_path: Path) -> Callable[[int], Path]:
"""Create a directory with *n* PNG images."""
def _create(n_frames: int) -> Path:
directory = tmp_path / f"imgdir_{n_frames}"
directory.mkdir(exist_ok=True)
frame = np.zeros((FRAME_HEIGHT, FRAME_WIDTH, 3), dtype=np.uint8)
for i in range(n_frames):
cv2.imwrite(str(directory / f"{i:04d}.png"), frame)
return directory
return _create
def _make_console() -> tuple[Console, StringIO]:
"""Return a Console that writes to a StringIO buffer."""
buf = StringIO()
console = Console(file=buf, force_terminal=True, width=200)
return console, buf
class TestClassifySource:
def test_video_file(self, video_factory: Callable[[int], Path]) -> None:
video_path = video_factory(10)
info = _classify_source(str(video_path))
assert info.source_type == "video"
assert info.total_frames is not None
assert info.total_frames > 0
assert info.fps is not None
assert info.fps > 0
def test_image_directory(
self, image_directory_factory: Callable[[int], Path]
) -> None:
directory = image_directory_factory(7)
info = _classify_source(str(directory))
assert info.source_type == "image_dir"
assert info.total_frames == 7
assert info.fps is None
def test_image_directory_path_object(
self, image_directory_factory: Callable[[int], Path]
) -> None:
directory = image_directory_factory(3)
info = _classify_source(directory)
assert info.source_type == "image_dir"
assert info.total_frames == 3
def test_webcam_from_int(self) -> None:
info = _classify_source(0)
assert info.source_type == "webcam"
assert info.total_frames is None
assert info.fps is None
def test_webcam_from_str(self) -> None:
info = _classify_source("0")
assert info.source_type == "webcam"
assert info.total_frames is None
@pytest.mark.parametrize(
"url",
[
"rtsp://192.168.1.10:554/stream",
"http://example.com/stream.mjpg",
"https://example.com/stream.mjpg",
],
)
def test_stream_url(self, url: str) -> None:
info = _classify_source(url)
assert info.source_type == "stream"
assert info.total_frames is None
assert info.fps is None
def test_video_with_zero_frame_count(self) -> None:
mock_cap = MagicMock()
mock_cap.isOpened.return_value = True
mock_cap.get.side_effect = lambda prop: {
cv2.CAP_PROP_FRAME_COUNT: 0.0,
cv2.CAP_PROP_FPS: 30.0,
}.get(prop, 0.0)
with patch("trackers.scripts.progress.cv2.VideoCapture", return_value=mock_cap):
info = _classify_source("some_video.mp4")
assert info.source_type == "video"
assert info.total_frames is None
mock_cap.release.assert_called_once()
def test_nonexistent_file(self) -> None:
info = _classify_source("/nonexistent/video.mp4")
assert info.source_type == "video"
assert info.total_frames is None
def test_empty_image_directory(self, tmp_path: Path) -> None:
empty_dir = tmp_path / "empty"
empty_dir.mkdir()
info = _classify_source(str(empty_dir))
assert info.source_type == "image_dir"
assert info.total_frames is None
class TestFormatTime:
@pytest.mark.parametrize(
"seconds,expected",
[
(0, "0:00"),
(5, "0:05"),
(65, "1:05"),
(3661, "1:01:01"),
(-1, "--"),
],
)
def test_format_time(self, seconds: float, expected: str) -> None:
assert _format_time(seconds) == expected
class TestBuildLine:
def test_bounded_format(self) -> None:
console, _ = _make_console()
source_info = _SourceInfo(source_type="video", total_frames=100)
progress = _TrackingProgress(source_info, console=console)
progress._start_time = time.monotonic() - 5.0
progress._frames_processed = 50
line = progress._build_line("⠹")
text = line.plain
assert "50 / 100" in text
assert "frames" in text
assert "50%" in text
assert "fps" in text
assert "elapsed" in text
assert "eta" in text
def test_unbounded_format(self) -> None:
console, _ = _make_console()
source_info = _SourceInfo(source_type="webcam")
progress = _TrackingProgress(source_info, console=console)
progress._start_time = time.monotonic() - 5.0
progress._frames_processed = 50
line = progress._build_line("⠹")
text = line.plain
assert "50 / --" in text
assert "frames" in text
assert "--" in text
assert "fps" in text
assert "elapsed" in text
assert "eta --" in text
def test_final_no_eta(self) -> None:
console, _ = _make_console()
source_info = _SourceInfo(source_type="video", total_frames=100)
progress = _TrackingProgress(source_info, console=console)
progress._start_time = time.monotonic() - 5.0
progress._frames_processed = 100
line = progress._build_line("✓", show_eta=False)
text = line.plain
assert "eta" not in text
assert "✓" in text
def test_suffix_appended(self) -> None:
console, _ = _make_console()
source_info = _SourceInfo(source_type="video", total_frames=100)
progress = _TrackingProgress(source_info, console=console)
progress._start_time = time.monotonic() - 5.0
progress._frames_processed = 50
line = progress._build_line("✗", show_eta=False, suffix="(interrupted)")
text = line.plain
assert text.endswith("(interrupted)")
def test_zero_elapsed_no_crash(self) -> None:
console, _ = _make_console()
source_info = _SourceInfo(source_type="video", total_frames=100)
progress = _TrackingProgress(source_info, console=console)
progress._start_time = time.monotonic()
progress._frames_processed = 0
# Should not raise ZeroDivisionError
line = progress._build_line("⠹")
text = line.plain
assert "fps" in text
class TestTrackingProgressLifecycle:
def test_bounded_completed(self) -> None:
console, buf = _make_console()
source_info = _SourceInfo(source_type="video", total_frames=5)
with _TrackingProgress(source_info, console=console) as progress:
for _ in range(5):
progress.update()
progress.complete()
output = buf.getvalue()
assert "✓" in output
assert "(interrupted)" not in output
def test_bounded_interrupted_by_display_quit(self) -> None:
console, buf = _make_console()
source_info = _SourceInfo(source_type="video", total_frames=10)
with _TrackingProgress(source_info, console=console) as progress:
for _ in range(5):
progress.update()
progress.complete(interrupted=True)
output = buf.getvalue()
assert "✗" in output
assert "(interrupted)" in output
def test_bounded_keyboard_interrupt(self) -> None:
console, buf = _make_console()
source_info = _SourceInfo(source_type="video", total_frames=10)
progress = _TrackingProgress(source_info, console=console)
progress.__enter__()
for _ in range(3):
progress.update()
# Simulate KeyboardInterrupt in __exit__
progress.__exit__(KeyboardInterrupt, KeyboardInterrupt(), None)
output = buf.getvalue()
assert "✗" in output
assert "(interrupted)" in output
def test_unbounded_completed(self) -> None:
console, buf = _make_console()
source_info = _SourceInfo(source_type="webcam")
with _TrackingProgress(source_info, console=console) as progress:
for _ in range(20):
progress.update()
progress.complete()
output = buf.getvalue()
assert "✓" in output
def test_unbounded_keyboard_interrupt(self) -> None:
console, buf = _make_console()
source_info = _SourceInfo(source_type="stream")
progress = _TrackingProgress(source_info, console=console)
progress.__enter__()
for _ in range(10):
progress.update()
progress.__exit__(KeyboardInterrupt, KeyboardInterrupt(), None)
output = buf.getvalue()
assert "✓" in output
assert "(interrupted)" not in output
def test_error_shows_source_lost(self) -> None:
console, buf = _make_console()
source_info = _SourceInfo(source_type="stream")
progress = _TrackingProgress(source_info, console=console)
progress.__enter__()
for _ in range(5):
progress.update()
err = RuntimeError("connection lost")
progress.__exit__(RuntimeError, err, None)
output = buf.getvalue()
assert "✗" in output
assert "(source lost)" in output
def test_frames_count_in_output(self) -> None:
console, buf = _make_console()
source_info = _SourceInfo(source_type="image_dir", total_frames=30)
with _TrackingProgress(source_info, console=console) as progress:
for _ in range(30):
progress.update()
progress.complete()
output = buf.getvalue()
assert "30 / 30" in output
def test_unbounded_frames_count_in_output(self) -> None:
console, buf = _make_console()
source_info = _SourceInfo(source_type="webcam")
with _TrackingProgress(source_info, console=console) as progress:
for _ in range(42):
progress.update()
progress.complete()
output = buf.getvalue()
assert "42 / --" in output