Fixed #32458 -- Made __repr__() for Index and BaseConstraint subclasses more consistent.

This commit is contained in:
Hannes Ljungberg 2021-02-19 12:16:24 +01:00 committed by Mariusz Felisiak
parent 7c18b22e2f
commit 87acbf0631
8 changed files with 84 additions and 42 deletions

View File

@ -155,12 +155,13 @@ class ExclusionConstraint(BaseConstraint):
return super().__eq__(other)
def __repr__(self):
return '<%s: index_type=%s, expressions=%s%s%s%s%s>' % (
return '<%s: index_type=%s expressions=%s name=%s%s%s%s%s>' % (
self.__class__.__qualname__,
self.index_type,
self.expressions,
'' if self.condition is None else ', condition=%s' % self.condition,
'' if self.deferrable is None else ', deferrable=%s' % self.deferrable,
'' if not self.include else ', include=%s' % repr(self.include),
'' if not self.opclasses else ', opclasses=%s' % repr(self.opclasses),
repr(self.index_type),
repr(self.expressions),
repr(self.name),
'' if self.condition is None else ' condition=%s' % self.condition,
'' if self.deferrable is None else ' deferrable=%s' % self.deferrable,
'' if not self.include else ' include=%s' % repr(self.include),
'' if not self.opclasses else ' opclasses=%s' % repr(self.opclasses),
)

View File

@ -58,7 +58,11 @@ class CheckConstraint(BaseConstraint):
return schema_editor._delete_check_sql(model, self.name)
def __repr__(self):
return "<%s: check='%s' name=%r>" % (self.__class__.__name__, self.check, self.name)
return '<%s: check=%s name=%s>' % (
self.__class__.__qualname__,
self.check,
repr(self.name),
)
def __eq__(self, other):
if isinstance(other, CheckConstraint):
@ -161,8 +165,10 @@ class UniqueConstraint(BaseConstraint):
)
def __repr__(self):
return '<%s: fields=%r name=%r%s%s%s%s>' % (
self.__class__.__name__, self.fields, self.name,
return '<%s: fields=%s name=%s%s%s%s%s>' % (
self.__class__.__qualname__,
repr(self.fields),
repr(self.name),
'' if self.condition is None else ' condition=%s' % self.condition,
'' if self.deferrable is None else ' deferrable=%s' % self.deferrable,
'' if not self.include else ' include=%s' % repr(self.include),

View File

@ -169,15 +169,17 @@ class Index:
self.name = 'D%s' % self.name[1:]
def __repr__(self):
return '<%s:%s%s%s%s%s>' % (
self.__class__.__name__,
'' if not self.fields else " fields='%s'" % ', '.join(self.fields),
'' if not self.expressions else " expressions='%s'" % ', '.join([
str(expression) for expression in self.expressions
]),
return '<%s:%s%s%s%s%s%s%s>' % (
self.__class__.__qualname__,
'' if not self.fields else ' fields=%s' % repr(self.fields),
'' if not self.expressions else ' expressions=%s' % repr(self.expressions),
'' if not self.name else ' name=%s' % repr(self.name),
''
if self.db_tablespace is None
else ' db_tablespace=%s' % repr(self.db_tablespace),
'' 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),
'' if not self.include else ' include=%s' % repr(self.include),
'' if not self.opclasses else ' opclasses=%s' % repr(self.opclasses),
)
def __eq__(self, other):

View File

@ -58,12 +58,14 @@ class CheckConstraintTests(TestCase):
self.assertNotEqual(models.CheckConstraint(check=check1, name='price'), 1)
def test_repr(self):
check = models.Q(price__gt=models.F('discounted_price'))
name = 'price_gt_discounted_price'
constraint = models.CheckConstraint(check=check, name=name)
constraint = models.CheckConstraint(
check=models.Q(price__gt=models.F('discounted_price')),
name='price_gt_discounted_price',
)
self.assertEqual(
repr(constraint),
"<CheckConstraint: check='{}' name='{}'>".format(check, name),
"<CheckConstraint: check=(AND: ('price__gt', F(discounted_price))) "
"name='price_gt_discounted_price'>",
)
def test_invalid_check_types(self):

View File

@ -1840,7 +1840,7 @@ class OperationTests(OperationTestBase):
project_state = self.set_up_test_model("test_adin")
msg = (
"Indexes passed to AddIndex operations require a name argument. "
"<Index: fields='pink'> doesn't have one."
"<Index: fields=['pink']> doesn't have one."
)
with self.assertRaisesMessage(ValueError, msg):
migrations.AddIndex("Pony", models.Index(fields=["pink"]))

View File

@ -1046,7 +1046,10 @@ class ModelStateTests(SimpleTestCase):
def test_sanity_index_name(self):
field = models.IntegerField()
options = {'indexes': [models.Index(fields=['field'])]}
msg = "Indexes passed to ModelState require a name attribute. <Index: fields='field'> doesn't have one."
msg = (
"Indexes passed to ModelState require a name attribute. <Index: "
"fields=['field']> doesn't have one."
)
with self.assertRaisesMessage(ValueError, msg):
ModelState('app', 'Model', [('field', field)], options=options)

View File

@ -16,6 +16,7 @@ class SimpleIndexesTests(SimpleTestCase):
def test_repr(self):
index = models.Index(fields=['title'])
named_index = models.Index(fields=['title'], name='title_idx')
multi_col_index = models.Index(fields=['title', 'author'])
partial_index = models.Index(fields=['title'], name='long_books_idx', condition=models.Q(pages__gt=400))
covering_index = models.Index(
@ -28,20 +29,43 @@ class SimpleIndexesTests(SimpleTestCase):
name='opclasses_idx',
opclasses=['varchar_pattern_ops', 'text_pattern_ops'],
)
func_index = models.Index(Lower('title'), name='book_func_idx')
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))>")
func_index = models.Index(Lower('title'), 'subtitle', name='book_func_idx')
tablespace_index = models.Index(
fields=['title'],
db_tablespace='idx_tbls',
name='book_tablespace_idx',
)
self.assertEqual(repr(index), "<Index: fields=['title']>")
self.assertEqual(
repr(named_index),
"<Index: fields=['title'] name='title_idx'>",
)
self.assertEqual(repr(multi_col_index), "<Index: fields=['title', 'author']>")
self.assertEqual(
repr(partial_index),
"<Index: fields=['title'] name='long_books_idx' "
"condition=(AND: ('pages__gt', 400))>",
)
self.assertEqual(
repr(covering_index),
"<Index: fields='title' include='author, pages'>",
"<Index: fields=['title'] name='include_idx' "
"include=('author', 'pages')>",
)
self.assertEqual(
repr(opclasses_index),
"<Index: fields='headline, body' "
"opclasses='varchar_pattern_ops, text_pattern_ops'>",
"<Index: fields=['headline', 'body'] name='opclasses_idx' "
"opclasses=['varchar_pattern_ops', 'text_pattern_ops']>",
)
self.assertEqual(
repr(func_index),
"<Index: expressions=(Lower(F(title)), F(subtitle)) "
"name='book_func_idx'>",
)
self.assertEqual(
repr(tablespace_index),
"<Index: fields=['title'] name='book_tablespace_idx' "
"db_tablespace='idx_tbls'>",
)
self.assertEqual(repr(func_index), "<Index: expressions='Lower(F(title))'>")
def test_eq(self):
index = models.Index(fields=['title'])

View File

@ -282,8 +282,8 @@ class ExclusionConstraintTests(PostgreSQLTestCase):
)
self.assertEqual(
repr(constraint),
"<ExclusionConstraint: index_type=GIST, expressions=["
"(F(datespan), '&&'), (F(room), '=')]>",
"<ExclusionConstraint: index_type='GIST' expressions=["
"(F(datespan), '&&'), (F(room), '=')] name='exclude_overlapping'>",
)
constraint = ExclusionConstraint(
name='exclude_overlapping',
@ -293,8 +293,9 @@ class ExclusionConstraintTests(PostgreSQLTestCase):
)
self.assertEqual(
repr(constraint),
"<ExclusionConstraint: index_type=SPGiST, expressions=["
"(F(datespan), '-|-')], condition=(AND: ('cancelled', False))>",
"<ExclusionConstraint: index_type='SPGiST' expressions=["
"(F(datespan), '-|-')] name='exclude_overlapping' "
"condition=(AND: ('cancelled', False))>",
)
constraint = ExclusionConstraint(
name='exclude_overlapping',
@ -303,8 +304,9 @@ class ExclusionConstraintTests(PostgreSQLTestCase):
)
self.assertEqual(
repr(constraint),
"<ExclusionConstraint: index_type=GIST, expressions=["
"(F(datespan), '-|-')], deferrable=Deferrable.IMMEDIATE>",
"<ExclusionConstraint: index_type='GIST' expressions=["
"(F(datespan), '-|-')] name='exclude_overlapping' "
"deferrable=Deferrable.IMMEDIATE>",
)
constraint = ExclusionConstraint(
name='exclude_overlapping',
@ -313,8 +315,9 @@ class ExclusionConstraintTests(PostgreSQLTestCase):
)
self.assertEqual(
repr(constraint),
"<ExclusionConstraint: index_type=GIST, expressions=["
"(F(datespan), '-|-')], include=('cancelled', 'room')>",
"<ExclusionConstraint: index_type='GIST' expressions=["
"(F(datespan), '-|-')] name='exclude_overlapping' "
"include=('cancelled', 'room')>",
)
constraint = ExclusionConstraint(
name='exclude_overlapping',
@ -323,8 +326,9 @@ class ExclusionConstraintTests(PostgreSQLTestCase):
)
self.assertEqual(
repr(constraint),
"<ExclusionConstraint: index_type=GIST, expressions=["
"(F(datespan), '-|-')], opclasses=['range_ops']>",
"<ExclusionConstraint: index_type='GIST' expressions=["
"(F(datespan), '-|-')] name='exclude_overlapping' "
"opclasses=['range_ops']>",
)
def test_eq(self):