Beispiel #1
0
def test_create_method_iterator_kwarg():
    args = [{
        "name": 'limit',
        "in": 'query',
        "required": False,
        "doc": ""
    }, {
        "name": 'page_num',
        "in": 'query',
        "required": False,
        "doc": ""
    }, {
        "name": 'order',
        "in": 'query',
        "required": False,
        "doc": ""
    }, {
        "name": 'order_by',
        "in": 'query',
        "required": False,
        "doc": ""
    }]
    method = _resources.create_method(args, 'get', 'mock_name', '/objects',
                                      'fake_doc')
    mock_endpoint = mock.MagicMock()

    method(mock_endpoint, iterator=True)
    mock_endpoint._call_api.assert_called_once_with('get',
                                                    '/objects', {}, {},
                                                    iterator=True)
Beispiel #2
0
def test_create_method_unexpected_kwargs():
    args = [{
        "name": 'foo',
        "in": 'query',
        "required": True,
        "doc": ""
    }, {
        "name": 'bar',
        "in": 'query',
        "required": False,
        "doc": ""
    }]
    method = _resources.create_method(args, 'get', 'mock_name', '/objects',
                                      'fake_doc')
    mock_endpoint = mock.MagicMock()

    # Method works without unexpected kwarg
    method(mock_endpoint, foo=0, bar=0)
    mock_endpoint._call_api.assert_called_once_with('get',
                                                    '/objects', {
                                                        "foo": 0,
                                                        "bar": 0
                                                    }, {},
                                                    iterator=False)

    # Method raises TypeError with unexpected kwarg
    expected_msg = "mock_name() got an unexpected keyword argument(s) {'baz'}"
    with pytest.raises(TypeError) as excinfo:
        method(mock_endpoint, foo=0, bar=0, baz=0)
    assert str(excinfo.value) == expected_msg
Beispiel #3
0
def test_create_method_no_iterator_kwarg():
    # We don't do any validation on keyword arguments if **kwargs is in the
    # signature. They are just ignored if they aren't in the expected body,
    # query, or path params. Therefore, method should return a Type Error,
    # but method2 contains **kwargs so should overwrite iterator = False
    args = [{"name": 'id', "in": 'query', "required": True, "doc": ""}]
    method = _resources.create_method(args, 'get', 'mock_name', '/objects',
                                      'fake_doc')
    mock_endpoint = mock.MagicMock()

    with pytest.raises(TypeError) as excinfo:
        method(mock_endpoint, id=202, iterator=True)

    assert 'keyword argument' in str(excinfo.value)

    args2 = [{"name": 'foo', "in": 'query', "required": False, "doc": ""}]
    method2 = _resources.create_method(args2, 'get', 'mock_name', '/objects',
                                       'fake_doc')
    mock_endpoint2 = mock.MagicMock()
    method2(mock_endpoint2, iterator=True)
    mock_endpoint2._call_api.assert_called_once_with('get',
                                                     '/objects', {}, {},
                                                     iterator=False)
Beispiel #4
0
def test_create_method_no_iterator_kwarg():

    # Test that dynamically-created function errors when an
    # unexpected "iterator" parameter is passed in
    args = [{"name": 'id', "in": 'query', "required": True, "doc": ""}]
    method = _resources.create_method(args, 'get', 'mock_name', '/objects',
                                      'fake_doc')
    mock_endpoint = mock.MagicMock()

    with pytest.raises(TypeError) as excinfo:
        method(mock_endpoint, id=202, iterator=True)

    assert 'keyword argument' in str(excinfo.value)

    # Dynamic functions handle optional argument through a different
    # code path; verify that this also rejects unexpected arguments.
    args2 = [{"name": 'foo', "in": 'query', "required": False, "doc": ""}]
    method2 = _resources.create_method(args2, 'get', 'mock_name', '/objects',
                                       'fake_doc')
    mock_endpoint2 = mock.MagicMock()
    with pytest.raises(TypeError) as excinfo:
        method2(mock_endpoint2, iterator=True)

    assert 'keyword argument' in str(excinfo.value)
Beispiel #5
0
def _create_mock_endpoint():
    args = [{
        "name": 'foo',
        "in": 'query',
        "required": True,
        "doc": ""
    }, {
        "name": 'bar',
        "in": 'query',
        "required": False,
        "doc": ""
    }]
    method = _resources.create_method(args, 'get', 'mock_name', '/objects',
                                      'fake_doc')
    mock_endpoint = mock.MagicMock()
    return mock_endpoint, method