|
2 | 2 | import queue |
3 | 3 | import sys |
4 | 4 | import threading |
5 | | -from datetime import timedelta |
| 5 | +from datetime import timedelta, datetime |
6 | 6 | from random import random |
7 | 7 | from time import sleep |
8 | 8 |
|
@@ -208,3 +208,64 @@ def test_sqlcore_importerror_without_sqlalchemy(monkeypatch): |
208 | 208 | finally: |
209 | 209 | sys.modules.clear() |
210 | 210 | sys.modules.update(modules_backup) |
| 211 | + |
| 212 | + |
| 213 | +@pytest.mark.sql |
| 214 | +def test_sqlcore_invalid_sql_engine(): |
| 215 | + with pytest.raises(ValueError, match="sql_engine must be a SQLAlchemy Engine"): |
| 216 | + _SQLCore(hash_func=None, sql_engine=12345) |
| 217 | + |
| 218 | + |
| 219 | +@pytest.mark.sql |
| 220 | +def test_sqlcore_get_entry_by_key_none_value(): |
| 221 | + core = _SQLCore(hash_func=None, sql_engine=SQL_CONN_STR) |
| 222 | + core.set_func(lambda x: x) |
| 223 | + # Insert a row with value=None |
| 224 | + with core._Session() as session: |
| 225 | + session.add(core._SQLCore__class__.CacheTable( |
| 226 | + id="testfunc:abc", |
| 227 | + function_id=core._func_str, |
| 228 | + key="abc", |
| 229 | + value=None, |
| 230 | + timestamp=datetime.now(), |
| 231 | + stale=False, |
| 232 | + processing=False, |
| 233 | + completed=True, |
| 234 | + )) |
| 235 | + session.commit() |
| 236 | + key, entry = core.get_entry_by_key("abc") |
| 237 | + assert entry is not None |
| 238 | + assert entry.value is None |
| 239 | + |
| 240 | + |
| 241 | +@pytest.mark.sql |
| 242 | +def test_sqlcore_set_entry_fallback(monkeypatch): |
| 243 | + core = _SQLCore(hash_func=None, sql_engine=SQL_CONN_STR) |
| 244 | + core.set_func(lambda x: x) |
| 245 | + # Monkeypatch insert to not have on_conflict_do_update |
| 246 | + orig_insert = core._Session().execute |
| 247 | + def fake_insert(stmt): |
| 248 | + class FakeInsert: |
| 249 | + def __init__(self): |
| 250 | + pass |
| 251 | + return FakeInsert() |
| 252 | + monkeypatch.setattr(core._Session(), "execute", fake_insert) |
| 253 | + # Should not raise |
| 254 | + core.set_entry("fallback", 123) |
| 255 | + monkeypatch.setattr(core._Session(), "execute", orig_insert) |
| 256 | + |
| 257 | + |
| 258 | +@pytest.mark.sql |
| 259 | +def test_sqlcore_wait_on_entry_calc_recalculation(): |
| 260 | + core = _SQLCore(hash_func=None, sql_engine=SQL_CONN_STR) |
| 261 | + core.set_func(lambda x: x) |
| 262 | + with pytest.raises(RecalculationNeeded): |
| 263 | + core.wait_on_entry_calc("missing_key") |
| 264 | + |
| 265 | + |
| 266 | +@pytest.mark.sql |
| 267 | +def test_sqlcore_clear_being_calculated_empty(): |
| 268 | + core = _SQLCore(hash_func=None, sql_engine=SQL_CONN_STR) |
| 269 | + core.set_func(lambda x: x) |
| 270 | + # Should not raise even if nothing is being calculated |
| 271 | + core.clear_being_calculated() |
0 commit comments