test_couchdb.py 2.7 KB

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