diff options
| author | Andrew Godwin <andrew@aeracode.org> | 2014-05-20 16:25:59 +0100 |
|---|---|---|
| committer | Andrew Godwin <andrew@aeracode.org> | 2014-05-20 16:25:59 +0100 |
| commit | 03900a02d5fd2efd6ae98bdb9974b78146f63040 (patch) | |
| tree | 1c5c2096336c9a8a6a9dc59cac54ff18fb754cd1 /tests | |
| parent | 125b3d440761efca8fa808a4b815384ece847d6a (diff) | |
Fixed #22432: SQLite M2M repointing now works. Thanks to xelnor.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/migrations/test_operations.py | 45 |
1 files changed, 44 insertions, 1 deletions
diff --git a/tests/migrations/test_operations.py b/tests/migrations/test_operations.py index 85fd42b60f..2366ef9ab8 100644 --- a/tests/migrations/test_operations.py +++ b/tests/migrations/test_operations.py @@ -36,12 +36,23 @@ class OperationTests(MigrationTestBase): with connection.schema_editor() as editor: return migration.unapply(project_state, editor) - def set_up_test_model(self, app_label, second_model=False, related_model=False, mti_model=False): + def set_up_test_model(self, app_label, second_model=False, third_model=False, related_model=False, mti_model=False): """ Creates a test model state and database table. """ # Delete the tables if they already exist with connection.cursor() as cursor: + # Start with ManyToMany tables + try: + cursor.execute("DROP TABLE %s_pony_stables" % app_label) + except DatabaseError: + pass + try: + cursor.execute("DROP TABLE %s_pony_vans" % app_label) + except DatabaseError: + pass + + # Then standard model tables try: cursor.execute("DROP TABLE %s_pony" % app_label) except DatabaseError: @@ -50,6 +61,10 @@ class OperationTests(MigrationTestBase): cursor.execute("DROP TABLE %s_stable" % app_label) except DatabaseError: pass + try: + cursor.execute("DROP TABLE %s_van" % app_label) + except DatabaseError: + pass # Make the "current" state operations = [migrations.CreateModel( "Pony", @@ -66,6 +81,13 @@ class OperationTests(MigrationTestBase): ("id", models.AutoField(primary_key=True)), ] )) + if third_model: + operations.append(migrations.CreateModel( + "Van", + [ + ("id", models.AutoField(primary_key=True)), + ] + )) if related_model: operations.append(migrations.CreateModel( "Rider", @@ -537,6 +559,27 @@ class OperationTests(MigrationTestBase): Pony = new_apps.get_model("test_alflmm", "Pony") self.assertTrue(Pony._meta.get_field('stables').blank) + def test_repoint_field_m2m(self): + project_state = self.set_up_test_model("test_alflmm", second_model=True, third_model=True) + + project_state = self.apply_operations("test_alflmm", project_state, operations=[ + migrations.AddField("Pony", "places", models.ManyToManyField("Stable", related_name="ponies")) + ]) + new_apps = project_state.render() + Pony = new_apps.get_model("test_alflmm", "Pony") + + project_state = self.apply_operations("test_alflmm", project_state, operations=[ + migrations.AlterField("Pony", "places", models.ManyToManyField(to="Van", related_name="ponies")) + ]) + + # Ensure the new field actually works + new_apps = project_state.render() + Pony = new_apps.get_model("test_alflmm", "Pony") + p = Pony.objects.create(pink=False, weight=4.55) + p.places.create() + self.assertEqual(p.places.count(), 1) + p.places.all().delete() + def test_remove_field_m2m(self): project_state = self.set_up_test_model("test_rmflmm", second_model=True) |
