Refs #31055 -- Augmented regression tests for database system checks.

We might want to change this in the future but it should be further
discussed first.
This commit is contained in:
Simon Charette 2025-11-08 15:26:36 -05:00 committed by Mariusz Felisiak
parent abfa4619fb
commit 1b539ef27e
2 changed files with 34 additions and 3 deletions

View file

@ -1,11 +1,11 @@
import multiprocessing
import sys
from io import StringIO
from unittest import skipIf
from unittest import mock, skipIf
from django.apps import apps
from django.core import checks
from django.core.checks import Error, Warning
from django.core.checks import Error, Tags, Warning
from django.core.checks.messages import CheckMessage
from django.core.checks.registry import CheckRegistry
from django.core.management import call_command
@ -268,6 +268,18 @@ class CheckCommandTests(SimpleTestCase):
with self.assertRaises(CommandError):
call_command("check", fail_level="WARNING")
def test_database_system_checks(self):
database_check = mock.Mock(return_value=[], tags=[Tags.database])
with override_system_checks([database_check]):
call_command("check")
database_check.assert_called_once_with(app_configs=None, databases=None)
database_check.reset_mock()
call_command("check", databases=["default"])
database_check.assert_called_once_with(
app_configs=None, databases=["default"]
)
def custom_error_system_check(app_configs, **kwargs):
return [Error("Error", id="myerrorcheck.E001")]

View file

@ -113,7 +113,7 @@ class MigrateTests(MigrationTestBase):
out = io.StringIO()
call_command("migrate", skip_checks=False, no_color=True, stdout=out)
self.assertIn("Apply all migrations: migrated_app", out.getvalue())
mocked_check.assert_called_once()
mocked_check.assert_called_once_with(databases=["default"])
def test_migrate_with_custom_system_checks(self):
original_checks = registry.registered_checks.copy()
@ -137,6 +137,25 @@ class MigrateTests(MigrationTestBase):
command = CustomMigrateCommandWithSecurityChecks()
call_command(command, skip_checks=False, stdout=io.StringIO())
@override_settings(
INSTALLED_APPS=[
"django.contrib.auth",
"django.contrib.contenttypes",
"migrations.migrations_test_apps.migrated_app",
]
)
def test_migrate_runs_database_system_checks(self):
original_checks = registry.registered_checks.copy()
self.addCleanup(setattr, registry, "registered_checks", original_checks)
out = io.StringIO()
mock_check = mock.Mock(return_value=[])
register(mock_check, Tags.database)
call_command("migrate", skip_checks=False, no_color=True, stdout=out)
self.assertIn("Apply all migrations: migrated_app", out.getvalue())
mock_check.assert_called_once_with(app_configs=None, databases=["default"])
@override_settings(
INSTALLED_APPS=[
"migrations",