MCPcopy Create free account
hub / github.com/PaddlePaddle/FastDeploy / test_validate_tools

Method test_validate_tools

tests/entrypoints/test_chat.py:129–191  ·  view source on GitHub ↗

Test both valid and invalid scenarios for _validate_tools method

(self)

Source from the content-addressed store, hash-verified

127 data_processor.messages2ids = data_processor.original_messages2ids
128
129 def test_validate_tools(self):
130 """Test both valid and invalid scenarios for _validate_tools method"""
131 # Prepare valid test data
132 valid_tool_dict = {
133 "type": "function",
134 "function": {
135 "name": "get_weather",
136 "description": "Get real-time weather of a city",
137 "parameters": {"type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"]},
138 },
139 }
140 valid_tool_model = ChatCompletionToolsParam(**valid_tool_dict)
141 valid_model_list = [valid_tool_model, valid_tool_model]
142 valid_dict_list = [valid_tool_dict, valid_tool_dict]
143
144 # Test valid scenarios
145 # 1. Input is None
146 self.assertIsNone(self.llm._validate_tools(None))
147
148 # 2. Input is single ChatCompletionToolsParam instance
149 result = self.llm._validate_tools(valid_tool_model)
150 self.assertEqual(len(result), 1)
151 self.assertIsInstance(result[0], ChatCompletionToolsParam)
152
153 # 3. Input is list of ChatCompletionToolsParam instances
154 self.assertEqual(self.llm._validate_tools(valid_model_list), valid_model_list)
155
156 # 4. Input is single valid dict
157 result = self.llm._validate_tools(valid_tool_dict)
158 self.assertEqual(len(result), 1)
159 self.assertIsInstance(result[0], dict)
160 self.assertEqual(result[0]["type"], "function")
161
162 # 5. Input is list of valid dicts
163 result = self.llm._validate_tools(valid_dict_list)
164 self.assertEqual(len(result), 2)
165 self.assertIsInstance(result[1], dict)
166
167 # 6. Input is empty list
168 self.assertIsNone(self.llm._validate_tools([]))
169
170 # Test invalid scenarios (should raise ValueError)
171 # 1. Input is string (invalid top-level type)
172 with self.assertRaises(ValueError):
173 self.llm._validate_tools("invalid_string")
174
175 # 2. Input list contains non-dict element
176 with self.assertRaises(ValueError):
177 self.llm._validate_tools([valid_tool_dict, 123])
178
179 # 3. Tool dict missing required field (function.name)
180 invalid_tool_missing_name = {"type": "function", "function": {"description": "Missing 'name' field"}}
181 with self.assertRaises(ValueError):
182 self.llm._validate_tools(invalid_tool_missing_name)
183
184 # 4. Tool dict with wrong 'type' value
185 invalid_tool_wrong_type = {"type": "invalid_type", "function": {"name": "test", "description": "Wrong type"}}
186 with self.assertRaises(ValueError):

Callers

nothing calls this directly

Calls 2

_validate_toolsMethod · 0.80

Tested by

no test coverage detected