Exemplo n.º 1
0
def test_insert_layer(clac_layers):
    alpha, beta, gamma = clac_layers
    expected_name_list = []

    cfg = CLAC(gamma)
    expected_name_list.append('gamma')
    assert list(cfg.layers) == expected_name_list

    cfg.insert_layers(beta)
    expected_name_list.insert(0, 'beta')
    assert list(cfg.layers) == expected_name_list

    cfg.insert_layers(beta, alpha)
    expected_name_list.insert(1, 'alpha')
    assert list(cfg.layers) == expected_name_list

    fake_beta = SimpleLayer('beta', {})
    with raises(LayerOverwriteError):
        cfg.insert_layers(fake_beta)

    cfg.insert_layers(fake_beta, raise_on_replace=False)

    assert cfg.layers == {
        'beta': fake_beta,
        'alpha': alpha,
        'gamma': gamma,
    }
Exemplo n.º 2
0
def test_assert_immutable(clac_layers):
    simple_clac = CLAC(*clac_layers)
    with raises(ImmutableLayer):
        simple_clac['test_key'] = 'hello'
    with raises(ImmutableLayer):
        clac_layers[0]['test_key'] = 'Hello'
    with raises(ImmutableLayer):
        simple_clac.setdefault('test_key')
Exemplo n.º 3
0
def test_remove_layer(clac_layers):
    alpha, beta, _ = clac_layers
    cfg = CLAC(alpha, beta)

    assert cfg['beta_secret'] == 'fghij'
    cfg.remove_layer('beta')
    with raises(NoConfigKey):
        _ = cfg['beta_secret']
Exemplo n.º 4
0
def test_build_nri(clac_layers):
    simple_clac = CLAC(*clac_layers)
    assert simple_clac.build_nri() == {
        ('alpha', 'test_key'),
        ('alpha', 'alpha_secret'),
        ('alpha', 'unique'),
        ('beta', 'beta_secret'),
        ('gamma', 'gamma_secret'),
    }
Exemplo n.º 5
0
def test_build_vri(clac_layers):
    simple_clac = CLAC(*clac_layers)
    assert simple_clac.build_vri() == {
        ('alpha', 'test_key', 'test_value_alpha'),
        ('alpha', 'alpha_secret', 'abcde'),
        ('alpha', 'unique', '0123456789'),
        ('beta', 'beta_secret', 'fghij'),
        ('gamma', 'gamma_secret', 'gamma rules!'),
    }
Exemplo n.º 6
0
def test_get_from_one_layer(clac_layers):
    alpha = clac_layers[0]
    simple_clac = CLAC(alpha)

    assert 'alpha' in simple_clac
    assert 'test_key' in simple_clac.names
    result1 = simple_clac['test_key']
    result2 = simple_clac.get('test_key')
    result3 = simple_clac.get('test_key', layer_name='alpha')
    assert result1 is result2 is result3
    assert result1 == 'test_value_alpha'
Exemplo n.º 7
0
def test_multi_layer_priority(clac_layers):
    alpha, beta, _ = clac_layers
    cfg = CLAC(alpha, beta)

    assert 'alpha' in cfg
    assert 'beta' in cfg
    assert 'test_key' in cfg.names
    assert 'beta_secret' in cfg.names
    assert cfg['test_key'] == 'test_value_alpha'
    assert cfg.get('test_key', layer_name='beta') == 'test_value_beta'
    assert cfg['beta_secret'] == 'fghij'
Exemplo n.º 8
0
def test_bad_layer():
    clac = CLAC()
    clac.remove_layer('none')
    with raises(MissingLayer):
        clac.remove_layer('none', error_ok=False)

    with raises(MissingLayer):
        clac.get('key', layer_name='none')
Exemplo n.º 9
0
def test_setdefault(mutable_layers):
    mutable_clac = CLAC(*mutable_layers)

    unique = object()
    default = object()

    assert mutable_clac.get('new_key', default=default) is default
    assert mutable_clac.get('new_key', layer_name='beta',
                            default=default) is default
    mutable_clac.setdefault('new_key', unique)
    assert mutable_clac.setdefault('new_key', 'acbde') is unique
    assert mutable_clac.get('new_key', default=default) is unique
    assert mutable_clac.get('new_key', layer_name='beta',
                            default=default) is unique
Exemplo n.º 10
0
def test_names_property(clac_layers):
    simple_clac = CLAC(*clac_layers)
    assert {
        'test_key',
        'alpha_secret',
        'beta_secret',
        'unique',
        'gamma_secret',
    } == simple_clac.names
Exemplo n.º 11
0
def test_mutate_existing_key(mutable_layers):
    mutable_clac = CLAC(*mutable_layers)

    unique = object()

    assert mutable_clac.get('test_key') == 'test_value_alpha'
    assert mutable_clac.get('test_key', layer_name='beta') == 'test_value_beta'
    mutable_clac['test_key'] = unique
    assert mutable_clac.get('test_key') == 'test_value_alpha'
    assert mutable_clac.get('test_key', layer_name='beta') is unique
Exemplo n.º 12
0
def test_add_layer(clac_layers):
    alpha, beta, _ = clac_layers
    cfg = CLAC(alpha)

    assert cfg['test_key'] == 'test_value_alpha'
    with raises(NoConfigKey):
        _ = cfg['beta_secret']
    with raises(MissingLayer):
        cfg.get('test_key', layer_name='beta')

    cfg.add_layers(beta)

    assert cfg['test_key'] == 'test_value_alpha'
    assert cfg['beta_secret'] == 'fghij'
    assert cfg.get('test_key', layer_name='beta') == 'test_value_beta'
Exemplo n.º 13
0
def test_add_new_key(mutable_layers):
    mutable_clac = CLAC(*mutable_layers)

    unique = object()
    default = object()

    assert mutable_clac.get('new_key', default=default) is default
    assert mutable_clac.get('new_key', layer_name='beta',
                            default=default) is default
    mutable_clac['new_key'] = unique
    assert mutable_clac.get('new_key', default=default) is unique
    assert mutable_clac.get('new_key', layer_name='beta',
                            default=default) is unique
Exemplo n.º 14
0
def test_layers(clac_layers):
    simple_clac = CLAC(*clac_layers)
    expected_lookup = {layer.name: layer for layer in clac_layers}
    assert simple_clac.layers == expected_lookup
Exemplo n.º 15
0
def test_resolve(clac_layers):
    simple_clac = CLAC(*clac_layers)

    assert simple_clac.resolve('beta_secret') == ('beta', 'fghij')
    with raises(NoConfigKey):
        simple_clac.resolve('missing.key')
Exemplo n.º 16
0
def test_no_key():
    clac = CLAC()
    with raises(ValueError):
        _ = clac['']
Exemplo n.º 17
0
def test_get_callback(clac_layers):
    simple_clac = CLAC(*clac_layers)

    assert simple_clac.get('unique', callback=int) == 123456789
    assert simple_clac.get('missing', default='not a list',
                           callback=list) == 'not a list'
Exemplo n.º 18
0
def test_get_default(clac_layers):
    simple_clac = CLAC(*clac_layers)
    default = object()
    assert simple_clac.get('missingkey', default=default) is default