Debian Codegraph @

t/unit/backends/test_arangodb.py

raw
1 """Tests for the ArangoDb."""
2 import datetime
3 from unittest.mock import MagicMock, Mock, patch, sentinel
4
5 import pytest
6
7 from celery.app import backends
8 from celery.backends import arangodb as module
9 from celery.backends.arangodb import ArangoDbBackend
10 from celery.exceptions import ImproperlyConfigured
11
12 try:
13 import pyArango
14 except ImportError:
15 pyArango = None
16
17 pytest.importorskip('pyArango')
18
19
20 class test_ArangoDbBackend:
21
22 def setup_method(self):
23 self.backend = ArangoDbBackend(app=self.app)
24
25 def test_init_no_arangodb(self):
26 prev, module.py_arango_connection = module.py_arango_connection, None
27 try:
28 with pytest.raises(ImproperlyConfigured):
29 ArangoDbBackend(app=self.app)
30 finally:
31 module.py_arango_connection = prev
32
33 def test_init_no_settings(self):
34 self.app.conf.arangodb_backend_settings = []
35 with pytest.raises(ImproperlyConfigured):
36 ArangoDbBackend(app=self.app)
37
38 def test_init_settings_is_None(self):
39 self.app.conf.arangodb_backend_settings = None
40 ArangoDbBackend(app=self.app)
41
42 def test_init_url(self):
43 url = None
44 expected_database = "celery"
45 expected_collection = "celery"
46 backend = ArangoDbBackend(app=self.app, url=url)
47 assert backend.database == expected_database
48 assert backend.collection == expected_collection
49
50 url = "arangodb://localhost:27017/celery-database/celery-collection"
51 expected_database = "celery-database"
52 expected_collection = "celery-collection"
53 backend = ArangoDbBackend(app=self.app, url=url)
54 assert backend.database == expected_database
55 assert backend.collection == expected_collection
56
57 def test_get_connection_connection_exists(self):
58 with patch('pyArango.connection.Connection') as mock_Connection:
59 self.backend._connection = sentinel.connection
60 connection = self.backend.connection
61 assert connection == sentinel.connection
62 mock_Connection.assert_not_called()
63
64 expected_connection = mock_Connection()
65 mock_Connection.reset_mock() # So the assert_called_once below is accurate.
66 self.backend._connection = None
67 connection = self.backend.connection
68 assert connection == expected_connection
69 mock_Connection.assert_called_once()
70
71 def test_get(self):
72 self.backend._connection = MagicMock(spec=["__getitem__"])
73
74 assert self.backend.get(None) is None
75 self.backend.db.AQLQuery.assert_not_called()
76
77 assert self.backend.get(sentinel.task_id) is None
78 self.backend.db.AQLQuery.assert_called_once_with(
79 "RETURN DOCUMENT(@@collection, @key).task",
80 rawResults=True,
81 bindVars={
82 "@collection": self.backend.collection,
83 "key": sentinel.task_id,
84 },
85 )
86
87 self.backend.get = Mock(return_value=sentinel.retval)
88 assert self.backend.get(sentinel.task_id) == sentinel.retval
89 self.backend.get.assert_called_once_with(sentinel.task_id)
90
91 def test_set(self):
92 self.backend._connection = MagicMock(spec=["__getitem__"])
93
94 assert self.backend.set(sentinel.key, sentinel.value) is None
95 self.backend.db.AQLQuery.assert_called_once_with(
96 """
97 UPSERT {_key: @key}
98 INSERT {_key: @key, task: @value}
99 UPDATE {task: @value} IN @@collection
100 """,
101 bindVars={
102 "@collection": self.backend.collection,
103 "key": sentinel.key,
104 "value": sentinel.value,
105 },
106 )
107
108 def test_mget(self):
109 self.backend._connection = MagicMock(spec=["__getitem__"])
110
111 result = list(self.backend.mget(None))
112 expected_result = []
113 assert result == expected_result
114 self.backend.db.AQLQuery.assert_not_called()
115
116 Query = MagicMock(spec=pyArango.query.Query)
117 query = Query()
118 query.nextBatch = MagicMock(side_effect=StopIteration())
119 self.backend.db.AQLQuery = Mock(return_value=query)
120
121 keys = [sentinel.task_id_0, sentinel.task_id_1]
122 result = list(self.backend.mget(keys))
123 expected_result = []
124 assert result == expected_result
125 self.backend.db.AQLQuery.assert_called_once_with(
126 "FOR k IN @keys RETURN DOCUMENT(@@collection, k).task",
127 rawResults=True,
128 bindVars={
129 "@collection": self.backend.collection,
130 "keys": keys,
131 },
132 )
133
134 values = [sentinel.value_0, sentinel.value_1]
135 query.__iter__.return_value = iter([sentinel.value_0, sentinel.value_1])
136 result = list(self.backend.mget(keys))
137 expected_result = values
138 assert result == expected_result
139
140 def test_delete(self):
141 self.backend._connection = MagicMock(spec=["__getitem__"])
142
143 assert self.backend.delete(None) is None
144 self.backend.db.AQLQuery.assert_not_called()
145
146 assert self.backend.delete(sentinel.task_id) is None
147 self.backend.db.AQLQuery.assert_called_once_with(
148 "REMOVE {_key: @key} IN @@collection",
149 bindVars={
150 "@collection": self.backend.collection,
151 "key": sentinel.task_id,
152 },
153 )
154
155 def test_config_params(self):
156 self.app.conf.arangodb_backend_settings = {
157 'host': 'test.arangodb.com',
158 'port': '8529',
159 'username': 'johndoe',
160 'password': 'mysecret',
161 'database': 'celery_database',
162 'collection': 'celery_collection',
163 'http_protocol': 'https',
164 'verify': True
165 }
166 x = ArangoDbBackend(app=self.app)
167 assert x.host == 'test.arangodb.com'
168 assert x.port == 8529
169 assert x.username == 'johndoe'
170 assert x.password == 'mysecret'
171 assert x.database == 'celery_database'
172 assert x.collection == 'celery_collection'
173 assert x.http_protocol == 'https'
174 assert x.arangodb_url == 'https://test.arangodb.com:8529'
175 assert x.verify is True
176
177 def test_backend_by_url(
178 self, url="arangodb://username:password@host:port/database/collection"
179 ):
180 from celery.backends.arangodb import ArangoDbBackend
181 backend, url_ = backends.by_url(url, self.app.loader)
182 assert backend is ArangoDbBackend
183 assert url_ == url
184
185 def test_backend_params_by_url(self):
186 url = (
187 "arangodb://johndoe:[email protected]:8529/"
188 "celery_database/celery_collection"
189 )
190 with self.Celery(backend=url) as app:
191 x = app.backend
192 assert x.host == 'test.arangodb.com'
193 assert x.port == 8529
194 assert x.username == 'johndoe'
195 assert x.password == 'mysecret'
196 assert x.database == 'celery_database'
197 assert x.collection == 'celery_collection'
198 assert x.http_protocol == 'http'
199 assert x.arangodb_url == 'http://test.arangodb.com:8529'
200 assert x.verify is False
201
202 def test_backend_cleanup(self):
203 self.backend._connection = MagicMock(spec=["__getitem__"])
204
205 self.backend.expires = None
206 self.backend.cleanup()
207 self.backend.db.AQLQuery.assert_not_called()
208
209 self.backend.expires = 0
210 self.backend.cleanup()
211 self.backend.db.AQLQuery.assert_not_called()
212
213 now = datetime.datetime.now(datetime.timezone.utc)
214 self.backend.app.now = Mock(return_value=now)
215 self.backend.expires = 86400
216 expected_checkpoint = (now - self.backend.expires_delta).isoformat()
217 self.backend.cleanup()
218 self.backend.db.AQLQuery.assert_called_once_with(
219 """
220 FOR record IN @@collection
221 FILTER record.task.date_done < @checkpoint
222 REMOVE record IN @@collection
223 """,
224 bindVars={
225 "@collection": self.backend.collection,
226 "checkpoint": expected_checkpoint,
227 },
228 )