| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- """
- Coverage补充测试:tools/query_order.py
- 目标路径:
- - call() — api_client is None → RuntimeError
- - call() — keyword strip 后为空 → ValueError
- """
- import unittest
- from unittest.mock import MagicMock
- from tools.query_order import QueryOrderTool
- class QueryOrderToolCoverageTest(unittest.TestCase):
- # ---- api_client is None ----
- def test_call_raises_when_no_api_client(self):
- tool = QueryOrderTool(api_client=None)
- with self.assertRaises(RuntimeError) as ctx:
- tool.call('some keyword')
- self.assertIn('api client is required', str(ctx.exception))
- def test_call_raises_when_api_client_not_set(self):
- tool = QueryOrderTool()
- with self.assertRaises(RuntimeError):
- tool.call('kw')
- # ---- keyword 为空 ----
- def test_call_raises_on_empty_string_keyword(self):
- tool = QueryOrderTool(api_client=MagicMock())
- with self.assertRaises(ValueError) as ctx:
- tool.call('')
- self.assertIn('keyword is required', str(ctx.exception))
- def test_call_raises_on_whitespace_only_keyword(self):
- tool = QueryOrderTool(api_client=MagicMock())
- with self.assertRaises(ValueError) as ctx:
- tool.call(' ')
- self.assertIn('keyword is required', str(ctx.exception))
- def test_call_raises_on_tab_keyword(self):
- tool = QueryOrderTool(api_client=MagicMock())
- with self.assertRaises(ValueError):
- tool.call('\t')
- # ---- 正常路径(验证 strip 和参数传递)----
- def test_call_strips_keyword_before_passing(self):
- client = MagicMock()
- client.call_tool.return_value = {'code': 'MCP_0000', 'data': {}}
- tool = QueryOrderTool(api_client=client)
- tool.call(' SO001 ', page=2, limit=50, request_id='rq_test')
- _, _, payload, _ = client.call_tool.call_args[0]
- self.assertEqual('SO001', payload['keyword'])
- def test_call_clamps_limit_to_100(self):
- client = MagicMock()
- client.call_tool.return_value = {'code': 'MCP_0000', 'data': {}}
- tool = QueryOrderTool(api_client=client)
- tool.call('kw', limit=999)
- _, _, payload, _ = client.call_tool.call_args[0]
- self.assertEqual(100, payload['limit'])
- def test_call_clamps_page_to_1(self):
- client = MagicMock()
- client.call_tool.return_value = {'code': 'MCP_0000', 'data': {}}
- tool = QueryOrderTool(api_client=client)
- tool.call('kw', page=0)
- _, _, payload, _ = client.call_tool.call_args[0]
- self.assertEqual(1, payload['page'])
- def test_metadata_has_required_keyword(self):
- tool = QueryOrderTool()
- meta = tool.metadata()
- self.assertIn('keyword', meta['input_schema']['required'])
- self.assertEqual('query_order', meta['name'])
- if __name__ == '__main__':
- unittest.main()
|