test_annotations.py 1.4 KB

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