test_filesystem.py 2.2 KB

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