test_annotations.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. from celery.app.annotations import MapAnnotation, prepare
  2. from celery.utils.imports import qualname
  3. class MyAnnotation:
  4. foo = 65
  5. class AnnotationCase:
  6. def setup(self):
  7. @self.app.task(shared=False)
  8. def add(x, y):
  9. return x + y
  10. self.add = add
  11. @self.app.task(shared=False)
  12. def mul(x, y):
  13. return x * y
  14. self.mul = mul
  15. class test_MapAnnotation(AnnotationCase):
  16. def test_annotate(self):
  17. x = MapAnnotation({self.add.name: {'foo': 1}})
  18. assert x.annotate(self.add) == {'foo': 1}
  19. assert x.annotate(self.mul) is None
  20. def test_annotate_any(self):
  21. x = MapAnnotation({'*': {'foo': 2}})
  22. assert x.annotate_any() == {'foo': 2}
  23. x = MapAnnotation()
  24. assert x.annotate_any() is None
  25. class test_prepare(AnnotationCase):
  26. def test_dict_to_MapAnnotation(self):
  27. x = prepare({self.add.name: {'foo': 3}})
  28. assert isinstance(x[0], MapAnnotation)
  29. def test_returns_list(self):
  30. assert prepare(1) == [1]
  31. assert prepare([1]) == [1]
  32. assert prepare((1,)) == [1]
  33. assert prepare(None) == ()
  34. def test_evalutes_qualnames(self):
  35. assert prepare(qualname(MyAnnotation))[0]().foo == 65
  36. assert prepare([qualname(MyAnnotation)])[0]().foo == 65