test_graph.py 1.5 KB

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