Merge pull request #1843 from terencehonles/add-__repr__-to-Q-and-QCombination
add __repr__ to Q and QCombination
This commit is contained in:
commit
f44a2f4857
@ -131,6 +131,10 @@ class QCombination(QNode):
|
|||||||
else:
|
else:
|
||||||
self.children.append(node)
|
self.children.append(node)
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
op = ' & ' if self.operation is self.AND else ' | '
|
||||||
|
return '(%s)' % op.join([repr(node) for node in self.children])
|
||||||
|
|
||||||
def accept(self, visitor):
|
def accept(self, visitor):
|
||||||
for i in range(len(self.children)):
|
for i in range(len(self.children)):
|
||||||
if isinstance(self.children[i], QNode):
|
if isinstance(self.children[i], QNode):
|
||||||
@ -151,6 +155,9 @@ class Q(QNode):
|
|||||||
def __init__(self, **query):
|
def __init__(self, **query):
|
||||||
self.query = query
|
self.query = query
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return 'Q(**%s)' % repr(self.query)
|
||||||
|
|
||||||
def accept(self, visitor):
|
def accept(self, visitor):
|
||||||
return visitor.visit_query(self)
|
return visitor.visit_query(self)
|
||||||
|
|
||||||
|
@ -296,6 +296,18 @@ class QTest(unittest.TestCase):
|
|||||||
obj = self.Person.objects(Q(name__not=re.compile('^Gui'))).first()
|
obj = self.Person.objects(Q(name__not=re.compile('^Gui'))).first()
|
||||||
self.assertEqual(obj, None)
|
self.assertEqual(obj, None)
|
||||||
|
|
||||||
|
def test_q_repr(self):
|
||||||
|
self.assertEqual(repr(Q()), 'Q(**{})')
|
||||||
|
self.assertEqual(repr(Q(name='test')), "Q(**{'name': 'test'})")
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
repr(Q(name='test') & Q(age__gte=18)),
|
||||||
|
"(Q(**{'name': 'test'}) & Q(**{'age__gte': 18}))")
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
repr(Q(name='test') | Q(age__gte=18)),
|
||||||
|
"(Q(**{'name': 'test'}) | Q(**{'age__gte': 18}))")
|
||||||
|
|
||||||
def test_q_lists(self):
|
def test_q_lists(self):
|
||||||
"""Ensure that Q objects query ListFields correctly.
|
"""Ensure that Q objects query ListFields correctly.
|
||||||
"""
|
"""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user