test_couchdb.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import pytest
  2. from case import MagicMock, Mock, sentinel, skip
  3. from celery.app import backends
  4. from celery.backends import couchdb as module
  5. from celery.backends.couchdb import CouchBackend
  6. from celery.exceptions import ImproperlyConfigured
  7. try:
  8. import pycouchdb
  9. except ImportError:
  10. pycouchdb = None # noqa
  11. COUCHDB_CONTAINER = 'celery_container'
  12. @skip.unless_module('pycouchdb')
  13. class test_CouchBackend:
  14. def setup(self):
  15. self.Server = self.patching('pycouchdb.Server')
  16. self.backend = CouchBackend(app=self.app)
  17. def test_init_no_pycouchdb(self):
  18. """test init no pycouchdb raises"""
  19. prev, module.pycouchdb = module.pycouchdb, None
  20. try:
  21. with pytest.raises(ImproperlyConfigured):
  22. CouchBackend(app=self.app)
  23. finally:
  24. module.pycouchdb = prev
  25. def test_get_container_exists(self):
  26. self.backend._connection = sentinel._connection
  27. connection = self.backend.connection
  28. assert connection is sentinel._connection
  29. self.Server.assert_not_called()
  30. def test_get(self):
  31. """test_get
  32. CouchBackend.get should return and take two params
  33. db conn to couchdb is mocked.
  34. TODO Should test on key not exists
  35. """
  36. x = CouchBackend(app=self.app)
  37. x._connection = Mock()
  38. get = x._connection.get = MagicMock()
  39. # should return None
  40. assert x.get('1f3fab') == get.return_value['value']
  41. x._connection.get.assert_called_once_with('1f3fab')
  42. def test_delete(self):
  43. """test_delete
  44. CouchBackend.delete should return and take two params
  45. db conn to pycouchdb is mocked.
  46. TODO Should test on key not exists
  47. """
  48. x = CouchBackend(app=self.app)
  49. x._connection = Mock()
  50. mocked_delete = x._connection.delete = Mock()
  51. mocked_delete.return_value = None
  52. # should return None
  53. assert x.delete('1f3fab') is None
  54. x._connection.delete.assert_called_once_with('1f3fab')
  55. def test_backend_by_url(self, url='couchdb://myhost/mycoolcontainer'):
  56. from celery.backends.couchdb import CouchBackend
  57. backend, url_ = backends.by_url(url, self.app.loader)
  58. assert backend is CouchBackend
  59. assert url_ == url
  60. def test_backend_params_by_url(self):
  61. url = 'couchdb://johndoe:mysecret@myhost:123/mycoolcontainer'
  62. with self.Celery(backend=url) as app:
  63. x = app.backend
  64. assert x.container == 'mycoolcontainer'
  65. assert x.host == 'myhost'
  66. assert x.username == 'johndoe'
  67. assert x.password == 'mysecret'
  68. assert x.port == 123