Mercurial > p > roundup > code
comparison test/session_common.py @ 6808:375d40a9e730
Add tests to BasicDatabase and merge implementations
test/session_common.py:
add new tests:
* test set with bad __timestamps
* test get on missing item with no default
* test clear() method
* test new lifetime() method
everything below here was needed to get the tests to work across all
db's.
roundup/backends/sessions_dbm.py:
make set() validate __timestamp as float. If invalid and item is new,
set it to time.time(). If invalid and item exists keep original
timestamp.
add lifetime(key_lifetime) method. Given key_lifetime in seconds,
generate a __timestamp that will be deleted after that many seconds.
roundup/backends/sessions_rdbms.py:
make set() behave the same as session_dbm.
add lifetime method as above.
roundup/backends/sessions_sqlite.py:
import session_rdbms::BasicDatabase and override __init__.
rather than cloning all the code. Kept a few logging and sql methods.
roundup/test/memorydb.py:
make get() on a missing key return KeyError
make set() conform to dbm/rdbms implementations.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Mon, 25 Jul 2022 21:21:26 -0400 |
| parents | bdd28b244839 |
| children | 3f60a71b0812 |
comparison
equal
deleted
inserted
replaced
| 6807:30bb17dc9f82 | 6808:375d40a9e730 |
|---|---|
| 1 import os, shutil, time, unittest | 1 import os, shutil, time, unittest |
| 2 | 2 |
| 3 from .db_test_base import config | 3 from .db_test_base import config |
| 4 | 4 |
| 5 """ | |
| 6 here are three different impementations for these. I am trying to fix | |
| 7 them so they all act the same. | |
| 5 | 8 |
| 9 set with invalid timestamp: | |
| 10 | |
| 11 session_dbm/memorydb - sets to invalid timestamp if new or existing item. | |
| 12 session_rdbms - sets to time.time if new item, keeps original | |
| 13 if item exists. (note that the timestamp is | |
| 14 a separate column, the timestamp embedded in the | |
| 15 value object in the db has the bad __timestamp. | |
| 16 reconciled: set to time.time for new item, keeps original time | |
| 17 of existing item. | |
| 18 | |
| 19 Also updateTimestamp does not update the marshalled values idea of | |
| 20 __timestamp. So get(item, '__timestamp') will not work as expected | |
| 21 for rdbms backends, need a sql query to get the timestamp column. | |
| 22 | |
| 23 FIXME need to add getTimestamp method to sessions_rdbms.py and | |
| 24 sessions_dbm.py. | |
| 25 | |
| 26 """ | |
| 6 class SessionTest(object): | 27 class SessionTest(object): |
| 7 def setUp(self): | 28 def setUp(self): |
| 8 # remove previous test, ignore errors | 29 # remove previous test, ignore errors |
| 9 if os.path.exists(config.DATABASE): | 30 if os.path.exists(config.DATABASE): |
| 10 shutil.rmtree(config.DATABASE) | 31 shutil.rmtree(config.DATABASE) |
| 31 self.sessions.set('random_key', text='hello, world!') | 52 self.sessions.set('random_key', text='hello, world!') |
| 32 self.sessions.set('random_key2', text='hello, world!') | 53 self.sessions.set('random_key2', text='hello, world!') |
| 33 self.assertEqual(self.sessions.list().sort(), | 54 self.assertEqual(self.sessions.list().sort(), |
| 34 [self.s2b('random_key'), self.s2b('random_key2')].sort()) | 55 [self.s2b('random_key'), self.s2b('random_key2')].sort()) |
| 35 | 56 |
| 57 def testGetMissingKey(self): | |
| 58 self.sessions.set('random_key', text='hello, world!', otherval='bar') | |
| 59 with self.assertRaises(KeyError) as e: | |
| 60 self.sessions.get('badc_key', 'text') | |
| 61 | |
| 36 def testGetAll(self): | 62 def testGetAll(self): |
| 37 self.sessions.set('random_key', text='hello, world!', otherval='bar') | 63 self.sessions.set('random_key', text='hello, world!', otherval='bar') |
| 38 self.assertEqual(self.sessions.getall('random_key'), | 64 self.assertEqual(self.sessions.getall('random_key'), |
| 39 {'text': 'hello, world!', 'otherval': 'bar'}) | 65 {'text': 'hello, world!', 'otherval': 'bar'}) |
| 40 | 66 |
| 42 self.sessions.set('random_key', text='hello, world!') | 68 self.sessions.set('random_key', text='hello, world!') |
| 43 self.assertEqual(self.sessions.getall('random_key'), | 69 self.assertEqual(self.sessions.getall('random_key'), |
| 44 {'text': 'hello, world!'}) | 70 {'text': 'hello, world!'}) |
| 45 self.sessions.destroy('random_key') | 71 self.sessions.destroy('random_key') |
| 46 self.assertRaises(KeyError, self.sessions.getall, 'random_key') | 72 self.assertRaises(KeyError, self.sessions.getall, 'random_key') |
| 73 | |
| 74 def testClear(self): | |
| 75 self.sessions.set('random_key', text='hello, world!') | |
| 76 self.sessions.set('random_key2', text='hello, world!') | |
| 77 self.sessions.set('random_key3', text='hello, world!') | |
| 78 self.assertEqual(self.sessions.getall('random_key3'), | |
| 79 {'text': 'hello, world!'}) | |
| 80 self.assertEqual(len(self.sessions.list()), 3) | |
| 81 self.sessions.clear() | |
| 82 self.assertEqual(len(self.sessions.list()), 0) | |
| 47 | 83 |
| 48 def testSetSession(self): | 84 def testSetSession(self): |
| 49 self.sessions.set('random_key', text='hello, world!', otherval='bar') | 85 self.sessions.set('random_key', text='hello, world!', otherval='bar') |
| 50 self.assertEqual(self.sessions.get('random_key', 'text'), | 86 self.assertEqual(self.sessions.get('random_key', 'text'), |
| 51 'hello, world!') | 87 'hello, world!') |
| 56 self.sessions.set('random_key', text='hello, world!') | 92 self.sessions.set('random_key', text='hello, world!') |
| 57 self.assertEqual(self.sessions.get('random_key', 'text'), | 93 self.assertEqual(self.sessions.get('random_key', 'text'), |
| 58 'hello, world!') | 94 'hello, world!') |
| 59 self.sessions.set('random_key', text='nope') | 95 self.sessions.set('random_key', text='nope') |
| 60 self.assertEqual(self.sessions.get('random_key', 'text'), 'nope') | 96 self.assertEqual(self.sessions.get('random_key', 'text'), 'nope') |
| 97 | |
| 98 def testBadTimestamp(self): | |
| 99 self.sessions.set('random_key', | |
| 100 text='hello, world!', | |
| 101 __timestamp='not a timestamp') | |
| 102 ts = self.sessions.get('random_key', '__timestamp') | |
| 103 self.assertNotEqual(ts, 'not a timestamp') | |
| 104 # use {1,7} because db's don't pad the fraction to 7 digits. | |
| 105 ts_re=r'^[0-9]{10,16}\.[0-9]{1,7}$' | |
| 106 try: | |
| 107 self.assertRegex(str(ts), ts_re) | |
| 108 except AttributeError: # 2.7 version | |
| 109 import warnings | |
| 110 with warnings.catch_warnings(): | |
| 111 warnings.filterwarnings("ignore",category=DeprecationWarning) | |
| 112 self.assertRegexpMatches(str(ts), ts_re) | |
| 113 | |
| 114 # now update with a bad timestamp, original timestamp should | |
| 115 # be kept. | |
| 116 self.sessions.set('random_key', | |
| 117 text='hello, world2!', | |
| 118 __timestamp='not a timestamp') | |
| 119 item = self.sessions.get('random_key', "text") | |
| 120 item_ts = self.sessions.get('random_key', "__timestamp") | |
| 121 self.assertEqual(item, 'hello, world2!') | |
| 122 self.assertAlmostEqual(ts, item_ts, 2) | |
| 61 | 123 |
| 62 # overridden in dbm and memory backends | 124 # overridden in dbm and memory backends |
| 63 def testUpdateTimestamp(self): | 125 def testUpdateTimestamp(self): |
| 64 def get_ts_via_sql(self): | 126 def get_ts_via_sql(self): |
| 65 sql = '''select %(name)s_time from %(name)ss | 127 sql = '''select %(name)s_time from %(name)ss |
| 86 # '__timestamp'), | 148 # '__timestamp'), |
| 87 # timestamp) | 149 # timestamp) |
| 88 | 150 |
| 89 # use 61 to allow a fudge factor | 151 # use 61 to allow a fudge factor |
| 90 self.assertGreater(get_ts_via_sql(self)[0] - timestamp, 61) | 152 self.assertGreater(get_ts_via_sql(self)[0] - timestamp, 61) |
| 153 | |
| 154 def testLifetime(self): | |
| 155 ts = self.sessions.lifetime(300) | |
| 156 week_ago = time.time() - 60*60*24*7 | |
| 157 self.assertGreater(week_ago + 302, ts) | |
| 158 self.assertLess(week_ago + 298, ts) |
