Beispiel #1
0
def test_create():
    # can create
    content = json.loads(load_fixture_json('person.json'))
    # no support for creation with custom fields
    del content['custom_fields']
    cn = make_cn_with_resp(
        method='post',
        status_code=codes.ok,
        content=content
    )
    person = Person(name='Slithey Tove')
    person.create(using=cn.name)
    expected_url = URLObject(_default_url + 'v1/people/')
    cn.post.assert_called_with(expected_url, json={'name': 'Slithey Tove'})

    # can't create with an ID
    with assert_raises(ValueError):
        id_person = Person(id=1)
        id_person.create(using=cn.name)

    # validation errors come back. note the `invalid` instance isn't really
    # invalid; we just simulate the invalid response.
    invalid = Person(name='Invalid')
    cn = make_cn_with_resp(
        method='post',
        status_code=codes.unprocessable_entity,
        content=dict(message='Something wrong')
    )
    try:
        invalid.create(using=cn.name)
    except ValueError as ex:
        assert 'Something wrong' in str(ex)
    else:
        raise AssertionError('Exception not thrown')
Beispiel #2
0
def test_update():
    content = json.loads(load_fixture_json('person.json'))
    cn = make_cn_with_resp(method='put', status_code=codes.ok, content=content)
    person = Person(id=1, name='Nantucket Terwilliger')
    person.update(using=cn.name)
    expected_url = URLObject(_default_url + 'v1/people/1/')
    cn.put.assert_called_with(
        expected_url,
        json={'name': 'Nantucket Terwilliger'}
    )

    # can't update without an id
    with assert_raises(ValueError):
        no_id_person = Person()
        no_id_person.update(using=cn.name)

    # validation errors come back. note the `invalid` instance isn't really
    # invalid; we just simulate the invalid response.
    invalid = Person(id=1, name='Invalid')
    cn = make_cn_with_resp(
        method='put',
        status_code=codes.unprocessable_entity,
        content=dict(message='Something wrong')
    )
    try:
        invalid.update(using=cn.name)
    except ValueError as ex:
        assert 'Something wrong' in str(ex)
    else:
        raise AssertionError('Exception not thrown')
Beispiel #3
0
def test_load_multiple_nested_resource():
    schema = MultipleParent()
    stage_3_and_4 = [
        {'id': 3, 'name': 'Third Stage'},
        {'id': 4, 'name': 'Fourth Stage'},
    ]
    cn = make_cn_with_resp(
        method='get',
        status_code=codes.ok,
        content=stage_3_and_4,
        name='default'
    )
    data = {
        'stage': [
            {'name': 'foo'},
            {'name': 'bar'}
        ],
        'stage_idonly': [
            {'id': 3},
            {'id': 4},
        ]
    }
    loaded, _ = schema.load(data)
    assert all(isinstance(s, PipelineStage) for s in loaded['stage'])
    assert {s.name for s in loaded['stage']} == {'foo', 'bar'}
    assert {s.name for s in loaded['stage_idonly']} == {'Third Stage', 'Fourth Stage'}  # noqa
Beispiel #4
0
def test_delete():
    cn = make_cn_with_resp(method='delete', status_code=codes.ok, content=[])
    person = Person(id=1)
    person.delete(using=cn.name)
    expected_url = URLObject(_default_url + 'v1/people/1/')
    cn.delete.assert_called_with(expected_url)

    # can't delete without an id
    with assert_raises(ValueError):
        no_id_person = Person()
        no_id_person.delete(using=cn.name)
Beispiel #5
0
def test_load_single_nested_resource():
    stage_3 = {'id': 3, 'name': 'Third Stage'}
    cn = make_cn_with_resp(
        method='get',
        status_code=codes.ok,
        content=[stage_3],
        name='default'
    )
    schema = SingleParent()
    data = {
        'stage': {'name': 'foo'},
        'stage_idonly': {'id': 3},
    }
    loaded, _ = schema.load(data)
    assert isinstance(loaded['stage'], PipelineStage)
    assert loaded['stage'].name == 'foo'
    assert loaded['stage_idonly'].name == 'Third Stage'
Beispiel #6
0
def test_load_single_nested_resource():
    stage_3 = {'id': 3, 'name': 'Third Stage'}
    cn = make_cn_with_resp(method='get',
                           status_code=codes.ok,
                           content=[stage_3],
                           name='default')
    schema = SingleParent()
    data = {
        'stage': {
            'name': 'foo'
        },
        'stage_idonly': {
            'id': 3
        },
    }
    loaded, _ = schema.load(data)
    assert isinstance(loaded['stage'], PipelineStage)
    assert loaded['stage'].name == 'foo'
    assert loaded['stage_idonly'].name == 'Third Stage'
Beispiel #7
0
def test_load_multiple_nested_resource():
    schema = MultipleParent()
    stage_3_and_4 = [
        {
            'id': 3,
            'name': 'Third Stage'
        },
        {
            'id': 4,
            'name': 'Fourth Stage'
        },
    ]
    cn = make_cn_with_resp(method='get',
                           status_code=codes.ok,
                           content=stage_3_and_4,
                           name='default')
    data = {
        'stage': [{
            'name': 'foo'
        }, {
            'name': 'bar'
        }],
        'stage_idonly': [
            {
                'id': 3
            },
            {
                'id': 4
            },
        ]
    }
    loaded, _ = schema.load(data)
    assert all(isinstance(s, PipelineStage) for s in loaded['stage'])
    assert {s.name for s in loaded['stage']} == {'foo', 'bar'}
    assert {s.name
            for s in loaded['stage_idonly']
            } == {'Third Stage', 'Fourth Stage'}  # noqa