From 3b810c5656774b4e1a89fd69ff26935e0a5833d2 Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Wed, 25 Sep 2013 13:58:07 +0100 Subject: Add RunPython migration operation and tests --- tests/migrations/test_operations.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/migrations/test_operations.py b/tests/migrations/test_operations.py index 72f938fe4e..1f247c1a97 100644 --- a/tests/migrations/test_operations.py +++ b/tests/migrations/test_operations.py @@ -282,7 +282,7 @@ class OperationTests(MigrationTestBase): def test_run_sql(self): """ - Tests the AlterIndexTogether operation. + Tests the RunSQL operation. """ project_state = self.set_up_test_model("test_runsql") # Create the operation @@ -306,6 +306,33 @@ class OperationTests(MigrationTestBase): operation.database_backwards("test_runsql", editor, new_state, project_state) self.assertTableNotExists("i_love_ponies") + def test_run_python(self): + """ + Tests the RunPython operation + """ + + project_state = self.set_up_test_model("test_runpython") + # Create the operation + operation = migrations.RunPython( + """ + Pony = models.get_model("test_runpython", "Pony") + Pony.objects.create(pink=2, weight=4.55) + Pony.objects.create(weight=1) + """, + ) + # Test the state alteration does nothing + new_state = project_state.clone() + operation.state_forwards("test_runpython", new_state) + self.assertEqual(new_state, project_state) + # Test the database alteration + self.assertEqual(project_state.render().get_model("test_runpython", "Pony").objects.count(), 0) + with connection.schema_editor() as editor: + operation.database_forwards("test_runpython", editor, project_state, new_state) + self.assertEqual(project_state.render().get_model("test_runpython", "Pony").objects.count(), 2) + # And test reversal fails + with self.assertRaises(NotImplementedError): + operation.database_backwards("test_runpython", None, new_state, project_state) + class MigrateNothingRouter(object): """ -- cgit v1.3