test_filesystem.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import, unicode_literals
  3. import os
  4. import tempfile
  5. import pytest
  6. from case import skip
  7. from celery import states, uuid
  8. from celery.backends.filesystem import FilesystemBackend
  9. from celery.exceptions import ImproperlyConfigured
  10. @skip.if_win32()
  11. class test_FilesystemBackend:
  12. def setup(self):
  13. self.directory = tempfile.mkdtemp()
  14. self.url = 'file://' + self.directory
  15. self.path = self.directory.encode('ascii')
  16. def test_a_path_is_required(self):
  17. with pytest.raises(ImproperlyConfigured):
  18. FilesystemBackend(app=self.app)
  19. def test_a_path_in_url(self):
  20. tb = FilesystemBackend(app=self.app, url=self.url)
  21. assert tb.path == self.path
  22. def test_path_is_incorrect(self):
  23. with pytest.raises(ImproperlyConfigured):
  24. FilesystemBackend(app=self.app, url=self.url + '-incorrect')
  25. def test_missing_task_is_PENDING(self):
  26. tb = FilesystemBackend(app=self.app, url=self.url)
  27. assert tb.get_state('xxx-does-not-exist') == states.PENDING
  28. def test_mark_as_done_writes_file(self):
  29. tb = FilesystemBackend(app=self.app, url=self.url)
  30. tb.mark_as_done(uuid(), 42)
  31. assert len(os.listdir(self.directory)) == 1
  32. def test_done_task_is_SUCCESS(self):
  33. tb = FilesystemBackend(app=self.app, url=self.url)
  34. tid = uuid()
  35. tb.mark_as_done(tid, 42)
  36. assert tb.get_state(tid) == states.SUCCESS
  37. def test_correct_result(self):
  38. data = {'foo': 'bar'}
  39. tb = FilesystemBackend(app=self.app, url=self.url)
  40. tid = uuid()
  41. tb.mark_as_done(tid, data)
  42. assert tb.get_result(tid) == data
  43. def test_get_many(self):
  44. data = {uuid(): 'foo', uuid(): 'bar', uuid(): 'baz'}
  45. tb = FilesystemBackend(app=self.app, url=self.url)
  46. for key, value in data.items():
  47. tb.mark_as_done(key, value)
  48. for key, result in tb.get_many(data.keys()):
  49. assert result['result'] == data[key]
  50. def test_forget_deletes_file(self):
  51. tb = FilesystemBackend(app=self.app, url=self.url)
  52. tid = uuid()
  53. tb.mark_as_done(tid, 42)
  54. tb.forget(tid)
  55. assert len(os.listdir(self.directory)) == 0