Skip to content

Commit

Permalink
Refs #26709 -- Added type check for models.Index fields argument.
Browse files Browse the repository at this point in the history
  • Loading branch information
akki authored and timgraham committed Aug 16, 2016
1 parent a71724c commit c969b17
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
2 changes: 2 additions & 0 deletions django/db/models/indexes.py
Expand Up @@ -14,6 +14,8 @@ class Index(object):
suffix = 'idx'

def __init__(self, fields=[], name=None):
if not isinstance(fields, list):
raise ValueError('Index.fields must be a list.')
if not fields:
raise ValueError('At least one field is required to define an index.')
self.fields = fields
Expand Down
4 changes: 4 additions & 0 deletions tests/model_indexes/tests.py
Expand Up @@ -22,6 +22,10 @@ def test_eq(self):
self.assertEqual(index, same_index)
self.assertNotEqual(index, another_index)

def test_index_fields_type(self):
with self.assertRaisesMessage(ValueError, 'Index.fields must be a list.'):
models.Index(fields='title')

def test_raises_error_without_field(self):
msg = 'At least one field is required to define an index.'
with self.assertRaisesMessage(ValueError, msg):
Expand Down

0 comments on commit c969b17

Please sign in to comment.