Debian Codegraph @

t/unit/utils/test_graph.py

raw
1 from unittest.mock import Mock
2
3 from celery.utils.graph import DependencyGraph
4 from celery.utils.text import WhateverIO
5
6
7 class test_DependencyGraph:
8
9 def graph1(self):
10 res_a = self.app.AsyncResult('A')
11 res_b = self.app.AsyncResult('B')
12 res_c = self.app.GroupResult('C', [res_a])
13 res_d = self.app.GroupResult('D', [res_c, res_b])
14 node_a = (res_a, [])
15 node_b = (res_b, [])
16 node_c = (res_c, [res_a])
17 node_d = (res_d, [res_c, res_b])
18 return DependencyGraph([
19 node_a,
20 node_b,
21 node_c,
22 node_d,
23 ])
24
25 def test_repr(self):
26 assert repr(self.graph1())
27
28 def test_topsort(self):
29 order = self.graph1().topsort()
30 # C must start before D
31 assert order.index('C') < order.index('D')
32 # and B must start before D
33 assert order.index('B') < order.index('D')
34 # and A must start before C
35 assert order.index('A') < order.index('C')
36
37 def test_edges(self):
38 edges = self.graph1().edges()
39 assert sorted(edges, key=str) == ['C', 'D']
40
41 def test_connect(self):
42 x, y = self.graph1(), self.graph1()
43 x.connect(y)
44
45 def test_valency_of_when_missing(self):
46 x = self.graph1()
47 assert x.valency_of('foobarbaz') == 0
48
49 def test_format(self):
50 x = self.graph1()
51 x.formatter = Mock()
52 obj = Mock()
53 assert x.format(obj)
54 x.formatter.assert_called_with(obj)
55 x.formatter = None
56 assert x.format(obj) is obj
57
58 def test_items(self):
59 assert dict(self.graph1().items()) == {
60 'A': [], 'B': [], 'C': ['A'], 'D': ['C', 'B'],
61 }
62
63 def test_repr_node(self):
64 x = self.graph1()
65 assert x.repr_node('fasdswewqewq')
66
67 def test_to_dot(self):
68 s = WhateverIO()
69 self.graph1().to_dot(s)
70 assert s.getvalue()