Skip to content

Commit

Permalink
Add request handling to Method
Browse files Browse the repository at this point in the history
  • Loading branch information
c-mita committed Jun 2, 2016
1 parent 12838cd commit 42a43c7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
6 changes: 6 additions & 0 deletions malcolm/core/method.py
Expand Up @@ -64,5 +64,11 @@ def __call__(self, *args, **kwargs):
self.returns.elements[r_name].validate(r_val)
return return_val

def handle_request(self, request):
"""Call exposed function using request parameters and respond with the
result"""
result = self(**request.parameters)
request.respond_with_return(result)

def to_dict(self):
pass
15 changes: 15 additions & 0 deletions tests/test_core/test_method.py
Expand Up @@ -121,5 +121,20 @@ def test_invalid_return(self):
func.assert_called_with({"first":1, "second":2})
validator2.assert_called_with(4)

def test_handle_request(self):
func = Mock(return_value = {"output":1})
args_meta = Mock(elements = {"first":Mock()})
return_meta = Mock(elements = {"output":Mock()})
m = Method("test_method")
m.set_function(func)
m.set_function_takes(args_meta)
m.set_function_returns(return_meta)
request = Mock(
id = (123, Mock()), type="Post", parameters = {"first":2},
respond_with_return = Mock())
m.handle_request(request)
func.assert_called_with({"first":2})
request.respond_with_return.assert_called_with({"output":1})

if __name__ == "__main__":
unittest.main(verbosity=2)

0 comments on commit 42a43c7

Please sign in to comment.