From 4f918041e3dcf247c371020c64f3659b6b134de5 Mon Sep 17 00:00:00 2001 From: Selwin Ong Date: Fri, 11 Apr 2014 16:47:31 +0700 Subject: [PATCH] Rearranged how explicit kwargs are passed into queue.enqueue(). Fixes #322 --- rq/queue.py | 17 +++++++---------- tests/fixtures.py | 4 ++++ tests/test_queue.py | 29 ++++++++++++++++++++++++++++- 3 files changed, 39 insertions(+), 11 deletions(-) diff --git a/rq/queue.py b/rq/queue.py index 49d43d7..730022f 100644 --- a/rq/queue.py +++ b/rq/queue.py @@ -190,17 +190,14 @@ class Queue(object): # Detect explicit invocations, i.e. of the form: # q.enqueue(foo, args=(1, 2), kwargs={'a': 1}, timeout=30) - timeout = None - description = None - result_ttl = None - depends_on = None - if 'args' in kwargs or 'kwargs' in kwargs or 'depends_on' in kwargs: + timeout = kwargs.pop('timeout', None) + description = kwargs.pop('description', None) + result_ttl = kwargs.pop('result_ttl', None) + depends_on = kwargs.pop('depends_on', None) + + if 'args' in kwargs or 'kwargs' in kwargs: assert args == (), 'Extra positional arguments cannot be used when using explicit args and kwargs.' # noqa - timeout = kwargs.pop('timeout', None) - description = kwargs.pop('description', None) - args = kwargs.pop('args', None) - result_ttl = kwargs.pop('result_ttl', None) - depends_on = kwargs.pop('depends_on', None) + args = kwargs.pop('args', None) kwargs = kwargs.pop('kwargs', None) return self.enqueue_call(func=f, args=args, kwargs=kwargs, diff --git a/tests/fixtures.py b/tests/fixtures.py index 337fc70..eaf426e 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -49,6 +49,10 @@ def access_self(): return job.id +def echo(*args, **kwargs): + return (args, kwargs) + + class Number(object): def __init__(self, value): self.value = value diff --git a/tests/test_queue.py b/tests/test_queue.py index 6dde3ea..8106b8d 100644 --- a/tests/test_queue.py +++ b/tests/test_queue.py @@ -1,5 +1,5 @@ from tests import RQTestCase -from tests.fixtures import Number, div_by_zero, say_hello, some_calculation +from tests.fixtures import Number, div_by_zero, echo, say_hello, some_calculation from rq import Queue, get_failed_queue from rq.job import Job, Status from rq.worker import Worker @@ -260,6 +260,33 @@ class TestQueue(RQTestCase): job = q.enqueue(say_hello) self.assertEqual(job.get_status(), Status.QUEUED) + def test_enqueue_explicit_args(self): + """enqueue() works for both implicit/explicit args.""" + q = Queue() + + # Implicit args/kwargs mode + job = q.enqueue(echo, 1, timeout=1, result_ttl=1, bar='baz') + self.assertEqual(job.timeout, 1) + self.assertEqual(job.result_ttl, 1) + self.assertEqual( + job.perform(), + ((1,), {'bar': 'baz'}) + ) + + # Explicit kwargs mode + kwargs = { + 'timeout': 1, + 'result_ttl': 1, + } + job = q.enqueue(echo, timeout=2, result_ttl=2, args=[1], kwargs=kwargs) + self.assertEqual(job.timeout, 2) + self.assertEqual(job.result_ttl, 2) + self.assertEqual( + job.perform(), + ((1,), {'timeout': 1, 'result_ttl': 1}) + ) + + def test_all_queues(self): """All queues""" q1 = Queue('first-queue')