From 39f106cdb305cf5dd5d9e634c7d56772934c06ad Mon Sep 17 00:00:00 2001 From: Vincent Driessen Date: Mon, 13 Feb 2012 09:06:39 +0100 Subject: [PATCH] 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. --- tests/__init__.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/__init__.py b/tests/__init__.py index b912bed6..b833a91e 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -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)