diff roundup/backends/sessions_rdbms.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 db437dd13ed5
children fe0091279f50
line wrap: on
line diff
--- a/roundup/backends/sessions_rdbms.py	Mon Jul 25 17:34:54 2022 -0400
+++ b/roundup/backends/sessions_rdbms.py	Mon Jul 25 21:21:26 2022 -0400
@@ -60,37 +60,34 @@
         c = self.cursor
         n = self.name
         a = self.db.arg
-        c.execute('select %s_value, %s_time from %ss where %s_key=%s'% \
-                  (n, n, n, n, a),
+        c.execute('select %s_value from %ss where %s_key=%s'% \
+                  (n, n, n, a),
             (infoid,))
         res = c.fetchone()
+
+        timestamp=time.time()
         if res:
             values = eval(res[0])
-            timestamp = res[1]
         else:
             values = {}
+
+        if '__timestamp' in newvalues:
+            try:
+                # __timestamp must be representable as a float. Check it.
+                timestamp = float(newvalues['__timestamp'])
+            except ValueError:
+                if res:
+                    # keep the original timestamp
+                    del(newvalues['__timestamp'])
+                else:
+                    # here timestamp is the new timestamp
+                    newvalues['__timestamp'] = timestamp
         values.update(newvalues)
         if res:
-            if '__timestamp' in newvalues:
-                try:
-                    # __timestamp must be representable as a float. Check it.
-                    timestamp = float(newvalues['__timestamp'])
-                except ValueError:
-                    pass
-
             sql = ('update %ss set %s_value=%s, %s_time=%s '
                        'where %s_key=%s'%(n, n, a, n, a, n, a))
             args = (repr(values), timestamp, infoid)
         else:
-            if '__timestamp' in newvalues:
-                try:
-                    # __timestamp must be represntable as a float. Check it.
-                    timestamp = float(newvalues['__timestamp'])
-                except ValueError:
-                    timestamp = time.time()
-            else:
-                timestamp = time.time()
-
             sql = 'insert into %ss (%s_key, %s_time, %s_value) '\
                 'values (%s, %s, %s)'%(n, n, n, n, a, a, a)
             args = (infoid, timestamp, repr(values))
@@ -128,6 +125,14 @@
         self.conn.commit()
         self.cursor = self.conn.cursor()
 
+    def lifetime(self, item_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 + item_lifetime
+
     def close(self):
         self.conn.close()
 

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