diff --git a/database/repositories/integration_repository.py b/database/repositories/integration_repository.py index 396de9f..aa621f3 100644 --- a/database/repositories/integration_repository.py +++ b/database/repositories/integration_repository.py @@ -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 {} diff --git a/database/repositories/job_repository.py b/database/repositories/job_repository.py index e8c130f..52a67d6 100644 --- a/database/repositories/job_repository.py +++ b/database/repositories/job_repository.py @@ -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 {} diff --git a/tests/test_integration_service.py b/tests/test_integration_service.py index e56e129..e9e2c66 100644 --- a/tests/test_integration_service.py +++ b/tests/test_integration_service.py @@ -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() diff --git a/tests/test_queue_service.py b/tests/test_queue_service.py index caef835..94ff68b 100644 --- a/tests/test_queue_service.py +++ b/tests/test_queue_service.py @@ -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()