示例#1
0
def test_json():
    cfg = Birch(NSPACE)
    print(cfg._val_dict)
    assert cfg['basekey'] == 'base_val'
    assert cfg['BASEKEY'] == 'base_val'
    res = cfg['server']
    assert isinstance(res, dict)
    assert len(res) == 2
    assert res['PORT'] == 1293
    assert res['port'] == 1293
    assert cfg['SERVER__PORT'] == 1293
    assert cfg['server__port'] == 1293
    assert cfg['SERVER']['PORT'] == 1293
    assert cfg['server']['PORT'] == 1293
    assert cfg['SERVER']['port'] == 1293
    assert cfg['server']['port'] == 1293
    with pytest.raises(KeyError):
        cfg['SERVER'][4]
    assert cfg['{}_SERVER__PORT'.format(NSPACE)] == 1293
    assert cfg['{}__SERVER__PORT'.format(NSPACE)] == 1293
    assert cfg['nega'] == 'Uvavo'
    assert cfg['NEGA'] == 'Uvavo'
    assert cfg['mike'] == '88'
    assert cfg['MAN']['HEIGHT'] == '175'
    assert cfg['MAN__WEIGHT'] == '73'
    with pytest.raises(ValueError):
        cfg[54]

    assert cfg['MOCK__LVL'] == 'A'
    assert cfg['mock__lvl'] == 'A'
    assert cfg['MOCK']['LVL'] == 'A'
    assert cfg['mock']['LVL'] == 'A'
    assert cfg['MOCK']['lvl'] == 'A'
    assert cfg['mock']['lvl'] == 'A'
    assert cfg['MOCK__LVL2'] == 'B'
    assert cfg['mock__lvl2'] == 'B'
    assert cfg['NOT_LVL2'] == 'C'
    assert cfg.get('NOT_LVL2') == 'C'
    assert cfg.get('NOT_LVL2', '32') == 'C'
    assert cfg.get('doesnt exists', '3321') == '3321'

    with pytest.raises(KeyError):
        assert cfg['JON'] == 'Hello'
    assert len(cfg) == 17
    for name, value in cfg:
        assert isinstance(name, str)
示例#2
0
def test_envvars_with_defaults():
    prepare_namespace_2()
    k1 = 'NAKOKO'
    full_k1 = '{}_{}'.format(NSPACE2, k1)
    v1 = 45
    k2_1 = 'NANA'
    k2_2 = 'BOKO'
    full_k2 = '{}__{}__{}'.format(NSPACE2, k2_1, k2_2)
    v2 = 'rar'
    k3 = 'lemer'
    v3 = 'yever'
    full_k4 = '{}_magi'.format(NSPACE2)
    v4 = 39
    k5_1 = 'anil'
    k5_2 = 'shanil'
    v5 = 'baril'
    defaults = {
        k1: v1,
        full_k2: v2,
        k3: v3,
        full_k4: v4,
        k5_1: {
            k5_2: v5
        },
    }
    cfg = Birch(
        NSPACE2,
        defaults=defaults,
    )
    assert cfg[k1] == v1
    assert cfg[full_k1] == v1
    assert cfg.mget(full_k1) == v1
    assert cfg.get(k1) == v1
    assert cfg[k2_1][k2_2] == v2
    assert cfg[full_k2] == v2
    assert cfg[k3] == v3
    assert cfg[full_k4] == v4
    assert cfg[k5_1][k5_2] == v5

    with pytest.raises(ValueError):
        cfg = Birch(
            NSPACE2,
            defaults={2: 'kpkp'},
        )