Exemplo n.º 1
0
def test_options_with_default():
    root = Options(None, [
        Group('a', [
            Value('x', int, 42),
            Value('y', str, 'hello'),
        ], True),
        Group('b', [
            Value('x', int, 6),
            Value('y', str, 'bye'),
        ]),
    ])

    conf = ConfigSimple(root)
    assert conf.to_dict() == {'a.x': 42, 'a.y': 'hello'}

    conf['a'] = True
    assert conf.to_dict() == {'a.x': 42, 'a.y': 'hello'}

    conf['a'] = False
    assert conf.to_dict() == {}

    conf['b.x'] = 12
    assert conf.to_dict() == {'b.x': 12, 'b.y': 'bye'}

    conf['a'] = True
    assert conf.to_dict() == {'a.x': 42, 'a.y': 'hello', 'b.x': 12, 'b.y': 'bye'}
Exemplo n.º 2
0
def test_deeper_Options():
    root = Options(None, [
        Group('a', [
            Options('p', [
                Value('x', int, 42),
                Value('y', str, 'hello'),
            ]),
        ], True),
        Group('b', [
            Options('q', [
                Value('x', int, 6),
                Value('y', str, 'bye'),
            ]),
        ]),
    ])
    root.validate()
    assert list(map(str, root.walk())) == [
        "(<Options None>,)",
        "(<Options None>, <Group 'a'>)",
        "(<Options None>, <Group 'a'>, <Options 'p'>, <Value 'x'>)",
        "(<Options None>, <Group 'a'>, <Options 'p'>, <Value 'y'>)",
        "(<Options None>, <Group 'b'>)",
        "(<Options None>, <Group 'b'>, <Options 'q'>, <Value 'x'>)",
        "(<Options None>, <Group 'b'>, <Options 'q'>, <Value 'y'>)",
    ]
Exemplo n.º 3
0
def test_invalid_Switch_multiple_activated():
    root = Switch(None, [
        Group('a', [
            Value('x', int, 42),
            Value('y', str, 'hello'),
        ], True),
        Group('b', [
            Value('x', int, 6),
            Value('y', str, 'bye'),
        ], False),
    ])
    root.validate()
Exemplo n.º 4
0
def test_empty_conf_default_under_exclusive():
    conf = ConfigSimple(
        Switch(None, [
            Group('a', default=True, nodes=[
                Value('x', int, 42),
            ]),
            Group('b', nodes=[
                Value('x', int, 12),
            ]),
        ])
    )
    assert conf.to_dict() == {'a.x': 42}
Exemplo n.º 5
0
def test_empty_conf_exclusive():
    conf = ConfigSimple(
        Switch(None, [
            Group('a', nodes=[
                Value('x', int, 42),
            ]),
            Group('b', nodes=[
                Value('x', int, 12),
            ]),
        ])
    )
    assert conf.to_dict() == {}
Exemplo n.º 6
0
def test_invalid_duplicate_2():
    root = Switch(None, [
        Group('a', [
            Value('x', int, 42),
            Value('y', str, 'hello'),
        ], True),
        Group('a', [
            Value('x', int, 6),
            Value('y', str, 'bye'),
        ]),
    ])
    with pytest.raises(ValueError):
        root.validate()
Exemplo n.º 7
0
def test_make_tree():
    root = Group(None, [
        Value("a1ob", bool, default=False, doc="a1ob doc"),
        Switch("a2e", [
            Switch('b1ef',
                   doc="b1ef doc",
                   nodes=[
                       Value("c1of", bool),
                       Value("c2of", bool),
                       Group("c3gf",
                             nodes=[
                                 Value("d1o", doc="d1o doc"),
                                 Value("d2o"),
                             ]),
                   ]),
            Group(
                'b2gf',
                nodes=[
                    Switch(None, [
                        Group("d3gf", nodes=[
                            Value("e1oi", int, default=50),
                        ]),
                        Value("d4of", bool),
                    ]),
                    Value("c4od", float, default=1e-8),
                    Value("c5oi", int, default=100),
                    Value("c6o"),
                ]),
        ]),
        Group("a3g", [
            Value("b3oi", int, default=30),
            Value("b4od", float, default=1e-6),
        ])
    ])
    root.validate()
    assert list(map(Value.name_from_path, root.walk())) == [
        '',
        'a1ob',
        'a2e.b1ef',
        'a2e.b1ef.c1of',
        'a2e.b1ef.c2of',
        'a2e.b1ef.c3gf',
        'a2e.b1ef.c3gf.d1o',
        'a2e.b1ef.c3gf.d2o',
        'a2e.b2gf',
        'a2e.b2gf.d3gf',
        'a2e.b2gf.d3gf.e1oi',
        'a2e.b2gf.d4of',
        'a2e.b2gf.c4od',
        'a2e.b2gf.c5oi',
        'a2e.b2gf.c6o',
        'a3g.b3oi',
        'a3g.b4od',
    ]
    return root
Exemplo n.º 8
0
def test_default_group_under_exclusif():
    root = Switch(None, [
        Group('a', default=True, nodes=[
            Value('x', int, 42),
            Value('y', bool, True)
        ]),
        Group('b', nodes=[
            Value('x', int, 42),
            Value('y', bool, True)
        ]),
    ])

    conf = ConfigSimple(root)

    assert conf['a']
    assert conf['a.x'] == 42
    assert conf['a.y']
    assert not conf['b']
    with pytest.raises(KeyError):
        conf['b.x']
    with pytest.raises(KeyError):
        conf['b.y']
    assert conf.to_dict() == {'a.x': 42, 'a.y': True}

    conf['b'] = True

    assert conf['b']
    assert conf['b.x'] == 42
    assert conf['b.y']
    assert not conf['a']
    with pytest.raises(KeyError):
        conf['a.x']
    with pytest.raises(KeyError):
        conf['a.y']
    assert conf.to_dict() == {'b.x': 42, 'b.y': True}

    conf['b'] = False
    assert not conf['a']
    with pytest.raises(KeyError):
        conf['a.x']
    with pytest.raises(KeyError):
        conf['a.y']
    assert not conf['b']
    with pytest.raises(KeyError):
        conf['b.x']
    with pytest.raises(KeyError):
        conf['b.y']
    assert conf.to_dict() == {}
Exemplo n.º 9
0
def test_simple_Switch():
    root = Switch(None, [
        Group('a', [
            Value('x', int, 42),
            Value('y', str, 'hello'),
        ], True),
        Group('b', [
            Value('x', int, 6),
            Value('y', str, 'bye'),
        ]),
    ])
    root.validate()
    assert list(map(str, root.walk())) == [
        "(<Switch None>,)",
        "(<Switch None>, <Group 'a'>)",
        "(<Switch None>, <Group 'a'>, <Value 'x'>)",
        "(<Switch None>, <Group 'a'>, <Value 'y'>)",
        "(<Switch None>, <Group 'b'>)",
        "(<Switch None>, <Group 'b'>, <Value 'x'>)",
        "(<Switch None>, <Group 'b'>, <Value 'y'>)",
    ]
Exemplo n.º 10
0
def test_default_under_flag_group():
    root = Options(None, [
        Group('a', nodes=[Value('x', int, 42),
                          Value('y', bool, False)]),
    ])

    conf = ConfigSimple(root)

    assert conf.to_dict() == {}
    assert conf.to_nested_dict() == {}

    conf['a'] = True

    assert conf.to_dict() == {
        'a.x': 42,
        'a.y': False,
    }
    assert conf.to_nested_dict() == {
        'a': {
            'x': 42,
            'y': False,
        },
    }

    conf['a'] = False

    assert conf.to_dict() == {}
    assert conf.to_nested_dict() == {}

    conf['a.x'] = 2

    assert conf.to_dict() == {
        'a.x': 2,
        'a.y': False,
    }
    assert conf.to_nested_dict() == {
        'a': {
            'x': 2,
            'y': False,
        },
    }
Exemplo n.º 11
0
def test_one_option():
    root = Options(None, [
        Group('a', nodes=[
            Value('x', int, 42),
            Value('y', bool, False)
        ]),
    ])

    conf = ConfigSimple(root)

    assert not conf['a']
    with pytest.raises(KeyError):
        conf['a.x']
    with pytest.raises(KeyError):
        conf['a.y']
    assert conf.to_dict() == {}

    conf['a'] = True

    assert conf['a']
    assert conf['a.x'] == 42
    assert conf['a.y'] == False
    assert conf.to_dict() == {'a.x': 42, 'a.y': False}

    conf['a'] = False

    assert not conf['a']
    with pytest.raises(KeyError):
        conf['a.x']
    with pytest.raises(KeyError):
        conf['a.y']
    assert conf.to_dict() == {}

    conf['a.x'] = 15

    assert conf['a']
    assert conf['a.x'] == 15
    assert conf['a.y'] == False
    assert conf.to_dict() == {'a.x': 15, 'a.y': False}