test_couchbase.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. """Tests for the CouchbaseBackend."""
  2. from __future__ import absolute_import, unicode_literals
  3. from datetime import timedelta
  4. import pytest
  5. from case import MagicMock, Mock, patch, sentinel, skip
  6. from celery.app import backends
  7. from celery.backends import couchbase as module
  8. from celery.backends.couchbase import CouchbaseBackend
  9. from celery.exceptions import ImproperlyConfigured
  10. try:
  11. import couchbase
  12. except ImportError:
  13. couchbase = None # noqa
  14. COUCHBASE_BUCKET = 'celery_bucket'
  15. @skip.unless_module('couchbase')
  16. class test_CouchbaseBackend:
  17. def setup(self):
  18. self.backend = CouchbaseBackend(app=self.app)
  19. def test_init_no_couchbase(self):
  20. prev, module.Couchbase = module.Couchbase, None
  21. try:
  22. with pytest.raises(ImproperlyConfigured):
  23. CouchbaseBackend(app=self.app)
  24. finally:
  25. module.Couchbase = prev
  26. def test_init_no_settings(self):
  27. self.app.conf.couchbase_backend_settings = []
  28. with pytest.raises(ImproperlyConfigured):
  29. CouchbaseBackend(app=self.app)
  30. def test_init_settings_is_None(self):
  31. self.app.conf.couchbase_backend_settings = None
  32. CouchbaseBackend(app=self.app)
  33. def test_get_connection_connection_exists(self):
  34. with patch('couchbase.connection.Connection') as mock_Connection:
  35. self.backend._connection = sentinel._connection
  36. connection = self.backend._get_connection()
  37. assert sentinel._connection == connection
  38. mock_Connection.assert_not_called()
  39. def test_get(self):
  40. self.app.conf.couchbase_backend_settings = {}
  41. x = CouchbaseBackend(app=self.app)
  42. x._connection = Mock()
  43. mocked_get = x._connection.get = Mock()
  44. mocked_get.return_value.value = sentinel.retval
  45. # should return None
  46. assert x.get('1f3fab') == sentinel.retval
  47. x._connection.get.assert_called_once_with('1f3fab')
  48. def test_set_no_expires(self):
  49. self.app.conf.couchbase_backend_settings = None
  50. x = CouchbaseBackend(app=self.app)
  51. x.expires = None
  52. x._connection = MagicMock()
  53. x._connection.set = MagicMock()
  54. # should return None
  55. assert x.set(sentinel.key, sentinel.value) is None
  56. def test_set_expires(self):
  57. self.app.conf.couchbase_backend_settings = None
  58. x = CouchbaseBackend(app=self.app, expires=30)
  59. assert x.expires == 30
  60. x._connection = MagicMock()
  61. x._connection.set = MagicMock()
  62. # should return None
  63. assert x.set(sentinel.key, sentinel.value) is None
  64. def test_delete(self):
  65. self.app.conf.couchbase_backend_settings = {}
  66. x = CouchbaseBackend(app=self.app)
  67. x._connection = Mock()
  68. mocked_delete = x._connection.delete = Mock()
  69. mocked_delete.return_value = None
  70. # should return None
  71. assert x.delete('1f3fab') is None
  72. x._connection.delete.assert_called_once_with('1f3fab')
  73. def test_config_params(self):
  74. self.app.conf.couchbase_backend_settings = {
  75. 'bucket': 'mycoolbucket',
  76. 'host': ['here.host.com', 'there.host.com'],
  77. 'username': 'johndoe',
  78. 'password': 'mysecret',
  79. 'port': '1234',
  80. }
  81. x = CouchbaseBackend(app=self.app)
  82. assert x.bucket == 'mycoolbucket'
  83. assert x.host == ['here.host.com', 'there.host.com']
  84. assert x.username == 'johndoe'
  85. assert x.password == 'mysecret'
  86. assert x.port == 1234
  87. def test_backend_by_url(self, url='couchbase://myhost/mycoolbucket'):
  88. from celery.backends.couchbase import CouchbaseBackend
  89. backend, url_ = backends.by_url(url, self.app.loader)
  90. assert backend is CouchbaseBackend
  91. assert url_ == url
  92. def test_backend_params_by_url(self):
  93. url = 'couchbase://johndoe:mysecret@myhost:123/mycoolbucket'
  94. with self.Celery(backend=url) as app:
  95. x = app.backend
  96. assert x.bucket == 'mycoolbucket'
  97. assert x.host == 'myhost'
  98. assert x.username == 'johndoe'
  99. assert x.password == 'mysecret'
  100. assert x.port == 123
  101. def test_expires_defaults_to_config(self):
  102. self.app.conf.result_expires = 10
  103. b = CouchbaseBackend(expires=None, app=self.app)
  104. assert b.expires == 10
  105. def test_expires_is_int(self):
  106. b = CouchbaseBackend(expires=48, app=self.app)
  107. assert b.expires == 48
  108. def test_expires_is_None(self):
  109. b = CouchbaseBackend(expires=None, app=self.app)
  110. assert b.expires == self.app.conf.result_expires.total_seconds()
  111. def test_expires_is_timedelta(self):
  112. b = CouchbaseBackend(expires=timedelta(minutes=1), app=self.app)
  113. assert b.expires == 60