From 2cb6caea8de509077315229d0e4938cb44ca3260 Mon Sep 17 00:00:00 2001 From: Timmy Welch Date: Fri, 16 Aug 2024 17:05:28 -0700 Subject: [PATCH] Ignore update with incomplete data when complete data is already cached --- comictalker/comiccacher.py | 5 +++++ tests/comiccacher_test.py | 24 +++++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/comictalker/comiccacher.py b/comictalker/comiccacher.py index c084e1b..3fe5eef 100644 --- a/comictalker/comiccacher.py +++ b/comictalker/comiccacher.py @@ -295,4 +295,9 @@ class ComicCacher: set_slots += key + " = ?" sql_ins = f"INSERT OR REPLACE INTO {tablename} ({keys}) VALUES ({ins_slots})" + if not data.get("complete", True): + sql_ins += f" ON CONFLICT DO UPDATE SET {set_slots} WHERE complete != ?" + vals.extend(vals) + vals.append(True) # If the cache is complete and this isn't complete we don't update it + cur.execute(sql_ins, vals) diff --git a/tests/comiccacher_test.py b/tests/comiccacher_test.py index 785e265..f41798f 100644 --- a/tests/comiccacher_test.py +++ b/tests/comiccacher_test.py @@ -28,10 +28,32 @@ def test_search_results(comic_cache): @pytest.mark.parametrize("series_info", search_results) def test_series_info(comic_cache, series_info): comic_cache.add_series_info( - series=comictalker.comiccacher.Series(id=series_info["id"], data=json.dumps(series_info)), + series=comictalker.comiccacher.Series(id=series_info["id"], data=json.dumps(series_info).encode("utf-8")), source="test", complete=True, ) vi = series_info.copy() cache_result = json.loads(comic_cache.get_series_info(series_id=series_info["id"], source="test")[0].data) assert vi == cache_result + + +@pytest.mark.parametrize("series_info", search_results) +def test_cache_overwrite(comic_cache, series_info): + vi = series_info.copy() + comic_cache.add_series_info( + series=comictalker.comiccacher.Series(id=series_info["id"], data=json.dumps(series_info).encode("utf-8")), + source="test", + complete=True, + ) # Populate the cache + + # Try to insert an incomplete series with different data + series_info["name"] = "test 3" + comic_cache.add_series_info( + series=comictalker.comiccacher.Series(id=series_info["id"], data=json.dumps(series_info).encode("utf-8")), + source="test", + complete=False, + ) + cache_result = json.loads(comic_cache.get_series_info(series_id=series_info["id"], source="test")[0].data) + + # Validate that the Series marked complete is still in the cache + assert vi == cache_result