test_imports.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. from __future__ import absolute_import, unicode_literals
  2. import pytest
  3. from case import Mock
  4. from celery.five import bytes_if_py2
  5. from celery.utils.imports import (NotAPackage, find_module, gen_task_name,
  6. module_file, qualname, reload_from_cwd)
  7. def test_find_module():
  8. assert find_module('celery')
  9. imp = Mock()
  10. imp.return_value = None
  11. with pytest.raises(NotAPackage):
  12. find_module('foo.bar.baz', imp=imp)
  13. assert find_module('celery.worker.request')
  14. def test_qualname():
  15. Class = type(bytes_if_py2('Fox'), (object,), {
  16. '__module__': 'quick.brown',
  17. })
  18. assert qualname(Class) == 'quick.brown.Fox'
  19. assert qualname(Class()) == 'quick.brown.Fox'
  20. def test_reload_from_cwd(patching):
  21. reload = patching('celery.utils.imports.reload')
  22. reload_from_cwd('foo')
  23. reload.assert_called()
  24. def test_reload_from_cwd_custom_reloader():
  25. reload = Mock()
  26. reload_from_cwd('foo', reload)
  27. reload.assert_called()
  28. def test_module_file():
  29. m1 = Mock()
  30. m1.__file__ = '/opt/foo/xyz.pyc'
  31. assert module_file(m1) == '/opt/foo/xyz.py'
  32. m2 = Mock()
  33. m2.__file__ = '/opt/foo/xyz.py'
  34. assert module_file(m1) == '/opt/foo/xyz.py'
  35. class test_gen_task_name:
  36. def test_no_module(self):
  37. app = Mock()
  38. app.name == '__main__'
  39. assert gen_task_name(app, 'foo', 'axsadaewe')