Have the test suite find an empty Redis database.

Since the test suite `flushdb()`'s after running each test, we should
make sure the database is empty before we even start running tests.
This patch will make sure to never destroy any local production data
inside the running Redis instance.

This fixes #25.
main
Vincent Driessen 13 years ago
parent 90a458ca8e
commit 39f106cdb3

@ -14,6 +14,18 @@ def failing_job(x):
return x / 0 return x / 0
def find_empty_redis_database():
"""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):
testconn = Redis(db=dbnum)
empty = len(testconn.keys('*')) == 0
if empty:
return testconn
assert False, 'No empty Redis database found to run tests in.'
class RQTestCase(unittest.TestCase): class RQTestCase(unittest.TestCase):
"""Base class to inherit test cases from for RQ. """Base class to inherit test cases from for RQ.
@ -27,7 +39,7 @@ class RQTestCase(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
# Set up connection to Redis # Set up connection to Redis
testconn = Redis() testconn = find_empty_redis_database()
conn.push(testconn) conn.push(testconn)
# Store the connection (for sanity checking) # Store the connection (for sanity checking)

Loading…
Cancel
Save