fix: harden state corp exchange imports

This commit is contained in:
2026-07-22 14:23:05 +02:00
parent bafe04dd40
commit dec81f6e7c
7 changed files with 152 additions and 1 deletions

View 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)