summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/migrations/test_operations.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/tests/migrations/test_operations.py b/tests/migrations/test_operations.py
index ef23dcf80f..ecd2fe3e89 100644
--- a/tests/migrations/test_operations.py
+++ b/tests/migrations/test_operations.py
@@ -90,6 +90,46 @@ class OperationTests(MigrationTestBase):
self.assertEqual(len(definition[2]), 0)
self.assertEqual(definition[1][0], "Pony")
+ def test_create_model_m2m(self):
+ """
+ Test the creation of a model with a ManyToMany field and the
+ auto-created "through" model.
+ """
+ project_state = self.set_up_test_model("test_crmomm")
+ operation = migrations.CreateModel(
+ "Stable",
+ [
+ ("id", models.AutoField(primary_key=True)),
+ ("ponies", models.ManyToManyField("Pony", related_name="stables"))
+ ]
+ )
+ # Test the state alteration
+ new_state = project_state.clone()
+ operation.state_forwards("test_crmomm", new_state)
+ # Test the database alteration
+ self.assertTableNotExists("test_crmomm_stable_ponies")
+ with connection.schema_editor() as editor:
+ operation.database_forwards("test_crmomm", editor, project_state, new_state)
+ self.assertTableExists("test_crmomm_stable")
+ self.assertTableExists("test_crmomm_stable_ponies")
+ self.assertColumnNotExists("test_crmomm", "ponies")
+ # Make sure the M2M field actually works
+ with atomic():
+ new_apps = new_state.render()
+ Pony = new_apps.get_model("test_crmomm", "Pony")
+ Stable = new_apps.get_model("test_crmomm", "Stable")
+ stable = Stable.objects.create()
+ p1 = Pony.objects.create(pink=False, weight=4.55)
+ p2 = Pony.objects.create(pink=True, weight=5.43)
+ stable.ponies.add(p1, p2)
+ self.assertEqual(stable.ponies.count(), 2)
+ stable.ponies.all().delete()
+ # And test reversal
+ with connection.schema_editor() as editor:
+ operation.database_backwards("test_crmomm", editor, new_state, project_state)
+ self.assertTableNotExists("test_crmomm_stable")
+ self.assertTableNotExists("test_crmomm_stable_ponies")
+
def test_create_model_inheritance(self):
"""
Tests the CreateModel operation on a multi-table inheritance setup.