-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathtests.py
More file actions
63 lines (51 loc) · 1.65 KB
/
tests.py
File metadata and controls
63 lines (51 loc) · 1.65 KB
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
from djcelery_transactions import task
from celery.registry import tasks
from django.db import transaction
from django.test import TransactionTestCase
my_global = []
marker = object()
@task
def my_task():
my_global.append(marker)
tasks.register(my_task)
class SpecificException(Exception):
pass
class DjangoCeleryTestCase(TransactionTestCase):
"""Test djcelery transaction safe task manager
"""
def tearDown(self):
my_global[:] = []
def test_commited_transaction_fire_task(self):
"""Check that task is consumed when no exception happens
"""
@transaction.commit_on_success
def do_something():
my_task.delay()
do_something()
self.assertTrue(my_global[0] is marker)
def test_rollbacked_transaction_discard_task(self):
"""Check that task is not consumed when exception happens
"""
@transaction.commit_on_success
def do_something():
my_task.delay()
raise SpecificException
try:
do_something()
except SpecificException:
self.assertFalse(my_global)
else:
self.fail('Exception not raised')
def test_django_db_transaction_managed(self):
"""
Check that django.db.transaction.managed is not affected
by monkey-patching
"""
from django.db import transaction
self.assertFalse(transaction.is_managed())
transaction.enter_transaction_management()
try:
transaction.managed()
self.assertTrue(transaction.is_managed())
finally:
transaction.leave_transaction_management()