-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TDD: tests which fail due to index reinstatement bugs
- Loading branch information
Showing
4 changed files
with
106 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('testapp', '0011_test_unique_constraints'), | ||
] | ||
|
||
# Issue #58 test prep | ||
operations = [ | ||
migrations.CreateModel( | ||
name='TestIndexesRetained', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('a', models.IntegerField(db_index=True)), | ||
('b', models.IntegerField(db_index=True)), | ||
('c', models.IntegerField(db_index=True)), | ||
], | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('testapp', '0012_test_indexes_retained_part1'), | ||
] | ||
|
||
# Issue #58 test operations which should leave index intact | ||
operations = [ | ||
migrations.AlterField( | ||
model_name='testindexesretained', | ||
name='a', | ||
field=models.IntegerField(db_index=True, null=True), | ||
), | ||
migrations.RenameField( | ||
model_name='testindexesretained', | ||
old_name='b', | ||
new_name='b_renamed', | ||
), | ||
migrations.RenameModel( | ||
old_name='TestIndexesRetained', | ||
new_name='TestIndexesRetainedRenamed', | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import django.db | ||
from django.test import TestCase | ||
|
||
from ..models import ( | ||
TestIndexesRetainedRenamed | ||
) | ||
|
||
|
||
class TestIndexesRetained(TestCase): | ||
""" | ||
Indexes dropped during a migration should be re-created afterwards | ||
assuming the field still has `db_index=True` (issue #58) | ||
""" | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
super().setUpClass() | ||
# Pre-fetch which indexes exist for the relevant test model | ||
# now that all the test migrations have run | ||
connection = django.db.connections[django.db.DEFAULT_DB_ALIAS] | ||
cls.constraints = connection.introspection.get_constraints( | ||
connection.cursor(), | ||
table_name=TestIndexesRetainedRenamed._meta.db_table | ||
) | ||
cls.indexes = {k: v for k, v in cls.constraints.items() if v['index'] is True} | ||
|
||
def _assert_index_exists(self, columns): | ||
matching = {k: v for k, v in self.indexes.items() if set(v['columns']) == columns} | ||
assert len(matching) == 1, ( | ||
"Expected 1 index for columns %s but found %d %s" % ( | ||
columns, | ||
len(matching), | ||
', '.join(matching.keys()) | ||
) | ||
) | ||
|
||
def test_field_made_nullable(self): | ||
# Issue #58 case (a) | ||
self._assert_index_exists({'a'}) | ||
|
||
def test_field_renamed(self): | ||
# Issue #58 case (b) | ||
self._assert_index_exists({'b_renamed'}) | ||
|
||
def test_table_renamed(self): | ||
# Issue #58 case (c) | ||
self._assert_index_exists({'c'}) |