def test_read_stacked_sources_with_strategies_for_none_values():
    config = StackedConfig(DictSource({'a': None}),
                           DictSource({'a': None}),
                           strategy_map={
                               'a': strategies.collect,
                           })

    result = [None, None]

    assert config.a == result
    assert list(config.items()) == [('a', result)]
def test_source_items(monkeypatch):
    monkeypatch.setenv('MVP_A', '10')
    config = StackedConfig(DictSource({
        'a': 1,
        'b': {
            'c': 2
        }
    }), Environment('MVP'), DictSource({
        'x': 6,
        'b': {
            'y': 7
        }
    }))

    items = list(config.items())
    assert items == [('a', 10), ('b', config.b), ('x', 6)]

    items = list(config.b.items())
    assert items == [('c', 2), ('y', 7)]
def test_source_items_prevent_shadowing_between_subsections_and_values(
        reverse):
    sources = [
        DictSource({
            'a': 1,
            'b': {
                'c': 2
            }
        }),
        DictSource({
            'x': 6,
            'b': 5
        }),
    ]
    config = StackedConfig(*sources, reverse=reverse)

    with pytest.raises(ValueError) as exc_info:
        list(config.items())

    assert "conflicts" in str(exc_info.value)
def test_source_items_with_strategies_and_untyped_source(
        monkeypatch, inimaker):
    monkeypatch.setenv('MVP_A', '100')
    untyped_source = inimaker(u"""
        [__root__]
        a=1000
    """)

    config = StackedConfig(
        Environment('MVP'),  # last source still needs a typed source
        DictSource({
            'a': 1,
            'x': [5, 6],
            'b': {
                'c': 2,
                'd': [3, 4]
            }
        }),
        DictSource({
            'a': 10,
            'x': [50, 60],
            'b': {
                'c': 20,
                'd': [30, 40]
            }
        }),
        INIFile(untyped_source),
        strategy_map={
            'a': strategies.add,
            'x': strategies.collect,  # keep lists intact
            'c': strategies.collect,  # collect values into list
            'd': strategies.merge,  # merge lists
        })

    items = list(config.items())
    assert items == [('a', 1111), ('b', config.b), ('x', [[50, 60], [5, 6]])]

    items = list(config.b.items())
    assert items == [('c', [20, 2]), ('d', [30, 40, 3, 4])]