Пример #1
0
 def test_not_writeable_stops_call(self):
     m = Method("test_description")
     m.set_function(Mock())
     m.set_writeable(False)
     with self.assertRaises(ValueError,
             msg="Cannot call a method that is not writeable"):
         m()
Пример #2
0
 def test_not_writeable_stops_call(self):
     m = Method("test_description")
     m.set_function(Mock())
     m.set_writeable(False)
     with self.assertRaises(
             ValueError, msg="Cannot call a method that is not writeable"):
         m()
Пример #3
0
 def test_defaults(self):
     func = Mock(return_value={"first_out": "test"})
     m = Method("test_method", "test_description")
     arg_meta = Mock()
     arg_meta.elements = {"first": Mock(), "second": Mock()}
     m.set_function_takes(arg_meta, {"second": "default"})
     m.set_function(func)
     self.assertEquals({"first_out": "test"}, m(first="test"))
     func.assert_called_with({"first": "test", "second": "default"})
Пример #4
0
 def test_defaults(self):
     func = Mock(return_value={"first_out": "test"})
     m = Method("test_method", "test_description")
     arg_meta = Mock()
     arg_meta.elements = {"first": Mock(), "second": Mock()}
     m.set_function_takes(arg_meta, {"second": "default"})
     m.set_function(func)
     self.assertEquals({"first_out": "test"}, m(first="test"))
     func.assert_called_with({"first": "test", "second": "default"})
Пример #5
0
 def test_simple_function(self):
     func = Mock(return_value={"first_out": "test"})
     m = Method("test_method", "test_description")
     m.set_function(func)
     args_meta = Mock()
     args_meta.elements = {"first": Mock()}
     m.set_function_takes(args_meta)
     result = m(first="test")
     self.assertEquals({"first_out": "test"}, result)
     func.assert_called_with({"first": "test"})
Пример #6
0
 def test_simple_function(self):
     func = Mock(return_value={"first_out": "test"})
     m = Method("test_method", "test_description")
     m.set_function(func)
     args_meta = Mock()
     args_meta.elements = {"first": Mock()}
     m.set_function_takes(args_meta)
     result = m(first="test")
     self.assertEquals({"first_out": "test"}, result)
     func.assert_called_with({"first": "test"})
Пример #7
0
 def create_methods(self):
     """Create a Method wrapper for say_hello and return it"""
     method = Method("say_hello", "says hello")
     method.set_function(self.say_hello)
     takes = MapMeta("takes")
     takes.add_element(StringMeta("name", "a name"))
     method.set_function_takes(takes)
     returns = MapMeta("returns")
     returns.add_element(StringMeta("greeting", "a greeting"))
     method.set_function_returns(returns)
     yield method
Пример #8
0
    def test_required(self):
        func = Mock(return_value={"first_out": "test"})
        m = Method("test_method", "test_description")
        m.set_function(func)
        args_meta = Mock()
        args_meta.elements = {"first": Mock(), "second": Mock()}
        args_meta.required = ["first"]
        m.set_function_takes(args_meta, {"first": "default"})
        self.assertEquals({"first_out": "test"}, m())
        func.assert_called_with({"first": "default"})

        m.set_function_takes(args_meta, {"second": "default"})
        self.assertRaises(ValueError, m)
Пример #9
0
    def test_handle_request_error(self):
        func = MagicMock()
        func.side_effect = ValueError("Test error")
        m = Method("test_method", "test_description")
        m.set_function(func)
        m.takes = MagicMock()
        m.returns = MagicMock()
        request = MagicMock()

        m.handle_request(request)

        request.respond_with_error.assert_called_once_with(
            "Method test_method raised an error: Test error")
Пример #10
0
    def test_required(self):
        func = Mock(return_value={"first_out": "test"})
        m = Method("test_method", "test_description")
        m.set_function(func)
        args_meta = Mock()
        args_meta.elements = {"first": Mock(), "second": Mock()}
        args_meta.required = ["first"]
        m.set_function_takes(args_meta, {"first": "default"})
        self.assertEquals({"first_out": "test"}, m())
        func.assert_called_with({"first": "default"})

        m.set_function_takes(args_meta, {"second": "default"})
        self.assertRaises(ValueError, m)
Пример #11
0
    def test_handle_request_error(self):
        func = MagicMock()
        func.side_effect = ValueError("Test error")
        m = Method("test_method", "test_description")
        m.set_function(func)
        m.takes = MagicMock()
        m.returns = MagicMock()
        request = MagicMock()

        m.handle_request(request)

        request.respond_with_error.assert_called_once_with(
            "Method test_method raised an error: Test error")
Пример #12
0
 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", "test_description")
     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})
Пример #13
0
    def test_get_response_raises(self):
        func = MagicMock()
        func.side_effect = ValueError("Test error")
        m = Method("test_description")
        m.set_parent(Mock(), "test_method")
        m.set_function(func)
        m.takes = MagicMock()
        m.returns = MagicMock()
        request = MagicMock()

        response = m.get_response(request)
        self.assertEquals("malcolm:core/Error:1.0", response.typeid)
        self.assertEquals("Method test_method raised an error: Test error",
                          response.message)
Пример #14
0
    def test_get_response_raises(self):
        func = MagicMock()
        func.side_effect = ValueError("Test error")
        m = Method("test_description")
        m.set_parent(Mock(), "test_method")
        m.set_function(func)
        m.takes = MagicMock()
        m.returns = MagicMock()
        request = MagicMock()

        response = m.get_response(request)
        self.assertEquals("malcolm:core/Error:1.0", response.typeid)
        self.assertEquals(
            "Method test_method raised an error: Test error", response.message)
Пример #15
0
    def test_call_calls_call_function(self):
        m = Method("test_description")
        call_func_mock = MagicMock()
        call_func_mock.return_value = {"output": 2}
        m.call_function = call_func_mock
        func = Mock(return_value={"first_out": "test"})
        m.set_function(func)
        args_meta = Mock(spec=MapMeta)
        args_meta.elements = {"first": Mock()}
        m.set_takes(args_meta)

        response = m(first="test")

        call_func_mock.assert_called_once_with(dict(first="test"))
        self.assertEqual(response, {"output": 2})
Пример #16
0
    def test_call_calls_call_function(self):
        m = Method("test_description")
        call_func_mock = MagicMock()
        call_func_mock.return_value = {"output": 2}
        m.call_function = call_func_mock
        func = Mock(return_value={"first_out": "test"})
        m.set_function(func)
        args_meta = Mock(spec=MapMeta)
        args_meta.elements = {"first": Mock()}
        m.set_takes(args_meta)

        response = m(first="test")

        call_func_mock.assert_called_once_with(dict(first="test"))
        self.assertEqual(response, {"output": 2})
Пример #17
0
 def test_incomplete_return(self):
     func = Mock(return_value={"output1": 2})
     m = Method("test_method", "test_description")
     m.set_function(func)
     args_meta = Mock()
     args_meta.elements = {"first": Mock(), "second": Mock()}
     return_meta = Mock()
     return_meta.elements = {"output1": Mock(), "output2": Mock()}
     validator = Mock(return_value=True)
     return_meta.elements["output1"].validate = validator
     return_meta.elements["output2"].validate = validator
     m.set_function_takes(args_meta)
     m.set_function_returns(return_meta)
     self.assertRaises(ValueError, m, first=1, second=2)
     func.assert_called_with({"first": 1, "second": 2})
Пример #18
0
 def test_incomplete_return(self):
     func = Mock(return_value={"output1": 2})
     m = Method("test_method", "test_description")
     m.set_function(func)
     args_meta = Mock()
     args_meta.elements = {"first": Mock(), "second": Mock()}
     return_meta = Mock()
     return_meta.elements = {"output1": Mock(), "output2": Mock()}
     validator = Mock(return_value=True)
     return_meta.elements["output1"].validate = validator
     return_meta.elements["output2"].validate = validator
     m.set_function_takes(args_meta)
     m.set_function_returns(return_meta)
     self.assertRaises(ValueError, m, first=1, second=2)
     func.assert_called_with({"first": 1, "second": 2})
Пример #19
0
 def test_positional_args(self):
     func = Mock(return_value={"output": 2})
     m = Method("test_method", "test_description")
     m.set_function(func)
     args_meta = Mock()
     validator = Mock(return_value=True)
     args_meta.elements = OrderedDict()
     args_meta.elements["first"] = Mock(validate=validator)
     args_meta.elements["second"] = Mock(validate=validator)
     args_meta.elements["third"] = Mock(validate=validator)
     args_meta.elements["fourth"] = Mock(validate=validator)
     args_meta.required = ["first", "third"]
     m.set_function_takes(args_meta)
     self.assertEquals({"output": 2}, m(2, 3, third=1, fourth=4))
     func.assert_called_with({"first": 2, "second": 3, "third": 1, "fourth": 4})
Пример #20
0
 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", "test_description")
     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})
Пример #21
0
    def test_get_response_no_parameters(self):
        m = Method("test_description")
        m.set_logger_name("mname")
        call_func_mock = MagicMock()
        m.call_function = call_func_mock
        func = Mock(return_value={"first_out": "test"})
        m.set_function(func)
        args_meta = Mock(spec=MapMeta)
        args_meta.elements = {"first": Mock()}
        m.set_takes(args_meta)
        request = MagicMock()
        del request.parameters  # Make sure mock doesn't have `parameters`

        m.get_response(request)

        call_func_mock.assert_called_once_with(dict())
Пример #22
0
    def test_defaults(self):
        func = Mock(return_value={"first_out": "test"})
        m = Method("test_description", writeable=True)
        m.set_parent(Mock(), "test_method")
        s = StringMeta(description='desc')
        args_meta = MapMeta()
        args_meta.elements = {"first": s, "second": s}
        m.set_takes(args_meta)
        m.set_defaults({"second": "default"})
        m.set_function(func)

        self.assertEquals({"first_out": "test"}, m.call_function(dict(first="test")))
        call_arg = func.call_args[0][0]
        self.assertEqual("test", call_arg.first)
        self.assertEqual("default", call_arg.second)
        self.assertEqual(args_meta, call_arg.meta)
Пример #23
0
    def test_get_response_no_parameters(self):
        m = Method("test_description")
        m.set_logger_name("mname")
        call_func_mock = MagicMock()
        m.call_function = call_func_mock
        func = Mock(return_value={"first_out": "test"})
        m.set_function(func)
        args_meta = Mock(spec=MapMeta)
        args_meta.elements = {"first": Mock()}
        m.set_takes(args_meta)
        request = MagicMock()
        del request.parameters  # Make sure mock doesn't have `parameters`

        m.get_response(request)

        call_func_mock.assert_called_once_with(dict())
Пример #24
0
    def test_defaults(self):
        func = Mock(return_value={"first_out": "test"})
        m = Method("test_description", writeable=True)
        m.set_parent(Mock(), "test_method")
        s = StringMeta(description='desc')
        args_meta = MapMeta()
        args_meta.elements = {"first": s, "second": s}
        m.set_takes(args_meta)
        m.set_defaults({"second": "default"})
        m.set_function(func)

        self.assertEquals({"first_out": "test"},
                          m.call_function(dict(first="test")))
        call_arg = func.call_args[0][0]
        self.assertEqual("test", call_arg.first)
        self.assertEqual("default", call_arg.second)
        self.assertEqual(args_meta, call_arg.meta)
Пример #25
0
 def test_invalid_return(self):
     func = Mock(return_value={"output1": 2, "output2": 4})
     m = Method("test_method", "test_description")
     m.set_function(func)
     args_meta = Mock()
     args_meta.elements = {"first": Mock(), "second": Mock()}
     return_meta = Mock()
     return_meta.elements = {"output1": Mock(), "output2": Mock()}
     validator1 = Mock(return_value=True)
     validator2 = Mock(side_effect=TypeError("Fake type error"))
     return_meta.elements["output1"].validate = validator1
     return_meta.elements["output2"].validate = validator2
     m.set_function_takes(args_meta)
     m.set_function_returns(return_meta)
     self.assertRaises(TypeError, m, first=1, second=2)
     func.assert_called_with({"first": 1, "second": 2})
     validator2.assert_called_with(4)
Пример #26
0
 def test_invalid_return(self):
     func = Mock(return_value={"output1": 2, "output2": 4})
     m = Method("test_method", "test_description")
     m.set_function(func)
     args_meta = Mock()
     args_meta.elements = {"first": Mock(), "second": Mock()}
     return_meta = Mock()
     return_meta.elements = {"output1": Mock(), "output2": Mock()}
     validator1 = Mock(return_value=True)
     validator2 = Mock(side_effect=TypeError("Fake type error"))
     return_meta.elements["output1"].validate = validator1
     return_meta.elements["output2"].validate = validator2
     m.set_function_takes(args_meta)
     m.set_function_returns(return_meta)
     self.assertRaises(TypeError, m, first=1, second=2)
     func.assert_called_with({"first": 1, "second": 2})
     validator2.assert_called_with(4)
Пример #27
0
 def test_to_dict_serialization(self):
     func = Mock(return_value={"out": "dummy"})
     defaults = {"in_attr": "default"}
     args_meta = Mock(elements={"first": Mock()},
                      to_dict=Mock(
                          return_value=OrderedDict({"dict": "args"})))
     return_meta = Mock(elements={"out": Mock()},
                        to_dict=Mock(
                            return_value=OrderedDict({"dict": "return"})))
     m = Method("test_method", "test_description")
     m.set_function(func)
     m.set_function_takes(args_meta, defaults)
     m.set_function_returns(return_meta)
     expected = OrderedDict()
     expected["description"] = "test_description"
     expected["takes"] = OrderedDict({"dict": "args"})
     expected["defaults"] = OrderedDict({"in_attr": "default"})
     expected["returns"] = OrderedDict({"dict": "return"})
     self.assertEquals(expected, m.to_dict())
Пример #28
0
 def test_valid_return(self):
     func = Mock(return_value={"output1": 2, "output2": 4})
     m = Method("test_method", "test_description")
     m.set_function(func)
     args_meta = Mock()
     args_meta.elements = {"first": Mock(), "second": Mock()}
     args_meta.required = ["first", "second"]
     return_meta = Mock()
     return_meta.elements = {"output1": Mock(), "output2": Mock()}
     validator1 = Mock(return_value=True)
     validator2 = Mock(return_value=True)
     return_meta.elements["output1"].validate = validator1
     return_meta.elements["output2"].validate = validator2
     m.set_function_takes(args_meta)
     m.set_function_returns(return_meta)
     self.assertEquals({"output1": 2, "output2": 4}, m(first=1, second=2))
     func.assert_called_with({"first": 1, "second": 2})
     validator1.assert_called_with(2)
     validator2.assert_called_with(4)
Пример #29
0
 def test_to_dict_serialization(self):
     func = Mock(return_value={"out": "dummy"})
     defaults = {"in_attr": "default"}
     args_meta = Mock(
         elements={"first": Mock()},
         to_dict=Mock(return_value=OrderedDict({"dict": "args"})))
     return_meta = Mock(
         elements={"out": Mock()},
         to_dict=Mock(return_value=OrderedDict({"dict": "return"})))
     m = Method("test_method", "test_description")
     m.set_function(func)
     m.set_function_takes(args_meta, defaults)
     m.set_function_returns(return_meta)
     expected = OrderedDict()
     expected["description"] = "test_description"
     expected["takes"] = OrderedDict({"dict": "args"})
     expected["defaults"] = OrderedDict({"in_attr": "default"})
     expected["returns"] = OrderedDict({"dict": "return"})
     self.assertEquals(expected, m.to_dict())
Пример #30
0
 def test_valid_return(self):
     func = Mock(return_value={"output1": 2, "output2": 4})
     m = Method("test_method", "test_description")
     m.set_function(func)
     args_meta = Mock()
     args_meta.elements = {"first": Mock(), "second": Mock()}
     args_meta.required = ["first", "second"]
     return_meta = Mock()
     return_meta.elements = {"output1": Mock(), "output2": Mock()}
     validator1 = Mock(return_value=True)
     validator2 = Mock(return_value=True)
     return_meta.elements["output1"].validate = validator1
     return_meta.elements["output2"].validate = validator2
     m.set_function_takes(args_meta)
     m.set_function_returns(return_meta)
     self.assertEquals({"output1": 2, "output2": 4}, m(first=1, second=2))
     func.assert_called_with({"first": 1, "second": 2})
     validator1.assert_called_with(2)
     validator2.assert_called_with(4)
Пример #31
0
 def test_positional_args(self):
     func = Mock(return_value={"output": 2})
     m = Method("test_method", "test_description")
     m.set_function(func)
     args_meta = Mock()
     validator = Mock(return_value=True)
     args_meta.elements = OrderedDict()
     args_meta.elements["first"] = Mock(validate=validator)
     args_meta.elements["second"] = Mock(validate=validator)
     args_meta.elements["third"] = Mock(validate=validator)
     args_meta.elements["fourth"] = Mock(validate=validator)
     args_meta.required = ["first", "third"]
     m.set_function_takes(args_meta)
     self.assertEquals({"output": 2}, m(2, 3, third=1, fourth=4))
     func.assert_called_with({
         "first": 2,
         "second": 3,
         "third": 1,
         "fourth": 4
     })
Пример #32
0
    def test_call_with_positional_args(self):
        func = Mock(return_value={"output": 2})
        m = Method("test_description")
        call_func_mock = MagicMock()
        m.call_function = call_func_mock
        m.set_function(func)
        args_meta = Mock(spec=MapMeta)
        validator = Mock(return_value=True)
        args_meta.elements = OrderedDict()
        args_meta.elements["first"] = Mock(validate=validator)
        args_meta.elements["second"] = Mock(validate=validator)
        args_meta.elements["third"] = Mock(validate=validator)
        args_meta.elements["fourth"] = Mock(validate=validator)
        args_meta.required = ["first", "third"]
        m.set_takes(args_meta)

        m(2, 3, third=1, fourth=4)

        call_func_mock.assert_called_once_with({'second': 3, 'fourth': 4,
                                                'third': 1, 'first': 2})
Пример #33
0
    def test_incomplete_return(self):
        func = Mock(return_value={"output1": 2})
        m = Method("test_description", writeable=True)
        m.name = "test_method"
        m.set_function(func)
        s = StringMeta(description='desc')
        args_meta = MapMeta()
        args_meta.set_elements({"first": s, "second": s})
        return_meta = MapMeta()
        return_meta.set_elements({"output1": s, "output2": s})
        return_meta.set_required(["output2"])
        m.set_takes(args_meta)
        m.set_returns(return_meta)

        with self.assertRaises(KeyError):
            m.call_function(dict(first=1, second=2))
        call_arg1, call_arg2 = func.call_args_list[0][0]
        self.assertEqual('1', call_arg1.first)
        self.assertEqual('2', call_arg1.second)
        self.assertEqual(args_meta, call_arg1.meta)
        self.assertEqual(return_meta, call_arg2.meta)
Пример #34
0
    def test_incomplete_return(self):
        func = Mock(return_value={"output1": 2})
        m = Method("test_description", writeable=True)
        m.name = "test_method"
        m.set_function(func)
        s = StringMeta(description='desc')
        args_meta = MapMeta()
        args_meta.set_elements({"first": s, "second": s})
        return_meta = MapMeta()
        return_meta.set_elements({"output1": s, "output2": s})
        return_meta.set_required(["output2"])
        m.set_takes(args_meta)
        m.set_returns(return_meta)

        with self.assertRaises(KeyError):
            m.call_function(dict(first=1, second=2))
        call_arg1, call_arg2 = func.call_args_list[0][0]
        self.assertEqual('1', call_arg1.first)
        self.assertEqual('2', call_arg1.second)
        self.assertEqual(args_meta, call_arg1.meta)
        self.assertEqual(return_meta, call_arg2.meta)
Пример #35
0
    def test_call_with_positional_args(self):
        func = Mock(return_value={"output": 2})
        m = Method("test_description")
        call_func_mock = MagicMock()
        m.call_function = call_func_mock
        m.set_function(func)
        args_meta = Mock(spec=MapMeta)
        validator = Mock(return_value=True)
        args_meta.elements = OrderedDict()
        args_meta.elements["first"] = Mock(validate=validator)
        args_meta.elements["second"] = Mock(validate=validator)
        args_meta.elements["third"] = Mock(validate=validator)
        args_meta.elements["fourth"] = Mock(validate=validator)
        args_meta.required = ["first", "third"]
        m.set_takes(args_meta)

        m(2, 3, third=1, fourth=4)

        call_func_mock.assert_called_once_with({
            'second': 3,
            'fourth': 4,
            'third': 1,
            'first': 2
        })