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.
This commit is contained in:
Vincent Driessen 2012-02-13 09:06:39 +01:00
parent 90a458ca8e
commit 39f106cdb3
1 changed files with 13 additions and 1 deletions

View File

@ -14,6 +14,18 @@ def failing_job(x):
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):
"""Base class to inherit test cases from for RQ.
@ -27,7 +39,7 @@ class RQTestCase(unittest.TestCase):
@classmethod
def setUpClass(cls):
# Set up connection to Redis
testconn = Redis()
testconn = find_empty_redis_database()
conn.push(testconn)
# Store the connection (for sanity checking)