예제 #1
0
def test_Config_on_shelf(tmpdir):
    with shelve.open(os.path.join(tmpdir.strpath, 'x.shelve')) as shelf:
        flatshelf = FlatMap(dictionary=shelf)
        config = Config(dictionary=flatshelf)
        config['x'] = 10
        config['y'] = {}
        config['y']['a'] = 'aaa'
        config['y']['b'] = 111
        config['y']['c'] = {'v': 10}
        config['y']['c']['w'] = 20
        config['y']['d'] = 888
        config['z'] = 999
        config_stream = string_io()
        config.dump(stream=config_stream)
        config_dump = config_stream.getvalue()
        assert config_dump == """\
z = 999
x = 10
[y]
    b = 111
    [c]
        v = 10
        w = 20
    a = 'aaa'
    d = 888
"""
    with shelve.open(os.path.join(tmpdir.strpath, 'x.shelve')) as shelf2:
        flatshelf2 = FlatMap(dictionary=shelf2)
        config2 = Config(dictionary=flatshelf2)
        config2_stream = string_io()
        config2.dump(stream=config2_stream)
        config2_dump = config2_stream.getvalue()
        assert config2_dump == config_dump
예제 #2
0
def test_FlatMap_update3():
    upd1 = {'a': 1}
    upd2 = {'b': 2}
    flatmap = FlatMap(dictionary=collections.OrderedDict())
    flatmap.update(upd1, **upd2)
    upd = {}
    upd.update(upd1)
    upd.update(upd2)
    assert flatmap == upd
예제 #3
0
def test_FlatMap_update3():
    upd1 = {'a': 1}
    upd2 = {'b': 2}
    flatmap = FlatMap(dictionary=collections.OrderedDict())
    flatmap.update(upd1, **upd2)
    upd = {}
    upd.update(upd1)
    upd.update(upd2)
    assert flatmap == upd
예제 #4
0
def test_FlatMap_key_err(content, key_error):
    flatmap = FlatMap(None, init=content)
    key, error = key_error
    with pytest.raises(error):
        a = flatmap[key]
    with pytest.raises(error):
        del flatmap[key]
    with pytest.raises(error):
        flatmap.has_key(key)
    with pytest.raises(error):
        flatmap.update({key: 'anything'})
예제 #5
0
def test_FlatMap_get(content):
    flatmap = FlatMap(None, init=content)
    value = flatmap.get('a', 123)
    assert value == 10
    value = flatmap.get('A', 123)
    assert value == 123
    value = flatmap.get('sub', 123)
    assert isinstance(value, FlatMap)
    assert value.dictionary == flatmap.dictionary
    assert value.dictionary is flatmap.dictionary
    assert value.prefix != flatmap.prefix
    assert value == FlatMap(None, init=content['sub'])
예제 #6
0
def test_Section_2(content):
    section = Section(dictionary=FlatMap(collections.OrderedDict()),
                      init=content)
    section2 = Section(dictionary=FlatMap(collections.OrderedDict()),
                       init=content)
    s_io = string_io()
    section.dump(stream=s_io)
    assert s_io.getvalue() == SECTION_DUMP
    section['sub']['y']['yfilename'] = 'y.dat'
    s_io1 = string_io()
    section.dump(stream=s_io1)
    assert s_io1.getvalue() != SECTION_DUMP
    s_io2 = string_io()
    section2.dump(stream=s_io2)
    assert s_io2.getvalue() == SECTION_DUMP
예제 #7
0
def test_FlatMap_eq_ne(content):
    flatmap0 = FlatMap(None, init=content)
    flatmape = FlatMap(None, init=content.copy())
    flatmap1 = FlatMap(None, init=content.copy())
    flatmap2 = FlatMap(None, init=content.copy())
    flatmap3 = FlatMap(None, init=content.copy())
    flatmap1['x'] = 10001
    flatmap2['sub']['x'] = 'alpha'
    del flatmap3['sub']['x']
    assert flatmap0 == flatmape
    assert flatmap0 != flatmap1
    assert flatmap1 != flatmap0
    assert flatmap0 != flatmap2
    assert flatmap2 != flatmap0
    assert flatmap0 != flatmap3
    assert flatmap3 != flatmap0
예제 #8
0
def test_FlatMap_bool(content):
    flatmap = FlatMap(None)
    assert not flatmap
    flatmap['x'] = 0
    assert flatmap
    del flatmap['x']
    assert not flatmap
예제 #9
0
def test_Config_on_shelf_mutable(tmpdir):
    with shelve.open(os.path.join(tmpdir.strpath, 'x_mutable.shelve')) as shelf:
        flatshelf = FlatMap(dictionary=shelf)
        config = Config(dictionary=flatshelf)
        config['a'] = [1, 2]
        config['a'].append(3)
        assert config['a'] == [1, 2]
예제 #10
0
def test_FlatMap_set_par_err(content):
    flatmap = FlatMap(None, init=content)
    with pytest.raises(ValueError) as exc_info:
        flatmap['b'] = {}
    assert str(exc_info.value) == "cannot replace key b with submap"
    with pytest.raises(ValueError) as exc_info:
        flatmap['sub']['x'] = {}
    assert str(exc_info.value) == "cannot replace key sub.x with submap"
예제 #11
0
def test_FlatMap_sub_clear(content):
    flatmap = FlatMap(None, init=content)
    lf = len(flatmap)
    ld = len(flatmap.dictionary)
    lfs = len(flatmap['sub'])
    flatmap['sub'].clear()
    assert len(flatmap) == lf
    assert len(flatmap.dictionary) == ld - lfs
    assert len(flatmap['sub'].dictionary) == len(flatmap.dictionary)
예제 #12
0
def test_FlatMap_create_init_pos(content):
    flatmap = FlatMap(None, init=content)
    assert len(flatmap) == len(content)
    assert flatmap.dictionary != content
    flattened_content = flatten(content)
    assert flatmap.dictionary == flattened_content
    assert flatmap.dictionary is not content
    assert isinstance(flatmap['a'], int)
    assert isinstance(flatmap['sub'], FlatMap)
    assert flatmap.dictionary == flatmap['sub'].dictionary
    assert flatmap.prefix != flatmap['sub'].prefix
예제 #13
0
def test_FlatMap_set(content):
    flatmap = FlatMap(None, init=content)
    flatmap['b'] = 100
    flatmap['sub'] = {'z': 888}
    assert flatmap == collections.OrderedDict([
        ('a', 10),
        ('b', 100),
        ('sub', collections.OrderedDict([
            ('z', 888),
        ])),
        ('c', 30),
    ])
예제 #14
0
def test_FlatMap_key_err(content, key_error):
    flatmap = FlatMap(None, init=content)
    key, error = key_error
    with pytest.raises(error):
        a = flatmap[key]
    with pytest.raises(error):
        del flatmap[key]
    with pytest.raises(error):
        flatmap.has_key(key)
    with pytest.raises(error):
        flatmap.update({key: 'anything'})
예제 #15
0
def test_FlatMap_get(content):
    flatmap = FlatMap(None, init=content)
    value = flatmap.get('a', 123)
    assert value == 10
    value = flatmap.get('A', 123)
    assert value == 123
    value = flatmap.get('sub', 123)
    assert isinstance(value, FlatMap)
    assert value.dictionary == flatmap.dictionary
    assert value.dictionary is flatmap.dictionary
    assert value.prefix != flatmap.prefix
    assert value == FlatMap(None, init=content['sub'])
예제 #16
0
def test_FlatMap_keys(flat_content, iterattribute):
    flatmap = FlatMap(None, init=flat_content)
    l0 = list(getattr(flatmap, iterattribute)())
    l1 = list(getattr(flat_content, iterattribute)())
    assert l0 == l1
예제 #17
0
def test_FlatMap_update2():
    upd1 = {'a': 1}
    flatmap = FlatMap(dictionary=collections.OrderedDict())
    flatmap.update(upd1.items())
    assert flatmap == upd1
예제 #18
0
def test_FlatMap_copy_covered(tdct):
    f0 = FlatMap(tdct)
    f1 = f0.copy()
    assert f0['x'] == f1['x']
    assert f0 is not f1
예제 #19
0
from zirkon.toolbox.serializer import Serializer


@pytest.fixture(params=tuple(Serializer.get_class_tags()))
def protocol(request):
    return request.param


@pytest.fixture
def string_io():
    return io.StringIO()


_dictionary_classes = [
    dict, collections.OrderedDict,
    type(None), lambda: FlatMap(collections.OrderedDict())
]


@pytest.fixture(params=_dictionary_classes,
                ids=[cc.__name__ for cc in _dictionary_classes])
def generic_dictionary(request):
    dictionary_class = request.param
    return dictionary_class()


@pytest.fixture
def dictionary():
    return collections.OrderedDict()

예제 #20
0
def test_FlatMap_create_dictionary_flat(flat_content):
    flatmap = FlatMap(flat_content)
    assert len(flatmap) == len(flat_content)
    assert flatmap.dictionary == flat_content
    assert flatmap.dictionary is flat_content
    assert isinstance(flatmap['a'], int)
예제 #21
0
def test_FlatMap_create_init_flat(flat_content):
    flatmap = FlatMap(collections.OrderedDict(), init=flat_content)
    assert len(flatmap) == len(flat_content)
    assert flatmap.dictionary == flat_content
    assert flatmap.dictionary is not flat_content
    assert isinstance(flatmap['a'], int)
예제 #22
0
def flatmap(content):
    return FlatMap(collections.OrderedDict(), init=content)
예제 #23
0
def test_FlatMap_create():
    flatmap = FlatMap(collections.OrderedDict())
    assert len(flatmap) == 0
    assert isinstance(flatmap.dictionary, collections.OrderedDict)
    assert len(flatmap.dictionary) == 0
예제 #24
0
def test_FlatMap_has_key(content):
    flatmap = FlatMap(None, init=content)
    assert flatmap.has_key('a')
    assert flatmap.has_key('sub')
    assert not flatmap.has_key('d')
예제 #25
0
def test_FlatMap_set_sub_err(content):
    flatmap = FlatMap(None, init=content)
    with pytest.raises(ValueError) as exc_info:
        flatmap['sub'] = 'alpha'
    assert str(exc_info.value) == "cannot replace submap sub with key"
예제 #26
0
def test_FlatMap_same_dict(content):
    flatmap = FlatMap(None, init=content)
    flatmap2 = FlatMap(flatmap.dictionary)
    assert flatmap2 == flatmap
예제 #27
0
def test_FlatMap_copy(content):
    flatmap = FlatMap(None, init=content)
    dct = flatmap.as_dict()
    assert dct == content
예제 #28
0
def test_FlatMap_same_dict_prefix(content):
    flatmap = FlatMap(None, init=content)
    flatmap2 = FlatMap(flatmap.dictionary, prefix='sub.')
    assert flatmap2 != flatmap
    assert flatmap2 == flatmap['sub']
예제 #29
0
def test_FlatMap_has_key(content):
    flatmap = FlatMap(None, init=content)
    assert flatmap.has_key('a')
    assert flatmap.has_key('sub')
    assert not flatmap.has_key('d')
예제 #30
0
def test_FlatMap_clear(content):
    flatmap = FlatMap(None, init=content)
    flatmap.clear()
    assert len(flatmap) == 0
    assert len(flatmap.dictionary) == 0
예제 #31
0
def test_FlatMap_clear(content):
    flatmap = FlatMap(None, init=content)
    flatmap.clear()
    assert len(flatmap) == 0
    assert len(flatmap.dictionary) == 0
예제 #32
0
def test_FlatMap_copy(content):
    flatmap = FlatMap(None, init=content)
    flatmap2 = flatmap.copy()
    assert flatmap2 == flatmap
예제 #33
0
def test_FlatMap_copy(content):
    flatmap = FlatMap(None, init=content)
    flatmap2 = flatmap.copy()
    assert flatmap2 == flatmap
예제 #34
0
def test_FlatMap_copy(content):
    flatmap = FlatMap(None, init=content)
    dct = flatmap.as_dict()
    assert dct == content
예제 #35
0
def test_FlatMap_update2():
    upd1 = {'a': 1}
    flatmap = FlatMap(dictionary=collections.OrderedDict())
    flatmap.update(upd1.items())
    assert flatmap == upd1
예제 #36
0
def test_Section_create(content, string_io):
    section = Section(dictionary=FlatMap(collections.OrderedDict()),
                      init=content)
    section.dump(stream=string_io)
    assert string_io.getvalue() == SECTION_DUMP