mirror of
				https://github.com/django/django.git
				synced 2025-10-31 09:41:08 +00:00 
			
		
		
		
	[5.0.x] Fixed #34849 -- Avoided raising RuntimeWarning about import-time queries when apps are reinitialized with test tools.
Regression infbd16438f4. Backport of4f2ae0644dfrom main
This commit is contained in:
		
				
					committed by
					
						 Mariusz Felisiak
						Mariusz Felisiak
					
				
			
			
				
	
			
			
			
						parent
						
							81663cc4ca
						
					
				
				
					commit
					4910920869
				
			| @@ -61,7 +61,9 @@ class CursorWrapper: | |||||||
|                 "Keyword parameters for callproc are not supported on this " |                 "Keyword parameters for callproc are not supported on this " | ||||||
|                 "database backend." |                 "database backend." | ||||||
|             ) |             ) | ||||||
|         if not apps.ready: |         # Raise a warning during app initialization (stored_app_configs is only | ||||||
|  |         # ever set during testing). | ||||||
|  |         if not apps.ready and not apps.stored_app_configs: | ||||||
|             warnings.warn(self.APPS_NOT_READY_WARNING_MSG, category=RuntimeWarning) |             warnings.warn(self.APPS_NOT_READY_WARNING_MSG, category=RuntimeWarning) | ||||||
|         self.db.validate_no_broken_transaction() |         self.db.validate_no_broken_transaction() | ||||||
|         with self.db.wrap_database_errors: |         with self.db.wrap_database_errors: | ||||||
| @@ -90,7 +92,9 @@ class CursorWrapper: | |||||||
|         return executor(sql, params, many, context) |         return executor(sql, params, many, context) | ||||||
|  |  | ||||||
|     def _execute(self, sql, params, *ignored_wrapper_args): |     def _execute(self, sql, params, *ignored_wrapper_args): | ||||||
|         if not apps.ready: |         # Raise a warning during app initialization (stored_app_configs is only | ||||||
|  |         # ever set during testing). | ||||||
|  |         if not apps.ready and not apps.stored_app_configs: | ||||||
|             warnings.warn(self.APPS_NOT_READY_WARNING_MSG, category=RuntimeWarning) |             warnings.warn(self.APPS_NOT_READY_WARNING_MSG, category=RuntimeWarning) | ||||||
|         self.db.validate_no_broken_transaction() |         self.db.validate_no_broken_transaction() | ||||||
|         with self.db.wrap_database_errors: |         with self.db.wrap_database_errors: | ||||||
| @@ -101,7 +105,9 @@ class CursorWrapper: | |||||||
|                 return self.cursor.execute(sql, params) |                 return self.cursor.execute(sql, params) | ||||||
|  |  | ||||||
|     def _executemany(self, sql, param_list, *ignored_wrapper_args): |     def _executemany(self, sql, param_list, *ignored_wrapper_args): | ||||||
|         if not apps.ready: |         # Raise a warning during app initialization (stored_app_configs is only | ||||||
|  |         # ever set during testing). | ||||||
|  |         if not apps.ready and not apps.stored_app_configs: | ||||||
|             warnings.warn(self.APPS_NOT_READY_WARNING_MSG, category=RuntimeWarning) |             warnings.warn(self.APPS_NOT_READY_WARNING_MSG, category=RuntimeWarning) | ||||||
|         self.db.validate_no_broken_transaction() |         self.db.validate_no_broken_transaction() | ||||||
|         with self.db.wrap_database_errors: |         with self.db.wrap_database_errors: | ||||||
|   | |||||||
| @@ -607,10 +607,9 @@ class QueryPerformingAppTests(TransactionTestCase): | |||||||
|         custom_settings = override_settings( |         custom_settings = override_settings( | ||||||
|             INSTALLED_APPS=[f"apps.query_performing_app.apps.{app_config_name}"] |             INSTALLED_APPS=[f"apps.query_performing_app.apps.{app_config_name}"] | ||||||
|         ) |         ) | ||||||
|         # Ignore the RuntimeWarning, as override_settings.enable() calls |  | ||||||
|         # AppConfig.ready() which will trigger the warning. |  | ||||||
|         with self.assertWarnsMessage(RuntimeWarning, self.expected_msg): |  | ||||||
|         custom_settings.enable() |         custom_settings.enable() | ||||||
|  |         old_stored_app_configs = apps.stored_app_configs | ||||||
|  |         apps.stored_app_configs = [] | ||||||
|         try: |         try: | ||||||
|             with patch.multiple(apps, ready=False, loading=False, app_configs={}): |             with patch.multiple(apps, ready=False, loading=False, app_configs={}): | ||||||
|                 with self.assertWarnsMessage(RuntimeWarning, self.expected_msg): |                 with self.assertWarnsMessage(RuntimeWarning, self.expected_msg): | ||||||
| @@ -619,4 +618,5 @@ class QueryPerformingAppTests(TransactionTestCase): | |||||||
|                 app_config = apps.get_app_config("query_performing_app") |                 app_config = apps.get_app_config("query_performing_app") | ||||||
|                 return app_config.query_results |                 return app_config.query_results | ||||||
|         finally: |         finally: | ||||||
|  |             setattr(apps, "stored_app_configs", old_stored_app_configs) | ||||||
|             custom_settings.disable() |             custom_settings.disable() | ||||||
|   | |||||||
| @@ -5,7 +5,7 @@ from django.db import connection | |||||||
| from django.db.backends.signals import connection_created | from django.db.backends.signals import connection_created | ||||||
| from django.db.migrations.writer import MigrationWriter | from django.db.migrations.writer import MigrationWriter | ||||||
| from django.test import TestCase | from django.test import TestCase | ||||||
| from django.test.utils import modify_settings | from django.test.utils import CaptureQueriesContext, modify_settings, override_settings | ||||||
|  |  | ||||||
| try: | try: | ||||||
|     from django.contrib.postgres.fields import ( |     from django.contrib.postgres.fields import ( | ||||||
| @@ -14,6 +14,7 @@ try: | |||||||
|         DecimalRangeField, |         DecimalRangeField, | ||||||
|         IntegerRangeField, |         IntegerRangeField, | ||||||
|     ) |     ) | ||||||
|  |     from django.contrib.postgres.signals import get_hstore_oids | ||||||
|     from django.db.backends.postgresql.psycopg_any import ( |     from django.db.backends.postgresql.psycopg_any import ( | ||||||
|         DateRange, |         DateRange, | ||||||
|         DateTimeRange, |         DateTimeRange, | ||||||
| @@ -27,6 +28,15 @@ except ImportError: | |||||||
|  |  | ||||||
| @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific tests") | @unittest.skipUnless(connection.vendor == "postgresql", "PostgreSQL specific tests") | ||||||
| class PostgresConfigTests(TestCase): | class PostgresConfigTests(TestCase): | ||||||
|  |     def test_install_app_no_warning(self): | ||||||
|  |         # Clear cache to force queries when (re)initializing the | ||||||
|  |         # "django.contrib.postgres" app. | ||||||
|  |         get_hstore_oids.cache_clear() | ||||||
|  |         with CaptureQueriesContext(connection) as captured_queries: | ||||||
|  |             with override_settings(INSTALLED_APPS=["django.contrib.postgres"]): | ||||||
|  |                 pass | ||||||
|  |         self.assertGreaterEqual(len(captured_queries), 1) | ||||||
|  |  | ||||||
|     def test_register_type_handlers_connection(self): |     def test_register_type_handlers_connection(self): | ||||||
|         from django.contrib.postgres.signals import register_type_handlers |         from django.contrib.postgres.signals import register_type_handlers | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user