Skip to content

Commit

Permalink
TDD: tests that fail due to ESSolutions#58
Browse files Browse the repository at this point in the history
Doubtless there are more cases that should be tested, but
these are the 3 specific ones that I've found to be broken
and reported in issue ESSolutions#58

These tests fail on latest django-mssql-backend (e.g. v2.8.1)
but passed on v2.4.2 before ESSolutions#24 went in.
  • Loading branch information
sparrowt committed Jul 6, 2020
1 parent b81a024 commit e0fe0ef
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 2 deletions.
21 changes: 21 additions & 0 deletions testapp/migrations/0008_test_indexes_retained_part1.py
@@ -0,0 +1,21 @@
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('testapp', '0007_test_remove_onetoone_field_part2'),
]

# 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)),
],
),
]
26 changes: 26 additions & 0 deletions testapp/migrations/0009_test_indexes_retained_part2.py
@@ -0,0 +1,26 @@
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('testapp', '0008_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',
),
]
14 changes: 12 additions & 2 deletions testapp/models.py
Expand Up @@ -67,7 +67,17 @@ class Meta:


class TestRemoveOneToOneFieldModel(models.Model):
# Fields used for testing removing OneToOne field. Verifies that delete_unique do not try to remove indexes
# thats already is removed.
# Fields used for testing removing OneToOne field. Verifies that delete_unique
# does not try to remove indexes that have already been removed (Issue #51)
# b = models.OneToOneField('self', on_delete=models.SET_NULL, null=True)
a = models.CharField(max_length=50)


class TestIndexesRetainedRenamed(models.Model):
# Issue #58 - in all these cases the column index should still exist afterwards
# case (a) `a` starts out not nullable, but then is changed to be nullable
a = models.IntegerField(db_index=True, null=True)
# case (b) column originally called `b` is renamed
b_renamed = models.IntegerField(db_index=True)
# case (c) this entire model is renamed - this is just a column whose index can be checked afterwards
c = models.IntegerField(db_index=True)
47 changes: 47 additions & 0 deletions testapp/tests/test_indexes.py
@@ -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'})

0 comments on commit e0fe0ef

Please sign in to comment.