1
0
forked from erp-dev/erp

fix: limit param for tiia(tencent)

This commit is contained in:
2026-01-20 17:45:09 +08:00
parent c170381383
commit a5e3312d5c
4 changed files with 134 additions and 0 deletions

View File

@@ -43,6 +43,47 @@ class TiiaSearchImageAPITestCase(TestCase):
self.assertEqual(resp.json(), {'Candidates': [{'EntityId': '1', 'Score': 99}]})
mock_search.assert_called()
@patch('api_v1.views.tiia.search_image_url_in_tencent_tiia')
def test_search_image_pass_limit_offset(self, mock_search):
self.client.force_authenticate(self.user)
mock_search.return_value = {'Candidates': []}
resp = self.client.post(
'/api/v1/tiia/search-image/',
data={
'imageUrl': 'https://example.com/a.png',
'limit': 50,
'offset': 10,
'matchThreshold': 80,
},
format='json',
)
self.assertEqual(resp.status_code, status.HTTP_200_OK)
_, kwargs = mock_search.call_args
self.assertEqual(kwargs.get('limit'), 50)
self.assertEqual(kwargs.get('offset'), 10)
self.assertEqual(kwargs.get('match_threshold'), 80)
def test_search_image_limit_too_large(self):
self.client.force_authenticate(self.user)
resp = self.client.post(
'/api/v1/tiia/search-image/',
data={'imageUrl': 'https://example.com/a.png', 'limit': 101},
format='json',
)
self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST)
self.assertIn('error', resp.json())
def test_search_image_offset_negative(self):
self.client.force_authenticate(self.user)
resp = self.client.post(
'/api/v1/tiia/search-image/',
data={'imageUrl': 'https://example.com/a.png', 'offset': -1},
format='json',
)
self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST)
self.assertIn('error', resp.json())
@patch('api_v1.views.tiia.search_image_url_in_tencent_tiia')
def test_search_image_success_base64(self, mock_search):
self.client.force_authenticate(self.user)

View File

@@ -39,11 +39,39 @@ class TiiaSearchImageView(APIView):
# - imageBase64
image_url = request.data.get('imageUrl')
image_base64 = request.data.get('imageBase64')
limit = request.data.get('limit', None)
offset = request.data.get('offset', None)
match_threshold = request.data.get('matchThreshold', None)
try:
if limit is not None and str(limit).strip() != '':
limit = int(limit)
# TencentCloud SearchImage: default 10, max 100
if limit <= 0 or limit > 100:
raise ValueError('limit 必须在 1~100 之间')
else:
limit = None
if offset is not None and str(offset).strip() != '':
offset = int(offset)
if offset < 0:
raise ValueError('offset 必须 >= 0')
else:
offset = None
if match_threshold is not None and str(match_threshold).strip() != '':
match_threshold = int(match_threshold)
if match_threshold < 0 or match_threshold > 100:
raise ValueError('matchThreshold 必须在 0~100 之间')
else:
match_threshold = None
limiter = SimpleRateLimiter(qps=float(getattr(settings, 'TENCENTCLOUD_TIIA_QPS', 10)))
resp = search_image_url_in_tencent_tiia(
image_url=str(image_url or '') if image_url is not None else None,
image_base64=str(image_base64 or '') if image_base64 is not None else None,
limit=limit,
offset=offset,
match_threshold=match_threshold,
rate_limiter=limiter,
)
return Response(resp, status=status.HTTP_200_OK)