test_graph.py 1.9 KB

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