/usr/lib/python2.7/dist-packages/celery/tests/backends/test_database.py is in python-celery 3.1.20-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | from __future__ import absolute_import, unicode_literals
from datetime import datetime
from pickle import loads, dumps
from celery import states
from celery.exceptions import ImproperlyConfigured
from celery.utils import uuid
from celery.tests.case import (
AppCase,
SkipTest,
depends_on_current_app,
mask_modules,
skip_if_pypy,
skip_if_jython,
)
try:
import sqlalchemy # noqa
except ImportError:
DatabaseBackend = Task = TaskSet = retry = None # noqa
else:
from celery.backends.database import DatabaseBackend, retry
from celery.backends.database.models import Task, TaskSet
class SomeClass(object):
def __init__(self, data):
self.data = data
class test_DatabaseBackend(AppCase):
@skip_if_pypy
@skip_if_jython
def setup(self):
if DatabaseBackend is None:
raise SkipTest('sqlalchemy not installed')
self.uri = 'sqlite:///test.db'
def test_retry_helper(self):
from celery.backends.database import DatabaseError
calls = [0]
@retry
def raises():
calls[0] += 1
raise DatabaseError(1, 2, 3)
with self.assertRaises(DatabaseError):
raises(max_retries=5)
self.assertEqual(calls[0], 5)
def test_missing_SQLAlchemy_raises_ImproperlyConfigured(self):
with mask_modules('sqlalchemy'):
from celery.backends.database import _sqlalchemy_installed
with self.assertRaises(ImproperlyConfigured):
_sqlalchemy_installed()
def test_missing_dburi_raises_ImproperlyConfigured(self):
self.app.conf.CELERY_RESULT_DBURI = None
with self.assertRaises(ImproperlyConfigured):
DatabaseBackend(app=self.app)
def test_missing_task_id_is_PENDING(self):
tb = DatabaseBackend(self.uri, app=self.app)
self.assertEqual(tb.get_status('xxx-does-not-exist'), states.PENDING)
def test_missing_task_meta_is_dict_with_pending(self):
tb = DatabaseBackend(self.uri, app=self.app)
self.assertDictContainsSubset({
'status': states.PENDING,
'task_id': 'xxx-does-not-exist-at-all',
'result': None,
'traceback': None
}, tb.get_task_meta('xxx-does-not-exist-at-all'))
def test_mark_as_done(self):
tb = DatabaseBackend(self.uri, app=self.app)
tid = uuid()
self.assertEqual(tb.get_status(tid), states.PENDING)
self.assertIsNone(tb.get_result(tid))
tb.mark_as_done(tid, 42)
self.assertEqual(tb.get_status(tid), states.SUCCESS)
self.assertEqual(tb.get_result(tid), 42)
def test_is_pickled(self):
tb = DatabaseBackend(self.uri, app=self.app)
tid2 = uuid()
result = {'foo': 'baz', 'bar': SomeClass(12345)}
tb.mark_as_done(tid2, result)
# is serialized properly.
rindb = tb.get_result(tid2)
self.assertEqual(rindb.get('foo'), 'baz')
self.assertEqual(rindb.get('bar').data, 12345)
def test_mark_as_started(self):
tb = DatabaseBackend(self.uri, app=self.app)
tid = uuid()
tb.mark_as_started(tid)
self.assertEqual(tb.get_status(tid), states.STARTED)
def test_mark_as_revoked(self):
tb = DatabaseBackend(self.uri, app=self.app)
tid = uuid()
tb.mark_as_revoked(tid)
self.assertEqual(tb.get_status(tid), states.REVOKED)
def test_mark_as_retry(self):
tb = DatabaseBackend(self.uri, app=self.app)
tid = uuid()
try:
raise KeyError('foo')
except KeyError as exception:
import traceback
trace = '\n'.join(traceback.format_stack())
tb.mark_as_retry(tid, exception, traceback=trace)
self.assertEqual(tb.get_status(tid), states.RETRY)
self.assertIsInstance(tb.get_result(tid), KeyError)
self.assertEqual(tb.get_traceback(tid), trace)
def test_mark_as_failure(self):
tb = DatabaseBackend(self.uri, app=self.app)
tid3 = uuid()
try:
raise KeyError('foo')
except KeyError as exception:
import traceback
trace = '\n'.join(traceback.format_stack())
tb.mark_as_failure(tid3, exception, traceback=trace)
self.assertEqual(tb.get_status(tid3), states.FAILURE)
self.assertIsInstance(tb.get_result(tid3), KeyError)
self.assertEqual(tb.get_traceback(tid3), trace)
def test_forget(self):
tb = DatabaseBackend(self.uri, backend='memory://', app=self.app)
tid = uuid()
tb.mark_as_done(tid, {'foo': 'bar'})
tb.mark_as_done(tid, {'foo': 'bar'})
x = self.app.AsyncResult(tid, backend=tb)
x.forget()
self.assertIsNone(x.result)
def test_process_cleanup(self):
tb = DatabaseBackend(self.uri, app=self.app)
tb.process_cleanup()
@depends_on_current_app
def test_reduce(self):
tb = DatabaseBackend(self.uri, app=self.app)
self.assertTrue(loads(dumps(tb)))
def test_save__restore__delete_group(self):
tb = DatabaseBackend(self.uri, app=self.app)
tid = uuid()
res = {'something': 'special'}
self.assertEqual(tb.save_group(tid, res), res)
res2 = tb.restore_group(tid)
self.assertEqual(res2, res)
tb.delete_group(tid)
self.assertIsNone(tb.restore_group(tid))
self.assertIsNone(tb.restore_group('xxx-nonexisting-id'))
def test_cleanup(self):
tb = DatabaseBackend(self.uri, app=self.app)
for i in range(10):
tb.mark_as_done(uuid(), 42)
tb.save_group(uuid(), {'foo': 'bar'})
s = tb.ResultSession()
for t in s.query(Task).all():
t.date_done = datetime.now() - tb.expires * 2
for t in s.query(TaskSet).all():
t.date_done = datetime.now() - tb.expires * 2
s.commit()
s.close()
tb.cleanup()
def test_Task__repr__(self):
self.assertIn('foo', repr(Task('foo')))
def test_TaskSet__repr__(self):
self.assertIn('foo', repr(TaskSet('foo', None)))
|