def test_find_ancestors(self): c0 = cnode.CNode() c1 = cnode.CNode({'x': c0}) c2 = cnode.CNode({'y': c1}) nodes = view._find_ancestors(c2, c0) self.assertEqual(len(nodes), 2) self.assertIs(nodes[0], c2) self.assertIs(nodes[1], c1)
def test_read_configs_node(self): root1 = cnode.CNode({'x': 1}) root2 = cnode.CNode({'y': 2}) a = Configuration(root1) b = Configuration(root2) assert a is not b assert a.read('x') == 1 assert a['x'] == 1
def test_update_n(self): c = cnode.CNode({'x': 1}) c = cnode.CNode({'y': 2, 's': c}) self.assertEqual(c['y'], 2) c.update(cnode.QueryKey('y'), 3) self.assertEqual(c['y'], 3) self.assertEqual(c.read(['s', 'x']), 1) c = c.update('s', {'x': 2}) self.assertEqual(c.read(['s', 'x']), 2)
def test_write_configs(self): root1 = cnode.CNode({'x': 1}) a = Configuration(root1) a.write('x/y', 2) a.write('x/z', {'z1': 11, 'z2': 22}) a.write('x/y/d', cnode.CNode({'d1': 33})) assert a['x']['z']['z2'] == 22 assert a['x']['y'] is not 2 assert a['x']['y']['d']['d1'] == 33
def test_singleton(self): root1 = cnode.CNode({'x': 1}) root2 = cnode.CNode({'y': 2}) c1 = Configuration(root1) c2 = Configuration(root2) a = ConfigProxy(c1, 'root1') b = ConfigProxy(c2, 'root2') assert a is b assert b.get_root_node('root2')['y'] == 2
def test_write_configs_with_overwrite(self): root1 = cnode.CNode({'x': 1}) node1 = cnode.CNode({'z': 3}) node2 = cnode.CNode({'d': 4}) a = Configuration(root1) a.write('x/y', node2) a.write('x/y', node1, is_overwrite=False) assert a['x']['y']['d'] == 4 a.write('x/y', node1, is_overwrite=True) assert a['x']['y']['d'] is None
def test_get_root_node(self): root = cnode.CNode({'z': 3}) c = Configuration(root) ConfigProxy.reset() cp = ConfigProxy() root1 = cnode.CNode({'x': 1}) root2 = cnode.CNode({'y': 2}) c1 = Configuration(root1) c2 = Configuration(root2) cp.add_proxy(c1, 'root1') cp.add_proxy(c2, 'root2') cp.add_proxy(c, 'root3') cp.write('root1', 'x/x1', {'x11': 3, 'x12': 4}) assert cp.get_root_node('root3')['z'] == 3
def test_setitem(self): c = cnode.CNode() c.update('x', {'a': 1}) v = view.create_view(c, 'x') v['b'] = 2 assert v['a'] == 1 assert v['b'] == 2
def test_add_proxy(self): root = cnode.CNode({'z': 3}) c = Configuration(root) ConfigProxy.reset() cp = ConfigProxy() root1 = cnode.CNode({'x': 1}) root2 = cnode.CNode({'y': 2}) c1 = Configuration(root1) c2 = Configuration(root2) cp.add_proxy(c1, 'root1') cp.add_proxy(c2, 'root2') cp.add_proxy(c, 'root3') assert cp['root3']['z'] == 3 assert cp['root1']['x'] == 1 assert cp['root2']['y'] == 2
def test_normal_dict(self): c = cnode.CNode({'x': 1, 'y': 2}) assert len(c) == 2 assert c['x'] == 1 assert c['y'] == 2 assert tuple(c.keys()) == ('x', 'y') assert tuple(c.values()) == (1, 2) assert [v for k, v in c.items()] == [1, 2] assert [k for k, v in c.items()] == ['x', 'y']
def test_set_configs_by_dic_way(self): root = cnode.CNode({'z': 3}) c = Configuration(root) ConfigProxy.reset() cp = ConfigProxy() root1 = cnode.CNode({'x': 1}) root2 = cnode.CNode({'y': 2}) c1 = Configuration(root1) c2 = Configuration(root2) cp.add_proxy(c1, 'root1') cp.add_proxy(c2, 'root2') cp.add_proxy(c, 'root3') cp.write('root1', 'x/x1', {'x11': 3, 'x12': 4}) node = cnode.CNode({'w': 6}) c_new = Configuration(node) cp['root3'] = c_new assert cp['root3']['w'] == 6 assert cp['root3']['z'] is None
def test_cview_set(self): root = cnode.CNode({'x': {'a': 1, 'b': 2}}) c1 = Configuration(root) ConfigProxy.reset() c = ConfigProxy() c.add_proxy(c1, 'root1') cfg = c.get_view('root1', 'x') assert cfg['a'] == 1 cfg['b'] = 3 assert cfg['b'] == 3
def test_basic_dict(self): dct = { 'f1': { 'x': 0, 'y': 1, }, 'z': 2, } c = cnode.CNode(dct) self.assertEqual(c.read(cnode.QueryKey(['z'])), 2) self.assertEqual(c.read(cnode.QueryKey(['f1', 'x'])), 0)
def test_write_overwrite(self): root = cnode.CNode({'z': 3}) c = Configuration(root) ConfigProxy.reset() cp = ConfigProxy() root1 = cnode.CNode({'x': 1}) root2 = cnode.CNode({'y': 2}) c1 = Configuration(root1) c2 = Configuration(root2) cp.add_proxy(c1, 'root1') cp.add_proxy(c2, 'root2') cp.add_proxy(c, 'root3') cp.write('root1', 'x/x1', {'x11': 3, 'x12': 4}) node = cnode.CNode({'yy': 11}) node2 = cnode.CNode({'yyy': 22}) node3 = cnode.CNode({'yyyy': 33}) cp.write('root2', 'y', node, is_overwrite=False) cp.write('root2', 'y', node3, is_overwrite=False) assert cp['root2']['y']['yy'] == 11 assert cp['root2']['y']['yyyy'] == 33 cp.write('root2', 'y', node2, is_overwrite=True) assert cp['root2']['y']['yyy'] == 22 assert cp['root2']['y']['yy'] is None
def test_read_value_recursive(self): c_sub = cnode.CNode({'x': 1}) c = cnode.CNode({'y': 2, 's': c_sub}) self.assertEqual(c.read(cnode.QueryKey(['s', 'x'])), 1)
def test_update_self(self): c = cnode.CNode({'x': 1}) c.update([], {'x': 2}) self.assertEqual(c['x'], 2) c.update(None, {'x': 3}) self.assertEqual(c['x'], 3)
def test_basic_init(self): c = cnode.CNode() c.update('x', {'a': 1, 'b': 2}) self.assertEqual(c.read(['x', 'a']), 1)
def test_construct(self): c_sub = cnode.CNode({'x': 1, 'y': 2}) c = cnode.CNode({'sub': c_sub, 'z': 3})
def test_update_default_without_conflict(self): c = cnode.CNode() c.update('x', {'a': 1}) v = view.create_view(c, 'x') v.update_default('b', 3) assert v['b'] == 3
def test_fast_create(self): c = cnode.CNode() c.create('x', cnode.CNode({'y': 1})) self.assertEqual(c.read(cnode.QueryKey(['x', 'y'])), 1)
def test_update_v(self): c = cnode.CNode({'x': 1}) c = cnode.CNode({'y': 2, 's': c}) self.assertEqual(c['y'], 2) c.update(cnode.QueryKey('y'), 3) self.assertEqual(c['y'], 3)
def test_father(self): c = cnode.CNode({'x': 1, 'y': {'z': 2}}) c2 = c['y'] assert c2.father == c
def test_keys(self): c = cnode.CNode() c.update('x', {'a': 1, 'b': 2}) v = view.create_view(c, 'x') v.keys() assert False
def test_read_value_simple(self): c = cnode.CNode({'x': 1}) self.assertEqual(c.read(cnode.QueryKey(['x'])), 1)
def test_update_value_and_default_value(self): c = cnode.CNode() c.update('x', {'a': 1}) v = view.create_view(c, 'x') v.update_value_and_default('b', 1, 3) assert v['b'] == 1
def test_base_node(self): sub = cnode.CNode() c = cnode.CNode({'x': sub}) self.assertIs(view.base_node(c, 'x'), sub)
def test_update_default(self): c = cnode.CNode() c.update('x', {'a': 1}) v = view.create_view(c, 'x') v.update_default('a', 3) assert v['a'] == 1
def test_basic_view(self): c = cnode.CNode() c.update('x', {'a': 1, 'b': 2}) v = view.create_view(c, 'x') self.assertEqual(v.get('a'), 1)
def test_update_with_none_without_conflict(self): c = cnode.CNode() c.update('x', {'a': 1}) v = view.create_view(c, 'x') v.update('b', None) assert v['b'] is None
def test_fast_read(self): c = cnode.CNode({'x': 1}) c = cnode.CNode({'s': c, 'y': 2}) self.assertEqual(c.read(['s', 'x']), 1) self.assertEqual(c.read('y'), 2)