37 lines
1.3 KiB
Python
37 lines
1.3 KiB
Python
import contextlib
|
|
import io
|
|
import json
|
|
import os
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
from sqlalchemy.exc import OperationalError
|
|
|
|
os.environ.setdefault("DATABASE_URL", "postgresql+psycopg://user:pass@localhost:5432/ai_ict_test")
|
|
|
|
from scripts import run_execution_ticket
|
|
|
|
|
|
class ExecutionTicketScriptTests(unittest.TestCase):
|
|
def test_build_execution_mode_defaults_to_semi_auto(self) -> None:
|
|
self.assertEqual(run_execution_ticket.build_execution_mode_from_env({}), "semi_auto")
|
|
self.assertEqual(run_execution_ticket.build_execution_mode_from_env({"EXECUTION_MODE": "paper"}), "paper")
|
|
|
|
def test_main_reports_database_unavailable_json(self) -> None:
|
|
output = io.StringIO()
|
|
|
|
with patch(
|
|
"scripts.run_execution_ticket.ExecutionService.create_execution_ticket",
|
|
side_effect=OperationalError("stmt", {}, Exception()),
|
|
), contextlib.redirect_stdout(output):
|
|
exit_code = run_execution_ticket.main(env={"EXECUTION_TICKET_OUTPUT_JSON": "1"})
|
|
|
|
payload = json.loads(output.getvalue())
|
|
self.assertEqual(exit_code, 2)
|
|
self.assertFalse(payload["passed"])
|
|
self.assertEqual(payload["error_type"], "OperationalError")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|