Normalize repository query limits
这个提交存在于:
父节点
7edd813d4b
当前提交
64cf87a0c9
共有 4 个文件被更改,包括 42 次插入 和 2 次删除
|
|
@ -81,7 +81,7 @@ class IntegrationRepository:
|
|||
ORDER BY created_at DESC
|
||||
LIMIT {placeholder}
|
||||
""",
|
||||
tuple(params + [limit]),
|
||||
tuple(params + [_limit(limit)]),
|
||||
).fetchall()
|
||||
finally:
|
||||
conn.close()
|
||||
|
|
@ -106,6 +106,14 @@ def _json_or_none(value: Any | None) -> str | None:
|
|||
return json.dumps(value, ensure_ascii=False, sort_keys=True)
|
||||
|
||||
|
||||
def _limit(value: int) -> int:
|
||||
try:
|
||||
limit = int(value)
|
||||
except (TypeError, ValueError):
|
||||
limit = 50
|
||||
return min(max(limit, 1), 200)
|
||||
|
||||
|
||||
def _json_loads(value: str | None) -> dict[str, Any]:
|
||||
if not value:
|
||||
return {}
|
||||
|
|
|
|||
|
|
@ -229,7 +229,7 @@ class JobRepository:
|
|||
ORDER BY created_at DESC
|
||||
LIMIT {placeholder}
|
||||
""",
|
||||
(limit,),
|
||||
(_limit(limit),),
|
||||
).fetchall()
|
||||
finally:
|
||||
conn.close()
|
||||
|
|
@ -262,6 +262,14 @@ def _json_or_none(value: Any | None) -> str | None:
|
|||
return json.dumps(value, ensure_ascii=False, sort_keys=True)
|
||||
|
||||
|
||||
def _limit(value: int) -> int:
|
||||
try:
|
||||
limit = int(value)
|
||||
except (TypeError, ValueError):
|
||||
limit = 10
|
||||
return min(max(limit, 1), 100)
|
||||
|
||||
|
||||
def _json_loads(value: str | None) -> dict[str, Any]:
|
||||
if not value:
|
||||
return {}
|
||||
|
|
|
|||
|
|
@ -138,6 +138,21 @@ class IntegrationServiceTests(unittest.TestCase):
|
|||
self.assertEqual(logs[0]["action"], "daily_outfit")
|
||||
self.assertEqual(logs[0]["response"]["error_code"], "VALIDATION_FAILED")
|
||||
|
||||
def test_integration_log_limit_is_normalized(self):
|
||||
for index in range(3):
|
||||
self.integration_repository.record(
|
||||
{
|
||||
"integration_log_id": f"log_{index}",
|
||||
"integration_name": "manual",
|
||||
"action": "sync",
|
||||
"status": "succeeded",
|
||||
}
|
||||
)
|
||||
|
||||
self.assertEqual(len(self.integration_repository.list(limit=0)), 1)
|
||||
self.assertEqual(len(self.integration_repository.list(limit=-10)), 1)
|
||||
self.assertEqual(len(self.integration_repository.list(limit=999)), 3)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
|
|
|||
|
|
@ -77,6 +77,15 @@ class QueueServiceTests(unittest.TestCase):
|
|||
self.assertEqual(retried["total"], 1)
|
||||
self.assertEqual(self.queue_service.queue_summary()["dead_letter"], 2)
|
||||
|
||||
def test_recent_job_limit_is_normalized(self):
|
||||
repository = self.queue_service.repository
|
||||
for index in range(3):
|
||||
self.queue_service.create_task({"status": "queued", "request": {"index": index}}, self.ctx)
|
||||
|
||||
self.assertEqual(len(repository.recent(limit=0)), 1)
|
||||
self.assertEqual(len(repository.recent(limit=-5)), 1)
|
||||
self.assertEqual(len(repository.recent(limit=999)), 3)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
|
|
|||
正在加载…
添加表格
在新工单中引用