def test_decode(self): self.assertEqual(decode(b'foo'), u'foo') self.assertEqual(decode((b'foo', u'bar')), (u'foo', u'bar')) self.assertEqual(decode({b'foo': b'bar'}), {u'foo': u'bar'}) self.assertEqual(decode(b'fo\xe4'), b'fo\xe4') node = BaseNode() node.allow_non_node_childs = True node[b'foo'] = b'\xc3\xa4' self.assertEqual(decode(node), {u'foo': u'\xe4'})
def test_children(self): # Node children serializition node = OrderedNode(name='base') node['child'] = OrderedNode() node['child']['sub'] = OrderedNode() self.assertEqual(node.treerepr(), ( '<class \'node.base.OrderedNode\'>: base\n' ' <class \'node.base.OrderedNode\'>: child\n' ' <class \'node.base.OrderedNode\'>: sub\n' )) json_data = serialize(node) data = json.loads(json_data) self.assertEqual(list(data.keys()), ['__node__']) node_data = data['__node__'] self.assertEqual( list(sorted(node_data.keys())), ['children', 'class', 'name'] ) self.assertEqual(node_data['class'], 'node.base.OrderedNode') self.assertEqual(node_data['name'], 'base') self.assertEqual(len(node_data['children']), 1) node_data = node_data['children'][0]['__node__'] self.assertEqual(node_data['class'], 'node.base.OrderedNode') self.assertEqual(node_data['name'], 'child') self.assertEqual(len(node_data['children']), 1) node_data = node_data['children'][0]['__node__'] self.assertEqual(list(sorted(node_data.keys())), ['class', 'name']) self.assertEqual(node_data['class'], 'node.base.OrderedNode') self.assertEqual(node_data['name'], 'sub') node = deserialize(json_data) self.assertEqual(node.treerepr(), ( '<class \'node.base.OrderedNode\'>: base\n' ' <class \'node.base.OrderedNode\'>: child\n' ' <class \'node.base.OrderedNode\'>: sub\n' )) # Deserialize using given root node root = BaseNode(name='root') node = deserialize(json_data, root=root) self.assertEqual(root.treerepr(), ( '<class \'node.base.BaseNode\'>: root\n' ' <class \'node.base.OrderedNode\'>: base\n' ' <class \'node.base.OrderedNode\'>: child\n' ' <class \'node.base.OrderedNode\'>: sub\n' ))
def test_BaseNode(self): self.assertEqual(BaseNode.__bases__, (object, )) basenode = BaseNode() self.assertTrue( str(basenode).startswith('<BaseNode object \'None\' at')) fmtester = FullMappingTester(BaseNode) fmtester.run() self.checkOutput( """\ ``__contains__``: OK ``__delitem__``: OK ``__getitem__``: OK ``__iter__``: OK ``__len__``: OK ``__setitem__``: OK ``clear``: OK ``copy``: OK ``get``: OK ``has_key``: OK ``items``: OK ``iteritems``: OK ``iterkeys``: OK ``itervalues``: OK ``keys``: OK ``pop``: OK ``popitem``: OK ``setdefault``: OK ``update``: OK ``values``: OK """, fmtester.combined)
def test_children(self): # Node children serializition node = OrderedNode(name='base') node['child'] = OrderedNode() node['child']['sub'] = OrderedNode() self.assertEqual(node.treerepr(), ('<class \'node.base.OrderedNode\'>: base\n' ' <class \'node.base.OrderedNode\'>: child\n' ' <class \'node.base.OrderedNode\'>: sub\n')) json_data = serialize(node) data = json.loads(json_data) self.assertEqual(list(data.keys()), ['__node__']) node_data = data['__node__'] self.assertEqual(list(sorted(node_data.keys())), ['children', 'class', 'name']) self.assertEqual(node_data['class'], 'node.base.OrderedNode') self.assertEqual(node_data['name'], 'base') self.assertEqual(len(node_data['children']), 1) node_data = node_data['children'][0]['__node__'] self.assertEqual(node_data['class'], 'node.base.OrderedNode') self.assertEqual(node_data['name'], 'child') self.assertEqual(len(node_data['children']), 1) node_data = node_data['children'][0]['__node__'] self.assertEqual(list(sorted(node_data.keys())), ['class', 'name']) self.assertEqual(node_data['class'], 'node.base.OrderedNode') self.assertEqual(node_data['name'], 'sub') node = deserialize(json_data) self.assertEqual(node.treerepr(), ('<class \'node.base.OrderedNode\'>: base\n' ' <class \'node.base.OrderedNode\'>: child\n' ' <class \'node.base.OrderedNode\'>: sub\n')) # Deserialize using given root node root = BaseNode(name='root') node = deserialize(json_data, root=root) self.assertEqual(root.treerepr(), ('<class \'node.base.BaseNode\'>: root\n' ' <class \'node.base.OrderedNode\'>: base\n' ' <class \'node.base.OrderedNode\'>: child\n' ' <class \'node.base.OrderedNode\'>: sub\n'))
def test_TreeLock(self): node = BaseNode() lock = locking.TreeLock(node) self.assertEqual(lock.lock.count, 0) lock.acquire() self.assertEqual(lock.lock.count, 1) lock.acquire() self.assertEqual(lock.lock.count, 2) lock.release() self.assertEqual(lock.lock.count, 1) lock.release() self.assertEqual(lock.lock.count, 0)
def test_INode(self): # Basic ``INode`` implementing object serialization json_data = serialize(BaseNode()) data = json.loads(json_data) self.assertEqual(list(data.keys()), ['__node__']) node_data = data['__node__'] self.assertEqual(list(sorted(node_data.keys())), ['class', 'name']) self.assertEqual(node_data['class'], 'node.base.BaseNode') self.assertEqual(node_data['name'], None) node = deserialize(json_data) self.assertTrue(str(node).startswith('<BaseNode object \'None\' at'))
def test_encode(self): self.assertEqual( encode( b'\x01\x05\x00\x00\x00\x00\x00\x05\x15\x00\x00\x00\xd4' b'\xa0\xff\xff\xaeW\x82\xa9P\xcf8\xaf&\x0e\x00\x00' ), ( b'\x01\x05\x00\x00\x00\x00\x00\x05\x15\x00\x00\x00\xd4' b'\xa0\xff\xff\xaeW\x82\xa9P\xcf8\xaf&\x0e\x00\x00' ) ) self.assertEqual(encode(u'\xe4'), b'\xc3\xa4') self.assertEqual(encode([u'\xe4']), [b'\xc3\xa4']) self.assertEqual( encode({u'\xe4': u'\xe4'}), {b'\xc3\xa4': b'\xc3\xa4'} ) self.assertEqual(encode(b'\xc3\xa4'), b'\xc3\xa4') node = BaseNode() node.allow_non_node_childs = True node['foo'] = u'\xe4' self.assertEqual(encode(node), {b'foo': b'\xc3\xa4'})
def test_node_by_path(self): root = BaseNode(name='root') child = root['child'] = BaseNode() sub = child['sub'] = BaseNode() self.assertEqual(node_by_path(root, ''), root) self.assertEqual(node_by_path(root, '/'), root) self.assertEqual(node_by_path(root, []), root) self.assertEqual(node_by_path(root, 'child'), child) self.assertEqual(node_by_path(root, '/child'), child) self.assertEqual(node_by_path(root, 'child/sub'), sub) self.assertEqual(node_by_path(root, ['child']), child) self.assertEqual(node_by_path(root, ['child', 'sub']), sub) class CustomPathIterator(object): def __iter__(self): yield 'child' yield 'sub' self.assertEqual(node_by_path(root, CustomPathIterator()), sub)
def test_simplified(self): # Serialize node trees without type information. Such data is not # deserializable by default deserializer. Supposed to be used for # domain specific (Browser-) applications dealing with node data node = BaseNode(name='base') child = node['child'] = AttributedNode() child.attrs['foo'] = 'Foo' child.attrs['ref'] = AbstractNode # If all nodes are the same type, call ``serialize`` with # ``simple_mode=True`` json_data = serialize(node, simple_mode=True) data = json.loads(json_data) self.assertEqual(sorted(list(data.keys())), ['children', 'name']) self.assertEqual(data['name'], 'base') self.assertEqual(len(data['children']), 1) child_data = data['children'][0] self.assertEqual(sorted(list(child_data.keys())), ['attrs', 'name']) self.assertEqual(child_data['name'], 'child') child_attrs = child_data['attrs'] self.assertEqual(sorted(list(child_attrs.keys())), ['foo', 'ref']) self.assertEqual(child_attrs['foo'], 'Foo') self.assertEqual(child_attrs['ref'], 'node.base.AbstractNode') # If nodes are different types and you do not care about exposing the # class name, pass ``include_class=True`` to ``serialize`` json_data = serialize(node, simple_mode=True, include_class=True) data = json.loads(json_data) self.assertEqual(sorted(list(data.keys())), ['children', 'class', 'name']) self.assertEqual(data['class'], 'node.base.BaseNode') self.assertEqual(data['name'], 'base') self.assertEqual(len(data['children']), 1) child_data = data['children'][0] self.assertEqual(sorted(list(child_data.keys())), ['attrs', 'class', 'name']) self.assertEqual(child_data['class'], 'node.base.AttributedNode') self.assertEqual(child_data['name'], 'child') child_attrs = child_data['attrs'] self.assertEqual(sorted(list(child_attrs.keys())), ['foo', 'ref']) self.assertEqual(child_attrs['foo'], 'Foo') self.assertEqual(child_attrs['ref'], 'node.base.AbstractNode')
def test_with_TreeLock(self): node = BaseNode() lock = locking.TreeLock(node) with lock: self.assertEqual(lock.lock.count, 1) self.assertEqual(lock.lock.count, 0)
def test_copy(self): node = BaseNode() node['child'] = BaseNode() # Shallow copy of BaseNode copied_node = node.copy() self.check_output("""\ <class 'node.base.BaseNode'>: None <class 'node.base.BaseNode'>: child """, copied_node.treerepr()) self.assertFalse(node is copied_node) self.assertTrue(node['child'] is copied_node['child']) copied_node = copy.copy(node) self.assertFalse(node is copied_node) self.assertTrue(node['child'] is copied_node['child']) # Deep copy of base node copied_node = node.deepcopy() self.check_output("""\ <class 'node.base.BaseNode'>: None <class 'node.base.BaseNode'>: child """, copied_node.treerepr()) self.assertFalse(node is copied_node) self.assertFalse(node['child'] is copied_node['child']) copied_node = copy.deepcopy(node) self.assertFalse(node is copied_node) self.assertFalse(node['child'] is copied_node['child']) # Shallow copy of ordered node node = OrderedNode() node['child'] = OrderedNode() copied_node = node.copy() self.check_output("""\ <class 'node.base.OrderedNode'>: None <class 'node.base.OrderedNode'>: child """, copied_node.treerepr()) self.assertFalse(node is copied_node) self.assertTrue(node['child'] is copied_node['child']) copied_node = copy.copy(node) self.assertFalse(node is copied_node) self.assertTrue(node['child'] is copied_node['child']) # Deep copy of ordered node node = OrderedNode() node['child'] = OrderedNode() copied_node = node.deepcopy() self.check_output("""\ <class 'node.base.OrderedNode'>: None <class 'node.base.OrderedNode'>: child """, copied_node.treerepr()) self.assertFalse(node is copied_node) self.assertFalse(node['child'] is copied_node['child']) copied_node = copy.deepcopy(node) self.assertFalse(node is copied_node) self.assertFalse(node['child'] is copied_node['child'])
def test_copy(self): node = BaseNode() node['child'] = BaseNode() # Shallow copy of BaseNode copied_node = node.copy() self.checkOutput( """\ <class 'node.base.BaseNode'>: None __<class 'node.base.BaseNode'>: child """, copied_node.treerepr(prefix='_')) self.assertFalse(node is copied_node) self.assertTrue(node['child'] is copied_node['child']) copied_node = copy.copy(node) self.assertFalse(node is copied_node) self.assertTrue(node['child'] is copied_node['child']) # Deep copy of base node copied_node = node.deepcopy() self.checkOutput( """\ <class 'node.base.BaseNode'>: None __<class 'node.base.BaseNode'>: child """, copied_node.treerepr(prefix='_')) self.assertFalse(node is copied_node) self.assertFalse(node['child'] is copied_node['child']) copied_node = copy.deepcopy(node) self.assertFalse(node is copied_node) self.assertFalse(node['child'] is copied_node['child']) # Shallow copy of ordered node node = OrderedNode() node['child'] = OrderedNode() copied_node = node.copy() self.checkOutput( """\ <class 'node.base.OrderedNode'>: None __<class 'node.base.OrderedNode'>: child """, copied_node.treerepr(prefix='_')) self.assertFalse(node is copied_node) self.assertTrue(node['child'] is copied_node['child']) copied_node = copy.copy(node) self.assertFalse(node is copied_node) self.assertTrue(node['child'] is copied_node['child']) # Deep copy of ordered node node = OrderedNode() node['child'] = OrderedNode() copied_node = node.deepcopy() self.checkOutput( """\ <class 'node.base.OrderedNode'>: None __<class 'node.base.OrderedNode'>: child """, copied_node.treerepr(prefix='_')) self.assertFalse(node is copied_node) self.assertFalse(node['child'] is copied_node['child']) copied_node = copy.deepcopy(node) self.assertFalse(node is copied_node) self.assertFalse(node['child'] is copied_node['child'])
def test_INode(self): # XXX: make tester object for INode contract # XXX: decide wether ``aliases`` or ``aliaser`` (still dunno) should be # kept in base interface. # printtree mynode = create_tree(MyNode) self.checkOutput( """\ <class 'node.testing.env.MyNode'>: None __<class 'node.testing.env.MyNode'>: child_0 ____<class 'node.testing.env.MyNode'>: subchild_0 ____<class 'node.testing.env.MyNode'>: subchild_1 __<class 'node.testing.env.MyNode'>: child_1 ____<class 'node.testing.env.MyNode'>: subchild_0 ____<class 'node.testing.env.MyNode'>: subchild_1 __<class 'node.testing.env.MyNode'>: child_2 ____<class 'node.testing.env.MyNode'>: subchild_0 ____<class 'node.testing.env.MyNode'>: subchild_1 """, mynode.treerepr(prefix='_')) basenode = create_tree(BaseNode) self.checkOutput( """\ <class 'node.base.BaseNode'>: None __<class 'node.base.BaseNode'>: child_... ____<class 'node.base.BaseNode'>: subchild_... ____<class 'node.base.BaseNode'>: subchild_... __<class 'node.base.BaseNode'>: child_... ____<class 'node.base.BaseNode'>: subchild_... ____<class 'node.base.BaseNode'>: subchild_... __<class 'node.base.BaseNode'>: child_... ____<class 'node.base.BaseNode'>: subchild_... ____<class 'node.base.BaseNode'>: subchild_... """, basenode.treerepr(prefix='_')) orderednode = create_tree(OrderedNode) self.checkOutput( """\ <class 'node.base.OrderedNode'>: None __<class 'node.base.OrderedNode'>: child_0 ____<class 'node.base.OrderedNode'>: subchild_0 ____<class 'node.base.OrderedNode'>: subchild_1 __<class 'node.base.OrderedNode'>: child_1 ____<class 'node.base.OrderedNode'>: subchild_0 ____<class 'node.base.OrderedNode'>: subchild_1 __<class 'node.base.OrderedNode'>: child_2 ____<class 'node.base.OrderedNode'>: subchild_0 ____<class 'node.base.OrderedNode'>: subchild_1 """, orderednode.treerepr(prefix='_')) # path mynode.__name__ = 'root' self.assertEqual(mynode.path, ['root']) self.assertEqual(mynode['child_1']['subchild_1'].path, ['root', 'child_1', 'subchild_1']) basenode.__name__ = 'root' self.assertEqual(basenode.path, ['root']) self.assertEqual(basenode['child_1']['subchild_1'].path, ['root', 'child_1', 'subchild_1']) orderednode.__name__ = 'root' self.assertEqual(orderednode.path, ['root']) self.assertEqual(orderednode['child_1']['subchild_1'].path, ['root', 'child_1', 'subchild_1']) # root self.assertTrue(mynode['child_1']['subchild_1'].root is mynode) self.assertTrue(basenode['child_1']['subchild_1'].root is basenode) self.assertTrue( orderednode['child_1']['subchild_1'].root is orderednode) # allow_non_node_childs self.assertFalse(mynode.allow_non_node_childs) def non_node_childs_not_allowed(): mynode['foo'] = object() err = self.expectError(ValueError, non_node_childs_not_allowed) self.assertEqual(str(err), 'Non-node childs are not allowed.') def no_classes_as_values_allowed(): mynode['foo'] = object err = self.expectError(ValueError, no_classes_as_values_allowed) expected = 'It isn\'t allowed to use classes as values.' self.assertEqual(str(err), expected) mynode.allow_non_node_childs = True obj = mynode['foo'] = object() self.assertEqual(mynode['foo'], obj) del mynode['foo'] mynode.allow_non_node_childs = False self.assertFalse(basenode.allow_non_node_childs) def non_node_childs_not_allowed2(): basenode['foo'] = object() err = self.expectError(ValueError, non_node_childs_not_allowed2) self.assertEqual(str(err), 'Non-node childs are not allowed.') def no_classes_as_values_allowed2(): basenode['foo'] = object err = self.expectError(ValueError, no_classes_as_values_allowed2) expected = 'It isn\'t allowed to use classes as values.' self.assertEqual(str(err), expected) basenode.allow_non_node_childs = True obj = basenode['foo'] = object() self.assertEqual(basenode['foo'], obj) del basenode['foo'] basenode.allow_non_node_childs = False self.assertFalse(orderednode.allow_non_node_childs) def non_node_childs_not_allowed3(): orderednode['foo'] = object() err = self.expectError(ValueError, non_node_childs_not_allowed3) self.assertEqual(str(err), 'Non-node childs are not allowed.') def no_classes_as_values_allowed3(): orderednode['foo'] = object err = self.expectError(ValueError, no_classes_as_values_allowed3) expected = 'It isn\'t allowed to use classes as values.' self.assertEqual(str(err), expected) orderednode.allow_non_node_childs = True obj = orderednode['foo'] = object() self.assertEqual(orderednode['foo'], obj) del orderednode['foo'] orderednode.allow_non_node_childs = False # filteredvalues and filtereditervalues class IFilter(Interface): pass directlyProvides(mynode['child_2'], IFilter) self.assertEqual(list(mynode.filtereditervalues(IFilter)), [mynode['child_2']]) self.assertEqual(mynode.filteredvalues(IFilter), [mynode['child_2']]) noLongerProvides(mynode['child_2'], IFilter) self.assertEqual(list(mynode.filtereditervalues(IFilter)), []) self.assertEqual(mynode.filteredvalues(IFilter), []) directlyProvides(basenode['child_2'], IFilter) self.assertEqual(list(basenode.filtereditervalues(IFilter)), [basenode['child_2']]) self.assertEqual(basenode.filteredvalues(IFilter), [basenode['child_2']]) noLongerProvides(basenode['child_2'], IFilter) self.assertEqual(list(basenode.filtereditervalues(IFilter)), []) self.assertEqual(basenode.filteredvalues(IFilter), []) directlyProvides(orderednode['child_2'], IFilter) self.assertEqual(list(orderednode.filtereditervalues(IFilter)), [orderednode['child_2']]) self.assertEqual(orderednode.filteredvalues(IFilter), [orderednode['child_2']]) noLongerProvides(orderednode['child_2'], IFilter) self.assertEqual(list(orderednode.filtereditervalues(IFilter)), []) self.assertEqual(orderednode.filteredvalues(IFilter), []) # as_attribute_access myattrs = mynode.as_attribute_access() self.assertEqual(myattrs.__class__, AttributeAccess) self.assertEqual(myattrs.child_1, mynode['child_1']) myattrs.child_3 = MyNode() self.assertEqual(mynode['child_3'], myattrs.child_3) def no_classes_as_values_allowed4(): myattrs.child_4 = object err = self.expectError(ValueError, no_classes_as_values_allowed4) expected = 'It isn\'t allowed to use classes as values.' self.assertEqual(str(err), expected) baseattrs = basenode.as_attribute_access() self.assertEqual(baseattrs.__class__, AttributeAccess) self.assertEqual(baseattrs.child_1, basenode['child_1']) baseattrs.child_3 = BaseNode() self.assertEqual(basenode['child_3'], baseattrs.child_3) def no_classes_as_values_allowed5(): baseattrs.child_4 = object err = self.expectError(ValueError, no_classes_as_values_allowed5) expected = 'It isn\'t allowed to use classes as values.' self.assertEqual(str(err), expected) orderedattrs = orderednode.as_attribute_access() self.assertEqual(orderedattrs.__class__, AttributeAccess) self.assertEqual(orderedattrs.child_1, orderednode['child_1']) orderedattrs.child_3 = OrderedNode() self.assertEqual(orderednode['child_3'], orderedattrs.child_3) def no_classes_as_values_allowed6(): orderedattrs.child_4 = object err = self.expectError(ValueError, no_classes_as_values_allowed6) expected = 'It isn\'t allowed to use classes as values.' self.assertEqual(str(err), expected)