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