Beispiel #1
0
    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'
        ))
Beispiel #2
0
    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'))