From b4bccb2d311986f0f7f333688130fcfbcce32160 Mon Sep 17 00:00:00 2001 From: msoedov Date: Sun, 4 May 2014 15:32:42 +0300 Subject: [PATCH 1/4] Moved @resolve_connection call into @delay wrapper --- rq/decorators.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rq/decorators.py b/rq/decorators.py index 90aebf7..169375a 100644 --- a/rq/decorators.py +++ b/rq/decorators.py @@ -20,7 +20,7 @@ class job(object): simple_add.delay(1, 2) # Puts simple_add function into queue """ self.queue = queue - self.connection = resolve_connection(connection) + self.connection = connection self.timeout = timeout self.result_ttl = result_ttl @@ -28,7 +28,7 @@ class job(object): @wraps(f) def delay(*args, **kwargs): if isinstance(self.queue, string_types): - queue = Queue(name=self.queue, connection=self.connection) + queue = Queue(name=self.queue, connection=resolve_connection(self.connection)) else: queue = self.queue if 'depends_on' in kwargs: From dd994142b3454a2d7c168b30026c18dd98c0377d Mon Sep 17 00:00:00 2001 From: msoedov Date: Sun, 4 May 2014 15:34:42 +0300 Subject: [PATCH 2/4] Added tests and updated requirements with mock lib. --- requirements.txt | 1 + tests/__init__.py | 1 + tests/test_decorator.py | 24 +++++++++++++++++++++++- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 7800f0f..0a95d3d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ redis +mock diff --git a/tests/__init__.py b/tests/__init__.py index ac66204..0a23efc 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,4 +1,5 @@ import logging +import mock from rq.compat import is_python_version if is_python_version((2, 7), (3, 2)): import unittest diff --git a/tests/test_decorator.py b/tests/test_decorator.py index 2521e2e..3c0fdd4 100644 --- a/tests/test_decorator.py +++ b/tests/test_decorator.py @@ -1,4 +1,6 @@ -from tests import RQTestCase +from redis import StrictRedis + +from tests import RQTestCase, mock from tests.fixtures import decorated_job from rq.decorators import job @@ -70,3 +72,23 @@ class TestDecorator(RQTestCase): self.assertEqual(bar_job.dependency, foo_job) self.assertEqual(bar_job._dependency_id, foo_job.id) + + @mock.patch('rq.decorators.resolve_connection') + def test_decorator_connection_laziness(self, resolve_connection): + """Ensure that job decorator resolve connection in `lazy` way """ + + resolve_connection.return_value = StrictRedis() + + @job(queue='queue_name') + def foo(): + return 'do something' + + self.assertEqual(resolve_connection.call_count, 0) + + foo() + + self.assertEqual(resolve_connection.call_count, 0) + + foo.delay() + + self.assertEqual(resolve_connection.call_count, 1) From e76020d8945d6daecc31ce5f6854d819a1bb2af0 Mon Sep 17 00:00:00 2001 From: msoedov Date: Sun, 4 May 2014 15:40:12 +0300 Subject: [PATCH 3/4] Removed @resolve_connection from job decorator. -- resolve_connection already presence inside Queue __init__. --- rq/decorators.py | 3 +-- tests/test_decorator.py | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/rq/decorators.py b/rq/decorators.py index 169375a..07a7a83 100644 --- a/rq/decorators.py +++ b/rq/decorators.py @@ -1,6 +1,5 @@ from functools import wraps from .queue import Queue -from .connections import resolve_connection from .worker import DEFAULT_RESULT_TTL from rq.compat import string_types @@ -28,7 +27,7 @@ class job(object): @wraps(f) def delay(*args, **kwargs): if isinstance(self.queue, string_types): - queue = Queue(name=self.queue, connection=resolve_connection(self.connection)) + queue = Queue(name=self.queue, connection=self.connection) else: queue = self.queue if 'depends_on' in kwargs: diff --git a/tests/test_decorator.py b/tests/test_decorator.py index 3c0fdd4..8af464f 100644 --- a/tests/test_decorator.py +++ b/tests/test_decorator.py @@ -73,7 +73,7 @@ class TestDecorator(RQTestCase): self.assertEqual(bar_job._dependency_id, foo_job.id) - @mock.patch('rq.decorators.resolve_connection') + @mock.patch('rq.queue.resolve_connection') def test_decorator_connection_laziness(self, resolve_connection): """Ensure that job decorator resolve connection in `lazy` way """ From 1f423436a5e7cf11ca397edf65aa7cfc70c588c6 Mon Sep 17 00:00:00 2001 From: Vincent Driessen Date: Tue, 6 May 2014 15:51:23 +0200 Subject: [PATCH 4/4] Move mock to test-only dependencies. --- dev-requirements.txt | 1 + requirements.txt | 1 - tox.ini | 5 ++++- 3 files changed, 5 insertions(+), 2 deletions(-) create mode 100644 dev-requirements.txt diff --git a/dev-requirements.txt b/dev-requirements.txt new file mode 100644 index 0000000..932a895 --- /dev/null +++ b/dev-requirements.txt @@ -0,0 +1 @@ +mock diff --git a/requirements.txt b/requirements.txt index 0a95d3d..7800f0f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1 @@ redis -mock diff --git a/tox.ini b/tox.ini index bb92511..c167801 100644 --- a/tox.ini +++ b/tox.ini @@ -3,9 +3,12 @@ envlist=py26,py27,py33,pypy [testenv] commands=py.test [] -deps=pytest +deps= + pytest + mock [testenv:py26] deps= pytest unittest2 + mock