From f0a9413bd282cce452a5b7418b28b096af10a027 Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Mon, 24 May 2021 07:23:00 +0200 Subject: [PATCH] Refs #24121 -- Improved Value.__repr__(). --- django/db/models/expressions.py | 2 +- tests/expressions/tests.py | 18 +++++++++++++++++- tests/postgres_tests/test_search.py | 18 +++++++++--------- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/django/db/models/expressions.py b/django/db/models/expressions.py index 2b61a0bfd4..feb04d4585 100644 --- a/django/db/models/expressions.py +++ b/django/db/models/expressions.py @@ -743,7 +743,7 @@ class Value(SQLiteNumericMixin, Expression): self.value = value def __repr__(self): - return "{}({})".format(self.__class__.__name__, self.value) + return f'{self.__class__.__name__}({self.value!r})' def as_sql(self, compiler, connection): connection.ops.check_expression_support(self) diff --git a/tests/expressions/tests.py b/tests/expressions/tests.py index 291bf00eac..ed972fd5f7 100644 --- a/tests/expressions/tests.py +++ b/tests/expressions/tests.py @@ -1755,6 +1755,22 @@ class ValueTests(TestCase): self.assertEqual(len(kwargs), 1) self.assertEqual(kwargs['output_field'].deconstruct(), CharField().deconstruct()) + def test_repr(self): + tests = [ + (None, 'Value(None)'), + ('str', "Value('str')"), + (True, 'Value(True)'), + (42, 'Value(42)'), + ( + datetime.datetime(2019, 5, 15), + 'Value(datetime.datetime(2019, 5, 15, 0, 0))', + ), + (Decimal('3.14'), "Value(Decimal('3.14'))"), + ] + for value, expected in tests: + with self.subTest(value=value): + self.assertEqual(repr(Value(value)), expected) + def test_equal(self): value = Value('name') self.assertEqual(value, Value('name')) @@ -1880,7 +1896,7 @@ class ReprTests(SimpleTestCase): ) self.assertEqual( repr(When(Q(age__gte=18), then=Value('legal'))), - " THEN Value(legal)>" + " THEN Value('legal')>" ) self.assertEqual(repr(Col('alias', 'field')), "Col(alias, field)") self.assertEqual(repr(F('published')), "F(published)") diff --git a/tests/postgres_tests/test_search.py b/tests/postgres_tests/test_search.py index 5e5f0f5544..bf6f53ddb2 100644 --- a/tests/postgres_tests/test_search.py +++ b/tests/postgres_tests/test_search.py @@ -527,26 +527,26 @@ class SearchVectorIndexTests(PostgreSQLTestCase): class SearchQueryTests(PostgreSQLSimpleTestCase): def test_str(self): tests = ( - (~SearchQuery('a'), '~SearchQuery(Value(a))'), + (~SearchQuery('a'), "~SearchQuery(Value('a'))"), ( (SearchQuery('a') | SearchQuery('b')) & (SearchQuery('c') | SearchQuery('d')), - '((SearchQuery(Value(a)) || SearchQuery(Value(b))) && ' - '(SearchQuery(Value(c)) || SearchQuery(Value(d))))', + "((SearchQuery(Value('a')) || SearchQuery(Value('b'))) && " + "(SearchQuery(Value('c')) || SearchQuery(Value('d'))))", ), ( SearchQuery('a') & (SearchQuery('b') | SearchQuery('c')), - '(SearchQuery(Value(a)) && (SearchQuery(Value(b)) || ' - 'SearchQuery(Value(c))))', + "(SearchQuery(Value('a')) && (SearchQuery(Value('b')) || " + "SearchQuery(Value('c'))))", ), ( (SearchQuery('a') | SearchQuery('b')) & SearchQuery('c'), - '((SearchQuery(Value(a)) || SearchQuery(Value(b))) && ' - 'SearchQuery(Value(c)))' + "((SearchQuery(Value('a')) || SearchQuery(Value('b'))) && " + "SearchQuery(Value('c')))" ), ( SearchQuery('a') & (SearchQuery('b') & (SearchQuery('c') | SearchQuery('d'))), - '(SearchQuery(Value(a)) && (SearchQuery(Value(b)) && ' - '(SearchQuery(Value(c)) || SearchQuery(Value(d)))))', + "(SearchQuery(Value('a')) && (SearchQuery(Value('b')) && " + "(SearchQuery(Value('c')) || SearchQuery(Value('d')))))", ), ) for query, expected_str in tests: