test_riak.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import, unicode_literals
  3. import pytest
  4. from case import MagicMock, Mock, patch, sentinel, skip
  5. from celery.backends import riak as module
  6. from celery.backends.riak import RiakBackend
  7. from celery.exceptions import ImproperlyConfigured
  8. RIAK_BUCKET = 'riak_bucket'
  9. @skip.unless_module('riak')
  10. class test_RiakBackend:
  11. def setup(self):
  12. self.app.conf.result_backend = 'riak://'
  13. @property
  14. def backend(self):
  15. return self.app.backend
  16. def test_init_no_riak(self):
  17. prev, module.riak = module.riak, None
  18. try:
  19. with pytest.raises(ImproperlyConfigured):
  20. RiakBackend(app=self.app)
  21. finally:
  22. module.riak = prev
  23. def test_init_no_settings(self):
  24. self.app.conf.riak_backend_settings = []
  25. with pytest.raises(ImproperlyConfigured):
  26. RiakBackend(app=self.app)
  27. def test_init_settings_is_None(self):
  28. self.app.conf.riak_backend_settings = None
  29. assert self.app.backend
  30. def test_get_client_client_exists(self):
  31. with patch('riak.client.RiakClient') as mock_connection:
  32. self.backend._client = sentinel._client
  33. mocked_is_alive = self.backend._client.is_alive = Mock()
  34. mocked_is_alive.return_value.value = True
  35. client = self.backend._get_client()
  36. assert sentinel._client == client
  37. mock_connection.assert_not_called()
  38. def test_get(self):
  39. self.app.conf.couchbase_backend_settings = {}
  40. self.backend._client = Mock(name='_client')
  41. self.backend._bucket = Mock(name='_bucket')
  42. mocked_get = self.backend._bucket.get = Mock(name='bucket.get')
  43. mocked_get.return_value.data = sentinel.retval
  44. # should return None
  45. assert self.backend.get('1f3fab') == sentinel.retval
  46. self.backend._bucket.get.assert_called_once_with('1f3fab')
  47. def test_set(self):
  48. self.app.conf.couchbase_backend_settings = None
  49. self.backend._client = MagicMock()
  50. self.backend._bucket = MagicMock()
  51. self.backend._bucket.set = MagicMock()
  52. # should return None
  53. assert self.backend.set(sentinel.key, sentinel.value) is None
  54. def test_delete(self):
  55. self.app.conf.couchbase_backend_settings = {}
  56. self.backend._client = Mock(name='_client')
  57. self.backend._bucket = Mock(name='_bucket')
  58. mocked_delete = self.backend._client.delete = Mock('client.delete')
  59. mocked_delete.return_value = None
  60. # should return None
  61. assert self.backend.delete('1f3fab') is None
  62. self.backend._bucket.delete.assert_called_once_with('1f3fab')
  63. def test_config_params(self):
  64. self.app.conf.riak_backend_settings = {
  65. 'bucket': 'mycoolbucket',
  66. 'host': 'there.host.com',
  67. 'port': '1234',
  68. }
  69. assert self.backend.bucket_name == 'mycoolbucket'
  70. assert self.backend.host == 'there.host.com'
  71. assert self.backend.port == 1234
  72. def test_backend_by_url(self, url='riak://myhost/mycoolbucket'):
  73. from celery.app import backends
  74. from celery.backends.riak import RiakBackend
  75. backend, url_ = backends.by_url(url, self.app.loader)
  76. assert backend is RiakBackend
  77. assert url_ == url
  78. def test_backend_params_by_url(self):
  79. self.app.conf.result_backend = 'riak://myhost:123/mycoolbucket'
  80. assert self.backend.bucket_name == 'mycoolbucket'
  81. assert self.backend.host == 'myhost'
  82. assert self.backend.port == 123
  83. def test_non_ASCII_bucket_raises(self):
  84. self.app.conf.riak_backend_settings = {
  85. 'bucket': 'héhé',
  86. 'host': 'there.host.com',
  87. 'port': '1234',
  88. }
  89. with pytest.raises(ValueError):
  90. RiakBackend(app=self.app)