comparison test/test_redis_session.py @ 6814:3f60a71b0812

Summary: Support selecion session/otk data store. Add redis as data store. Allow admin to select the backend data store. Compatibility matrix: main\/ session>| anydbm | sqlite | redis | mysql | postgresql | anydbm | D | | X | | | sqlite | X | D | X | | | mysql | | | | D | | postgresql | | | | | D | --------------------------------------------------------------+ D - default if unconfigured, X - compatible choice DETAILS roundup/configuration.py: add config.ini section sessiondb with settings: backend and redis_url. CHANGES.txt, doc/admin_guide.txt, doc/installation.txt, doc/upgrading.txt: doc on config of session db and redis. Plus some other fixes: admin - clarified why we do not drop __words and __testids table in native-fts conversion. TYpo fix. upgrading - doc how you can keep using anydbm for session data with sqlite. Fix dupe sentence in an upgrading config.ini section. roundup/backends/back_anydbm.py, roundup/backends/back_sqlite.py: code to support redis, redis/anydbm backends respectively. roundup/backends/sessions_redis.py new storage backend for redis. roundup/rest.py, roundup/cgi/actions.py, roundup/cgi/templating.py redis uses a different way of calculating lifetime/timestamp. Since expiration of an item occurred if its timestamp was more than 1 week old, code would calculate: now - 1 week + lifetime. But this results in faster expiration in redis if used for lifetime/timestamp. Convert code to use the lifetime() method in BasicDatabase that generates the right timestamp for each backend. test/session_common.py: added tests for more cases, get without default, getall non-existing key etc. timestamp test changed to use new self.get_ts which is overridden in other tests. Test that datatypes survive storage. test/test_redis_session.py: test redis session store with sqlite and anydbm primary databases test/test_anydbm.py, test/test_sqlite.py add test to make sure the databases are properly set up sqlite - add test cases where anydbm is used as datastore anydbm - remove updateTimestamp override add get_ts(). test/test_config.py tests on redis_url and compatibility on choice of sessiondb backend .travis.yml: add redis db and redis-py
author John Rouillard <rouilj@ieee.org>
date Thu, 04 Aug 2022 14:41:58 -0400
parents
children 35f0952f4bc5
comparison
equal deleted inserted replaced
6813:6b636fb29740 6814:3f60a71b0812
1 #
2 # Copyright (c) 2001 Bizar Software Pty Ltd (http://www.bizarsoftware.com.au/)
3 # This module is free software, and you may redistribute it and/or modify
4 # under the same terms as Python, so long as this copyright message and
5 # disclaimer are retained in their original form.
6 #
7 # IN NO EVENT SHALL BIZAR SOFTWARE PTY LTD BE LIABLE TO ANY PARTY FOR
8 # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
9 # OUT OF THE USE OF THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE
10 # POSSIBILITY OF SUCH DAMAGE.
11 #
12 # BIZAR SOFTWARE PTY LTD SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
13 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
14 # FOR A PARTICULAR PURPOSE. THE CODE PROVIDED HEREUNDER IS ON AN "AS IS"
15 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
17
18 import unittest, os, shutil, time
19 from roundup.anypy import strings
20
21 import pytest
22
23 try:
24 from roundup.backends.sessions_redis import Sessions, OneTimeKeys
25 skip_redis = lambda func, *args, **kwargs: func
26 except ModuleNotFoundError as e:
27 from .pytest_patcher import mark_class
28 skip_redis = mark_class(pytest.mark.skip(
29 reason='Skipping redis tests: redis module not available'))
30
31 from .test_sqlite import sqliteOpener
32 from .test_anydbm import anydbmOpener
33
34 from .session_common import SessionTest
35
36 class RedisSessionTest(SessionTest):
37 def setUp(self):
38 SessionTest.setUp(self)
39
40 # redefine the session db's as redis.
41 self.db.config.SESSIONDB_BACKEND = "redis"
42 self.db.config.SESSIONDB_REDIS_URL = 'redis://:roundupSysadmin@localhost:6379/15?health_check_interval=2'
43 self.db.Session = None
44 self.db.Otk = None
45 self.sessions = self.db.getSessionManager()
46 self.otks = self.db.getOTKManager()
47
48 # database should be empty. Verify so we don't clobber
49 # somebody's working database.
50 self.assertEqual(self.sessions.redis.keys(), [],
51 "Tests will not run on a db with keys. "
52 "Run flushdb in 'redis-cli -n 15 -p 6379 -h localhost' "
53 "to empty db first")
54 self.assertEqual(self.otks.redis.keys(), [],
55 "Tests will not run on a db with keys. "
56 "Run flushdb in 'redis-cli -n 15 -p 6379 -h localhost' "
57 "to empty db first")
58
59 def tearDown(self):
60 self.sessions.clear()
61 self.otks.clear()
62
63 SessionTest.tearDown(self)
64
65 # reset to default session backend
66 self.db.config.SESSIONDB_BACKEND = ""
67 self.db.Session = None
68 self.db.Otk = None
69 self.sessions = self.db.getSessionManager()
70 self.otks = self.db.getOTKManager()
71
72
73 def get_ts(self, key="random_session"):
74 db_tstamp = self.db.Session.redis.ttl(
75 self.db.Session.makekey(key)) + \
76 time.time()
77 print(db_tstamp)
78 return (db_tstamp,)
79
80 @skip_redis
81 class redis_sqliteSessionTest(sqliteOpener, RedisSessionTest, unittest.TestCase):
82 s2b = lambda x,y : y
83
84 def testLifetime(self):
85 ts = self.sessions.lifetime(300)
86 print(ts)
87 now = time.time()
88 print(now)
89 self.assertGreater(now + 302, ts)
90 self.assertLess(now + 298, ts)
91
92 def testDbType(self):
93 self.assertIn("roundlite", repr(self.db))
94 self.assertIn("roundup.backends.sessions_redis.Sessions", repr(self.db.Session))
95
96 @skip_redis
97 class redis_anydbmSessionTest(anydbmOpener, RedisSessionTest, unittest.TestCase):
98 s2b = lambda x,y: strings.s2b(y)
99
100 def testLifetime(self):
101 ts = self.sessions.lifetime(300)
102 print(ts)
103 now = time.time()
104 print(now)
105 self.assertGreater(now + 302, ts)
106 self.assertLess(now + 298, ts)
107
108 def testDbType(self):
109 self.assertIn("back_anydbm", repr(self.db))
110 self.assertIn("roundup.backends.sessions_redis.Sessions", repr(self.db.Session))

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