Ejemplo n.º 1
0
    def test_errors(self):
        # Check we can't connect nodes from different graphs
        a = self._graph_query(self.graph_a, ['A.A', ])[0]
        b = self._graph_query(self.graph_a, ['B.A', ])[0]
        
        with self.assertRaises(AttributeError):
            a.connect_child(b)

        # check we can't create a graph without a DataNode extender
        with self.assertRaises(AttributeError):
            DCCGraph.factory(object)
Ejemplo n.º 2
0
    def setUp(self):
        # create a bunch of test graphs

        # ---
        #    A.A
        #     |
        #    A.B
        self.graph_a = DCCGraph.factory(Label, name='A.A')
        self.graph_a.root.add_child(name='A.B')

        # verify
        self.assertEqual(Node.objects.filter(graph=self.graph_a).count(), 2)
        a, b = self._graph_query(self.graph_a, ['A.A', 'A.B'])
        self.assertEqual(a.parents.count(), 0)
        self.assertEqual(list(a.children.all()), [b])
        self.assertEqual(list(b.parents.all()), [a])
        self.assertEqual(b.children.count(), 0)

        # ---
        #      B.A
        #     /   \
        #   B.B   B.C
        #           \
        #           B.A (cycle)
        self.graph_b = DCCGraph.factory(Label, name='B.A')
        self.graph_b.root.add_child(name='B.B')
        node = self.graph_b.root.add_child(name='B.C')
        node.connect_child(self.graph_b.root)

        # verify
        self.assertEqual(Node.objects.filter(graph=self.graph_b).count(), 3)
        a,b,c = self._graph_query(self.graph_b, ['B.A', 'B.B', 'B.C'])

        self.assertEqual(a, self.graph_b.root)
        self.assertEqual(list(a.parents.all()), [c])
        self.assertEqual(set(a.children.all()), set([b, c]))
        self.assertEqual(list(b.parents.all()), [a])
        self.assertEqual(b.children.count(), 0)
        self.assertEqual(list(c.parents.all()), [a])
        self.assertEqual(list(c.children.all()), [a])

        # ---
        #           C.A
        #          /   \
        #        C.B   C.C
        #             /   \
        #            /     \
        #           /       \
        #          /         \
        #         /           \
        #       C.D           C.E
        #      /   \            \
        #    C.G   C.H          C.F
        #    /       \             \
        #  C.I        C.A (cycle)  C.A (cycle)
        #    \
        #    C.H (loop back)

        self.graph_c = DCCGraph.factory_from_graph(Label, 
            {'name':'C.A'}, [
                ({'name':'C.B'}, []),
                ({'name':'C.C'}, [
                    ({'name':'C.D'}, [
                        ({'name':'C.G'}, [
                            ({'name':'C.I'}, []),
                        ]),
                        ({'name':'C.H'}, []),
                    ]),
                    ({'name':'C.E'}, [
                        ({'name':'C.F'}, []),
                    ]),
                ]),
            ])
        i = self.graph_c.find_nodes(name='C.I').first()
        h = self.graph_c.find_nodes(name='C.H').first()
        i.connect_child(h)
        h.connect_child(self.graph_c.root)
        f = self.graph_c.find_nodes(name='C.F').first()
        f.connect_child(self.graph_c.root)

        # verify
        self.assertEqual(Node.objects.filter(graph=self.graph_c).count(), 9)
        a,b,c,d,e,f,g,h,i = self._graph_query(self.graph_c, ['C.A', 'C.B',
            'C.C', 'C.D', 'C.E', 'C.F', 'C.G', 'C.H', 'C.I'])

        self.assertEqual(a, self.graph_c.root)
        self.assertEqual(set(a.parents.all()), set([f, h]))
        self.assertEqual(set(a.children.all()), set([b, c]))
        self.assertEqual(list(b.parents.all()), [a])
        self.assertEqual(b.children.count(), 0)

        self.assertEqual(list(c.parents.all()), [a])
        self.assertEqual(set(c.children.all()), set([d, e]))

        self.assertEqual(list(d.parents.all()), [c])
        self.assertEqual(set(d.children.all()), set([g, h]))

        self.assertEqual(list(e.parents.all()), [c])
        self.assertEqual(set(e.children.all()), set([f]))

        self.assertEqual(list(f.parents.all()), [e])
        self.assertEqual(set(f.children.all()), set([a]))

        self.assertEqual(list(g.parents.all()), [d])
        self.assertEqual(set(g.children.all()), set([i]))

        self.assertEqual(list(h.parents.all()), [d, i])
        self.assertEqual(set(h.children.all()), set([a]))

        self.assertEqual(list(i.parents.all()), [g])
        self.assertEqual(set(i.children.all()), set([h]))