diff options
| author | David Wobrock <david.wobrock@gmail.com> | 2021-10-16 11:20:33 +0200 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2021-10-25 10:45:35 +0200 |
| commit | ea00a0843eb7a7bb074625a663ca4f5c86b8c5bd (patch) | |
| tree | b3fb925bf40e5ace19eefa8dc09941e60054e2b7 /tests | |
| parent | c9ebe4ca4e3f5d5d76bfbdae489e3f44e32416e5 (diff) | |
[4.0.x] Fixed #31503 -- Made autodetector remove unique/index_together before altering fields.
Backport of 0314593fe8e7dc685bbb6585eee40e755588864e from main
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/migrations/test_autodetector.py | 147 |
1 files changed, 135 insertions, 12 deletions
diff --git a/tests/migrations/test_autodetector.py b/tests/migrations/test_autodetector.py index 6dd4bea4e2..67cc39dec7 100644 --- a/tests/migrations/test_autodetector.py +++ b/tests/migrations/test_autodetector.py @@ -1564,9 +1564,26 @@ class AutodetectorTests(TestCase): ) # Right number/type of migrations? self.assertNumberMigrations(changes, "otherapp", 1) - self.assertOperationTypes(changes, "otherapp", 0, ["AlterUniqueTogether", "AlterIndexTogether"]) - self.assertOperationAttributes(changes, "otherapp", 0, 0, name="book", unique_together={("title", "author")}) - self.assertOperationAttributes(changes, "otherapp", 0, 1, name="book", index_together={("title", "author")}) + self.assertOperationTypes(changes, 'otherapp', 0, [ + 'AlterUniqueTogether', + 'AlterIndexTogether', + 'AlterUniqueTogether', + 'AlterIndexTogether', + ]) + self.assertOperationAttributes( + changes, 'otherapp', 0, 0, name='book', unique_together=set(), + ) + self.assertOperationAttributes( + changes, 'otherapp', 0, 1, name='book', index_together=set(), + ) + self.assertOperationAttributes( + changes, 'otherapp', 0, 2, name='book', + unique_together={('title', 'author')}, + ) + self.assertOperationAttributes( + changes, 'otherapp', 0, 3, name='book', + index_together={('title', 'author')}, + ) def test_add_field_and_foo_together(self): """ @@ -1613,10 +1630,100 @@ class AutodetectorTests(TestCase): ) # Right number/type of migrations? self.assertNumberMigrations(changes, "otherapp", 1) - self.assertOperationTypes(changes, "otherapp", 0, ["AlterUniqueTogether", "AlterIndexTogether", "RemoveField"]) - self.assertOperationAttributes(changes, "otherapp", 0, 0, name="book", unique_together={("author", "title")}) - self.assertOperationAttributes(changes, "otherapp", 0, 1, name="book", index_together={("author", "title")}) - self.assertOperationAttributes(changes, "otherapp", 0, 2, model_name="book", name="newfield") + self.assertOperationTypes(changes, 'otherapp', 0, [ + 'AlterUniqueTogether', + 'AlterIndexTogether', + 'AlterUniqueTogether', + 'AlterIndexTogether', + 'RemoveField', + ]) + self.assertOperationAttributes( + changes, 'otherapp', 0, 0, name='book', unique_together=set(), + ) + self.assertOperationAttributes( + changes, 'otherapp', 0, 1, name='book', index_together=set(), + ) + self.assertOperationAttributes( + changes, 'otherapp', 0, 2, name='book', + unique_together={('author', 'title')}, + ) + self.assertOperationAttributes( + changes, 'otherapp', 0, 3, name='book', + index_together={('author', 'title')}, + ) + self.assertOperationAttributes( + changes, 'otherapp', 0, 4, model_name='book', name='newfield', + ) + + def test_alter_field_and_foo_together(self): + """Fields are altered after deleting some index/unique_together.""" + initial_author = ModelState('testapp', 'Author', [ + ('id', models.AutoField(primary_key=True)), + ('name', models.CharField(max_length=200)), + ('age', models.IntegerField(db_index=True)), + ], { + 'unique_together': {('name',)}, + }) + author_reversed_constraints = ModelState('testapp', 'Author', [ + ('id', models.AutoField(primary_key=True)), + ('name', models.CharField(max_length=200, unique=True)), + ('age', models.IntegerField()), + ], { + 'index_together': {('age',)}, + }) + changes = self.get_changes([initial_author], [author_reversed_constraints]) + + self.assertNumberMigrations(changes, 'testapp', 1) + self.assertOperationTypes(changes, 'testapp', 0, [ + 'AlterUniqueTogether', + 'AlterField', + 'AlterField', + 'AlterIndexTogether', + ]) + self.assertOperationAttributes( + changes, 'testapp', 0, 0, name='author', unique_together=set(), + ) + self.assertOperationAttributes( + changes, 'testapp', 0, 1, model_name='author', name='age', + ) + self.assertOperationAttributes( + changes, 'testapp', 0, 2, model_name='author', name='name', + ) + self.assertOperationAttributes( + changes, 'testapp', 0, 3, name='author', index_together={('age',)}, + ) + + def test_partly_alter_foo_together(self): + initial_author = ModelState('testapp', 'Author', [ + ('id', models.AutoField(primary_key=True)), + ('name', models.CharField(max_length=200)), + ('age', models.IntegerField()), + ], { + 'unique_together': {('name',), ('age',)}, + 'index_together': {('name',)}, + }) + author_reversed_constraints = ModelState('testapp', 'Author', [ + ('id', models.AutoField(primary_key=True)), + ('name', models.CharField(max_length=200)), + ('age', models.IntegerField()), + ], { + 'unique_together': {('age',)}, + 'index_together': {('name',), ('age',)}, + }) + changes = self.get_changes([initial_author], [author_reversed_constraints]) + + self.assertNumberMigrations(changes, 'testapp', 1) + self.assertOperationTypes(changes, 'testapp', 0, [ + 'AlterUniqueTogether', + 'AlterIndexTogether', + ]) + self.assertOperationAttributes( + changes, 'testapp', 0, 0, name='author', unique_together={('age',)}, + ) + self.assertOperationAttributes( + changes, 'testapp', 0, 1, name='author', + index_together={('name',), ('age',)}, + ) def test_rename_field_and_foo_together(self): """ @@ -1629,11 +1736,27 @@ class AutodetectorTests(TestCase): ) # Right number/type of migrations? self.assertNumberMigrations(changes, "otherapp", 1) - self.assertOperationTypes(changes, "otherapp", 0, ["RenameField", "AlterUniqueTogether", "AlterIndexTogether"]) - self.assertOperationAttributes(changes, "otherapp", 0, 1, name="book", unique_together={ - ("title", "newfield2") - }) - self.assertOperationAttributes(changes, "otherapp", 0, 2, name="book", index_together={("title", "newfield2")}) + self.assertOperationTypes(changes, 'otherapp', 0, [ + 'RenameField', + 'AlterUniqueTogether', + 'AlterIndexTogether', + 'AlterUniqueTogether', + 'AlterIndexTogether', + ]) + self.assertOperationAttributes( + changes, 'otherapp', 0, 1, name='book', unique_together=set(), + ) + self.assertOperationAttributes( + changes, 'otherapp', 0, 2, name='book', index_together=set(), + ) + self.assertOperationAttributes( + changes, 'otherapp', 0, 3, name='book', + unique_together={('title', 'newfield2')}, + ) + self.assertOperationAttributes( + changes, 'otherapp', 0, 4, name='book', + index_together={('title', 'newfield2')}, + ) def test_proxy(self): """The autodetector correctly deals with proxy models.""" |
