Skip to content

Commit

Permalink
Added support to is_empty/is_nonempty functions
Browse files Browse the repository at this point in the history
  • Loading branch information
marrony committed Apr 17, 2018
1 parent 1e4fb64 commit 265b46d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## 2.0.1.dev
- Added `ngram` function
- Added `is_empty` and `is_nonempty` functions

## 2.0.0 (March 19, 2018)

Expand Down
8 changes: 8 additions & 0 deletions faunadb/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,14 @@ def append(elements, collection):
"""See the `docs <https://fauna.com/documentation/queries#collection_functions>`__."""
return _fn({"append": elements, "collection": collection})

def is_empty(collection):
"""See the `docs <https://fauna.com/documentation/queries#collection_functions>`__."""
return _fn({"is_empty": collection})

def is_nonempty(collection):
"""See the `docs <https://fauna.com/documentation/queries#collection_functions>`__."""
return _fn({"is_nonempty": collection})

#endregion

#region Read functions
Expand Down
14 changes: 14 additions & 0 deletions tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,20 @@ def test_prepend(self):
def test_append(self):
self.assertEqual(self._q(query.append([4, 5, 6], [1, 2, 3])), [1, 2, 3, 4, 5, 6])

def test_collection_predicates(self):
self.assertTrue(self._q(query.is_empty([])))
self.assertFalse(self._q(query.is_empty([1, 2, 3])))

self.assertFalse(self._q(query.is_nonempty([])))
self.assertTrue(self._q(query.is_nonempty([1, 2, 3])))

self._create(n=111)
self.assertFalse(self._q(query.is_empty(query.paginate(query.match(self.n_index_ref, 111)))))
self.assertTrue(self._q(query.is_empty(query.paginate(query.match(self.n_index_ref, 112)))))

self.assertTrue(self._q(query.is_nonempty(query.paginate(query.match(self.n_index_ref, 111)))))
self.assertFalse(self._q(query.is_nonempty(query.paginate(query.match(self.n_index_ref, 112)))))

#endregion

#region Read functions
Expand Down
6 changes: 6 additions & 0 deletions tests/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ def test_prepend(self):
def test_append(self):
self.assertJson(query.append([1, 2], [3, 4]), '{"append":[1,2],"collection":[3,4]}')

def test_is_empty(self):
self.assertJson(query.is_empty([1, 2]), '{"is_empty":[1,2]}')

def test_is_nonempty(self):
self.assertJson(query.is_nonempty([1, 2]), '{"is_nonempty":[1,2]}')

#endregion

#region Read functions
Expand Down

0 comments on commit 265b46d

Please sign in to comment.