Skip to content

Commit

Permalink
Merge pull request #164 from fauna/missing-type-fns
Browse files Browse the repository at this point in the history
DRV-271: Add type conversion functions
  • Loading branch information
Trevor Sibanda authored Sep 24, 2020
2 parents 7382ad5 + 971154f commit 6dd3734
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
16 changes: 16 additions & 0 deletions faunadb/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,22 @@ def not_(boolean):
def to_string(expr):
return _fn({"to_string": expr})

def to_array(expr):
"""See the `docs <https://docs.fauna.com/fauna/current/api/fql/functions/toarray>`__."""
return _fn({"to_array": expr})

def to_object(expr):
"""See the `docs <https://docs.fauna.com/fauna/current/api/fql/functions/toobject>`__."""
return _fn({"to_object": expr})

def to_double(expr):
"""See the `docs <https://docs.fauna.com/fauna/current/api/fql/functions/todouble>`__."""
return _fn({"to_double": expr})

def to_integer(expr):
"""See the `docs <https://docs.fauna.com/fauna/current/api/fql/functions/tointeger>`__."""
return _fn({"to_integer": expr})

def to_number(expr):
"""See the `docs <https://docs.fauna.com/fauna/current/api/fql/functions/tonumber>`__."""
return _fn({"to_number": expr})
Expand Down
17 changes: 17 additions & 0 deletions tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,23 @@ def test_not(self):
def test_to_string(self):
self.assertEqual(self._q(query.to_string(42)), "42")

def test_to_array(self):
self.assertEqual(self._q(query.to_array({'x':0,'y':1})), [['x',0], ['y', 1]])
self._assert_bad_query(query.to_array(23))

def test_to_object(self):
self.assertEqual(self._q(query.to_object([['x', 0], ['y', 1]])), {'x':0, 'y':1})
self._assert_bad_query(query.to_object('hey there'))

def test_to_integer(self):
self.assertEqual(self._q(query.to_integer(4.2343)), 4)
self.assertEqual(self._q(query.to_integer(4.8999)), 4)
self._assert_bad_query(query.to_integer({'x':1}))

def test_to_double(self):
self.assertEqual(self._q(query.to_double(42)), 42.0)
self._assert_bad_query(query.to_double([]))

def test_to_number(self):
self.assertEqual(self._q(query.to_number("42")), 42)

Expand Down
14 changes: 14 additions & 0 deletions tests/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,20 @@ def test_not_expr(self):
def test_to_string_expr(self):
self.assertJson(query.to_string(42), '{"to_string":42}')

def test_to_array_expr(self):
self.assertJson(query.to_array(
{'x': 0, 'y': 1}), '{"to_array":{"object":{"x":0,"y":1}}}')

def test_to_object_expr(self):
self.assertJson(query.to_object(
[['x', 0], ['y', 1]]), '{"to_object":[["x",0],["y",1]]}')

def test_to_double_expr(self):
self.assertJson(query.to_double(42), '{"to_double":42}')

def test_to_integer_expr(self):
self.assertJson(query.to_integer(3.1415), '{"to_integer":3.1415}')

def test_to_number_expr(self):
self.assertJson(query.to_number("42"), '{"to_number":"42"}')

Expand Down

0 comments on commit 6dd3734

Please sign in to comment.