diff --git a/rq/queue.py b/rq/queue.py index 793fb39..f43bdd0 100644 --- a/rq/queue.py +++ b/rq/queue.py @@ -1,4 +1,5 @@ import uuid +from functools import total_ordering from pickle import loads, dumps from .proxy import conn @@ -35,6 +36,7 @@ class Job(object): return self.func(*self.args, **self.kwargs) +@total_ordering class Queue(object): redis_queue_namespace_prefix = 'rq:' @@ -118,5 +120,21 @@ class Queue(object): return job + # Total ordering defition (the rest of the required Python methods are + # auto-generated by the @total_ordering decorator) + def __eq__(self, other): + if not isinstance(other, Queue): + raise TypeError('Cannot compare queues to other objects.') + return self.name == other.name + + def __lt__(self, other): + if not isinstance(other, Queue): + raise TypeError('Cannot compare queues to other objects.') + return self.name <= other.name + + def __hash__(self): + return hash(self.name) + + def __str__(self): return self.name diff --git a/tests/test_rq.py b/tests/test_rq.py index fb8dced..1701537 100644 --- a/tests/test_rq.py +++ b/tests/test_rq.py @@ -60,6 +60,19 @@ class TestQueue(RQTestCase): q = Queue() self.assertEquals(q.name, 'default') + + def test_equality(self): + """Mathematical equality of queues.""" + q1 = Queue('foo') + q2 = Queue('foo') + q3 = Queue('bar') + + self.assertEquals(q1, q2) + self.assertEquals(q2, q1) + self.assertNotEquals(q1, q3) + self.assertNotEquals(q2, q3) + + def test_queue_empty(self): """Detecting empty queues.""" q = Queue('my-queue')