rq/tests/__init__.py

79 lines
2.3 KiB
Python
Raw Normal View History

import logging
import os
2014-05-05 08:49:29 +00:00
from redis import Redis
2014-05-05 08:49:29 +00:00
from rq import pop_connection, push_connection
import unittest
2012-02-24 10:36:33 +00:00
def find_empty_redis_database(ssl=False):
"""Tries to connect to a random Redis database (starting from 4), and
will use/connect it when no keys are in there.
"""
for dbnum in range(4, 17):
connection_kwargs = {'db': dbnum}
if ssl:
connection_kwargs['port'] = 9736
connection_kwargs['ssl'] = True
connection_kwargs['ssl_cert_reqs'] = None # disable certificate validation
testconn = Redis(**connection_kwargs)
empty = testconn.dbsize() == 0
if empty:
return testconn
assert False, 'No empty Redis database found to run tests in.'
def slow(f):
return unittest.skipUnless(os.environ.get('RUN_SLOW_TESTS_TOO'), "Slow tests disabled")(f)
def ssl_test(f):
return unittest.skipUnless(os.environ.get('RUN_SSL_TESTS'), "SSL tests disabled")(f)
2012-01-28 06:58:40 +00:00
class RQTestCase(unittest.TestCase):
"""Base class to inherit test cases from for RQ.
It sets up the Redis connection (available via self.testconn), turns off
logging to the terminal and flushes the Redis database before and after
running each test.
Also offers assertQueueContains(queue, that_func) assertion method.
"""
@classmethod
def setUpClass(cls):
# Set up connection to Redis
testconn = find_empty_redis_database()
push_connection(testconn)
2012-01-28 06:58:40 +00:00
# Store the connection (for sanity checking)
cls.testconn = testconn
cls.connection = testconn
2012-01-28 06:58:40 +00:00
# Shut up logging
logging.disable(logging.ERROR)
2012-01-28 06:58:40 +00:00
def setUp(self):
# Flush beforewards (we like our hygiene)
self.testconn.flushdb()
2012-01-28 06:58:40 +00:00
def tearDown(self):
# Flush afterwards
self.testconn.flushdb()
2012-01-28 06:58:40 +00:00
# Implement assertIsNotNone for Python runtimes < 2.7 or < 3.1
if not hasattr(unittest.TestCase, 'assertIsNotNone'):
2017-09-08 09:28:10 +00:00
def assertIsNotNone(self, value, *args): # noqa
self.assertNotEqual(value, None, *args)
2012-01-28 06:58:40 +00:00
@classmethod
def tearDownClass(cls):
logging.disable(logging.NOTSET)
2012-01-28 06:58:40 +00:00
# Pop the connection to Redis
testconn = pop_connection()
2014-05-05 08:49:43 +00:00
assert testconn == cls.testconn, \
'Wow, something really nasty happened to the Redis connection stack. Check your setup.'