Skip to content

Commit

Permalink
Add return None and modify body arguments (tableau#387)
Browse files Browse the repository at this point in the history
* modified regex and query timeout, included remove endpoint method

* modified regex and query timeout, included remove endpoint method

* modified regex and query timeout, included remove endpoint method

* modified regex and query timeout, included remove endpoint method

* rm client

* Create client.py

* add None script and test

* update client.py

* update client.py

* tryig to add newlines to client.py

* .

* fix unit tests

* remove blank line

* pep8 fixes

* pep8 fixes
  • Loading branch information
phantomcosmonaut authored and 0golovatyi committed Dec 26, 2019
1 parent 048f620 commit 22adbf5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
29 changes: 12 additions & 17 deletions tabpy/tabpy_server/handlers/evaluation_plane_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,17 @@ def post(self):
400, "Script parameters need to be provided as a dictionary."
)
return
args_in = sorted(arguments.keys())
n = len(arguments)
if sorted('_arg'+str(i+1) for i in range(n)) == args_in:
arguments_str = ", " + ", ".join(args_in)
else:
arguments_expected = []
for i in range(1, len(arguments.keys()) + 1):
arguments_expected.append("_arg" + str(i))
if sorted(arguments_expected) == sorted(arguments.keys()):
arguments_str = ", " + ", ".join(arguments.keys())
else:
self.error_out(
400,
"Variables names should follow "
"the format _arg1, _arg2, _argN",
)
return
self.error_out(
400,
"Variables names should follow "
"the format _arg1, _arg2, _argN",
)
return

function_to_evaluate = f"def _user_script(tabpy{arguments_str}):\n"
for u in user_code.splitlines():
Expand All @@ -99,11 +97,8 @@ def post(self):
self.error_out(408, self._error_message_timeout)
return

if result is None:
self.error_out(400, "Error running script. No return value")
else:
self.write(simplejson.dumps(result, ignore_nan=True))
self.finish()
self.write(simplejson.dumps(result, ignore_nan=True))
self.finish()

except Exception as e:
err_msg = f"{e.__class__.__name__} : {str(e)}"
Expand Down
Empty file modified tabpy/tabpy_tools/client.py
100755 → 100644
Empty file.
20 changes: 20 additions & 0 deletions tests/unit/server_tests/test_evaluation_plane_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ def setUpClass(cls):
'{"data":{"_arg1":[2,3],"_arg2":[3,-1]},'\
'"script":"return [float(1), float(\\"NaN\\"), float(2)]"}'

cls.script_returns_none = (
'{"data":{"_arg1":[2,3],"_arg2":[3,-1]},'
'"script":"return None"}'
)

@classmethod
def tearDownClass(cls):
cls.patcher.stop()
Expand Down Expand Up @@ -163,6 +168,7 @@ def test_arguments_not_present(self):
"utf-8"
)
)

},
)
self.assertEqual(500, response.code)
Expand Down Expand Up @@ -195,3 +201,17 @@ def test_nan_converts_to_null(self):
})
self.assertEqual(200, response.code)
self.assertEqual(b'[1.0, null, 2.0]', response.body)

def test_script_returns_none(self):
response = self.fetch(
'/evaluate',
method='POST',
body=self.script_returns_none,
headers={
'Authorization': 'Basic {}'.
format(
base64.b64encode('username:password'.encode('utf-8')).
decode('utf-8'))
})
self.assertEqual(200, response.code)
self.assertEqual(b'null', response.body)

0 comments on commit 22adbf5

Please sign in to comment.