Skip to content
Open
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
73 changes: 69 additions & 4 deletions tests/test_libzim_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Loading