test_graph.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. return DependencyGraph([
  8. ('A', []),
  9. ('B', []),
  10. ('C', ['A']),
  11. ('D', ['C', 'B']),
  12. ])
  13. def test_repr(self):
  14. assert repr(self.graph1())
  15. def test_topsort(self):
  16. order = self.graph1().topsort()
  17. # C must start before D
  18. assert order.index('C') < order.index('D')
  19. # and B must start before D
  20. assert order.index('B') < order.index('D')
  21. # and A must start before C
  22. assert order.index('A') < order.index('C')
  23. def test_edges(self):
  24. assert sorted(list(self.graph1().edges())) == ['C', 'D']
  25. def test_connect(self):
  26. x, y = self.graph1(), self.graph1()
  27. x.connect(y)
  28. def test_valency_of_when_missing(self):
  29. x = self.graph1()
  30. assert x.valency_of('foobarbaz') == 0
  31. def test_format(self):
  32. x = self.graph1()
  33. x.formatter = Mock()
  34. obj = Mock()
  35. assert x.format(obj)
  36. x.formatter.assert_called_with(obj)
  37. x.formatter = None
  38. assert x.format(obj) is obj
  39. def test_items(self):
  40. assert dict(items(self.graph1())) == {
  41. 'A': [], 'B': [], 'C': ['A'], 'D': ['C', 'B'],
  42. }
  43. def test_repr_node(self):
  44. x = self.graph1()
  45. assert x.repr_node('fasdswewqewq')
  46. def test_to_dot(self):
  47. s = WhateverIO()
  48. self.graph1().to_dot(s)
  49. assert s.getvalue()