From 4d0ae5da981af5ccc40f655ff287077a847595e5 Mon Sep 17 00:00:00 2001 From: Vincent Driessen Date: Wed, 3 Jun 2015 09:52:26 +0200 Subject: [PATCH] Clean up type checking of Worker constructor --- rq/exceptions.py | 4 --- rq/worker.py | 4 +-- tests/test_worker.py | 60 ++++++++++++++++++-------------------------- 3 files changed, 27 insertions(+), 41 deletions(-) diff --git a/rq/exceptions.py b/rq/exceptions.py index 94b22bf..88bbbb0 100644 --- a/rq/exceptions.py +++ b/rq/exceptions.py @@ -11,10 +11,6 @@ class InvalidJobOperationError(Exception): pass -class NoQueueError(Exception): - pass - - class UnpickleError(Exception): def __init__(self, message, raw_data, inner_exception=None): super(UnpickleError, self).__init__(message, inner_exception) diff --git a/rq/worker.py b/rq/worker.py index 27df2dd..ee048c3 100644 --- a/rq/worker.py +++ b/rq/worker.py @@ -17,7 +17,7 @@ import warnings from rq.compat import as_text, string_types, text_type from .connections import get_current_connection -from .exceptions import DequeueTimeout, NoQueueError +from .exceptions import DequeueTimeout from .job import Job, JobStatus from .logutils import setup_loghandlers from .queue import get_failed_queue, Queue @@ -163,7 +163,7 @@ class Worker(object): """Sanity check for the given queues.""" for queue in self.queues: if not isinstance(queue, self.queue_class): - raise NoQueueError('{0} is not a queue'.format(queue)) + raise TypeError('{0} is not a Queue or a string'.format(queue)) def process_queue_args(self, queue_args): """Allow for a string, a queue an iterable of strings or an iterable of queues""" diff --git a/tests/test_worker.py b/tests/test_worker.py index 62d2876..016f83c 100644 --- a/tests/test_worker.py +++ b/tests/test_worker.py @@ -13,7 +13,6 @@ from tests.helpers import strip_microseconds from rq import get_failed_queue, Queue, SimpleWorker, Worker from rq.compat import as_text -from rq.exceptions import NoQueueError from rq.job import Job, JobStatus from rq.registry import StartedJobRegistry from rq.suspension import resume, suspend @@ -26,44 +25,35 @@ class CustomJob(Job): class TestWorker(RQTestCase): def test_create_worker(self): - """Worker creation.""" - fooq, barq = Queue('foo'), Queue('bar') - w = Worker([fooq, barq]) - self.assertEquals(w.queues, [fooq, barq]) - - def test_create_worker_args_single_queue(self): - """Test Worker creation with single queue instance arg""" - fooq = Queue('foo') - w = Worker(fooq) - self.assertEquals(w.queue_keys(), ['rq:queue:foo']) + """Worker creation using various inputs.""" - def test_create_worker_args_single_string(self): - """ Test Worker creation with single string arg""" + # With single string argument w = Worker('foo') - self.assertEquals(w.queue_keys(),['rq:queue:foo']) + self.assertEquals(w.queues[0].name, 'foo') - def test_create_worker_args_iterable_strings(self): - """ Test Worker creation with iterable of strings""" + # With list of strings w = Worker(['foo', 'bar']) - self.assertEquals(w.queue_keys(),['rq:queue:foo', 'rq:queue:bar']) - - def test_create_worker_args_iterable_queues(self): - """ Test Worker test worker creation - with an iterable of queue instance args""" - w = Worker(map(Queue, ['foo', 'bar'])) - self.assertEquals(w.queue_keys(),['rq:queue:foo', 'rq:queue:bar']) - - def test_create_worker_args_list_map(self): - """ Test Worker test worker creation - with a list of queue from map""" - w = Worker(list(map(Queue, ['foo', 'bar']))) - self.assertEquals(w.queue_keys(),['rq:queue:foo', 'rq:queue:bar']) - - def test_create_worker_raises_noqueue_error(self): - """ make sure raises noqueue error if a - a non string or queue is passed""" - with self.assertRaises(NoQueueError): - w = Worker([1]) + self.assertEquals(w.queues[0].name, 'foo') + self.assertEquals(w.queues[1].name, 'bar') + + # With iterable of strings + w = Worker(iter(['foo', 'bar'])) + self.assertEquals(w.queues[0].name, 'foo') + self.assertEquals(w.queues[1].name, 'bar') + + # With single Queue + w = Worker(Queue('foo')) + self.assertEquals(w.queues[0].name, 'foo') + + # With iterable of Queues + w = Worker(iter([Queue('foo'), Queue('bar')])) + self.assertEquals(w.queues[0].name, 'foo') + self.assertEquals(w.queues[1].name, 'bar') + + # With list of Queues + w = Worker([Queue('foo'), Queue('bar')]) + self.assertEquals(w.queues[0].name, 'foo') + self.assertEquals(w.queues[1].name, 'bar') def test_work_and_quit(self): """Worker processes work, then quits."""