Mercurial > p > roundup > code
comparison test/session_common.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 | 375d40a9e730 |
| children | fe0091279f50 |
comparison
equal
deleted
inserted
replaced
| 6813:6b636fb29740 | 6814:3f60a71b0812 |
|---|---|
| 52 self.sessions.set('random_key', text='hello, world!') | 52 self.sessions.set('random_key', text='hello, world!') |
| 53 self.sessions.set('random_key2', text='hello, world!') | 53 self.sessions.set('random_key2', text='hello, world!') |
| 54 self.assertEqual(self.sessions.list().sort(), | 54 self.assertEqual(self.sessions.list().sort(), |
| 55 [self.s2b('random_key'), self.s2b('random_key2')].sort()) | 55 [self.s2b('random_key'), self.s2b('random_key2')].sort()) |
| 56 | 56 |
| 57 def testGetMissingKey(self): | 57 def testGetGetAllMissingKey(self): |
| 58 self.sessions.set('random_key', text='hello, world!', otherval='bar') | 58 self.assertEqual(self.sessions.get('badc_key', |
| 59 'text', 'default_val'), | |
| 60 'default_val') | |
| 61 | |
| 59 with self.assertRaises(KeyError) as e: | 62 with self.assertRaises(KeyError) as e: |
| 60 self.sessions.get('badc_key', 'text') | 63 self.sessions.get('badc_key', 'text') |
| 64 | |
| 65 with self.assertRaises(KeyError) as e: | |
| 66 self.sessions.getall('badc_key') | |
| 61 | 67 |
| 62 def testGetAll(self): | 68 def testGetAll(self): |
| 63 self.sessions.set('random_key', text='hello, world!', otherval='bar') | 69 self.sessions.set('random_key', text='hello, world!', otherval='bar') |
| 64 self.assertEqual(self.sessions.getall('random_key'), | 70 self.assertEqual(self.sessions.getall('random_key'), |
| 65 {'text': 'hello, world!', 'otherval': 'bar'}) | 71 {'text': 'hello, world!', 'otherval': 'bar'}) |
| 119 item = self.sessions.get('random_key', "text") | 125 item = self.sessions.get('random_key', "text") |
| 120 item_ts = self.sessions.get('random_key', "__timestamp") | 126 item_ts = self.sessions.get('random_key', "__timestamp") |
| 121 self.assertEqual(item, 'hello, world2!') | 127 self.assertEqual(item, 'hello, world2!') |
| 122 self.assertAlmostEqual(ts, item_ts, 2) | 128 self.assertAlmostEqual(ts, item_ts, 2) |
| 123 | 129 |
| 124 # overridden in dbm and memory backends | 130 # overridden in test_memory |
| 125 def testUpdateTimestamp(self): | 131 def testUpdateTimestamp(self): |
| 126 def get_ts_via_sql(self): | 132 # make sure timestamp is older than one minute so update |
| 127 sql = '''select %(name)s_time from %(name)ss | 133 # will apply |
| 128 where %(name)s_key = '%(session)s';'''% \ | |
| 129 {'name': self.sessions.name, | |
| 130 'session': 'random_session'} | |
| 131 | |
| 132 self.sessions.cursor.execute(sql) | |
| 133 db_tstamp = self.sessions.cursor.fetchone() | |
| 134 return db_tstamp | |
| 135 | |
| 136 # make sure timestamp is older than one minute so update will apply | |
| 137 timestamp = time.time() - 62 | 134 timestamp = time.time() - 62 |
| 138 self.sessions.set('random_session', text='hello, world!', | 135 self.sessions.set('random_session', text='hello, world!', |
| 139 __timestamp=timestamp) | 136 __timestamp=timestamp) |
| 140 | 137 |
| 141 self.sessions.updateTimestamp('random_session') | 138 self.sessions.updateTimestamp('random_session') |
| 146 # for dbm. | 143 # for dbm. |
| 147 #self.assertNotEqual (self.sessions.get('random_session', | 144 #self.assertNotEqual (self.sessions.get('random_session', |
| 148 # '__timestamp'), | 145 # '__timestamp'), |
| 149 # timestamp) | 146 # timestamp) |
| 150 | 147 |
| 151 # use 61 to allow a fudge factor | 148 # use 61 to allow a 1 second delay in test |
| 152 self.assertGreater(get_ts_via_sql(self)[0] - timestamp, 61) | 149 self.assertGreater(self.get_ts()[0] - timestamp, 61) |
| 150 | |
| 151 # overridden in test_anydbm | |
| 152 def get_ts(self, key="random_session"): | |
| 153 sql = '''select %(name)s_time from %(name)ss | |
| 154 where %(name)s_key = '%(session)s';'''% \ | |
| 155 {'name': self.sessions.name, | |
| 156 'session': key} | |
| 157 | |
| 158 self.sessions.cursor.execute(sql) | |
| 159 db_tstamp = self.sessions.cursor.fetchone() | |
| 160 return db_tstamp | |
| 161 | |
| 162 def testDataTypes(self): | |
| 163 """make sure all data survives a round trip through the | |
| 164 session database including data types. | |
| 165 | |
| 166 Found this was a problem when trying to store the | |
| 167 data using a redis hash that has no native data types | |
| 168 for booleans and numbers get returned by redis module | |
| 169 as strings. | |
| 170 """ | |
| 171 in_data = {"text": 'hello, world!', | |
| 172 "integer": 56, | |
| 173 "float": 3.1425, | |
| 174 "list": [ 1, "Two", 3.0, "Four" ], | |
| 175 "boolean": True, | |
| 176 "tuple": ("f", 4), | |
| 177 } | |
| 178 | |
| 179 self.sessions.set('random_data', **in_data) | |
| 180 out_data = self.sessions.getall('random_data') | |
| 181 self.assertEqual(in_data, out_data) | |
| 153 | 182 |
| 154 def testLifetime(self): | 183 def testLifetime(self): |
| 155 ts = self.sessions.lifetime(300) | 184 ts = self.sessions.lifetime(300) |
| 156 week_ago = time.time() - 60*60*24*7 | 185 week_ago = time.time() - 60*60*24*7 |
| 157 self.assertGreater(week_ago + 302, ts) | 186 self.assertGreater(week_ago + 302, ts) |
