@ -11,13 +11,11 @@ from tests.fixtures import decorated_job
class TestDecorator ( RQTestCase ) :
def setUp ( self ) :
super ( ) . setUp ( )
def test_decorator_preserves_functionality ( self ) :
""" Ensure that a decorated function ' s functionality is still preserved.
"""
""" Ensure that a decorated function ' s functionality is still preserved. """
self . assertEqual ( decorated_job ( 1 , 2 ) , 3 )
def test_decorator_adds_delay_attr ( self ) :
@ -34,9 +32,11 @@ class TestDecorator(RQTestCase):
""" Ensure that passing in queue name to the decorator puts the job in
the right queue .
"""
@job ( queue = ' queue_name ' )
def hello ( ) :
return ' Hi '
result = hello . delay ( )
self . assertEqual ( result . origin , ' queue_name ' )
@ -51,12 +51,12 @@ class TestDecorator(RQTestCase):
@job ( ' default ' , result_ttl = 10 )
def hello ( ) :
return ' Why hello '
result = hello . delay ( )
self . assertEqual ( result . result_ttl , 10 )
def test_decorator_accepts_ttl_as_argument ( self ) :
""" Ensure that passing in ttl to the decorator sets the ttl on the job
"""
""" Ensure that passing in ttl to the decorator sets the ttl on the job """
# Ensure default
result = decorated_job . delay ( 1 , 2 )
self . assertEqual ( result . ttl , None )
@ -64,12 +64,12 @@ class TestDecorator(RQTestCase):
@job ( ' default ' , ttl = 30 )
def hello ( ) :
return ' Hello '
result = hello . delay ( )
self . assertEqual ( result . ttl , 30 )
def test_decorator_accepts_meta_as_argument ( self ) :
""" Ensure that passing in meta to the decorator sets the meta on the job
"""
""" Ensure that passing in meta to the decorator sets the meta on the job """
# Ensure default
result = decorated_job . delay ( 1 , 2 )
self . assertEqual ( result . meta , { } )
@ -82,6 +82,7 @@ class TestDecorator(RQTestCase):
@job ( ' default ' , meta = test_meta )
def hello ( ) :
return ' Hello '
result = hello . delay ( )
self . assertEqual ( result . meta , test_meta )
@ -153,16 +154,19 @@ class TestDecorator(RQTestCase):
""" Ensure that passing in on_failure function to the decorator sets the
correct on_failure function on the job .
"""
# Only functions and builtins are supported as callback
@job ( ' default ' , on_failure = Job . fetch )
def foo ( ) :
return ' Foo '
with self . assertRaises ( ValueError ) :
result = foo . delay ( )
@job ( ' default ' , on_failure = print )
def hello ( ) :
return ' Hello '
result = hello . delay ( )
result_job = Job . fetch ( id = result . id , connection = self . testconn )
self . assertEqual ( result_job . failure_callback , print )
@ -171,23 +175,26 @@ class TestDecorator(RQTestCase):
""" Ensure that passing in on_failure function to the decorator sets the
correct on_success function on the job .
"""
# Only functions and builtins are supported as callback
@job ( ' default ' , on_failure = Job . fetch )
def foo ( ) :
return ' Foo '
with self . assertRaises ( ValueError ) :
result = foo . delay ( )
@job ( ' default ' , on_success = print )
def hello ( ) :
return ' Hello '
result = hello . delay ( )
result_job = Job . fetch ( id = result . id , connection = self . testconn )
self . assertEqual ( result_job . success_callback , print )
@mock.patch ( ' rq.queue.resolve_connection ' )
def test_decorator_connection_laziness ( self , resolve_connection ) :
""" Ensure that job decorator resolve connection in `lazy` way """
""" Ensure that job decorator resolve connection in `lazy` way """
resolve_connection . return_value = Redis ( )
@ -207,12 +214,11 @@ class TestDecorator(RQTestCase):
def test_decorator_custom_queue_class ( self ) :
""" Ensure that a custom queue class can be passed to the job decorator """
class CustomQueue ( Queue ) :
pass
CustomQueue . enqueue_call = mock . MagicMock (
spec = lambda * args , * * kwargs : None ,
name = ' enqueue_call '
)
CustomQueue . enqueue_call = mock . MagicMock ( spec = lambda * args , * * kwargs : None , name = ' enqueue_call ' )
custom_decorator = job ( queue = ' default ' , queue_class = CustomQueue )
self . assertIs ( custom_decorator . queue_class , CustomQueue )
@ -226,12 +232,11 @@ class TestDecorator(RQTestCase):
def test_decorate_custom_queue ( self ) :
""" Ensure that a custom queue instance can be passed to the job decorator """
class CustomQueue ( Queue ) :
pass
CustomQueue . enqueue_call = mock . MagicMock (
spec = lambda * args , * * kwargs : None ,
name = ' enqueue_call '
)
CustomQueue . enqueue_call = mock . MagicMock ( spec = lambda * args , * * kwargs : None , name = ' enqueue_call ' )
queue = CustomQueue ( )
@job ( queue = queue )
@ -252,11 +257,12 @@ class TestDecorator(RQTestCase):
@job ( ' default ' , failure_ttl = 10 )
def hello ( ) :
return ' Why hello '
result = hello . delay ( )
self . assertEqual ( result . failure_ttl , 10 )
def test_decorator_custom_retry ( self ) :
""" Ensure that passing in retry to the decorator sets the
""" Ensure that passing in retry to the decorator sets the
retry on the job
"""
# Ensure default
@ -267,6 +273,7 @@ class TestDecorator(RQTestCase):
@job ( ' default ' , retry = Retry ( 3 , [ 2 ] ) )
def hello ( ) :
return ' Why hello '
result = hello . delay ( )
self . assertEqual ( result . retries_left , 3 )
self . assertEqual ( result . retry_intervals , [ 2 ] )