Example #1
0
def test_resolve_bean_will_return_first_matching_response(monkeypatch):
    monkeypatch.setattr('bakedbeans.contents.request',
                        Mock(args={'key': 'value2'}))
    bean = {
        '_bean':
        True,
        'responses': [{
            'params': {
                'key': 'value'
            },
            'contents': {
                'response': 1
            }
        }, {
            'params': {
                'key': 'value2'
            },
            'contents': {
                'response': 2
            }
        }]
    }
    resolver = contents.ContentResolver(base=Path(), url='', method='get')
    response, status_code = resolver.resolve_bean(bean)
    assert status_code == 200
    assert response['response'] == 2
Example #2
0
def test_bean_validation_raises_exception_when_content_not_in_responses():
    bean = {'_bean': True, 'responses': [{'params': {'key': 'value'}}]}
    resolver = contents.ContentResolver(base=Path(), url='', method='get')

    resolver.full_path = ''

    with pytest.raises(exceptions.BeanValidationError):
        resolver.validate_bean(bean)
Example #3
0
def test_bean_validation_raises_exception_when_response_is_not_present():
    bean = {'_bean': True}
    resolver = contents.ContentResolver(base=Path(), url='', method='get')

    resolver.full_path = ''

    with pytest.raises(exceptions.BeanValidationError):
        resolver.validate_bean(bean)
Example #4
0
def test_loading_invalid_json_file_raises_exception(tmpdir):
    path = tmpdir / 'test.json'
    with path.open('w') as f:
        f.write('{')
    resolver = contents.ContentResolver(base=tmpdir, url='', method='get')
    resolver.full_path = path
    with pytest.raises(exceptions.InvalidContent):
        resolver.load_content()
Example #5
0
def test_resolve_path_returns_full_path_when_file_exists(tmpdir):
    path = tmpdir / 'index.get.json'
    with path.open('w') as f:
        f.write('{"key": "value"}')
    resolver = contents.ContentResolver(base=tmpdir, url='', method='get')
    resolver.path = 'index.get.json'
    full_path = resolver.resolve_path()
    assert full_path.exists()
Example #6
0
def test_loading_valid_json_file_returns_content(tmpdir):
    path = tmpdir / 'test.json'
    with path.open('w') as f:
        f.write('{"key": "value"}')
    resolver = contents.ContentResolver(base=tmpdir, url='', method='get')
    resolver.full_path = path
    content = resolver.load_content()
    assert content == {'key': 'value'}
Example #7
0
def test_response_property_loads_and_returns_bean_contents(tmpdir):
    content_file = tmpdir / 'index.get.json'
    with content_file.open('w') as r:
        r.write('{"_bean": true, "responses": [{"contents": {"yes": true}}]}')
    resolver = contents.ContentResolver(base=tmpdir, url='', method='get')
    response, status_code = resolver.response
    assert status_code == 200
    assert response == {'yes': True}
Example #8
0
def test_bean_validation_does_not_raise_exception_on_valid_bean():
    bean = {'_bean': True, 'responses': [{'contents': {}}]}
    resolver = contents.ContentResolver(base=Path(), url='', method='get')

    resolver.full_path = ''
    try:
        resolver.validate_bean(bean)
    except exceptions.BeanValidationError:
        pytest.fail("Valid bean raised unexcepted validation error")
Example #9
0
def test_is_bean_returns_false_when_data_passed_is_not_a_dict():
    data = ''
    resolver = contents.ContentResolver(base=Path(), url='', method='get')
    assert not resolver.is_bean(data)
Example #10
0
def test_is_bean_returns_false_when_data_passed_is_marked_as_not_a_bean():
    data = {'_bean': False}
    resolver = contents.ContentResolver(base=Path(), url='', method='get')
    assert not resolver.is_bean(data)
Example #11
0
def test_is_bean_returns_true_when_data_passed_is_a_bean():
    data = {'_bean': True}
    resolver = contents.ContentResolver(base=Path(), url='', method='get')
    assert resolver.is_bean(data)
Example #12
0
def test_build_path_mirrors_url_path():
    resolver = contents.ContentResolver(base=Path(),
                                        url='a/path/to/a/thing',
                                        method='get')
    path = resolver.build_path()
    assert str(path) == 'a/path/to/a/thing.get.json'
Example #13
0
def test_build_path_resolves_empty_url_to_index_file(method, filename):
    resolver = contents.ContentResolver(base=Path(), url='', method=method)
    path = resolver.build_path()
    assert str(path) == filename
Example #14
0
def test_resolve_path_raises_exception_if_file_not_found():
    resolver = contents.ContentResolver(base=Path(), url='', method='get')
    resolver.path = 'nope.get.json'
    with pytest.raises(exceptions.ContentNotFoundError):
        resolver.resolve_path()
Example #15
0
def test_build_path_resolves_folder_urls_to_index_file():
    resolver = contents.ContentResolver(base=Path(),
                                        url='somewhere/',
                                        method='get')
    path = resolver.build_path()
    assert str(path).endswith('index.get.json')
Example #16
0
def test_default_response_code_is_based_on_method(method, status_code):
    resolver = contents.ContentResolver(base=Path(), url='', method=method)
    assert resolver.default_response_code() == status_code
Example #17
0
def test_match_params_returns_true_when_params_match(params, request_args,
                                                     expected, monkeypatch):
    monkeypatch.setattr('bakedbeans.contents.request', Mock(args=request_args))
    resolver = contents.ContentResolver(base=Path(), url='', method='get')
    assert resolver.matches_params(params) is expected