Repository: django/django
File: tests/migrations/test_autodetector.py (Python)

Code before:
        after = [
            ModelState(
                "testapp",
                "Book",
                [("id", models.AutoField(primary_key=True))],
            ),
            ModelState(
                "testapp",
                "Magazine",
                [("id", models.AutoField(primary_key=True))],
            ),
            ModelState(
                "testapp",
                "Author",
                [
                    ("id", models.AutoField(primary_key=True)),
                    ("readings", models.ManyToManyField("testapp.Magazine")),
                ],
            ),
        ]

        changes = self.get_changes(before, after)

        # We expect 1 migration for testapp
        self.assertIn("testapp", changes)
        self.assertEqual(len(changes["testapp"]), 1)

        migration = changes["testapp"][0]
        operation_types = [op.__class__.__name__ for op in migration.operations]

        # Should be RemoveField and AddField, NOT AlterField
        # Note: Order might vary but usually Remove then Add
        self.assertIn("RemoveField", operation_types)
        self.assertIn("AddField", operation_types)
        self.assertNotIn("AlterField", operation_types)

        # Verify details
        remove_op = next(
            op for op in migration.operations if op.__class__.__name__ == "RemoveField"
        )
        self.assertEqual(remove_op.name, "readings")

        add_op = next(
            op for op in migration.operations if op.__class__.__name__ == "AddField"
        )
        self.assertEqual(add_op.name, "readings")
        self.assertEqual(add_op.field.remote_field.model, "testapp.Magazine")


class MigrationSuggestNameTests(SimpleTestCase):
    def test_no_operations(self):

Review comment (suggestion):
use `assertNumberMigrations()` (see surrounding tests)

Code after:
        ]

        changes = self.get_changes(before, after)
        self.assertNumberMigrations(changes, "app", 1)
        self.assertOperationTypes(changes, "app", 0, ["RemoveField", "AlterField"])
        self.assertOperationAttributes(
            changes,
            "app",
            0,
            0,
            name="pk",
            model_name="foo",
        )
        self.assertOperationAttributes(
            changes,
            "app",
            0,
            1,
            name="id",
            model_name="foo",
            preserve_default=True,
        )


class MigrationSuggestNameTests(SimpleTestCase):
    def test_no_operations(self):
        class Migration(migrations.Migration):
            operations = []

        migration = Migration("some_migration", "test_app")
        self.assertIs(migration.suggest_name().startswith("auto_"), True)

    def test_no_operations_initial(self):
        class Migration(migrations.Migration):
            initial = True
            operations = []

        migration = Migration("some_migration", "test_app")
        self.assertEqual(migration.suggest_name(), "initial")

    def test_single_operation(self):
        class Migration(migrations.Migration):
            operations = [migrations.CreateModel("Person", fields=[])]

        migration = Migration("0001_initial", "test_app")
        self.assertEqual(migration.suggest_name(), "person")

        class Migration(migrations.Migration):
            operations = [migrations.DeleteModel("Person")]

        migration = Migration("0002_initial", "test_app")