Skip to content

Commit

Permalink
Merge pull request #162 from fauna/missing-math-fns
Browse files Browse the repository at this point in the history
DRV-269: Add missing math functions
  • Loading branch information
Trevor Sibanda authored Oct 5, 2020
2 parents 6dd3734 + ab01029 commit 63e7df8
Show file tree
Hide file tree
Showing 3 changed files with 333 additions and 0 deletions.
138 changes: 138 additions & 0 deletions faunadb/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,144 @@ def divide(*numbers):
return _fn({"divide": _varargs(numbers)})


def pow(base, exp):
"""See the `docs <https://docs.fauna.com/fauna/current/api/fql/functions/pow>` __."""
return _fn({"pow": base, "exp": exp})


def max(*numbers):
"""See the `docs <https://docs.fauna.com/fauna/current/api/fql/functions/max>` __."""
return _fn({"max": _varargs(numbers)})


def min(*numbers):
"""See the `docs <https://docs.fauna.com/fauna/current/api/fql/functions/min>` __."""
return _fn({"min": _varargs(numbers)})

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


def trunc(num, precision=None):
"""See the `docs <https://docs.fauna.com/fauna/current/api/fql/functions/trunc>` __."""
return _params({"trunc": num}, {"precision": precision})


def bitor(*numbers):
"""See the `docs <https://docs.fauna.com/fauna/current/api/fql/functions/bitor>` __."""
return _fn({"bitor": _varargs(numbers)})


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


def hypot(num, b):
"""See the `docs <https://docs.fauna.com/fauna/current/api/fql/functions/hypot>` __."""
return _fn({"hypot": num, "b": b})


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


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


def bitnot(*num):
"""See the `docs <https://docs.fauna.com/fauna/current/api/fql/functions/bitnot>` __."""
return _fn({"bitnot": _varargs(num)})


def bitxor(*num):
"""See the `docs <https://docs.fauna.com/fauna/current/api/fql/functions/bitxor>` __."""
return _fn({"bitxor": _varargs(num)})


def bitand(*num):
"""See the `docs <https://docs.fauna.com/fauna/current/api/fql/functions/bitand>` __."""
return _fn({"bitand": _varargs(num)})


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


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


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


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


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


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


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


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


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


def round(num, precision=None):
"""See the `docs <https://docs.fauna.com/fauna/current/api/fql/functions/round>` __."""
return _params({"round": num}, {"precision": precision})


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

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


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


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


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


def any(collection):
"""See the `docs <https://docs.fauna.com/fauna/current/api/fql/functions/any>`__."""
return _fn({"any": collection})
Expand Down
102 changes: 102 additions & 0 deletions tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,108 @@ def test_divide(self):
self._assert_bad_query(query.divide(1, 0))
self._assert_bad_query(query.divide())

def test_pow(self):
self.assertEqual(self._q(query.pow(2, 3)), 8)
self.assertEqual(self._q(query.pow(0, 3)), 0)
self._assert_bad_query(query.pow(1, 'n'))

def test_max(self):
self.assertEqual(self._q(query.max(1, 2, 3)), 3)
self.assertEqual(self._q(query.max([-1, -2, -3])), -1)
self._assert_bad_query(query.max(2))
self._assert_bad_query(query.max('n'))

def test_min(self):
self.assertEqual(self._q(query.min(1, 2, 3)), 1)
self.assertEqual(self._q(query.min([-1, -2, -3])), -3)
self._assert_bad_query(query.min(1))
self._assert_bad_query(query.min('n'))

def test_abs(self):
self.assertEqual(self._q(query.abs(-10)), 10)
self.assertEqual(self._q(query.abs(1)), 1)
self._assert_bad_query(query.abs('n'))

def test_trunc(self):
self.assertEqual(self._q(query.trunc(45.56789)), 45.56)
self.assertEqual(self._q(query.trunc(45.56789, 3)), 45.567)

def test_bitor(self):
self.assertEqual(self._q(query.bitor(1)), 1)
self.assertEqual(self._q(query.bitor(1, 0, 1, 0)), 1)
self.assertEqual(self._q(query.bitor([1, 2, 4])), 7)
self._assert_bad_query(query.bitor('n'))

def test_degrees(self):
self.assertEqual(self._q(query.degrees(0)), 0)

def test_cos(self):
self.assertEqual(self._q(query.cos(120)), 0.8141809705265618)

def test_cosh(self):
self.assertEqual(self._q(query.cosh(120)), 6.520904391968161e+51)

def test_hypot(self):
self.assertEqual(self._q(query.hypot(query.hypot(3, 4), 0)), 5)

def test_atan(self):
self.assertEqual(self._q(query.atan(120)), 1.5624631863547607)

def test_log(self):
self.assertEqual(self._q(query.log(100)), 2)

def test_bitnot(self):
self.assertEqual(self._q(query.bitnot(0)), -1)
self.assertEqual(self._q(query.bitnot(1)), -2)

def test_bitxor(self):
self.assertEqual(self._q(query.bitxor(0, 1)), 1)
self.assertEqual(self._q(query.bitxor(1, 2, 4)), 7)

def test_bitand(self):
self.assertEqual(self._q(query.bitand(1, 0)), 0)
self.assertEqual(self._q(query.bitand(7, 3)), 3)

def test_ceil(self):
self.assertEqual(self._q(query.ceil(67.789)), 68)

def test_sqrt(self):
self.assertEqual(self._q(query.sqrt(9)), 3)

def test_tan(self):
self.assertEqual(self._q(query.tan(90)), -1.995200412208242)

def test_tanh(self):
self.assertEqual(self._q(query.tanh(45)), 1.0)
self._assert_bad_query(query.tanh('45.6'))

def test_acos(self):
self.assertEqual(self._q(query.acos(0)), 1.5707963267948966)

def test_asin(self):
self.assertEqual(self._q(query.asin(0)), 0.0)

def test_radians(self):
self.assertEqual(self._q(query.radians(90)), 1.5707963267948966)

def test_round(self):
self.assertEqual(self._q(query.round(123.456)), 123.46)
self.assertEqual(self._q(query.round(123.456, 1)),
123.5)

def test_floor(self):
self.assertEqual(self._q(query.floor(89.1234)), 89.0)
self._assert_bad_query(query.floor('let the bodies hit the '))

def test_sign(self):
self.assertEqual(self._q(query.sign(-90)), -1)

def test_exp(self):
self.assertEqual(self._q(query.exp(2)), 7.38905609893065)

def test_ln(self):
self.assertEqual(self._q(query.ln(1)), 0)

def test_any_all(self):
expected = [True, True, False, False]
self.assertEqual(self._q([
Expand Down
93 changes: 93 additions & 0 deletions tests/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,99 @@ def test_divide(self):
self.assertJson(query.divide(1, 2, 3), '{"divide":[1,2,3]}')
self.assertJson(query.divide([1, 2, 3]), '{"divide":[1,2,3]}')

def test_pow(self):
self.assertJson(query.pow(2, 3), '{"exp":3,"pow":2}')

def test_max(self):
self.assertJson(query.max(1, 2), '{"max":[1,2]}')
self.assertJson(query.max(1, 2, 3), '{"max":[1,2,3]}')
self.assertJson(query.max([1, 2, 3]), '{"max":[1,2,3]}')

def test_min(self):
self.assertJson(query.min(1, 2), '{"min":[1,2]}')
self.assertJson(query.min(1, 2, 3), '{"min":[1,2,3]}')
self.assertJson(query.min([1, 2, 3]), '{"min":[1,2,3]}')

def test_abs(self):
self.assertJson(query.abs(-10), '{"abs":-10}')

def test_trunc(self):
self.assertJson(query.trunc(45), '{"trunc":45}')
self.assertJson(query.trunc(45, 3), '{"precision":3,"trunc":45}')

def test_bitor(self):
self.assertJson(query.bitor(1), '{"bitor":1}')
self.assertJson(query.bitor(1, 2, 3), '{"bitor":[1,2,3]}')
self.assertJson(query.bitor([1, 2, 3]), '{"bitor":[1,2,3]}')

def test_degrees(self):
self.assertJson(query.degrees(90), '{"degrees":90}')

def test_cos(self):
self.assertJson(query.cos(60), '{"cos":60}')

def test_sqrt(self):
self.assertJson(query.sqrt(9), '{"sqrt":9}')

def test_tan(self):
self.assertJson(query.tan(45), '{"tan":45}')

def test_tanh(self):
self.assertJson(query.tanh(45), '{"tanh":45}')

def test_acos(self):
self.assertJson(query.acos(0), '{"acos":0}')

def test_cosh(self):
self.assertJson(query.cosh(120), '{"cosh":120}')

def test_hypot(self):
self.assertJson(query.hypot(9, 0), '{"b":0,"hypot":9}')

def test_atan(self):
self.assertJson(query.atan(120), '{"atan":120}')

def test_log(self):
self.assertJson(query.log(100), '{"log":100}')

def test_bitnot(self):
self.assertJson(query.bitnot(0), '{"bitnot":0}')
self.assertJson(query.bitnot(1), '{"bitnot":1}')

def test_bitxor(self):
self.assertJson(query.bitxor(0, 1), '{"bitxor":[0,1]}')
self.assertJson(query.bitxor(1, 2, 4), '{"bitxor":[1,2,4]}')

def test_bitand(self):
self.assertJson(query.bitand(1, 0), '{"bitand":[1,0]}')
self.assertJson(query.bitand(7, 3), '{"bitand":[7,3]}')

def test_ceil(self):
self.assertJson(query.ceil(67.789), '{"ceil":67.789}')

def test_asin(self):
self.assertJson(query.asin(0), '{"asin":0}')

def test_radians(self):
self.assertJson(query.radians(45), '{"radians":45}')

def test_round(self):
self.assertJson(query.round(123.456), '{"round":123.456}')
self.assertJson(query.round(123.456, 2),
'{"precision":2,"round":123.456}')

def test_floor(self):
self.assertJson(query.floor(89.1234), '{"floor":89.1234}')

def test_sign(self):
self.assertJson(query.sign(-90), '{"sign":-90}')

def test_exp(self):
self.assertJson(query.exp(2.1), '{"exp":2.1}')

def test_ln(self):
self.assertJson(query.ln(900), '{"ln":900}')

def test_any(self):
self.assertJson(query.any([True, True, True]), '{"any":[true,true,true]}')

Expand Down

0 comments on commit 63e7df8

Please sign in to comment.