comparison test/test_redis_session.py @ 7398:07f8737fe04c

Skip redis tests if unable to communicate with the server. If the redis module is in the test environment, the redis tests will not be skipped. If connecting to redis during testing fails with a ConnectionError because there is no redis server at localhost, or if it fails with an AuthenticationError, you would fail a slew of tests. This causes the tests to report as skipped if either of the two errors occurs. It is very inefficient as it fails in setup() for the tests, but at least it does report skipping the tests. Also documented how to pass the redis password to the tests in the test part of the install docs. Future note: running tests needs proper docs in development.txt (including database setup) and a link left to that doc in installation.txt.
author John Rouillard <rouilj@ieee.org>
date Wed, 24 May 2023 12:52:43 -0400
parents 75a5946cf897
children 1b7162938988
comparison
equal deleted inserted replaced
7397:6795bd384115 7398:07f8737fe04c
21 import pytest 21 import pytest
22 22
23 try: 23 try:
24 from roundup.backends.sessions_redis import Sessions, OneTimeKeys 24 from roundup.backends.sessions_redis import Sessions, OneTimeKeys
25 skip_redis = lambda func, *args, **kwargs: func 25 skip_redis = lambda func, *args, **kwargs: func
26
27 from redis import AuthenticationError, ConnectionError
26 except ImportError as e: 28 except ImportError as e:
27 from .pytest_patcher import mark_class 29 from .pytest_patcher import mark_class
28 skip_redis = mark_class(pytest.mark.skip( 30 skip_redis = mark_class(pytest.mark.skip(
29 reason='Skipping redis tests: redis module not available')) 31 reason='Skipping redis tests: redis module not available'))
30 32
33 35
34 from .session_common import SessionTest 36 from .session_common import SessionTest
35 37
36 class RedisSessionTest(SessionTest): 38 class RedisSessionTest(SessionTest):
37 def setUp(self): 39 def setUp(self):
40 '''This must not be called if redis can not be loaded. It will
41 cause an error since the ConnectionError and
42 AuthenticationError exceptions aren't defined.
43 '''
44
38 SessionTest.setUp(self) 45 SessionTest.setUp(self)
39 46
40 import os 47 import os
41 if 'pytest_redis_pw' in os.environ: 48 if 'pytest_redis_pw' in os.environ:
42 pw = os.environ['pytest_redis_pw'] 49 pw = os.environ['pytest_redis_pw']
55 'redis://%slocalhost:6379/15?health_check_interval=2' % pw 62 'redis://%slocalhost:6379/15?health_check_interval=2' % pw
56 self.db.Session = None 63 self.db.Session = None
57 self.db.Otk = None 64 self.db.Otk = None
58 self.sessions = self.db.getSessionManager() 65 self.sessions = self.db.getSessionManager()
59 self.otks = self.db.getOTKManager() 66 self.otks = self.db.getOTKManager()
67
68 try:
69 self.sessions.redis.keys()
70 except (AuthenticationError, ConnectionError) as e:
71 self.skipTest('Redis server unavailable: "%s".' % e)
60 72
61 # database should be empty. Verify so we don't clobber 73 # database should be empty. Verify so we don't clobber
62 # somebody's working database. 74 # somebody's working database.
63 self.assertEqual(self.sessions.redis.keys(), [], 75 self.assertEqual(self.sessions.redis.keys(), [],
64 "Tests will not run on a db with keys. " 76 "Tests will not run on a db with keys. "

Roundup Issue Tracker: http://roundup-tracker.org/