summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorHannes Ljungberg <hannes.ljungberg@gmail.com>2021-02-19 12:16:24 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2021-02-19 20:25:11 +0100
commit87acbf063156ebb3fec3d35ef139895335061e0b (patch)
tree16e449178666825bc507d0701220e150aa9f8643 /tests
parent7c18b22e2fa70aa8dcfadb33beb17933abdf7ee4 (diff)
downloaddjango-87acbf063156ebb3fec3d35ef139895335061e0b.tar.gz
Fixed #32458 -- Made __repr__() for Index and BaseConstraint subclasses more consistent.
Diffstat (limited to 'tests')
-rw-r--r--tests/constraints/tests.py10
-rw-r--r--tests/migrations/test_operations.py2
-rw-r--r--tests/migrations/test_state.py5
-rw-r--r--tests/model_indexes/tests.py40
-rw-r--r--tests/postgres_tests/test_constraints.py24
5 files changed, 57 insertions, 24 deletions
diff --git a/tests/constraints/tests.py b/tests/constraints/tests.py
index 2796a0f30b..cb366d8e45 100644
--- a/tests/constraints/tests.py
+++ b/tests/constraints/tests.py
@@ -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):
diff --git a/tests/migrations/test_operations.py b/tests/migrations/test_operations.py
index 984aefa23d..73a0c63921 100644
--- a/tests/migrations/test_operations.py
+++ b/tests/migrations/test_operations.py
@@ -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"]))
diff --git a/tests/migrations/test_state.py b/tests/migrations/test_state.py
index 5ac9bf858f..4d87a1ebc5 100644
--- a/tests/migrations/test_state.py
+++ b/tests/migrations/test_state.py
@@ -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)
diff --git a/tests/model_indexes/tests.py b/tests/model_indexes/tests.py
index ab231edd5e..46f769cddc 100644
--- a/tests/model_indexes/tests.py
+++ b/tests/model_indexes/tests.py
@@ -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'])
diff --git a/tests/postgres_tests/test_constraints.py b/tests/postgres_tests/test_constraints.py
index 1bf52d0cc0..80c2bb77b6 100644
--- a/tests/postgres_tests/test_constraints.py
+++ b/tests/postgres_tests/test_constraints.py
@@ -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):