diff --git a/src/zeep/cache.py b/src/zeep/cache.py index e39ab2eb..06d99881 100644 --- a/src/zeep/cache.py +++ b/src/zeep/cache.py @@ -130,9 +130,10 @@ def db_connection(self): assert sqlite3 with self._lock: - connection = sqlite3.connect( - self._db_path, detect_types=sqlite3.PARSE_DECLTYPES - ) + # Timestamps are stored as ISO 8601 text and parsed in get(), so + # this doesn't rely on sqlite3's default datetime adapter and + # converter (deprecated since Python 3.12). + connection = sqlite3.connect(self._db_path) yield connection connection.close() @@ -145,7 +146,11 @@ def add(self, url, content): cursor.execute("DELETE FROM request WHERE url = ?", (url,)) cursor.execute( "INSERT INTO request (created, url, content) VALUES (?, ?, ?)", - (datetime.datetime.now(datetime.timezone.utc), url, data), + ( + datetime.datetime.now(datetime.timezone.utc).isoformat(" "), + url, + data, + ), ) conn.commit() @@ -157,6 +162,9 @@ def get(self, url): if rows: created, data = rows[0] + if isinstance(created, bytes): + created = created.decode() + created = datetime.datetime.fromisoformat(created) if not _is_expired(created, self._timeout): logger.debug("Cache HIT for %s", url) return self._decode_data(data) diff --git a/tests/test_cache.py b/tests/test_cache.py index bdf3b42b..373fdee3 100644 --- a/tests/test_cache.py +++ b/tests/test_cache.py @@ -53,6 +53,47 @@ def test_has_not_expired(self, tmpdir): result = c.get("http://tests.python-zeep.org/example.wsdl") assert result == b"content" + def test_cache_on_whole_second(self, tmpdir): + # isoformat() leaves out the microseconds when they're zero, which the + # old sqlite3 timestamp converter couldn't parse. + c = cache.SqliteCache(path=tmpdir.join("sqlite.cache.db").strpath) + frozen = datetime.datetime(2026, 11, 15, 9, 0, 0, tzinfo=datetime.timezone.utc) + with freezegun.freeze_time(frozen): + c.add("http://tests.python-zeep.org/example.wsdl", b"content") + result = c.get("http://tests.python-zeep.org/example.wsdl") + assert result == b"content" + + @pytest.mark.parametrize( + "created", + [ + "2026-11-15 09:00:00.123456+00:00", + "2026-11-15 09:00:00+00:00", + "2026-11-15 09:00:00.123456", + ], + ) + def test_reads_existing_rows(self, tmpdir, created): + # Rows written by older zeep versions through the default sqlite3 + # adapter, with and without a UTC offset. + path = tmpdir.join("sqlite.cache.db").strpath + c = cache.SqliteCache(path=path, timeout=None) + with c.db_connection() as conn: + conn.execute( + "INSERT INTO request (created, url, content) VALUES (?, ?, ?)", + ( + created, + "http://tests.python-zeep.org/example.wsdl", + c._encode_data(b"content"), + ), + ) + conn.commit() + assert c.get("http://tests.python-zeep.org/example.wsdl") == b"content" + + def test_no_sqlite3_datetime_deprecation(self, tmpdir, recwarn): + c = cache.SqliteCache(path=tmpdir.join("sqlite.cache.db").strpath) + c.add("http://tests.python-zeep.org/example.wsdl", b"content") + assert c.get("http://tests.python-zeep.org/example.wsdl") == b"content" + assert not [w for w in recwarn if issubclass(w.category, DeprecationWarning)] + @pytest.mark.network def test_memory_cache_timeout(tmpdir):