diff options
| author | Markus Holtermann <info@markusholtermann.eu> | 2014-09-09 23:28:45 +0200 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2014-09-10 07:57:31 -0400 |
| commit | 67872bfff12de7dcf22c966970fe21f65fe593c8 (patch) | |
| tree | 36f9e57c165ff6331552e5ab23ff261b9651b4d8 /tests | |
| parent | c2fe985733ecf5ec113a92f155cc6b769932d544 (diff) | |
[1.7.x] Fixed #23452 -- Prevented infinite migrations for empty unique/index_together.
Thanks fwkroon for the report.
Backport of 6d5958c7a3 from master
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/migrations/test_autodetector.py | 48 |
1 files changed, 46 insertions, 2 deletions
diff --git a/tests/migrations/test_autodetector.py b/tests/migrations/test_autodetector.py index 526c018907..0ba05d3495 100644 --- a/tests/migrations/test_autodetector.py +++ b/tests/migrations/test_autodetector.py @@ -502,6 +502,50 @@ class AutodetectorTests(TestCase): # Right number of migrations? self.assertEqual(len(changes), 0) + def test_empty_foo_together(self): + "#23452 - Empty unique/index_togther shouldn't generate a migration." + # Explicitly testing for not specified, since this is the case after + # a CreateModel operation w/o any definition on the original model + model_state_not_secified = ModelState("a", "model", + [("id", models.AutoField(primary_key=True))] + ) + # Explicitly testing for None, since this was the issue in #23452 after + # a AlterFooTogether operation with e.g. () as value + model_state_none = ModelState("a", "model", + [("id", models.AutoField(primary_key=True))], + {"unique_together": None, "index_together": None} + ) + # Explicitly testing for the empty set, since we now always have sets. + # During removal (('col1', 'col2'),) --> () this becomes set([]) + model_state_empty = ModelState("a", "model", + [("id", models.AutoField(primary_key=True))], + {"unique_together": set(), "index_together": set()} + ) + + def test(from_state, to_state, msg): + before = self.make_project_state([from_state]) + after = self.make_project_state([to_state]) + autodetector = MigrationAutodetector(before, after) + changes = autodetector._detect_changes() + if len(changes) > 0: + ops = ', '.join(o.__class__.__name__ for o in changes['a'][0].operations) + self.fail('Created operation(s) %s from %s' % (ops, msg)) + + tests = ( + (model_state_not_secified, model_state_not_secified, '"not specified" to "not specified"'), + (model_state_not_secified, model_state_none, '"not specified" to "None"'), + (model_state_not_secified, model_state_empty, '"not specified" to "empty"'), + (model_state_none, model_state_not_secified, '"None" to "not specified"'), + (model_state_none, model_state_none, '"None" to "None"'), + (model_state_none, model_state_empty, '"None" to "empty"'), + (model_state_empty, model_state_not_secified, '"empty" to "not specified"'), + (model_state_empty, model_state_none, '"empty" to "None"'), + (model_state_empty, model_state_empty, '"empty" to "empty"'), + ) + + for t in tests: + test(*t) + def test_unique_together_ordering(self): "Tests that unique_together also triggers on ordering changes" # Make state @@ -555,7 +599,7 @@ class AutodetectorTests(TestCase): # Right actions? action = migration.operations[0] self.assertEqual(action.__class__.__name__, "AlterIndexTogether") - self.assertEqual(action.index_together, None) + self.assertEqual(action.index_together, set()) def test_remove_unique_together(self): author_unique_together = ModelState("testapp", "Author", [ @@ -574,7 +618,7 @@ class AutodetectorTests(TestCase): # Right actions? action = migration.operations[0] self.assertEqual(action.__class__.__name__, "AlterUniqueTogether") - self.assertEqual(action.unique_together, None) + self.assertEqual(action.unique_together, set()) def test_proxy(self): "Tests that the autodetector correctly deals with proxy models" |
