1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Refs #24121 -- Improved Value.__repr__().

This commit is contained in:
Mariusz Felisiak 2021-05-24 07:23:00 +02:00
parent 3f6d4e22f8
commit f0a9413bd2
3 changed files with 27 additions and 11 deletions

View File

@ -743,7 +743,7 @@ class Value(SQLiteNumericMixin, Expression):
self.value = value self.value = value
def __repr__(self): def __repr__(self):
return "{}({})".format(self.__class__.__name__, self.value) return f'{self.__class__.__name__}({self.value!r})'
def as_sql(self, compiler, connection): def as_sql(self, compiler, connection):
connection.ops.check_expression_support(self) connection.ops.check_expression_support(self)

View File

@ -1755,6 +1755,22 @@ class ValueTests(TestCase):
self.assertEqual(len(kwargs), 1) self.assertEqual(len(kwargs), 1)
self.assertEqual(kwargs['output_field'].deconstruct(), CharField().deconstruct()) 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): def test_equal(self):
value = Value('name') value = Value('name')
self.assertEqual(value, Value('name')) self.assertEqual(value, Value('name'))
@ -1880,7 +1896,7 @@ class ReprTests(SimpleTestCase):
) )
self.assertEqual( self.assertEqual(
repr(When(Q(age__gte=18), then=Value('legal'))), repr(When(Q(age__gte=18), then=Value('legal'))),
"<When: WHEN <Q: (AND: ('age__gte', 18))> THEN Value(legal)>" "<When: WHEN <Q: (AND: ('age__gte', 18))> THEN Value('legal')>"
) )
self.assertEqual(repr(Col('alias', 'field')), "Col(alias, field)") self.assertEqual(repr(Col('alias', 'field')), "Col(alias, field)")
self.assertEqual(repr(F('published')), "F(published)") self.assertEqual(repr(F('published')), "F(published)")

View File

@ -527,26 +527,26 @@ class SearchVectorIndexTests(PostgreSQLTestCase):
class SearchQueryTests(PostgreSQLSimpleTestCase): class SearchQueryTests(PostgreSQLSimpleTestCase):
def test_str(self): def test_str(self):
tests = ( tests = (
(~SearchQuery('a'), '~SearchQuery(Value(a))'), (~SearchQuery('a'), "~SearchQuery(Value('a'))"),
( (
(SearchQuery('a') | SearchQuery('b')) & (SearchQuery('c') | SearchQuery('d')), (SearchQuery('a') | SearchQuery('b')) & (SearchQuery('c') | SearchQuery('d')),
'((SearchQuery(Value(a)) || SearchQuery(Value(b))) && ' "((SearchQuery(Value('a')) || SearchQuery(Value('b'))) && "
'(SearchQuery(Value(c)) || SearchQuery(Value(d))))', "(SearchQuery(Value('c')) || SearchQuery(Value('d'))))",
), ),
( (
SearchQuery('a') & (SearchQuery('b') | SearchQuery('c')), SearchQuery('a') & (SearchQuery('b') | SearchQuery('c')),
'(SearchQuery(Value(a)) && (SearchQuery(Value(b)) || ' "(SearchQuery(Value('a')) && (SearchQuery(Value('b')) || "
'SearchQuery(Value(c))))', "SearchQuery(Value('c'))))",
), ),
( (
(SearchQuery('a') | SearchQuery('b')) & SearchQuery('c'), (SearchQuery('a') | SearchQuery('b')) & SearchQuery('c'),
'((SearchQuery(Value(a)) || SearchQuery(Value(b))) && ' "((SearchQuery(Value('a')) || SearchQuery(Value('b'))) && "
'SearchQuery(Value(c)))' "SearchQuery(Value('c')))"
), ),
( (
SearchQuery('a') & (SearchQuery('b') & (SearchQuery('c') | SearchQuery('d'))), SearchQuery('a') & (SearchQuery('b') & (SearchQuery('c') | SearchQuery('d'))),
'(SearchQuery(Value(a)) && (SearchQuery(Value(b)) && ' "(SearchQuery(Value('a')) && (SearchQuery(Value('b')) && "
'(SearchQuery(Value(c)) || SearchQuery(Value(d)))))', "(SearchQuery(Value('c')) || SearchQuery(Value('d')))))",
), ),
) )
for query, expected_str in tests: for query, expected_str in tests: