test_couchbase.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. from __future__ import absolute_import
  2. from mock import MagicMock, Mock, patch, sentinel
  3. from nose import SkipTest
  4. from celery.backends import couchbase as module
  5. from celery.backends.couchbase import CouchBaseBackend
  6. from celery.exceptions import ImproperlyConfigured
  7. from celery import backends
  8. from celery.tests.case import AppCase
  9. try:
  10. import couchbase
  11. except ImportError:
  12. couchbase = None # noqa
  13. COUCHBASE_BUCKET = 'celery_bucket'
  14. class test_CouchBaseBackend(AppCase):
  15. def setup(self):
  16. if couchbase is None:
  17. raise SkipTest('couchbase is not installed.')
  18. self.backend = CouchBaseBackend(app=self.app)
  19. def test_init_no_couchbase(self):
  20. """test init no couchbase raises"""
  21. prev, module.couchbase = module.couchbase, None
  22. try:
  23. with self.assertRaises(ImproperlyConfigured):
  24. CouchBaseBackend(app=self.app)
  25. finally:
  26. module.couchbase = prev
  27. def test_init_no_settings(self):
  28. """test init no settings"""
  29. self.app.conf.CELERY_COUCHBASE_BACKEND_SETTINGS = []
  30. with self.assertRaises(ImproperlyConfigured):
  31. CouchBaseBackend(app=self.app)
  32. def test_init_settings_is_None(self):
  33. """Test init settings is None"""
  34. self.app.conf.CELERY_COUCHBASE_BACKEND_SETTINGS = None
  35. CouchBaseBackend(app=self.app)
  36. def test_get_connection_connection_exists(self):
  37. with patch('couchbase.connection.Connection') as mock_Connection:
  38. self.backend._connection = sentinel._connection
  39. connection = self.backend._get_connection()
  40. self.assertEqual(sentinel._connection, connection)
  41. self.assertFalse(mock_Connection.called)
  42. def test_get(self):
  43. """test_get
  44. CouchBaseBackend.get should return and take two params
  45. db conn to couchbase is mocked.
  46. TODO Should test on key not exists
  47. """
  48. self.app.conf.CELERY_COUCHBASE_BACKEND_SETTINGS = {}
  49. x = CouchBaseBackend(app=self.app)
  50. x._connection = Mock()
  51. mocked_get = x._connection.get = Mock()
  52. mocked_get.return_value.value = sentinel.retval
  53. # should return None
  54. self.assertEqual(x.get('1f3fab'), sentinel.retval)
  55. x._connection.get.assert_called_once_with('1f3fab')
  56. def test_set(self):
  57. """test_set
  58. CouchBaseBackend.set should return None and take two params
  59. db conn to couchbase is mocked.
  60. """
  61. self.app.conf.CELERY_COUCHBASE_BACKEND_SETTINGS = None
  62. x = CouchBaseBackend(app=self.app)
  63. x._connection = MagicMock()
  64. x._connection.set = MagicMock()
  65. # should return None
  66. self.assertIsNone(x.set(sentinel.key, sentinel.value))
  67. def test_delete(self):
  68. """test_delete
  69. CouchBaseBackend.delete should return and take two params
  70. db conn to couchbase is mocked.
  71. TODO Should test on key not exists
  72. """
  73. self.app.conf.CELERY_COUCHBASE_BACKEND_SETTINGS = {}
  74. x = CouchBaseBackend(app=self.app)
  75. x._connection = Mock()
  76. mocked_delete = x._connection.delete = Mock()
  77. mocked_delete.return_value = None
  78. # should return None
  79. self.assertIsNone(x.delete('1f3fab'))
  80. x._connection.delete.assert_called_once_with('1f3fab')
  81. def test_config_params(self):
  82. """test_config_params
  83. celery.conf.CELERY_COUCHBASE_BACKEND_SETTINGS is properly set
  84. """
  85. self.app.conf.CELERY_COUCHBASE_BACKEND_SETTINGS = {
  86. 'bucket': 'mycoolbucket',
  87. 'host': ['here.host.com', 'there.host.com'],
  88. 'username': 'johndoe',
  89. 'password': 'mysecret',
  90. 'port': '1234',
  91. }
  92. x = CouchBaseBackend(app=self.app)
  93. self.assertEqual(x.bucket, 'mycoolbucket')
  94. self.assertEqual(x.host, ['here.host.com', 'there.host.com'],)
  95. self.assertEqual(x.username, 'johndoe',)
  96. self.assertEqual(x.password, 'mysecret')
  97. self.assertEqual(x.port, 1234)
  98. def test_backend_by_url(self, url='couchbase://myhost/mycoolbucket'):
  99. from celery.backends.couchbase import CouchBaseBackend
  100. backend, url_ = backends.get_backend_by_url(url, self.app.loader)
  101. self.assertIs(backend, CouchBaseBackend)
  102. self.assertEqual(url_, url)
  103. def test_backend_params_by_url(self):
  104. url = 'couchbase://johndoe:mysecret@myhost:123/mycoolbucket'
  105. with self.Celery(backend=url) as app:
  106. x = app.backend
  107. self.assertEqual(x.bucket, "mycoolbucket")
  108. self.assertEqual(x.host, "myhost")
  109. self.assertEqual(x.username, "johndoe")
  110. self.assertEqual(x.password, "mysecret")
  111. self.assertEqual(x.port, 123)