mirror of
				https://github.com/django/django.git
				synced 2025-10-31 09:41:08 +00:00 
			
		
		
		
	[2.2.x] Made test table cleanup in OperationTestBase more robust.
Some non-unique constraint names were added inb69f8eb04cwhich resulted in failures depending on the order in which tests were run. Backport of62b8596616from master.
This commit is contained in:
		
				
					committed by
					
						 Tim Graham
						Tim Graham
					
				
			
			
				
	
			
			
			
						parent
						
							d3b4f4b962
						
					
				
				
					commit
					2ca200a7c3
				
			| @@ -122,13 +122,16 @@ class MultiDBOperationTests(OperationTestBase): | ||||
|             self.assertEqual(Pony.objects.count(), 0) | ||||
|  | ||||
|     @override_settings(DATABASE_ROUTERS=[MigrateNothingRouter()]) | ||||
|     def test_run_sql(self): | ||||
|     def test_run_sql_migrate_nothing_router(self): | ||||
|         self._test_run_sql("test_mltdb_runsql", should_run=False) | ||||
|  | ||||
|     @override_settings(DATABASE_ROUTERS=[MigrateWhenFooRouter()]) | ||||
|     def test_run_sql2(self): | ||||
|     def test_run_sql_migrate_foo_router_without_hints(self): | ||||
|         self._test_run_sql("test_mltdb_runsql2", should_run=False) | ||||
|         self._test_run_sql("test_mltdb_runsql2", should_run=True, hints={'foo': True}) | ||||
|  | ||||
|     @override_settings(DATABASE_ROUTERS=[MigrateWhenFooRouter()]) | ||||
|     def test_run_sql_migrate_foo_router_with_hints(self): | ||||
|         self._test_run_sql('test_mltdb_runsql3', should_run=True, hints={'foo': True}) | ||||
|  | ||||
|     def _test_run_python(self, app_label, should_run, hints=None): | ||||
|         with override_settings(DATABASE_ROUTERS=[MigrateEverythingRouter()]): | ||||
| @@ -156,10 +159,13 @@ class MultiDBOperationTests(OperationTestBase): | ||||
|             self.assertEqual(Pony.objects.count(), 0) | ||||
|  | ||||
|     @override_settings(DATABASE_ROUTERS=[MigrateNothingRouter()]) | ||||
|     def test_run_python(self): | ||||
|     def test_run_python_migrate_nothing_router(self): | ||||
|         self._test_run_python("test_mltdb_runpython", should_run=False) | ||||
|  | ||||
|     @override_settings(DATABASE_ROUTERS=[MigrateWhenFooRouter()]) | ||||
|     def test_run_python2(self): | ||||
|     def test_run_python_migrate_foo_router_without_hints(self): | ||||
|         self._test_run_python("test_mltdb_runpython2", should_run=False) | ||||
|         self._test_run_python("test_mltdb_runpython2", should_run=True, hints={'foo': True}) | ||||
|  | ||||
|     @override_settings(DATABASE_ROUTERS=[MigrateWhenFooRouter()]) | ||||
|     def test_run_python_migrate_foo_router_with_hints(self): | ||||
|         self._test_run_python('test_mltdb_runpython3', should_run=True, hints={'foo': True}) | ||||
|   | ||||
| @@ -22,6 +22,24 @@ class OperationTestBase(MigrationTestBase): | ||||
|     Common functions to help test operations. | ||||
|     """ | ||||
|  | ||||
|     @classmethod | ||||
|     def setUpClass(cls): | ||||
|         super().setUpClass() | ||||
|         cls._initial_table_names = frozenset(connection.introspection.table_names()) | ||||
|  | ||||
|     def tearDown(self): | ||||
|         self.cleanup_test_tables() | ||||
|         super().tearDown() | ||||
|  | ||||
|     def cleanup_test_tables(self): | ||||
|         table_names = frozenset(connection.introspection.table_names()) - self._initial_table_names | ||||
|         with connection.schema_editor() as editor: | ||||
|             with connection.constraint_checks_disabled(): | ||||
|                 for table_name in table_names: | ||||
|                     editor.execute(editor.sql_delete_table % { | ||||
|                         'table': editor.quote_name(table_name), | ||||
|                     }) | ||||
|  | ||||
|     def apply_operations(self, app_label, project_state, operations, atomic=True): | ||||
|         migration = Migration('name', app_label) | ||||
|         migration.operations = operations | ||||
| @@ -51,26 +69,6 @@ class OperationTestBase(MigrationTestBase): | ||||
|         """ | ||||
|         Creates a test model state and database table. | ||||
|         """ | ||||
|         # Delete the tables if they already exist | ||||
|         table_names = [ | ||||
|             # Start with ManyToMany tables | ||||
|             '_pony_stables', '_pony_vans', | ||||
|             # Then standard model tables | ||||
|             '_pony', '_stable', '_van', | ||||
|         ] | ||||
|         tables = [(app_label + table_name) for table_name in table_names] | ||||
|         with connection.cursor() as cursor: | ||||
|             table_names = connection.introspection.table_names(cursor) | ||||
|             connection.disable_constraint_checking() | ||||
|             sql_delete_table = connection.schema_editor().sql_delete_table | ||||
|             with transaction.atomic(): | ||||
|                 for table in tables: | ||||
|                     if table in table_names: | ||||
|                         cursor.execute(sql_delete_table % { | ||||
|                             "table": connection.ops.quote_name(table), | ||||
|                         }) | ||||
|             connection.enable_constraint_checking() | ||||
|  | ||||
|         # Make the "current" state | ||||
|         model_options = { | ||||
|             "swappable": "TEST_SWAP_MODEL", | ||||
|   | ||||
		Reference in New Issue
	
	Block a user