fix: harden state corp exchange imports
This commit is contained in:
38
tests/apps/core/test_startup_checks.py
Normal file
38
tests/apps/core/test_startup_checks.py
Normal file
@@ -0,0 +1,38 @@
|
||||
"""Tests for fail-fast dependency checks."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
from apps.core.startup_checks import _check_db
|
||||
from django.test import SimpleTestCase
|
||||
|
||||
|
||||
class DatabaseStartupCheckTest(SimpleTestCase):
|
||||
"""Require a Unicode-safe PostgreSQL database before startup."""
|
||||
|
||||
@staticmethod
|
||||
def _connection_with_encoding(encoding: str) -> MagicMock:
|
||||
connection = MagicMock()
|
||||
cursor = connection.cursor.return_value.__enter__.return_value
|
||||
cursor.fetchone.side_effect = [(1,), (encoding,)]
|
||||
return connection
|
||||
|
||||
@patch("apps.core.startup_checks.psycopg2.connect")
|
||||
def test_check_db_accepts_utf8_database(self, connect):
|
||||
connect.return_value = self._connection_with_encoding("UTF8")
|
||||
|
||||
ok, message = _check_db(timeout_seconds=3)
|
||||
|
||||
self.assertTrue(ok)
|
||||
self.assertEqual(message, "OK")
|
||||
|
||||
@patch("apps.core.startup_checks.psycopg2.connect")
|
||||
def test_check_db_rejects_non_utf8_database(self, connect):
|
||||
connect.return_value = self._connection_with_encoding("SQL_ASCII")
|
||||
|
||||
ok, message = _check_db(timeout_seconds=3)
|
||||
|
||||
self.assertFalse(ok)
|
||||
self.assertIn("database encoding SQL_ASCII is unsupported", message)
|
||||
self.assertIn("UTF8 is required", message)
|
||||
Reference in New Issue
Block a user