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
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
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'})
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'])
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
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
def test_FlatMap_bool(content): flatmap = FlatMap(None) assert not flatmap flatmap['x'] = 0 assert flatmap del flatmap['x'] assert not flatmap
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]
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"
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)
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
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), ])
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
def test_FlatMap_update2(): upd1 = {'a': 1} flatmap = FlatMap(dictionary=collections.OrderedDict()) flatmap.update(upd1.items()) assert flatmap == upd1
def test_FlatMap_copy_covered(tdct): f0 = FlatMap(tdct) f1 = f0.copy() assert f0['x'] == f1['x'] assert f0 is not f1
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()
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)
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)
def flatmap(content): return FlatMap(collections.OrderedDict(), init=content)
def test_FlatMap_create(): flatmap = FlatMap(collections.OrderedDict()) assert len(flatmap) == 0 assert isinstance(flatmap.dictionary, collections.OrderedDict) assert len(flatmap.dictionary) == 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')
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"
def test_FlatMap_same_dict(content): flatmap = FlatMap(None, init=content) flatmap2 = FlatMap(flatmap.dictionary) assert flatmap2 == flatmap
def test_FlatMap_copy(content): flatmap = FlatMap(None, init=content) dct = flatmap.as_dict() assert dct == content
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']
def test_FlatMap_clear(content): flatmap = FlatMap(None, init=content) flatmap.clear() assert len(flatmap) == 0 assert len(flatmap.dictionary) == 0
def test_FlatMap_copy(content): flatmap = FlatMap(None, init=content) flatmap2 = flatmap.copy() assert flatmap2 == flatmap
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