Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions tests/test_q.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ def test_q_basic():
assert q.join_type == "AND"


def test_q_to_bool():
q = Q(row="data")
q_empty = Q()
q_children = Q(Q(row="data"), Q(row="data"))
q_children_empty = Q(Q(), Q())
assert bool(q) is True
assert bool(q_empty) is False
assert bool(q_children) is True
assert bool(q_children_empty) is False

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add more test cases, such as:

q = ~q
q = Q(Q(), Q(), join_type='OR')
...


def test_q_compound():
q1 = Q(moo="cow")
q2 = Q(moo="bull")
Expand Down
3 changes: 3 additions & 0 deletions tortoise/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,9 @@ def __eq__(self, other: object) -> bool:
and self.filters == other.filters
)

def __bool__(self) -> bool:
return any((self.filters, *(children for children in self.children)))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about using the following code?

if self.filters:
    return True
return any(self.children)


def negate(self) -> None:
"""
Negates the current Q object. (mutation)
Expand Down
Loading