diff roundup/backends/sessions_dbm.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 b4d0b48b3096
children fe0091279f50
line wrap: on
line diff
--- a/roundup/backends/sessions_dbm.py	Mon Jul 25 17:34:54 2022 -0400
+++ b/roundup/backends/sessions_dbm.py	Mon Jul 25 21:21:26 2022 -0400
@@ -88,11 +88,26 @@
 
     def set(self, infoid, **newvalues):
         db = self.opendb('c')
+        timestamp=None
         try:
             if infoid in db:
                 values = marshal.loads(db[infoid])
+                try:
+                    timestamp = values['__timestamp']
+                except KeyError:
+                    pass  # stay at None
             else:
-                values = {'__timestamp': time.time()}
+                values = {}
+
+            if '__timestamp' in newvalues:
+                try:
+                    float(newvalues['__timestamp'])
+                except ValueError:
+                    # keep original timestamp if present
+                    newvalues['__timestamp'] = timestamp or time.time()
+            else:
+                newvalues['__timestamp'] = time.time()
+
             values.update(newvalues)
             db[infoid] = marshal.dumps(values)
         finally:
@@ -157,6 +172,14 @@
     def commit(self):
         pass
 
+    def lifetime(self, key_lifetime=0):
+        """Return the proper timestamp for a key with key_lifetime specified
+           in seconds. Default lifetime is 0.
+        """
+        now = time.time()
+        week = 60*60*24*7
+        return now - week + key_lifetime
+
     def close(self):
         pass
 

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