From efea449be72451590163d8d5baaa95c791a96cb3 Mon Sep 17 00:00:00 2001 From: Sriram-Nambiar Date: Mon, 19 Oct 2026 22:36:55 +0530 Subject: [PATCH] Add tests for indexdata helper functions --- tests/test_libzim_creator.py | 73 ++++++++++++++++++++++++++++++++++-- 1 file changed, 69 insertions(+), 4 deletions(-) diff --git a/tests/test_libzim_creator.py b/tests/test_libzim_creator.py index 360048e2..aa778bd7 100644 --- a/tests/test_libzim_creator.py +++ b/tests/test_libzim_creator.py @@ -760,16 +760,81 @@ def test_indexdata_interface(): assert default_id.get_geoposition() is None -def test_exc_in_indexdata(fpath, lipsum): - item = StaticItem(path=HOME_PATH + "custom", content=lipsum, mimetype="text/html") +def test_indexdata_geoposition(fpath, lipsum): + item = StaticItem(path=HOME_PATH + "geo", content=lipsum, mimetype="text/html") class CustomIndexData(IndexData): def has_indexdata(self): - raise IndexError + return True + + def get_title(self): + return "Eiffel Tower" + + def get_content(self): + return "Landmark in Paris" + + def get_keywords(self): + return "tower paris" + + def get_wordcount(self): + return 42 + + def get_geoposition(self): + return (48.8584, 2.2945) item.get_indexdata = CustomIndexData - with pytest.raises(RuntimeError, match="IndexError"): + with Creator(fpath).config_indexing(True, "eng") as c: + c.add_item(item) + + zim = Archive(fpath) + searcher = Searcher(zim) + query = Query().set_query("Landmark") + assert searcher.search(query).getEstimatedMatches() == 1 + + +@pytest.mark.parametrize( + "method_name", + ["get_indexdata", "has_indexdata", "get_wordcount", "get_geoposition"], +) +def test_exc_in_indexdata(fpath, lipsum, method_name): + item = StaticItem(path=HOME_PATH + "custom", content=lipsum, mimetype="text/html") + + class CustomIndexData(IndexData): + def has_indexdata(self): + if method_name == "has_indexdata": + raise IndexError("custom index error") + return True + + def get_title(self): + return "Title" + + def get_content(self): + return "Content" + + def get_keywords(self): + return "" + + def get_wordcount(self): + if method_name == "get_wordcount": + raise ValueError("custom wordcount error") + return 1 + + def get_geoposition(self): + if method_name == "get_geoposition": + raise ValueError("custom geoposition error") + return None + + if method_name == "get_indexdata": + + def failing_get_indexdata(): + raise RuntimeError("custom get_indexdata error") + + item.get_indexdata = failing_get_indexdata + else: + item.get_indexdata = CustomIndexData + + with pytest.raises(RuntimeError): with Creator(fpath).config_indexing(True, "eng") as c: c.add_item(item)