Skip to content

Commit 262482f

Browse files
committed
Add minimal test suite to check any regression and prove bug fixing
1 parent fa9c27f commit 262482f

File tree

6 files changed

+104
-2
lines changed

6 files changed

+104
-2
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,8 @@ Celery's [user guide][1]. Send tasks from signal handlers without fear!
5656
print_model.delay(model_pk)
5757
transaction.commit()
5858

59+
## Run test suite
60+
61+
$ python setup.py test
62+
5963
[1]: http://celery.readthedocs.org/en/latest/userguide/tasks.html#database-transactions

setup.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,39 @@
11
# coding=utf-8
2-
from setuptools import setup, find_packages
2+
import os
3+
import sys
34

5+
from setuptools import setup, Command, find_packages
6+
7+
8+
class RunTests(Command):
9+
"""RunTests class borrowed from django-celery project
10+
"""
11+
description = 'Run the django test suite from the tests dir.'
12+
user_options = []
13+
extra_args = []
14+
15+
def run(self):
16+
from django.core.management import execute_from_command_line
17+
settings_module_name = 'tests.settings'
18+
os.environ['DJANGO_SETTINGS_MODULE'] = os.environ.get(
19+
'DJANGO_SETTINGS_MODULE',
20+
settings_module_name)
21+
prev_argv = sys.argv[:]
22+
try:
23+
sys.argv = [__file__, 'test'] + self.extra_args
24+
execute_from_command_line(argv=sys.argv)
25+
finally:
26+
sys.argv[:] = prev_argv
27+
28+
def initialize_options(self):
29+
pass
30+
31+
def finalize_options(self):
32+
pass
433

534
setup(
635
name="django-celery-transactions",
7-
version="0.1.3",
36+
version="0.1.4",
837
description="Django transaction support for Celery tasks.",
938
long_description="See https://github.com/chrisdoble/django-celery-transactions",
1039
author="Chris Doble",
@@ -15,6 +44,7 @@
1544
install_requires=[
1645
"celery>=2.2.7",
1746
"Django>=1.2.4",
47+
"django-celery>=2.2.7",
1848
],
1949
classifiers=[
2050
"Framework :: Django",
@@ -24,4 +54,5 @@
2454
"Programming Language :: Python",
2555
"Topic :: Database",
2656
],
57+
cmdclass={'test': RunTests},
2758
)

tests.settings

Whitespace-only changes.

tests/__init__.py

Whitespace-only changes.

tests/settings.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
__doc__ = """Minimal django settings to run manage.py test command"""
2+
3+
import djcelery
4+
djcelery.setup_loader()
5+
6+
DATABASES = {
7+
'default': {
8+
'ENGINE': 'django.db.backends.sqlite3',
9+
'NAME': __name__,
10+
}
11+
}
12+
13+
CELERY_ALWAYS_EAGER = True
14+
CELERY_EAGER_PROPAGATES_EXCEPTIONS = True
15+
16+
INSTALLED_APPS = ('tests',
17+
'djcelery',
18+
)

tests/tests.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from djcelery_transactions import task
2+
from celery.registry import tasks
3+
from django.db import transaction
4+
from django.test import TransactionTestCase
5+
6+
my_global = []
7+
8+
marker = object()
9+
10+
@task
11+
def my_task():
12+
my_global.append(marker)
13+
14+
tasks.register(my_task)
15+
16+
class SpecificException(Exception):
17+
pass
18+
19+
class DjangoCeleryTestCase(TransactionTestCase):
20+
"""Test djcelery transaction safe task manager
21+
"""
22+
def tearDown(self):
23+
my_global[:] = []
24+
25+
def test_commited_transaction_fire_task(self):
26+
"""Check that task is consumed when no exception happens
27+
"""
28+
29+
@transaction.commit_on_success
30+
def do_something():
31+
my_task.delay()
32+
33+
do_something()
34+
self.assertTrue(my_global[0] is marker)
35+
36+
def test_rollbacked_transaction_discard_task(self):
37+
"""Check that task is not consumed when exception happens
38+
"""
39+
40+
@transaction.commit_on_success
41+
def do_something():
42+
my_task.delay()
43+
raise SpecificException
44+
try:
45+
do_something()
46+
except SpecificException:
47+
self.assertFalse(my_global)
48+
else:
49+
self.fail('Exception not raised')

0 commit comments

Comments
 (0)