Ejemplo n.º 1
0
def test_create(mocker):
    cinp = CInP('http://localhost:8080', '/api/v1/', None)
    mocked_open = mocker.patch('urllib.request.OpenerDirector.open')
    mocked_open.return_value = MockResponse(201, {'Object-Id': 'test'},
                                            '{"asdf": "erere"}')

    with pytest.raises(InvalidRequest):
        cinp.create('/api/v1/', {'asdf': 'xcv'})

    with pytest.raises(InvalidRequest):
        cinp.create('/api/v1/model:adsf:', {'asdf': 'xcv'})

    with pytest.raises(InvalidRequest):
        cinp.create('/api/v1/model', 'adsf')

    mocked_open.reset_mock()
    rec_values = cinp.create('/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 == {'Content-type': 'application/json;charset=utf-8'}
    assert req.get_method() == 'CREATE'
    assert rec_values == ('test', {'asdf': 'erere'})

    mocked_open.reset_mock()
    rec_values = cinp.create('/api/v1/model', {'asdf': 'xcv'})
    req = mocked_open.call_args[0][0]
    assert req.full_url == 'http://localhost:8080/api/v1/model'
    assert req.data == b'{"asdf": "xcv"}'
    assert req.headers == {'Content-type': 'application/json;charset=utf-8'}
    assert req.get_method() == 'CREATE'
    assert rec_values == ('test', {'asdf': 'erere'})

    mocked_open.reset_mock()
    mocked_open.return_value = MockResponse(200, {}, '"/api/v1/model:123:"')
    with pytest.raises(ResponseError):
        cinp.create('/api/v1/model', {'asdf': 'xcv'})

    mocked_open.reset_mock()
    mocked_open.return_value = MockResponse(201, {}, '["/api/v1/model:123:"]')
    with pytest.raises(ResponseError):
        cinp.create('/api/v1/model', {'asdf': 'xcv'})

    mocked_open.return_value = MockResponse(200, {}, '{ "hi": "there"}')
    with pytest.raises(ResponseError):
        cinp.create('/api/v1/model', {'asdf': 'xcv'})
Ejemplo n.º 2
0
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
    }))
print(
    client.create('/api/v1/Car/Part', {
        'part_type': '/api/v1/Car/PartType:MotorMount:',
        'price': 42.00
    }))
print(
    client.create('/api/v1/Car/Part', {
        'part_type': '/api/v1/Car/PartType:Wheel:',
        'price': 5.12
    }))
print(
    client.create('/api/v1/Car/Part', {
        'part_type': '/api/v1/Car/PartType:Wheel:',
        'price': 5.12