示例#1
0
def test_list(mocker):
    cinp = CInP('http://localhost:8080', '/api/v1/', None)
    mocked_open = mocker.patch('urllib.request.OpenerDirector.open')
    mocked_open.return_value = MockResponse(
        200, {
            'Position': '0',
            'Count': '2',
            'Total': '20'
        }, '["/api/v1/model:123:","/api/v1/model:124:"]')

    with pytest.raises(InvalidRequest):
        cinp.list('/api/v1/')

    with pytest.raises(InvalidRequest):
        cinp.list('/api/v1/model:asdf:')

    mocked_open.reset_mock()
    (items, count_map) = cinp.list('/api/v1/model')
    req = mocked_open.call_args[0][0]
    assert req.full_url == 'http://localhost:8080/api/v1/model'
    assert req.data == b'{}'
    assert req.headers == {
        'Position': '0',
        'Count': '10',
        'Content-type': 'application/json;charset=utf-8'
    }
    assert req.get_method() == 'LIST'
    assert items == ['/api/v1/model:123:', '/api/v1/model:124:']
    assert count_map == {'position': 0, 'count': 2, 'total': 20}

    mocked_open.reset_mock()
    (items, count_map) = cinp.list('/api/v1/model', count=5, position=20)
    req = mocked_open.call_args[0][0]
    assert req.full_url == 'http://localhost:8080/api/v1/model'
    assert req.data == b'{}'
    assert req.headers == {
        'Position': '20',
        'Count': '5',
        'Content-type': 'application/json;charset=utf-8'
    }
    assert req.get_method() == 'LIST'
    assert items == ['/api/v1/model:123:', '/api/v1/model:124:']
    assert count_map == {'position': 0, 'count': 2, 'total': 20}

    mocked_open.reset_mock()
    (items, count_map) = cinp.list('/api/v1/model',
                                   filter_name='alpha',
                                   filter_value_map={'sort_by': 'age'})
    req = mocked_open.call_args[0][0]
    assert req.full_url == 'http://localhost:8080/api/v1/model'
    assert req.data == b'{"sort_by": "age"}'
    assert req.headers == {
        'Filter': 'alpha',
        'Position': '0',
        'Count': '10',
        'Content-type': 'application/json;charset=utf-8'
    }
    assert req.get_method() == 'LIST'
    assert items == ['/api/v1/model:123:', '/api/v1/model:124:']
    assert count_map == {'position': 0, 'count': 2, 'total': 20}

    with pytest.raises(InvalidRequest):
        cinp.list('/api/v1/', filter_value_map='asdf')

    with pytest.raises(InvalidRequest):
        cinp.list('/api/v1/', position=-1)

    with pytest.raises(InvalidRequest):
        cinp.list('/api/v1/', count=-1)

    with pytest.raises(InvalidRequest):
        cinp.list('/api/v1/', position='adf')

    with pytest.raises(InvalidRequest):
        cinp.list('/api/v1/', count='asdf')

    mocked_open.reset_mock()
    mocked_open.return_value = MockResponse(
        201, {
            'Position': '0',
            'Count': '2',
            'Total': '20'
        }, '["/api/v1/model:123:","/api/v1/model:124:"]')
    with pytest.raises(ResponseError):
        cinp.list('/api/v1/model')

    mocked_open.reset_mock()
    mocked_open.return_value = MockResponse(200, {
        'Position': '0',
        'Count': '2',
        'Total': '20'
    }, '"adsf"')
    with pytest.raises(ResponseError):
        cinp.list('/api/v1/model')

    mocked_open.reset_mock()
    mocked_open.return_value = MockResponse(200, {
        'Position': '0',
        'Count': '2',
        'Total': '20'
    }, '{"adsf":"sdf"}')
    with pytest.raises(ResponseError):
        cinp.list('/api/v1/model')

    mocked_open.reset_mock()
    mocked_open.return_value = MockResponse(
        200, {}, '["/api/v1/model:123:","/api/v1/model:124:"]')
    (items, count_map) = cinp.list('/api/v1/model')
    req = mocked_open.call_args[0][0]
    assert req.full_url == 'http://localhost:8080/api/v1/model'
    assert req.data == b'{}'
    assert req.headers == {
        'Position': '0',
        'Count': '10',
        'Content-type': 'application/json;charset=utf-8'
    }
    assert req.get_method() == 'LIST'
    assert items == ['/api/v1/model:123:', '/api/v1/model:124:']
    assert count_map == {'position': 0, 'count': 0, 'total': 0}

    mocked_open.reset_mock()
    mocked_open.return_value = MockResponse(
        200, {
            'Position': 'a',
            'Count': 'b',
            'Total': 'c'
        }, '["/api/v1/model:123:","/api/v1/model:124:"]')
    (items, count_map) = cinp.list('/api/v1/model')
    req = mocked_open.call_args[0][0]
    assert req.full_url == 'http://localhost:8080/api/v1/model'
    assert req.data == b'{}'
    assert req.headers == {
        'Position': '0',
        'Count': '10',
        'Content-type': 'application/json;charset=utf-8'
    }
    assert req.get_method() == 'LIST'
    assert items == ['/api/v1/model:123:', '/api/v1/model:124:']
    assert count_map == {'position': 0, 'count': 0, 'total': 0}
示例#2
0
文件: run_demo.py 项目: cinp/python
print('Now We will DESCRIBE, see what it is we are going to play with')
print(client.describe('/api/v1/'))
print(client.describe('/api/v1/User/'))
print(client.describe('/api/v1/User/User'))
print(client.describe('/api/v1/User/User(setPassword)'))
print(client.describe('/api/v1/User/Session(login)'))
print(client.describe('/api/v1/User/Session(logout)'))
print(client.describe('/api/v1/User/Session(hearbeat)'))
print(client.describe('/api/v1/Car/'))
print(client.describe('/api/v1/Car/PartType'))
print(client.describe('/api/v1/Car/Part'))
print(client.describe('/api/v1/Car/Car'))

print('List is not allowed on Users')
try:
    print(client.list('/api/v1/User/User'))
except NotAuthorized:
    pass

print('Let' 's get ford' 's user info')
print(client.get('/api/v1/User/User:ford:'))

print('What kind of Part Types do we have to work with')
print(client.list('/api/v1/Car/PartType'))

print('Let' 's make some parts')
print(
    client.create('/api/v1/Car/Part', {
        'part_type': '/api/v1/Car/PartType:Bumper:',
        'price': 10.00
    }))