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_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_children_as_list(self): # Serialize list of nodes node = OrderedNode(name='base') node['child_1'] = OrderedNode() node['child_2'] = OrderedNode() self.assertEqual(node.treerepr(), ( '<class \'node.base.OrderedNode\'>: base\n' ' <class \'node.base.OrderedNode\'>: child_1\n' ' <class \'node.base.OrderedNode\'>: child_2\n' )) json_data = serialize(node.values()) data = json.loads(json_data) self.assertTrue(isinstance(data, list)) self.assertEqual(len(data), 2) node_data = data[0]['__node__'] self.assertEqual(list(sorted(node_data.keys())), ['class', 'name']) self.assertEqual(node_data['class'], 'node.base.OrderedNode') self.assertEqual(node_data['name'], 'child_1') node_data = data[1]['__node__'] self.assertEqual(list(sorted(node_data.keys())), ['class', 'name']) self.assertEqual(node_data['class'], 'node.base.OrderedNode') self.assertEqual(node_data['name'], 'child_2') # Deserialize list of nodes using given root node root = OrderedNode(name='root') nodes = deserialize(json_data, root=root) self.assertEqual(len(nodes), 2) self.check_output("""\ [<OrderedNode object 'child_1' at ...>, <OrderedNode object 'child_2' at ...>] """, str(nodes)) self.assertEqual(root.treerepr(), ( '<class \'node.base.OrderedNode\'>: root\n' ' <class \'node.base.OrderedNode\'>: child_1\n' ' <class \'node.base.OrderedNode\'>: child_2\n' ))
def test_children_as_list(self): # Serialize list of nodes node = OrderedNode(name='base') node['child_1'] = OrderedNode() node['child_2'] = OrderedNode() self.assertEqual(node.treerepr(), ('<class \'node.base.OrderedNode\'>: base\n' ' <class \'node.base.OrderedNode\'>: child_1\n' ' <class \'node.base.OrderedNode\'>: child_2\n')) json_data = serialize(node.values()) data = json.loads(json_data) self.assertTrue(isinstance(data, list)) self.assertEqual(len(data), 2) node_data = data[0]['__node__'] self.assertEqual(list(sorted(node_data.keys())), ['class', 'name']) self.assertEqual(node_data['class'], 'node.base.OrderedNode') self.assertEqual(node_data['name'], 'child_1') node_data = data[1]['__node__'] self.assertEqual(list(sorted(node_data.keys())), ['class', 'name']) self.assertEqual(node_data['class'], 'node.base.OrderedNode') self.assertEqual(node_data['name'], 'child_2') # Deserialize list of nodes using given root node root = OrderedNode(name='root') nodes = deserialize(json_data, root=root) self.assertEqual(len(nodes), 2) self.check_output( """\ [<OrderedNode object 'child_1' at ...>, <OrderedNode object 'child_2' at ...>] """, str(nodes)) self.assertEqual(root.treerepr(), ('<class \'node.base.OrderedNode\'>: root\n' ' <class \'node.base.OrderedNode\'>: child_1\n' ' <class \'node.base.OrderedNode\'>: child_2\n'))
def test_OrderedNode(self): self.assertEqual(OrderedNode.__bases__, (object, )) orderednode = OrderedNode() self.assertTrue( str(orderednode).startswith('<OrderedNode object \'None\' at')) fmtester = FullMappingTester(OrderedNode) 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) orderednode['child'] = OrderedNode() self.checkOutput( """\ <class 'node.base.OrderedNode'>: None __<class 'node.base.OrderedNode'>: child """, orderednode.treerepr(prefix='_')) unpickled = pickle.loads(pickle.dumps(orderednode)) self.checkOutput( """\ <class 'node.base.OrderedNode'>: None __<class 'node.base.OrderedNode'>: child """, unpickled.treerepr(prefix='_'))
def test_OrderedNode(self): self.assertEqual(OrderedNode.__bases__, (object,)) orderednode = OrderedNode() self.assertTrue( str(orderednode).startswith('<OrderedNode object \'None\' at') ) fmtester = FullMappingTester(OrderedNode) fmtester.run() self.check_output("""\ ``__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) orderednode['child'] = OrderedNode() self.check_output("""\ <class 'node.base.OrderedNode'>: None <class 'node.base.OrderedNode'>: child """, orderednode.treerepr()) unpickled = pickle.loads(pickle.dumps(orderednode)) self.check_output("""\ <class 'node.base.OrderedNode'>: None <class 'node.base.OrderedNode'>: child """, unpickled.treerepr())