diff options
| author | Markus Holtermann <info@markusholtermann.eu> | 2014-12-02 19:25:46 +0100 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2014-12-29 13:42:29 -0500 |
| commit | 1702bc52cc20ed0729893177fc8f4391b4b3183c (patch) | |
| tree | 72fbd4f134f8b44f194038536ab43e9bb1fc8f59 /tests | |
| parent | 1cbdb49b0aaba726435db55c4d71dc6a8ade1d2b (diff) | |
[1.7.x] Fixed #23938 -- Added migration support for m2m to concrete fields and vice versa
Thanks to Michael D. Hoyle for the report and Tim Graham for the review.
Backport of 623ccdd598625591d1a12fc1564cf3ef9a87581f from master
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/migrations/test_autodetector.py | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/tests/migrations/test_autodetector.py b/tests/migrations/test_autodetector.py index 8beda8e552..d097d4f2da 100644 --- a/tests/migrations/test_autodetector.py +++ b/tests/migrations/test_autodetector.py @@ -116,6 +116,10 @@ class AutodetectorTests(TestCase): ("id", models.AutoField(primary_key=True)), ("publishers", models.ManyToManyField("testapp.Publisher", through="testapp.Contract")), ]) + author_with_former_m2m = ModelState("testapp", "Author", [ + ("id", models.AutoField(primary_key=True)), + ("publishers", models.CharField(max_length=100)), + ]) author_with_options = ModelState("testapp", "Author", [ ("id", models.AutoField(primary_key=True)), ], { @@ -1274,6 +1278,39 @@ class AutodetectorTests(TestCase): self.assertOperationAttributes(changes, "testapp", 0, 3, name="publisher", model_name='contract') self.assertOperationAttributes(changes, "testapp", 0, 4, name="Contract") + def test_concrete_field_changed_to_many_to_many(self): + """ + #23938 - Tests that changing a concrete field into a ManyToManyField + first removes the concrete field and then adds the m2m field. + """ + before = self.make_project_state([self.author_with_former_m2m]) + after = self.make_project_state([self.author_with_m2m, self.publisher]) + autodetector = MigrationAutodetector(before, after) + changes = autodetector._detect_changes() + # Right number/type of migrations? + self.assertNumberMigrations(changes, "testapp", 1) + self.assertOperationTypes(changes, "testapp", 0, ["CreateModel", "RemoveField", "AddField"]) + self.assertOperationAttributes(changes, 'testapp', 0, 0, name='Publisher') + self.assertOperationAttributes(changes, 'testapp', 0, 1, name="publishers", model_name='author') + self.assertOperationAttributes(changes, 'testapp', 0, 2, name="publishers", model_name='author') + + def test_many_to_many_changed_to_concrete_field(self): + """ + #23938 - Tests that changing a ManyToManyField into a concrete field + first removes the m2m field and then adds the concrete field. + """ + before = self.make_project_state([self.author_with_m2m, self.publisher]) + after = self.make_project_state([self.author_with_former_m2m]) + autodetector = MigrationAutodetector(before, after) + changes = autodetector._detect_changes() + # Right number/type of migrations? + self.assertNumberMigrations(changes, "testapp", 1) + self.assertOperationTypes(changes, "testapp", 0, ["RemoveField", "AddField", "DeleteModel"]) + self.assertOperationAttributes(changes, 'testapp', 0, 0, name="publishers", model_name='author') + self.assertOperationAttributes(changes, 'testapp', 0, 1, name="publishers", model_name='author') + self.assertOperationAttributes(changes, 'testapp', 0, 2, name='Publisher') + self.assertOperationFieldAttributes(changes, 'testapp', 0, 1, max_length=100) + def test_non_circular_foreignkey_dependency_removal(self): """ If two models with a ForeignKey from one to the other are removed at the |
