Subject: changeset in trytond:default Cast timestamp as datetime for sqlite back-end From: Cédric Krier <[email protected]>
changeset 4ae2d4ab6704 in trytond:default details: https://hg.tryton.org/trytond?cmd=changeset;node=4ae2d4ab6704 description: Cast timestamp as datetime for sqlite back-end SQLite backend returns a string for the datetime so the comparison between timestamp fails. issue8080 review48721002 diffstat: trytond/cache.py | 17 ++++++++++++++--- trytond/tests/test_cache.py | 19 +++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diffs (94 lines): diff -r 5075df5e9872 -r 4ae2d4ab6704 trytond/cache.py --- a/trytond/cache.py Wed Feb 13 10:17:27 2019 +0100 +++ b/trytond/cache.py Wed Feb 13 10:23:44 2019 +0100 @@ -6,8 +6,9 @@ from sql import Table from sql.aggregate import Max -from sql.functions import CurrentTimestamp +from sql.functions import CurrentTimestamp, Function +from trytond import backend from trytond.config import config from trytond.transaction import Transaction from trytond.tools import resolve @@ -16,6 +17,16 @@ _clear_timeout = config.getint('cache', 'clean_timeout', default=5 * 60) +def _cast(column): + class SQLite_DateTime(Function): + __slots__ = () + _function = 'DATETIME' + + if backend.name() == 'sqlite': + column = SQLite_DateTime(column) + return column + + def freeze(o): if isinstance(o, (set, tuple, list)): return tuple(freeze(x) for x in o) @@ -121,7 +132,7 @@ dbname = transaction.database.name with transaction.connection.cursor() as cursor: table = Table('ir_cache') - cursor.execute(*table.select(table.timestamp, table.name)) + cursor.execute(*table.select(_cast(table.timestamp), table.name)) timestamps = {} for timestamp, name in cursor.fetchall(): timestamps[name] = timestamp @@ -159,7 +170,7 @@ [[CurrentTimestamp(), name]])) cursor.execute(*table.select( - Max(table.timestamp), + _cast(Max(table.timestamp)), where=table.name == name)) timestamp, = cursor.fetchone() diff -r 5075df5e9872 -r 4ae2d4ab6704 trytond/tests/test_cache.py --- a/trytond/tests/test_cache.py Wed Feb 13 10:17:27 2019 +0100 +++ b/trytond/tests/test_cache.py Wed Feb 13 10:23:44 2019 +0100 @@ -1,8 +1,10 @@ # This file is part of Tryton. The COPYRIGHT file at the top level of # this repository contains the full copyright notices and license terms. +import time import unittest +from trytond import cache as cache_mod from trytond.cache import freeze, MemoryCache from trytond.tests.test_tryton import with_transaction, activate_module from trytond.tests.test_tryton import DB_NAME, USER @@ -19,6 +21,13 @@ def setUpClass(cls): activate_module('tests') + def setUp(self): + super().setUp() + clear_timeout = cache_mod._clear_timeout + cache_mod._clear_timeout = 1 + self.addCleanup( + setattr, cache_mod, '_clear_timeout', clear_timeout) + def tearDown(self): MemoryCache.drop(DB_NAME) @@ -83,6 +92,16 @@ transaction2.commit() self.assertEqual(cache.get('foo'), None) + def test_memory_cache_sync(self): + "Test MemoryCache synchronisation" + with Transaction().start(DB_NAME, USER): + cache.clear() + time.sleep(cache_mod._clear_timeout) + last = cache._clean_last + + with Transaction().start(DB_NAME, USER): + self.assertGreater(cache._clean_last, last) + def suite(): func = unittest.TestLoader().loadTestsFromTestCase
