|
| 1 | +import unittest |
| 2 | +from unittest.mock import patch |
| 3 | + |
| 4 | +from fastapi import FastAPI |
| 5 | +from fastapi.testclient import TestClient |
| 6 | + |
| 7 | +from ocr_service.api.health import health_api |
| 8 | + |
| 9 | + |
| 10 | +class DummySubprocess: |
| 11 | + def __init__(self, pid: int, returncode: int | None = None) -> None: |
| 12 | + self.pid = pid |
| 13 | + self._returncode = returncode |
| 14 | + |
| 15 | + def poll(self) -> int | None: |
| 16 | + return self._returncode |
| 17 | + |
| 18 | + |
| 19 | +class DummyPsutilProcess: |
| 20 | + def __init__(self, running: bool, process_status: str) -> None: |
| 21 | + self._running = running |
| 22 | + self._status = process_status |
| 23 | + |
| 24 | + def is_running(self) -> bool: |
| 25 | + return self._running |
| 26 | + |
| 27 | + def status(self) -> str: |
| 28 | + return self._status |
| 29 | + |
| 30 | + |
| 31 | +class DummyProcessor: |
| 32 | + def __init__(self, loffice_process_list): |
| 33 | + self.loffice_process_list = loffice_process_list |
| 34 | + |
| 35 | + |
| 36 | +class TestHealthApi(unittest.TestCase): |
| 37 | + def setUp(self) -> None: |
| 38 | + self.app = FastAPI() |
| 39 | + self.app.include_router(health_api) |
| 40 | + self.client = TestClient(self.app) |
| 41 | + |
| 42 | + def tearDown(self) -> None: |
| 43 | + self.client.close() |
| 44 | + |
| 45 | + def test_health_returns_healthy(self): |
| 46 | + response = self.client.get("/api/health") |
| 47 | + self.assertEqual(response.status_code, 200) |
| 48 | + self.assertEqual(response.json(), {"status": "healthy"}) |
| 49 | + |
| 50 | + def test_ready_returns_503_when_processor_not_initialized(self): |
| 51 | + response = self.client.get("/api/ready") |
| 52 | + self.assertEqual(response.status_code, 503) |
| 53 | + data = response.json() |
| 54 | + self.assertEqual(data.get("status"), "not_ready") |
| 55 | + self.assertIn("processor_not_initialized", data.get("issues", [])) |
| 56 | + |
| 57 | + def test_ready_returns_503_when_libreoffice_process_exited(self): |
| 58 | + self.app.state.processor = DummyProcessor( |
| 59 | + { |
| 60 | + "9900": { |
| 61 | + "process": DummySubprocess(pid=12345, returncode=1), |
| 62 | + "pid": 12345, |
| 63 | + "unhealthy": False, |
| 64 | + } |
| 65 | + } |
| 66 | + ) |
| 67 | + |
| 68 | + response = self.client.get("/api/ready") |
| 69 | + self.assertEqual(response.status_code, 503) |
| 70 | + data = response.json() |
| 71 | + self.assertEqual(data.get("status"), "not_ready") |
| 72 | + self.assertIn("libreoffice_process_exited:9900", data.get("issues", [])) |
| 73 | + |
| 74 | + @patch("ocr_service.api.health.psutil.Process") |
| 75 | + @patch("ocr_service.api.health.psutil.pid_exists", return_value=True) |
| 76 | + def test_ready_returns_200_for_running_libreoffice_process(self, _pid_exists, process_mock): |
| 77 | + process_mock.return_value = DummyPsutilProcess(running=True, process_status="sleeping") |
| 78 | + self.app.state.processor = DummyProcessor( |
| 79 | + { |
| 80 | + "9900": { |
| 81 | + "process": DummySubprocess(pid=12345, returncode=None), |
| 82 | + "pid": 12345, |
| 83 | + "unhealthy": False, |
| 84 | + } |
| 85 | + } |
| 86 | + ) |
| 87 | + |
| 88 | + response = self.client.get("/api/ready") |
| 89 | + self.assertEqual(response.status_code, 200) |
| 90 | + self.assertEqual(response.json(), {"status": "ready", "libreoffice_processes": 1}) |
| 91 | + |
0 commit comments