Ejemplo n.º 1
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)
Ejemplo n.º 2
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)
Ejemplo n.º 3
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})
Ejemplo n.º 4
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})
Ejemplo n.º 5
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())
Ejemplo n.º 6
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())
Ejemplo n.º 7
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)
Ejemplo n.º 8
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)
Ejemplo n.º 9
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})
Ejemplo n.º 10
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
        })