Skip to content

Commit 15ce02e

Browse files
committed
addressing copilot comments
1 parent 36f0d8e commit 15ce02e

2 files changed

Lines changed: 9 additions & 10 deletions

File tree

src/cachier/cores/sql.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,9 @@ def set_entry(self, key: str, func_res: Any) -> None:
120120
with self._lock, self._Session() as session:
121121
thebytes = pickle.dumps(func_res)
122122
now = datetime.now()
123+
base_insert = insert(CacheTable)
123124
stmt = (
124-
insert(CacheTable)
125+
base_insert
125126
.values(
126127
id=f"{self._func_str}:{key}",
127128
function_id=self._func_str,
@@ -142,7 +143,7 @@ def set_entry(self, key: str, func_res: Any) -> None:
142143
"completed": True,
143144
},
144145
)
145-
if hasattr(insert(CacheTable), "on_conflict_do_update")
146+
if hasattr(base_insert, "on_conflict_do_update")
146147
else None
147148
)
148149
# Fallback for non-SQLite/Postgres: try update, else insert

tests/test_sql_core.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -254,22 +254,20 @@ def test_sqlcore_get_entry_by_key_none_value():
254254

255255
@pytest.mark.sql
256256
def test_sqlcore_set_entry_fallback(monkeypatch):
257+
from sqlalchemy.orm import Session
257258
core = _SQLCore(hash_func=None, sql_engine=SQL_CONN_STR)
258259
core.set_func(lambda x: x)
259-
# Monkeypatch insert to not have on_conflict_do_update
260-
orig_insert = core._Session().execute
261-
262-
def fake_insert(stmt):
260+
# Monkeypatch Session.execute to simulate fallback path
261+
orig_execute = Session.execute
262+
def fake_execute(self, stmt, *args, **kwargs):
263263
class FakeInsert:
264264
def __init__(self):
265265
pass
266-
267266
return FakeInsert()
268-
269-
monkeypatch.setattr(core._Session(), "execute", fake_insert)
267+
monkeypatch.setattr(Session, "execute", fake_execute)
270268
# Should not raise
271269
core.set_entry("fallback", 123)
272-
monkeypatch.setattr(core._Session(), "execute", orig_insert)
270+
monkeypatch.setattr(Session, "execute", orig_execute)
273271

274272

275273
@pytest.mark.sql

0 commit comments

Comments
 (0)