diff options
| author | Andrew Godwin <andrew@aeracode.org> | 2014-06-15 14:55:44 -0700 |
|---|---|---|
| committer | Andrew Godwin <andrew@aeracode.org> | 2014-06-15 14:56:51 -0700 |
| commit | a8ce5fdc28bdbe091476d49d014d68f8ba4353e7 (patch) | |
| tree | 32cd5ca0fb78bf2df14843a3d75294c9a2562ceb /tests | |
| parent | a58f49d104e5a0d7adb8df1af6f1e36fa37b09b6 (diff) | |
Fixed #22470: Full migration support for order_with_respect_to
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/migrations/test_autodetector.py | 62 | ||||
| -rw-r--r-- | tests/migrations/test_operations.py | 22 | ||||
| -rw-r--r-- | tests/migrations/test_state.py | 31 |
3 files changed, 115 insertions, 0 deletions
diff --git a/tests/migrations/test_autodetector.py b/tests/migrations/test_autodetector.py index bd305a874f..9a8be73f27 100644 --- a/tests/migrations/test_autodetector.py +++ b/tests/migrations/test_autodetector.py @@ -31,6 +31,7 @@ class AutodetectorTests(TestCase): author_name_deconstructable_3 = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200, default=models.IntegerField()))]) author_name_deconstructable_4 = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200, default=models.IntegerField()))]) author_with_book = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200)), ("book", models.ForeignKey("otherapp.Book"))]) + author_with_book_order_wrt = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200)), ("book", models.ForeignKey("otherapp.Book"))], options={"order_with_respect_to": "book"}) author_renamed_with_book = ModelState("testapp", "Writer", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200)), ("book", models.ForeignKey("otherapp.Book"))]) author_with_publisher_string = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200)), ("publisher_name", models.CharField(max_length=200))]) author_with_publisher = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200)), ("publisher", models.ForeignKey("testapp.Publisher"))]) @@ -814,3 +815,64 @@ class AutodetectorTests(TestCase): self.assertNumberMigrations(changes, "testapp", 1) # Right actions in right order? self.assertOperationTypes(changes, "testapp", 0, ["AlterModelOptions"]) + + def test_set_alter_order_with_respect_to(self): + "Tests that setting order_with_respect_to adds a field" + # Make state + before = self.make_project_state([self.book, self.author_with_book]) + after = self.make_project_state([self.book, self.author_with_book_order_wrt]) + autodetector = MigrationAutodetector(before, after) + changes = autodetector._detect_changes() + # Right number of migrations? + self.assertNumberMigrations(changes, 'testapp', 1) + self.assertOperationTypes(changes, 'testapp', 0, ["AlterOrderWithRespectTo"]) + self.assertOperationAttributes(changes, 'testapp', 0, 0, name="author", order_with_respect_to="book") + + def test_add_alter_order_with_respect_to(self): + """ + Tests that setting order_with_respect_to when adding the FK too + does things in the right order. + """ + # Make state + before = self.make_project_state([self.author_name]) + after = self.make_project_state([self.book, self.author_with_book_order_wrt]) + autodetector = MigrationAutodetector(before, after) + changes = autodetector._detect_changes() + # Right number of migrations? + self.assertNumberMigrations(changes, 'testapp', 1) + self.assertOperationTypes(changes, 'testapp', 0, ["AddField", "AlterOrderWithRespectTo"]) + self.assertOperationAttributes(changes, 'testapp', 0, 0, model_name="author", name="book") + self.assertOperationAttributes(changes, 'testapp', 0, 1, name="author", order_with_respect_to="book") + + def test_remove_alter_order_with_respect_to(self): + """ + Tests that removing order_with_respect_to when removing the FK too + does things in the right order. + """ + # Make state + before = self.make_project_state([self.book, self.author_with_book_order_wrt]) + after = self.make_project_state([self.author_name]) + autodetector = MigrationAutodetector(before, after) + changes = autodetector._detect_changes() + # Right number of migrations? + self.assertNumberMigrations(changes, 'testapp', 1) + self.assertOperationTypes(changes, 'testapp', 0, ["AlterOrderWithRespectTo", "RemoveField"]) + self.assertOperationAttributes(changes, 'testapp', 0, 0, name="author", order_with_respect_to=None) + self.assertOperationAttributes(changes, 'testapp', 0, 1, model_name="author", name="book") + + def test_add_model_order_with_respect_to(self): + """ + Tests that setting order_with_respect_to when adding the whole model + does things in the right order. + """ + # Make state + before = self.make_project_state([]) + after = self.make_project_state([self.book, self.author_with_book_order_wrt]) + autodetector = MigrationAutodetector(before, after) + changes = autodetector._detect_changes() + # Right number of migrations? + self.assertNumberMigrations(changes, 'testapp', 1) + self.assertOperationTypes(changes, 'testapp', 0, ["CreateModel", "AlterOrderWithRespectTo"]) + self.assertOperationAttributes(changes, 'testapp', 0, 1, name="author", order_with_respect_to="book") + # Make sure the _order field is not in the CreateModel fields + self.assertNotIn("_order", [name for name, field in changes['testapp'][0].operations[0].fields]) diff --git a/tests/migrations/test_operations.py b/tests/migrations/test_operations.py index d1a23a375f..64703d9140 100644 --- a/tests/migrations/test_operations.py +++ b/tests/migrations/test_operations.py @@ -803,6 +803,28 @@ class OperationTests(MigrationTestBase): self.assertEqual(len(new_state.models["test_almoop", "pony"].options.get("permissions", [])), 1) self.assertEqual(new_state.models["test_almoop", "pony"].options["permissions"][0][0], "can_groom") + def test_alter_order_with_respect_to(self): + """ + Tests the AlterOrderWithRespectTo operation. + """ + project_state = self.set_up_test_model("test_alorwrtto", related_model=True) + # Test the state alteration + operation = migrations.AlterOrderWithRespectTo("Rider", "pony") + new_state = project_state.clone() + operation.state_forwards("test_alorwrtto", new_state) + self.assertEqual(project_state.models["test_alorwrtto", "rider"].options.get("order_with_respect_to", None), None) + self.assertEqual(new_state.models["test_alorwrtto", "rider"].options.get("order_with_respect_to", None), "pony") + # Make sure there's no matching index + self.assertColumnNotExists("test_alorwrtto_rider", "_order") + # Test the database alteration + with connection.schema_editor() as editor: + operation.database_forwards("test_alorwrtto", editor, project_state, new_state) + self.assertColumnExists("test_alorwrtto_rider", "_order") + # And test reversal + with connection.schema_editor() as editor: + operation.database_backwards("test_alorwrtto", editor, new_state, project_state) + self.assertColumnNotExists("test_alorwrtto_rider", "_order") + @unittest.skipIf(sqlparse is None and connection.features.requires_sqlparse_for_splitting, "Missing sqlparse") def test_run_sql(self): """ diff --git a/tests/migrations/test_state.py b/tests/migrations/test_state.py index 9be04f3dfc..45b3d363de 100644 --- a/tests/migrations/test_state.py +++ b/tests/migrations/test_state.py @@ -367,6 +367,37 @@ class StateTests(TestCase): 1, ) + def test_ignore_order_wrt(self): + """ + Makes sure ProjectState doesn't include OrderWrt fields when + making from existing models. + """ + new_apps = Apps() + + class Author(models.Model): + name = models.TextField() + + class Meta: + app_label = "migrations" + apps = new_apps + + class Book(models.Model): + author = models.ForeignKey(Author) + + class Meta: + app_label = "migrations" + apps = new_apps + order_with_respect_to = "author" + + # Make a valid ProjectState and render it + project_state = ProjectState() + project_state.add_model_state(ModelState.from_model(Author)) + project_state.add_model_state(ModelState.from_model(Book)) + self.assertEqual( + [name for name, field in project_state.models["migrations", "book"].fields], + ["id", "author"], + ) + class ModelStateTests(TestCase): def test_custom_model_base(self): |
