class TestHandleRequest(unittest.TestCase): def setUp(self): self.block = Block() self.block.set_parent(MagicMock(), "TestBlock") self.method = MagicMock() self.attribute = MagicMock() self.response = MagicMock() self.block.add_method('get_things', self.method) self.block.add_attribute('test_attribute', self.attribute) def test_given_request_then_pass_to_correct_method(self): endpoint = ["TestBlock", "get_things"] request = Post(MagicMock(), MagicMock(), endpoint) self.block.handle_request(request) self.method.get_response.assert_called_once_with(request) response = self.method.get_response.return_value self.block.parent.block_respond.assert_called_once_with( response, request.response_queue) def test_given_put_then_update_attribute(self): endpoint = ["TestBlock", "test_attribute", "value"] value = "5" request = Put(MagicMock(), MagicMock(), endpoint, value) self.block.handle_request(request) self.attribute.put.assert_called_once_with(value) self.attribute.set_value.assert_called_once_with(value) response = self.block.parent.block_respond.call_args[0][0] self.assertEqual("malcolm:core/Return:1.0", response.typeid) self.assertIsNone(response.value) response_queue = self.block.parent.block_respond.call_args[0][1] self.assertEqual(request.response_queue, response_queue) def test_invalid_request_fails(self): request = MagicMock() request.type_ = "Get" self.assertRaises(AssertionError, self.block.handle_request, request) def test_invalid_request_fails(self): endpoint = ["a","b","c","d"] request = Post(MagicMock(), MagicMock(), endpoint) self.assertRaises(ValueError, self.block.handle_request, request) request = Put(MagicMock(), MagicMock(), endpoint) self.assertRaises(ValueError, self.block.handle_request, request)
class TestHandleRequest(unittest.TestCase): def setUp(self): self.block = Block() self.block.set_parent(MagicMock(), "TestBlock") self.method = MagicMock() self.attribute = MagicMock() self.response = MagicMock() self.block.add_method('get_things', self.method) self.block.add_attribute('test_attribute', self.attribute) def test_given_request_then_pass_to_correct_method(self): endpoint = ["TestBlock", "get_things"] request = Post(MagicMock(), MagicMock(), endpoint) self.block.handle_request(request) self.method.get_response.assert_called_once_with(request) response = self.method.get_response.return_value self.block.parent.block_respond.assert_called_once_with( response, request.response_queue) def test_given_put_then_update_attribute(self): endpoint = ["TestBlock", "test_attribute", "value"] value = "5" request = Put(MagicMock(), MagicMock(), endpoint, value) self.block.handle_request(request) self.attribute.put.assert_called_once_with(value) self.attribute.set_value.assert_called_once_with(value) response = self.block.parent.block_respond.call_args[0][0] self.assertEqual("malcolm:core/Return:1.0", response.typeid) self.assertIsNone(response.value) response_queue = self.block.parent.block_respond.call_args[0][1] self.assertEqual(request.response_queue, response_queue) def test_invalid_request_fails(self): request = MagicMock() request.type_ = "Get" self.assertRaises(AssertionError, self.block.handle_request, request) def test_invalid_request_fails(self): endpoint = ["a", "b", "c", "d"] request = Post(MagicMock(), MagicMock(), endpoint) self.assertRaises(ValueError, self.block.handle_request, request) request = Put(MagicMock(), MagicMock(), endpoint) self.assertRaises(ValueError, self.block.handle_request, request)
class TestHandleRequest(unittest.TestCase): def setUp(self): self.block = Block("TestBlock") self.method = MagicMock() self.method.name = "get_things" self.response = MagicMock() self.block.add_method(self.method) def test_given_request_then_pass_to_correct_method(self): request = MagicMock() request.POST = "Post" request.type_ = "Post" request.endpoint = ["TestBlock", "device", "get_things"] self.block.handle_request(request) self.method.handle_request.assert_called_once_with(request) def test_given_get_then_return_attribute(self): self.block.state = MagicMock() self.block.state.value = "Running" request = MagicMock() request.type_ = "Get" request.endpoint = ["TestBlock", "state", "value"] self.block.handle_request(request) request.respond_with_return.assert_called_once_with("Running") def test_given_get_block_then_return_self(self): request = MagicMock() request.type_ = "Get" request.endpoint = ["TestBlock"] expected_call = self.block.to_dict() self.block.handle_request(request) request.respond_with_return.assert_called_once_with(expected_call)