Exemplo n.º 1
0
def test_yaml_source_throws_import_error_when_module_missing(mocker):
    import sys

    # buggy mocker.patch.dict, using manual context resoration
    _yaml = sys.modules.get('yaml', None)
    sys.modules['yaml'] = None

    with pytest.raises(ConcreteSettingsError) as excinfo:
        get_source('/test/settings.yaml')
    assert isinstance(excinfo.value.__cause__, ImportError)

    if _yaml is not None:
        sys.modules['yaml'] = _yaml
    else:
        del sys.modules['yaml']
Exemplo n.º 2
0
def test_yaml_source_read_nested_object_value(fs):
    fs.create_file(
        '/test/settings.yaml',
        contents='''
    A:
      B: 10
    ''',
    )
    ysrc = get_source('/test/settings.yaml')
    assert ysrc.read(S('B'), parents=('A', )) == 10
Exemplo n.º 3
0
def test_yaml_source_read_array_value(fs):
    fs.create_file(
        '/test/settings.yaml',
        contents='''
    A:
      - 1
      - 2
      - 3
    ''',
    )
    ysrc = get_source('/test/settings.yaml')
    assert ysrc.read(S('A')) == [1, 2, 3]
Exemplo n.º 4
0
def test_yaml_source_read_non_existing_setting_returns_not_found(fs):
    fs.create_file('/test/settings.yaml', contents='')
    ysrc = get_source('/test/settings.yaml')

    setting = S('NOT_EXISTS')
    assert ysrc.read(setting) == NotFound
def test_json_source_has_expected_path(fs):
    fs.create_file('/test/settings.json', contents='{}')
    jsrc = get_source('/test/settings.json')
    assert jsrc.path == '/test/settings.json'
Exemplo n.º 6
0
def test_dict_source_read_non_existing_setting_returns_not_found(fs):
    dsrc = sources.get_source({})
    setting = S('NOT_EXISTS')
    assert dsrc.read(setting) == NotFound
Exemplo n.º 7
0
def test_python_source_read_nested_object_value():
    src = get_source(PYTHON_SOURCE_PATH)
    assert src.read(S('PORT'), parents=('DATABASE',)) == 1234
Exemplo n.º 8
0
def test_python_source_read_non_existing_setting_returns_not_found():
    src = get_source(PYTHON_SOURCE_PATH)
    setting = S('NOT_EXISTS')
    assert src.read(setting) == NotFound
Exemplo n.º 9
0
def test_dict_source_one_level_values():
    dsrc = sources.get_source({'a': 10, 'b': 20})
    assert dsrc.read(S('a')) == 10
    assert dsrc.read(S('b')) == 20
Exemplo n.º 10
0
def test_yaml_source_read_float_value(fs):
    fs.create_file('/test/settings.yaml', contents='A: 10.25')
    ysrc = get_source('/test/settings.yaml')
    assert ysrc.read(S('A')) == 10.25
Exemplo n.º 11
0
def test_yaml_source_read_null_value(fs):
    fs.create_file('/test/settings.yaml', contents='A: null')
    ysrc = get_source('/test/settings.yaml')
    assert ysrc.read(S('A')) is None
Exemplo n.º 12
0
def test_get_json_file_returns_json_source():
    src = get_source('/test/settings.json')
    assert isinstance(src, JsonSource)
Exemplo n.º 13
0
def test_get_yaml_file_returns_yaml_source():
    src = get_source('/test/settings.yaml')
    assert isinstance(src, YamlSource)
Exemplo n.º 14
0
def test_env_source_read_non_existing_setting_returns_not_found():
    esrc = get_source(EnvVarSource())
    setting = S('NOT_EXISTS')
    assert esrc.read(setting) == NotFound
Exemplo n.º 15
0
def test_env_source_with_parents(monkeypatch):
    monkeypatch.setenv('DB_USER', 'alex')
    esrc = get_source(EnvVarSource())
    assert esrc.read(S('USER', str), ('DB', )) == 'alex'
    assert esrc.read(S('USER', str), ('db', )) == 'alex'
Exemplo n.º 16
0
def test_env_source_float_hint(monkeypatch):
    monkeypatch.setenv('a', '10.25')
    esrc = get_source(EnvVarSource())
    assert esrc.read(S('a', float)) == 10.25
Exemplo n.º 17
0
def test_env_source_one_level_values(monkeypatch):
    monkeypatch.setenv('a', '10')
    esrc = get_source(EnvVarSource())
    assert esrc.read(S('a')) == '10'
Exemplo n.º 18
0
def test_get_env_source_returns_env_source():
    esrc = get_source(EnvVarSource())
    assert isinstance(esrc, EnvVarSource)
Exemplo n.º 19
0
def test_yaml_source_has_expected_path(fs):
    fs.create_file('/test/settings.yaml', contents='')
    ysrc = get_source('/test/settings.yaml')
    assert ysrc.path == '/test/settings.yaml'
Exemplo n.º 20
0
def test_json_source_read_float_value(fs):
    fs.create_file('/test/settings.json', contents='{"A": 10.25}')
    jsrc = get_source('/test/settings.json')
    assert jsrc.read(S('A')) == 10.25
Exemplo n.º 21
0
def test_yaml_source_read_str_value(fs):
    fs.create_file('/test/settings.yaml', contents='A: abc')
    ysrc = get_source('/test/settings.yaml')
    assert ysrc.read(S('A')) == "abc"
Exemplo n.º 22
0
def test_json_source_read_str_value(fs):
    fs.create_file('/test/settings.json', contents='{"A": "abc"}')
    jsrc = get_source('/test/settings.json')
    assert jsrc.read(S('A')) == "abc"
Exemplo n.º 23
0
def test_json_source_read_null_value(fs):
    fs.create_file('/test/settings.json', contents='{"A": null}')
    jsrc = get_source('/test/settings.json')
    assert jsrc.read(S('A')) is None
Exemplo n.º 24
0
def test_json_source_read_array_value(fs):
    fs.create_file('/test/settings.json', contents='{"A": [1, 2, 3]}')
    jsrc = get_source('/test/settings.json')
    assert jsrc.read(S('A')) == [1, 2, 3]
Exemplo n.º 25
0
def test_get_source_for_dict_retuns_dict_source():
    dsrc = sources.get_source({'a': 10})
    assert isinstance(dsrc, sources.Source)
Exemplo n.º 26
0
def test_json_source_read_nested_object_value(fs):
    fs.create_file('/test/settings.json', contents='{"A": {"B": 10}}')
    jsrc = get_source('/test/settings.json')
    assert jsrc.read(S('B'), parents=('A',)) == 10
Exemplo n.º 27
0
def test_dict_source_two_levels_nested_dicts_values():
    dsrc = sources.get_source({'a': 10, 'c': {'d': 30}})
    assert dsrc.read(S('a')) == 10
    assert dsrc.read(S('c')) == {'d': 30}
    assert dsrc.read(S('d'), parents=('c', )) == 30
Exemplo n.º 28
0
def test_json_source_read_non_existing_setting_returns_not_found(fs):
    fs.create_file('/test/settings.json', contents='{"A": {"B": 10}}')
    jsrc = get_source('/test/settings.json')

    setting = S('NOT_EXISTS')
    assert jsrc.read(setting) == NotFound
Exemplo n.º 29
0
def test_get_source_fail_for_unknown_source():
    with pytest.raises(sources.NoSuitableSourceFound):
        assert sources.get_source('/test/dummy')
Exemplo n.º 30
0
def test_python_source_read_object_value():
    src = get_source(PYTHON_SOURCE_PATH)
    assert src.read(S('DICT')) == {'KEY': 'VALUE'}