Add equality and comparison methods.

main
Vincent Driessen 13 years ago
parent a5ea45af57
commit b4c1c85276

@ -1,4 +1,5 @@
import uuid import uuid
from functools import total_ordering
from pickle import loads, dumps from pickle import loads, dumps
from .proxy import conn from .proxy import conn
@ -35,6 +36,7 @@ class Job(object):
return self.func(*self.args, **self.kwargs) return self.func(*self.args, **self.kwargs)
@total_ordering
class Queue(object): class Queue(object):
redis_queue_namespace_prefix = 'rq:' redis_queue_namespace_prefix = 'rq:'
@ -118,5 +120,21 @@ class Queue(object):
return job 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): def __str__(self):
return self.name return self.name

@ -60,6 +60,19 @@ class TestQueue(RQTestCase):
q = Queue() q = Queue()
self.assertEquals(q.name, 'default') 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): def test_queue_empty(self):
"""Detecting empty queues.""" """Detecting empty queues."""
q = Queue('my-queue') q = Queue('my-queue')

Loading…
Cancel
Save