From 1b539ef27e776bd6112e8f22e653feca5279c4fb Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Sat, 8 Nov 2025 15:26:36 -0500 Subject: 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. --- tests/check_framework/tests.py | 16 ++++++++++++++-- tests/migrations/test_commands.py | 21 ++++++++++++++++++++- 2 files changed, 34 insertions(+), 3 deletions(-) (limited to 'tests') diff --git a/tests/check_framework/tests.py b/tests/check_framework/tests.py index f926308fd7..c5f53c4789 100644 --- a/tests/check_framework/tests.py +++ b/tests/check_framework/tests.py @@ -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")] diff --git a/tests/migrations/test_commands.py b/tests/migrations/test_commands.py index b5817081d2..83c5575fd3 100644 --- a/tests/migrations/test_commands.py +++ b/tests/migrations/test_commands.py @@ -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", -- cgit v1.3