Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/zeep/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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()

Expand All @@ -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)
Expand Down
41 changes: 41 additions & 0 deletions tests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down