Skip to content

Commit

Permalink
Refs #28077 -- Added opclasses to Index.__repr__().
Browse files Browse the repository at this point in the history
This also removes unnecessary commas between attributes.
  • Loading branch information
felixxm authored Jun 17, 2020
1 parent 1621f06 commit 82da72b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
7 changes: 4 additions & 3 deletions django/db/models/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,11 @@ def set_name_with_model(self, model):
self.name = 'D%s' % self.name[1:]

def __repr__(self):
return "<%s: fields='%s'%s%s>" % (
return "<%s: fields='%s'%s%s%s>" % (
self.__class__.__name__, ', '.join(self.fields),
'' if self.condition is None else ', condition=%s' % self.condition,
'' if not self.include else ", include='%s'" % ', '.join(self.include),
'' if self.condition is None else ' condition=%s' % self.condition,
'' if not self.include else " include='%s'" % ', '.join(self.include),
'' if not self.opclasses else " opclasses='%s'" % ', '.join(self.opclasses),
)

def __eq__(self, other):
Expand Down
14 changes: 12 additions & 2 deletions tests/model_indexes/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,22 @@ def test_repr(self):
name='include_idx',
include=['author', 'pages'],
)
opclasses_index = models.Index(
fields=['headline', 'body'],
name='opclasses_idx',
opclasses=['varchar_pattern_ops', 'text_pattern_ops'],
)
self.assertEqual(repr(index), "<Index: fields='title'>")
self.assertEqual(repr(multi_col_index), "<Index: fields='title, author'>")
self.assertEqual(repr(partial_index), "<Index: fields='title', condition=(AND: ('pages__gt', 400))>")
self.assertEqual(repr(partial_index), "<Index: fields='title' condition=(AND: ('pages__gt', 400))>")
self.assertEqual(
repr(covering_index),
"<Index: fields='title', include='author, pages'>",
"<Index: fields='title' include='author, pages'>",
)
self.assertEqual(
repr(opclasses_index),
"<Index: fields='headline, body' "
"opclasses='varchar_pattern_ops, text_pattern_ops'>",
)

def test_eq(self):
Expand Down

0 comments on commit 82da72b

Please sign in to comment.