diff options
| author | Andrew Godwin <andrew@aeracode.org> | 2013-07-02 11:19:02 +0100 |
|---|---|---|
| committer | Andrew Godwin <andrew@aeracode.org> | 2013-07-02 11:19:02 +0100 |
| commit | 67dcea711e92025d0e8676b869b7ef15dbc6db73 (patch) | |
| tree | 7163f5c4a710f316551bbc76d35e37939732aa14 /tests | |
| parent | 310cdf492d2642e7cf00bcc169895f5954f10369 (diff) | |
Add unique_together altering operation
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/migrations/test_autodetector.py | 4 | ||||
| -rw-r--r-- | tests/migrations/test_operations.py | 40 | ||||
| -rw-r--r-- | tests/migrations/test_state.py | 2 |
3 files changed, 39 insertions, 7 deletions
diff --git a/tests/migrations/test_autodetector.py b/tests/migrations/test_autodetector.py index 540e84e8df..659b45dbd2 100644 --- a/tests/migrations/test_autodetector.py +++ b/tests/migrations/test_autodetector.py @@ -83,7 +83,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" @@ -100,7 +100,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 2e72e11954..b2912de53c 100644 --- a/tests/migrations/test_operations.py +++ b/tests/migrations/test_operations.py @@ -1,5 +1,6 @@ from django.test import TestCase from django.db import connection, models, migrations +from django.db.utils import IntegrityError from django.db.migrations.state import ProjectState @@ -38,6 +39,7 @@ class OperationTests(TestCase): [ ("id", models.AutoField(primary_key=True)), ("pink", models.BooleanField(default=True)), + ("weight", models.FloatField()), ], ) project_state = ProjectState() @@ -50,7 +52,7 @@ class OperationTests(TestCase): def test_create_model(self): """ Tests the CreateModel operation. - Most other tests use this as part of setup, so check failures here first. + Most other tests use this operation as part of setup, so check failures here first. """ operation = migrations.CreateModel( "Pony", @@ -63,7 +65,7 @@ class OperationTests(TestCase): 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") @@ -110,7 +112,7 @@ class OperationTests(TestCase): operation = migrations.AddField("Pony", "height", models.FloatField(null=True)) new_state = project_state.clone() operation.state_forwards("test_adfl", new_state) - self.assertEqual(len(new_state.models["test_adfl", "pony"].fields), 3) + self.assertEqual(len(new_state.models["test_adfl", "pony"].fields), 4) # Test the database alteration self.assertColumnNotExists("test_adfl_pony", "height") with connection.schema_editor() as editor: @@ -130,7 +132,7 @@ class OperationTests(TestCase): operation = migrations.RemoveField("Pony", "pink") new_state = project_state.clone() operation.state_forwards("test_rmfl", new_state) - self.assertEqual(len(new_state.models["test_rmfl", "pony"].fields), 1) + self.assertEqual(len(new_state.models["test_rmfl", "pony"].fields), 2) # Test the database alteration self.assertColumnExists("test_rmfl_pony", "pink") with connection.schema_editor() as editor: @@ -208,3 +210,33 @@ class OperationTests(TestCase): operation.database_backwards("test_rnfl", editor, new_state, project_state) self.assertColumnExists("test_rnfl_pony", "pink") self.assertColumnNotExists("test_rnfl_pony", "blue") + + def test_alter_unique_together(self): + """ + Tests the AlterUniqueTogether operation. + """ + project_state = self.set_up_test_model("test_alunto") + # Test the state alteration + operation = migrations.AlterUniqueTogether("Pony", [("pink", "weight")]) + new_state = project_state.clone() + operation.state_forwards("test_alunto", new_state) + self.assertEqual(len(project_state.models["test_alunto", "pony"].options.get("unique_together", set())), 0) + self.assertEqual(len(new_state.models["test_alunto", "pony"].options.get("unique_together", set())), 1) + # Make sure we can insert duplicate rows + cursor = connection.cursor() + cursor.execute("INSERT INTO test_alunto_pony (id, pink, weight) VALUES (1, 1, 1)") + cursor.execute("INSERT INTO test_alunto_pony (id, pink, weight) VALUES (2, 1, 1)") + cursor.execute("DELETE FROM test_alunto_pony") + # Test the database alteration + with connection.schema_editor() as editor: + operation.database_forwards("test_alunto", editor, project_state, new_state) + cursor.execute("INSERT INTO test_alunto_pony (id, pink, weight) VALUES (1, 1, 1)") + with self.assertRaises(IntegrityError): + cursor.execute("INSERT INTO test_alunto_pony (id, pink, weight) VALUES (2, 1, 1)") + cursor.execute("DELETE FROM test_alunto_pony") + # And test reversal + with connection.schema_editor() as editor: + operation.database_backwards("test_alunto", editor, new_state, project_state) + cursor.execute("INSERT INTO test_alunto_pony (id, pink, weight) VALUES (1, 1, 1)") + cursor.execute("INSERT INTO test_alunto_pony (id, pink, weight) VALUES (2, 1, 1)") + cursor.execute("DELETE FROM test_alunto_pony") diff --git a/tests/migrations/test_state.py b/tests/migrations/test_state.py index c6930873ef..e5b3fbfa08 100644 --- a/tests/migrations/test_state.py +++ b/tests/migrations/test_state.py @@ -44,7 +44,7 @@ class StateTests(TestCase): self.assertEqual(author_state.fields[1][1].max_length, 255) self.assertEqual(author_state.fields[2][1].null, False) self.assertEqual(author_state.fields[3][1].null, True) - self.assertEqual(author_state.options, {"unique_together": ["name", "bio"]}) + self.assertEqual(author_state.options, {"unique_together": set(("name", "bio"))}) self.assertEqual(author_state.bases, (models.Model, )) self.assertEqual(book_state.app_label, "migrations") |
