From a758c9c1866559ab5dbf4a7705c07e8532dcd253 Mon Sep 17 00:00:00 2001 From: Andrew Godwin Date: Thu, 25 Jul 2013 16:31:34 +0100 Subject: [PATCH] Add test for creating M2Ms --- django/db/migrations/operations/models.py | 10 +++--- tests/migrations/test_autodetector.py | 4 +-- tests/migrations/test_operations.py | 37 +++++++++++++++++++---- 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/django/db/migrations/operations/models.py b/django/db/migrations/operations/models.py index 0c9e69e127f..bf152011944 100644 --- a/django/db/migrations/operations/models.py +++ b/django/db/migrations/operations/models.py @@ -9,7 +9,7 @@ class CreateModel(Operation): """ def __init__(self, name, fields, options=None, bases=None): - self.name = name.lower() + self.name = name self.fields = fields self.options = options or {} self.bases = bases or (models.Model,) @@ -35,7 +35,7 @@ class DeleteModel(Operation): """ def __init__(self, name): - self.name = name.lower() + self.name = name def state_forwards(self, app_label, state): del state.models[app_label, self.name.lower()] @@ -58,7 +58,7 @@ class AlterModelTable(Operation): """ def __init__(self, name, table): - self.name = name.lower() + self.name = name self.table = table def state_forwards(self, app_label, state): @@ -87,7 +87,7 @@ class AlterUniqueTogether(Operation): """ def __init__(self, name, unique_together): - self.name = name.lower() + self.name = name self.unique_together = set(tuple(cons) for cons in unique_together) def state_forwards(self, app_label, state): @@ -117,7 +117,7 @@ class AlterIndexTogether(Operation): """ def __init__(self, name, index_together): - self.name = name.lower() + self.name = name self.index_together = set(tuple(cons) for cons in index_together) def state_forwards(self, app_label, state): diff --git a/tests/migrations/test_autodetector.py b/tests/migrations/test_autodetector.py index 7bed4eb57e9..9b7fbd5e8a8 100644 --- a/tests/migrations/test_autodetector.py +++ b/tests/migrations/test_autodetector.py @@ -85,7 +85,7 @@ class AutodetectorTests(TestCase): # Right action? action = migration.operations[0] self.assertEqual(action.__class__.__name__, "CreateModel") - self.assertEqual(action.name, "author") + self.assertEqual(action.name, "Author") def test_old_model(self): "Tests deletion of old models" @@ -102,7 +102,7 @@ class AutodetectorTests(TestCase): # Right action? action = migration.operations[0] self.assertEqual(action.__class__.__name__, "DeleteModel") - self.assertEqual(action.name, "author") + self.assertEqual(action.name, "Author") def test_add_field(self): "Tests autodetection of new fields" diff --git a/tests/migrations/test_operations.py b/tests/migrations/test_operations.py index c74d40e4f26..ad909f7fddc 100644 --- a/tests/migrations/test_operations.py +++ b/tests/migrations/test_operations.py @@ -12,24 +12,28 @@ class OperationTests(MigrationTestBase): both forwards and backwards. """ - def set_up_test_model(self, app_label): + def set_up_test_model(self, app_label, second_model=False): """ Creates a test model state and database table. """ # Make the "current" state - creation = migrations.CreateModel( + operations = [migrations.CreateModel( "Pony", [ ("id", models.AutoField(primary_key=True)), ("pink", models.IntegerField(default=3)), ("weight", models.FloatField()), ], - ) + )] + if second_model: + operations.append(migrations.CreateModel("Stable", [("id", models.AutoField(primary_key=True))])) project_state = ProjectState() - creation.state_forwards(app_label, project_state) + for operation in operations: + operation.state_forwards(app_label, project_state) # Set up the database with connection.schema_editor() as editor: - creation.database_forwards(app_label, editor, ProjectState(), project_state) + for operation in operations: + operation.database_forwards(app_label, editor, ProjectState(), project_state) return project_state def test_create_model(self): @@ -48,7 +52,7 @@ class OperationTests(MigrationTestBase): project_state = ProjectState() new_state = project_state.clone() operation.state_forwards("test_crmo", new_state) - self.assertEqual(new_state.models["test_crmo", "pony"].name, "pony") + self.assertEqual(new_state.models["test_crmo", "pony"].name, "Pony") self.assertEqual(len(new_state.models["test_crmo", "pony"].fields), 2) # Test the database alteration self.assertTableNotExists("test_crmo_pony") @@ -106,6 +110,27 @@ class OperationTests(MigrationTestBase): operation.database_backwards("test_adfl", editor, new_state, project_state) self.assertColumnNotExists("test_adfl_pony", "height") + def test_add_field_m2m(self): + """ + Tests the AddField operation with a ManyToManyField. + """ + project_state = self.set_up_test_model("test_adflmm", second_model=True) + # Test the state alteration + operation = migrations.AddField("Pony", "stables", models.ManyToManyField("Stable")) + new_state = project_state.clone() + operation.state_forwards("test_adflmm", new_state) + self.assertEqual(len(new_state.models["test_adflmm", "pony"].fields), 4) + # Test the database alteration + self.assertTableNotExists("test_adflmm_pony_stables") + with connection.schema_editor() as editor: + operation.database_forwards("test_adflmm", editor, project_state, new_state) + self.assertTableExists("test_adflmm_pony_stables") + self.assertColumnNotExists("test_adflmm_pony", "stables") + # And test reversal + with connection.schema_editor() as editor: + operation.database_backwards("test_adflmm", editor, new_state, project_state) + self.assertTableNotExists("test_adflmm_pony_stables") + def test_remove_field(self): """ Tests the RemoveField operation.