44 lines
1.3 KiB
Python
44 lines
1.3 KiB
Python
import asyncio
|
|
import os
|
|
import tempfile
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
import backend.main as main
|
|
from backend.history_store import HistoryStore
|
|
|
|
|
|
class HistoryApiTest(unittest.TestCase):
|
|
def setUp(self):
|
|
fd, self.path = tempfile.mkstemp(suffix=".db")
|
|
os.close(fd)
|
|
self.store = HistoryStore(self.path)
|
|
self.original_store = main.history_store
|
|
main.history_store = self.store
|
|
|
|
def tearDown(self):
|
|
main.history_store = self.original_store
|
|
os.remove(self.path)
|
|
|
|
def test_lists_and_gets_full_diagnosis_history(self):
|
|
record_id = self.store.save_diagnosis(
|
|
{"timestamp": "2026-07-25T12:00:00", "namespace": "prod", "score": 50, "status": "critical"},
|
|
"report",
|
|
)
|
|
|
|
records = asyncio.run(main.list_diagnosis_history())
|
|
detail = asyncio.run(main.get_diagnosis_history(record_id))
|
|
|
|
self.assertEqual(records["total"], 1)
|
|
self.assertEqual(records["items"][0]["id"], record_id)
|
|
self.assertEqual(detail["report"], "report")
|
|
|
|
def test_get_history_returns_404_for_unknown_id(self):
|
|
with self.assertRaises(main.HTTPException) as context:
|
|
asyncio.run(main.get_diagnosis_history("missing"))
|
|
self.assertEqual(context.exception.status_code, 404)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|