test_couchbase.py 5.3 KB

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