Skip to content

Commit 6f96ca9

Browse files
committed
test: add unit tests for ernie4_5_vl_processor.py (NO.22)
1 parent a12eaf9 commit 6f96ca9

File tree

1 file changed

+173
-20
lines changed

1 file changed

+173
-20
lines changed

tests/input/test_ernie_vl_processor.py renamed to tests/input/test_ernie4_5_vl_processor.py

Lines changed: 173 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -119,26 +119,179 @@ def test_process_request_dict_with_options(self):
119119
self.processor.process_request_dict(request_dict, 100)
120120
self.assertEqual(request_dict["enable_thinking"], True)
121121

122-
request_dict = {
123-
"messages": [{"role": "user", "content": "Hello"}],
124-
"chat_template_kwargs": {"options": {"thinking_mode": "close"}},
125-
}
126-
self.processor.process_request_dict(request_dict, 100)
127-
self.assertEqual(request_dict["enable_thinking"], False)
128-
129-
request_dict = {
130-
"messages": [{"role": "user", "content": "Hello"}],
131-
"chat_template_kwargs": {"options": {"thinking_mode": "false"}},
132-
}
133-
self.processor.process_request_dict(request_dict, 100)
134-
self.assertEqual(request_dict["enable_thinking"], False)
135-
136-
request_dict = {
137-
"messages": [{"role": "user", "content": "Hello"}],
138-
"chat_template_kwargs": {"enable_thinking": False},
139-
}
140-
self.processor.process_request_dict(request_dict, 100)
141-
self.assertEqual(request_dict["enable_thinking"], False)
122+
def test_parse_processor_kwargs_valid(self):
123+
"""Test _parse_processor_kwargs with valid kwargs (lines 128-163)"""
124+
with patch.object(Ernie4_5_VLProcessor, "__init__", return_value=None):
125+
processor = Ernie4_5_VLProcessor("model_path")
126+
processor._parse_processor_kwargs = Ernie4_5_VLProcessor._parse_processor_kwargs.__get__(
127+
processor, Ernie4_5_VLProcessor
128+
)
129+
130+
valid_kwargs = {
131+
"spatial_conv_size": 14,
132+
"temporal_conv_size": 2,
133+
"image_min_pixels": 1000,
134+
"image_max_pixels": 10000,
135+
}
136+
result = processor._parse_processor_kwargs(valid_kwargs)
137+
self.assertEqual(result, valid_kwargs)
138+
139+
def test_parse_processor_kwargs_invalid_type(self):
140+
"""Test _parse_processor_kwargs with invalid type (line 155)
141+
142+
Note: The implementation catches ValueError and returns empty dict with warning log,
143+
rather than raising the exception.
144+
"""
145+
with patch.object(Ernie4_5_VLProcessor, "__init__", return_value=None):
146+
processor = Ernie4_5_VLProcessor("model_path")
147+
148+
invalid_kwargs = {"spatial_conv_size": "invalid"} # Should be int
149+
# Implementation catches exception and returns empty dict
150+
result = Ernie4_5_VLProcessor._parse_processor_kwargs(processor, invalid_kwargs)
151+
self.assertEqual(result, {})
152+
153+
def test_parse_processor_kwargs_not_dict(self):
154+
"""Test _parse_processor_kwargs with non-dict input (line 135)
155+
156+
Note: The implementation catches ValueError and returns empty dict with warning log,
157+
rather than raising the exception.
158+
"""
159+
with patch.object(Ernie4_5_VLProcessor, "__init__", return_value=None):
160+
processor = Ernie4_5_VLProcessor("model_path")
161+
162+
# Implementation catches exception and returns empty dict
163+
result = Ernie4_5_VLProcessor._parse_processor_kwargs(processor, "not a dict")
164+
self.assertEqual(result, {})
165+
166+
def test_parse_processor_kwargs_exception_handling(self):
167+
"""Test _parse_processor_kwargs exception handling (line 162)"""
168+
with patch.object(Ernie4_5_VLProcessor, "__init__", return_value=None):
169+
processor = Ernie4_5_VLProcessor("model_path")
170+
processor._parse_processor_kwargs = Ernie4_5_VLProcessor._parse_processor_kwargs.__get__(
171+
processor, Ernie4_5_VLProcessor
172+
)
173+
174+
# This should return empty dict on exception
175+
with patch("fastdeploy.input.ernie4_5_vl_processor.ernie4_5_vl_processor.data_processor_logger"):
176+
result = processor._parse_processor_kwargs(None)
177+
self.assertEqual(result, {})
178+
179+
def test_parse_limits_valid(self):
180+
"""Test _parse_limits with valid input (lines 165-179)"""
181+
with patch.object(Ernie4_5_VLProcessor, "__init__", return_value=None):
182+
processor = Ernie4_5_VLProcessor("model_path")
183+
processor._parse_limits = Ernie4_5_VLProcessor._parse_limits.__get__(processor, Ernie4_5_VLProcessor)
184+
185+
valid_limits = {"image": 5, "video": 3}
186+
result = processor._parse_limits(valid_limits)
187+
self.assertEqual(result["image"], 5)
188+
self.assertEqual(result["video"], 3)
189+
self.assertEqual(result["audio"], 1) # Default value
190+
191+
def test_parse_limits_invalid_type(self):
192+
"""Test _parse_limits with invalid type (line 174)
193+
194+
Note: The implementation catches ValueError and returns DEFAULT_LIMITS with warning log,
195+
rather than raising the exception.
196+
"""
197+
with patch.object(Ernie4_5_VLProcessor, "__init__", return_value=None):
198+
processor = Ernie4_5_VLProcessor("model_path")
199+
200+
# Implementation catches exception and returns default limits
201+
result = Ernie4_5_VLProcessor._parse_limits(processor, "not a dict")
202+
# Should return DEFAULT_LIMITS = {"image": 1, "video": 1, "audio": 1}
203+
self.assertEqual(result["image"], 1)
204+
self.assertEqual(result["video"], 1)
205+
self.assertEqual(result["audio"], 1)
206+
207+
def test_parse_limits_empty(self):
208+
"""Test _parse_limits with empty input (line 170)"""
209+
with patch.object(Ernie4_5_VLProcessor, "__init__", return_value=None):
210+
processor = Ernie4_5_VLProcessor("model_path")
211+
processor._parse_limits = Ernie4_5_VLProcessor._parse_limits.__get__(processor, Ernie4_5_VLProcessor)
212+
213+
result = processor._parse_limits(None)
214+
self.assertEqual(result["image"], 1)
215+
self.assertEqual(result["video"], 1)
216+
self.assertEqual(result["audio"], 1)
217+
218+
def test_check_mm_limits_with_dict(self):
219+
"""Test _check_mm_limits with dict input (lines 182-184)"""
220+
with patch.object(Ernie4_5_VLProcessor, "__init__", return_value=None):
221+
processor = Ernie4_5_VLProcessor("model_path")
222+
processor.limit_mm_per_prompt = {"image": 2, "video": 1}
223+
processor._check_mm_limits = Ernie4_5_VLProcessor._check_mm_limits.__get__(processor, Ernie4_5_VLProcessor)
224+
225+
mm_data = {"image": [1, 2], "video": [1]}
226+
# Should not raise
227+
processor._check_mm_limits(mm_data)
228+
229+
def test_check_mm_limits_with_messages(self):
230+
"""Test _check_mm_limits with messages input (lines 186-195)"""
231+
with patch.object(Ernie4_5_VLProcessor, "__init__", return_value=None):
232+
processor = Ernie4_5_VLProcessor("model_path")
233+
processor.limit_mm_per_prompt = {"image": 2, "video": 1}
234+
processor._check_mm_limits = Ernie4_5_VLProcessor._check_mm_limits.__get__(processor, Ernie4_5_VLProcessor)
235+
236+
messages = [
237+
{"role": "user", "content": [{"type": "image", "data": "img1"}]},
238+
{"role": "user", "content": [{"type": "video", "data": "vid1"}]},
239+
]
240+
# Should not raise
241+
processor._check_mm_limits(messages)
242+
243+
def test_check_mm_limits_exceeded(self):
244+
"""Test _check_mm_limits when limit is exceeded (line 201)"""
245+
with patch.object(Ernie4_5_VLProcessor, "__init__", return_value=None):
246+
processor = Ernie4_5_VLProcessor("model_path")
247+
processor.limit_mm_per_prompt = {"image": 1, "video": 1}
248+
processor._check_mm_limits = Ernie4_5_VLProcessor._check_mm_limits.__get__(processor, Ernie4_5_VLProcessor)
249+
250+
mm_data = {"image": [1, 2, 3], "video": []} # 3 images, limit is 1
251+
with self.assertRaises(ValueError) as context:
252+
processor._check_mm_limits(mm_data)
253+
self.assertIn("Too many image items", str(context.exception))
254+
255+
def test_apply_default_parameters_with_dict(self):
256+
"""Test _apply_default_parameters with dict request (lines 102-116)"""
257+
with patch.object(Ernie4_5_VLProcessor, "__init__", return_value=None):
258+
processor = Ernie4_5_VLProcessor("model_path")
259+
processor.generation_config = MagicMock()
260+
processor.generation_config.top_p = 0.8
261+
processor.generation_config.temperature = 0.9
262+
processor._apply_default_parameters = Ernie4_5_VLProcessor._apply_default_parameters.__get__(
263+
processor, Ernie4_5_VLProcessor
264+
)
265+
266+
request = {}
267+
result = processor._apply_default_parameters(request)
268+
self.assertEqual(result["top_p"], 0.8)
269+
self.assertEqual(result["temperature"], 0.9)
270+
271+
def test_apply_default_parameters_with_object(self):
272+
"""Test _apply_default_parameters with object request (lines 108-109)"""
273+
with patch.object(Ernie4_5_VLProcessor, "__init__", return_value=None):
274+
processor = Ernie4_5_VLProcessor("model_path")
275+
processor.generation_config = MagicMock()
276+
processor.generation_config.top_p = 0.8
277+
processor._apply_default_parameters = Ernie4_5_VLProcessor._apply_default_parameters.__get__(
278+
processor, Ernie4_5_VLProcessor
279+
)
280+
281+
class MockRequest:
282+
def __init__(self):
283+
self.top_p = None
284+
self.temperature = None
285+
286+
def get(self, key):
287+
return getattr(self, key, None)
288+
289+
def set(self, key, value):
290+
setattr(self, key, value)
291+
292+
request = MockRequest()
293+
result = processor._apply_default_parameters(request)
294+
self.assertEqual(result.top_p, 0.8)
142295

143296

144297
class TestDataProcessorTargetMethods(unittest.TestCase):

0 commit comments

Comments
 (0)