Browse Source

Correctly building graphs with `GroupResult`. (#4793)

* Implement `__str__` and `__hash__` methods for `GroupResult`. Update `__eq__` method to treat a uuid string as equivalent like in `AsyncResult` class

* Update graph unit test to use proper `AsyncResult` and `GroupResult` classes
Josue Balandrano Coronel 6 years ago
parent
commit
56d6cbb0e2
2 changed files with 28 additions and 7 deletions
  1. 14 2
      celery/result.py
  2. 14 5
      t/unit/utils/test_graph.py

+ 14 - 2
celery/result.py

@@ -907,6 +907,8 @@ class GroupResult(ResultSet):
                 other.results == self.results and
                 other.parent == self.parent
             )
+        elif isinstance(other, string_t):
+            return other == self.id
         return NotImplemented
 
     def __ne__(self, other):
@@ -914,8 +916,18 @@ class GroupResult(ResultSet):
         return True if res is NotImplemented else not res
 
     def __repr__(self):
-        return '<{0}: {1} [{2}]>'.format(type(self).__name__, self.id,
-                                         ', '.join(r.id for r in self.results))
+        return '<{0}: {1} [{2}]>'.format(
+            type(self).__name__, self.id,
+            ', '.join(r.id for r in self.results)
+        )
+
+    def __str__(self):
+        """`str(self) -> self.id`."""
+        return str(self.id)
+
+    def __hash__(self):
+        """`hash(self) -> hash(self.id)`."""
+        return hash(self.id)
 
     def as_tuple(self):
         return (

+ 14 - 5
t/unit/utils/test_graph.py

@@ -9,11 +9,19 @@ from celery.utils.graph import DependencyGraph
 class test_DependencyGraph:
 
     def graph1(self):
+        res_a = self.app.AsyncResult('A')
+        res_b = self.app.AsyncResult('B')
+        res_c = self.app.GroupResult('C', [res_a])
+        res_d = self.app.GroupResult('D', [res_c, res_b])
+        node_a = (res_a, [])
+        node_b = (res_b, [])
+        node_c = (res_c, [res_a])
+        node_d = (res_d, [res_c, res_b])
         return DependencyGraph([
-            ('A', []),
-            ('B', []),
-            ('C', ['A']),
-            ('D', ['C', 'B']),
+            node_a,
+            node_b,
+            node_c,
+            node_d,
         ])
 
     def test_repr(self):
@@ -29,7 +37,8 @@ class test_DependencyGraph:
         assert order.index('A') < order.index('C')
 
     def test_edges(self):
-        assert sorted(list(self.graph1().edges())) == ['C', 'D']
+        edges = self.graph1().edges()
+        assert sorted(edges, key=str) == ['C', 'D']
 
     def test_connect(self):
         x, y = self.graph1(), self.graph1()