test_imports.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 (
  6. NotAPackage,
  7. qualname,
  8. gen_task_name,
  9. reload_from_cwd,
  10. module_file,
  11. find_module,
  12. )
  13. def test_find_module():
  14. assert find_module('celery')
  15. imp = Mock()
  16. imp.return_value = None
  17. with pytest.raises(NotAPackage):
  18. find_module('foo.bar.baz', imp=imp)
  19. assert find_module('celery.worker.request')
  20. def test_qualname():
  21. Class = type(bytes_if_py2('Fox'), (object,), {
  22. '__module__': 'quick.brown',
  23. })
  24. assert qualname(Class) == 'quick.brown.Fox'
  25. assert qualname(Class()) == 'quick.brown.Fox'
  26. def test_reload_from_cwd(patching):
  27. reload = patching('celery.utils.imports.reload')
  28. reload_from_cwd('foo')
  29. reload.assert_called()
  30. def test_reload_from_cwd_custom_reloader():
  31. reload = Mock()
  32. reload_from_cwd('foo', reload)
  33. reload.assert_called()
  34. def test_module_file():
  35. m1 = Mock()
  36. m1.__file__ = '/opt/foo/xyz.pyc'
  37. assert module_file(m1) == '/opt/foo/xyz.py'
  38. m2 = Mock()
  39. m2.__file__ = '/opt/foo/xyz.py'
  40. assert module_file(m1) == '/opt/foo/xyz.py'
  41. class test_gen_task_name:
  42. def test_no_module(self):
  43. app = Mock()
  44. app.name == '__main__'
  45. assert gen_task_name(app, 'foo', 'axsadaewe')