Skip to content

Commit 81c30b3

Browse files
Rafal Maciagclaude
andcommitted
Fix Python controller tests for 16-byte FrameMetadata prefix
Tests were not updated when FrameMetadata prefix was added to frame data. Now all tests include the 16-byte prefix and set frame.size attribute on MagicMock frames, matching the C# reference implementation. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 071a18f commit 81c30b3

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

python/tests/test_controllers.py

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,13 @@ def test_process_oneway_frame(self, controller):
9393
controller._gst_caps = GstCaps.from_simple(width=2, height=2, format="RGB")
9494
on_frame = Mock()
9595

96-
# Create mock frame with correct data
97-
frame_data = np.zeros((12,), dtype=np.uint8) # 2x2x3
96+
# Create mock frame with 16-byte metadata prefix + pixel data (2x2x3 = 12 bytes)
97+
metadata_prefix = bytes(16) # 16-byte FrameMetadata
98+
pixel_data = np.zeros((12,), dtype=np.uint8) # 2x2x3
99+
frame_data = metadata_prefix + bytes(pixel_data)
98100
mock_frame = MagicMock()
99101
mock_frame.data = memoryview(frame_data)
102+
mock_frame.size = len(frame_data)
100103

101104
# Process the frame (simulate what happens in the read loop)
102105
mat = controller._create_mat_from_frame(mock_frame)
@@ -124,8 +127,12 @@ def test_stop_with_reader(self, controller):
124127
def test_create_mat_from_frame_no_caps(self, controller):
125128
"""Test _create_mat_from_frame when no caps are available."""
126129
frame = MagicMock()
127-
# Use 5 bytes so it's not a perfect square (no square root of 5)
128-
frame.data = memoryview(b"tests")
130+
# Use 16-byte prefix + 5 bytes pixel data (not a perfect square)
131+
metadata_prefix = bytes(16)
132+
pixel_data = b"tests"
133+
frame_data = metadata_prefix + pixel_data
134+
frame.data = memoryview(frame_data)
135+
frame.size = len(frame_data)
129136

130137
result = controller._create_mat_from_frame(frame)
131138
assert result is None
@@ -135,9 +142,13 @@ def test_create_mat_from_frame_with_caps(self, controller):
135142
# Set up GstCaps
136143
controller._gst_caps = GstCaps.from_simple(width=2, height=2, format="RGB")
137144

138-
# Create frame with correct data size (2x2x3 = 12 bytes)
145+
# Create frame with 16-byte prefix + pixel data (2x2x3 = 12 bytes)
146+
metadata_prefix = bytes(16)
147+
pixel_data = np.zeros((12,), dtype=np.uint8)
148+
frame_data = metadata_prefix + bytes(pixel_data)
139149
frame = MagicMock()
140-
frame.data = memoryview(np.zeros((12,), dtype=np.uint8))
150+
frame.data = memoryview(frame_data)
151+
frame.size = len(frame_data)
141152

142153
result = controller._create_mat_from_frame(frame)
143154
assert result is not None
@@ -147,9 +158,13 @@ def test_create_mat_from_frame_grayscale(self, controller):
147158
"""Test _create_mat_from_frame with grayscale format."""
148159
controller._gst_caps = GstCaps.from_simple(width=2, height=2, format="GRAY8")
149160

150-
# Create frame with correct data size (2x2x1 = 4 bytes)
161+
# Create frame with 16-byte prefix + pixel data (2x2x1 = 4 bytes)
162+
metadata_prefix = bytes(16)
163+
pixel_data = np.zeros((4,), dtype=np.uint8)
164+
frame_data = metadata_prefix + bytes(pixel_data)
151165
frame = MagicMock()
152-
frame.data = memoryview(np.zeros((4,), dtype=np.uint8))
166+
frame.data = memoryview(frame_data)
167+
frame.size = len(frame_data)
153168

154169
result = controller._create_mat_from_frame(frame)
155170
assert result is not None
@@ -159,9 +174,13 @@ def test_create_mat_from_frame_rgba(self, controller):
159174
"""Test _create_mat_from_frame with RGBA format."""
160175
controller._gst_caps = GstCaps.from_simple(width=2, height=2, format="RGBA")
161176

162-
# Create frame with correct data size (2x2x4 = 16 bytes)
177+
# Create frame with 16-byte prefix + pixel data (2x2x4 = 16 bytes)
178+
metadata_prefix = bytes(16)
179+
pixel_data = np.zeros((16,), dtype=np.uint8)
180+
frame_data = metadata_prefix + bytes(pixel_data)
163181
frame = MagicMock()
164-
frame.data = memoryview(np.zeros((16,), dtype=np.uint8))
182+
frame.data = memoryview(frame_data)
183+
frame.size = len(frame_data)
165184

166185
result = controller._create_mat_from_frame(frame)
167186
assert result is not None

0 commit comments

Comments
 (0)