From dd994142b3454a2d7c168b30026c18dd98c0377d Mon Sep 17 00:00:00 2001 From: msoedov Date: Sun, 4 May 2014 15:34:42 +0300 Subject: [PATCH] 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)