Skip to content

Commit

Permalink
Add QuerySet support __bool__
Browse files Browse the repository at this point in the history
* Add QuerySet support __bool__

* Fix __bool__

* Add pragma no cover
  • Loading branch information
EvgeniyMakhmudov authored and zzzsochi committed May 29, 2018
1 parent b3703fd commit 9e564a2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
9 changes: 9 additions & 0 deletions tests/test_queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ def test_len(qs):
assert len(qs) == 4


@pytest.mark.parametrize('additional_qs, result', [
({}, True),
({'i': {'$gte': 6}}, True),
({'i': {'$gte': 1000}}, False),
])
def test_bool_(qs, additional_qs, result):
assert bool(qs.find(additional_qs)) is result


@pytest.mark.parametrize('slice, result', [
(slice(None, 2), [6, 7]),
(slice(2, None), [8, 9]),
Expand Down
7 changes: 7 additions & 0 deletions yadm/queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,9 @@ def __iter__(self):
def __contains__(self, document):
raise NotImplementedError

def __bool__(self): # pragma: no cover
raise NotImplementedError

def find_one(self, criteria=None, projection=None, *, exc=None):
raise NotImplementedError

Expand Down Expand Up @@ -324,6 +327,10 @@ def __len__(self):
def __contains__(self, document):
return self.find_one(document.id) is not None

def __bool__(self):
qs = self.copy(sort=[], projection={'_id': True})
return qs.find_one() is not None

def _get_one(self, index):
return self._from_mongo_one(self._cursor[index])

Expand Down

0 comments on commit 9e564a2

Please sign in to comment.