test_filesystem.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # -*- coding: utf-8 -*-
  2. import os
  3. import pytest
  4. import tempfile
  5. from case import skip
  6. from celery import uuid
  7. from celery import states
  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