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