mirror of https://github.com/rq/rq.git
Add equality and comparison methods.
This commit is contained in:
parent
a5ea45af57
commit
b4c1c85276
18
rq/queue.py
18
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
|
||||
|
|
|
@ -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')
|
||||
|
|
Loading…
Reference in New Issue