test_riak.py 3.7 KB

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