test_imports.py 1.2 KB

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