From 971154f74661e7f7a51847a5a5deaa564ab64a03 Mon Sep 17 00:00:00 2001 From: Trevor Sibanda Date: Sun, 20 Sep 2020 18:02:57 +0200 Subject: [PATCH] Add type conversion functions --- faunadb/query.py | 16 ++++++++++++++++ tests/test_query.py | 17 +++++++++++++++++ tests/test_serialization.py | 14 ++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/faunadb/query.py b/faunadb/query.py index 88b64237..55df1875 100644 --- a/faunadb/query.py +++ b/faunadb/query.py @@ -790,6 +790,22 @@ def not_(boolean): def to_string(expr): return _fn({"to_string": expr}) +def to_array(expr): + """See the `docs `__.""" + return _fn({"to_array": expr}) + +def to_object(expr): + """See the `docs `__.""" + return _fn({"to_object": expr}) + +def to_double(expr): + """See the `docs `__.""" + return _fn({"to_double": expr}) + +def to_integer(expr): + """See the `docs `__.""" + return _fn({"to_integer": expr}) + def to_number(expr): """See the `docs `__.""" return _fn({"to_number": expr}) diff --git a/tests/test_query.py b/tests/test_query.py index 28c5f279..1436bc9e 100644 --- a/tests/test_query.py +++ b/tests/test_query.py @@ -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) diff --git a/tests/test_serialization.py b/tests/test_serialization.py index f0e56a1d..73b6ebfa 100644 --- a/tests/test_serialization.py +++ b/tests/test_serialization.py @@ -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"}')