def test_list_attributes(): test_dict = {'i': 2, 'n': {'b': 5, 'n2': {}}} cfg.set('/t1', test_dict) result = cfg.root().list_attributes('/t1') print ' '.join(result) cfg.root().delete('/t1') assert result == ['i']
def test_get_immutable(): global global_val test_dict = {'i': 2, 'n': {'b': global_val, 'n2': {}}} cfg.set('/t1', test_dict) cfg_dict = cfg.get('/t1') # Should be 5 for both print 'Direct get: ' + str(cfg.get('/t1/n/b')) print 'Local map: ' + str(cfg_dict['n']['b']) # Change value of global variable global_val = 10 result1 = cfg.get('/t1/n/b') # Should print 5 print '1: ' + str(result1) # Change value of local map attribute cfg_dict['n']['b'] = global_val result2 = cfg.get('/t1/n/b') # Should still print 5 print '2: ' + str(result2) # Change local map node cfg_dict['n'] = {'b': 'abc'} result3 = cfg.get('/t1/n/b') # Should still print 5 print '3: ' + str(result3) cfg.root().delete('/t1') assert result1 == result2 == result3 == 5
def test_search_by_attr_recursive_depth_match(): test_dict = {'i': 2, 'n': {'b': 5, 'n2': {}}} cfg.set('/t1', test_dict) result = cfg.root().search('/', None, lambda x: x == 5, 2, True) print result cfg.root().delete('/t1') assert result == ['/t1/n', '/t1']
def test_list_nodes_fullpath(): test_dict = {'i': 2, 'n': {'b': 5, 'n2': {}}} cfg.set('/t1', test_dict) result = cfg.root().list_nodes('/t1', True) print ' '.join(result) cfg.root().delete('/t1') assert result == ['/t1/n']
def test_delete_attr(): test_dict = {'new_section': {'attr1': 'val1', 'attr2': 'val2'}} cfg.set('/', test_dict) cfg.root().delete('/new_section/attr1') result = cfg.get('/new_section/attr1') cfg.root().delete('/new_section') assert result is None
def test_search_by_attr_name_match(): test_dict = {'i': 2, 'n': {'b': True, 'n2': {}}} cfg.set('/t1', test_dict) result = cfg.root().search('/', 'i', lambda x: x == 2) print result cfg.root().delete('/t1') assert result == ['/t1']
def test_node_type_propagation(): test_dict = { 'n1': { 'n11': { 'n111': { 'attr': 1 }, 'n112': { 'attr': 1 }, 'n113': { 'attr': 1 } }, 'n12': { 'n121': { 'attr': 1 }, 'n122': { 'attr': 1 }, 'n123': { 'attr': 1 } }, 'n13': { 'n131': { 'attr': 1 }, 'n132': { 'attr': 1 }, 'n133': { 'attr': 1 } } } } cfg.set('/t1', test_dict) cfg.root().set_node_type('AN') r = {} r['r1'] = cfg.root( ).node_type == 'AN' # CN->AN (and remains after C->CN upward propagation) r['r2'] = cfg.root()._get_obj( '/t1').node_type == 'CN' # C->CN (CN->AN downward propagation) r['r3'] = cfg.root()._get_obj( '/t1/n1').node_type == 'C' # C-> C (C->CN downward propagation) cfg.root()._get_obj('/t1/n1/n11/n111').set_node_type('CN') r['r4'] = cfg.root()._get_obj('/t1/n1/n11/n111').node_type == 'CN' # C->CN r['r5'] = cfg.root()._get_obj( '/t1/n1/n11').node_type == 'AN' # C->AN (C->CN upward propagation) r['r6'] = cfg.root()._get_obj( '/t1/n1').node_type == 'AN' # C->AN (C->CN upward propagation) r['r7'] = cfg.root()._get_obj( '/t1/n1/n11/n112' ).node_type == 'CN' # C->CN (C->AN downward propagation) r['r8'] = cfg.root()._get_obj( '/t1/n1/n12').node_type == 'CN' # C->CN (C->AN downward propagation) cfg.root().print_fmt() try: cfg.root()._get_obj('/t1/n1/n11/n111').set_node_type('C') except AttributeError: cfg.root()._get_obj('/t1/n1/n11/n111').set_node_type('CN') r['r9'] = True cfg.root()._get_obj('/t1/n1').set_node_type('CN') r['r10'] = cfg.root()._get_obj('/t1/n1').node_type == 'CN' # AN->CN r['r11'] = cfg.root()._get_obj( '/t1/n1/n12').node_type == 'C' # CN->C (AN->CN downward propagation) r['r12'] = cfg.root()._get_obj( '/t1/n1/n11/n112' ).node_type == 'C' # CN->C (AN->CN downward propagation) r['r13'] = cfg.root()._get_obj( '/t1/n1/n11').node_type == 'C' # AN->C (AN->CN downward propagation cfg.root().delete('/t1') print r assert len([x for x in r.values() if not x]) == 0
def test_search_by_attr_name_no_attr(): test_dict = {'i': 2, 'n': {'b': True, 'n2': {}}} cfg.set('/t1', test_dict) result = cfg.root().search('/', 'x', lambda x: x == 5) cfg.root().delete('/t1') assert result == []
def test_delete_node(): test_dict = {'new_section': {'attr1': 'val1', 'attr2': 'val2'}} cfg.set('/', test_dict) cfg.root().delete('/new_section') assert cfg.get('/new_section') is None
def test_get_ok(): test_dict = {'new_section': {'attr1': 'val1', 'attr2': 'val2'}} cfg.set('/', test_dict) result = cfg.get('/new_section') cfg.root().delete('/new_section') assert cmp(result, test_dict)
def test_add_attr_ok(): cfg.set('/', cfg.ConfigAttribute('add_attr_ok', True)) result = cfg.get('/add_attr_ok') print result cfg.root().delete('/add_attr_ok') assert isinstance(result, bool) and result
def test_add_node_ok(): cfg.set('/', cfg.ConfigNode('add_node_ok')) result = cfg.get('/add_node_ok') print result cfg.root().delete('/add_node_ok') assert isinstance(result, dict)
def test_add_duplicate(): cfg.set('/', {'new_section': {'attr1': 'val1', 'attr2': 'val2'}}) cfg.set('/', {'new_section': {'attr3': 'val1', 'attr4': 'val2'}}) result = cfg.get('/new_section/attr1') cfg.root().delete('/new_section') assert result is None
def test_add_invalid_path(): cfg.set('/invalid_section/attribute', 'value')