Update index check and test

This commit is contained in:
Veronica K. B. Olsen
2021-01-29 20:43:04 +01:00
parent 9486749345
commit b7766946d8
2 changed files with 85 additions and 21 deletions
+19 -4
View File
@@ -220,12 +220,9 @@ class NWIndex():
self._checkNovelNoteIndex("novelIndex") self._checkNovelNoteIndex("novelIndex")
self._checkNovelNoteIndex("noteIndex") self._checkNovelNoteIndex("noteIndex")
self._checkTextCounts() self._checkTextCounts()
self._checkFirstTitles()
self.indexBroken = False self.indexBroken = False
for tHandle in self._firstTitle:
if len(self._firstTitle[tHandle]) != 2:
self.indexBroken = True
except Exception: except Exception:
logger.error("Error while checking index") logger.error("Error while checking index")
nw.logException() nw.logException()
@@ -897,4 +894,22 @@ class NWIndex():
return return
def _checkFirstTitles(self):
"""Scan the first titles index for errors.
Waring: This function raises exceptions.
"""
for tHandle in self._firstTitle:
if not isHandle(tHandle):
raise KeyError("firstTitle key is not a handle")
tEntry = self._firstTitle[tHandle]
if len(tEntry) != 2:
raise IndexError("firstTitle[a] expected 2 values")
if not tEntry[0] in self.H_VALID:
raise ValueError("firstTitle[a][0] is not a header level")
if not isTitleTag(tEntry[1]):
raise ValueError("firstTitle[a][1] is not a title tag")
return
# END Class NWIndex # END Class NWIndex
+66 -17
View File
@@ -492,9 +492,8 @@ def testCoreIndex_ExtractData(nwMinimal, dummyGUI):
assert wC == 12 # Words in text and title only assert wC == 12 # Words in text and title only
assert pC == 2 # Paragraphs in text only assert pC == 2 # Paragraphs in text only
## # getReferences
# getReferences # =============
##
# Look up an ivalid handle # Look up an ivalid handle
theRefs = theIndex.getReferences("Not a handle") theRefs = theIndex.getReferences("Not a handle")
@@ -506,9 +505,8 @@ def testCoreIndex_ExtractData(nwMinimal, dummyGUI):
assert theRefs["@pov"] == ["Jane"] assert theRefs["@pov"] == ["Jane"]
assert theRefs["@char"] == ["Jane"] assert theRefs["@char"] == ["Jane"]
## # getBackReferenceList
# getBackReferenceList # ====================
##
# None handle should return an empty dict # None handle should return an empty dict
assert theIndex.getBackReferenceList(None) == {} assert theIndex.getBackReferenceList(None) == {}
@@ -517,16 +515,15 @@ def testCoreIndex_ExtractData(nwMinimal, dummyGUI):
theRefs = theIndex.getBackReferenceList(cHandle) theRefs = theIndex.getBackReferenceList(cHandle)
assert theRefs == {nHandle: "T000001"} assert theRefs == {nHandle: "T000001"}
## # getTagSource
# getTagSource # ============
##
assert theIndex.getTagSource("Jane") == (cHandle, 2, "T000001") assert theIndex.getTagSource("Jane") == (cHandle, 2, "T000001")
assert theIndex.getTagSource("John") == (None, 0, "T000000") assert theIndex.getTagSource("John") == (None, 0, "T000000")
## # getCounts
# getCounts for whole text and sections # =========
## # For whole text and sections
# Get section counts for a novel file # Get section counts for a novel file
assert theIndex.scanText(nHandle, ( assert theIndex.scanText(nHandle, (
@@ -555,7 +552,7 @@ def testCoreIndex_ExtractData(nwMinimal, dummyGUI):
assert wC == 12 assert wC == 12
assert pC == 2 assert pC == 2
# First part # Second part
cC, wC, pC = theIndex.getCounts(nHandle, "T000011") cC, wC, pC = theIndex.getCounts(nHandle, "T000011")
assert cC == 62 assert cC == 62
assert wC == 12 assert wC == 12
@@ -588,15 +585,19 @@ def testCoreIndex_ExtractData(nwMinimal, dummyGUI):
assert wC == 12 assert wC == 12
assert pC == 2 assert pC == 2
# First part # Second part
cC, wC, pC = theIndex.getCounts(cHandle, "T000011") cC, wC, pC = theIndex.getCounts(cHandle, "T000011")
assert cC == 62 assert cC == 62
assert wC == 12 assert wC == 12
assert pC == 2 assert pC == 2
## # getFirstTitle
# Novel Stats # =============
##
assert theIndex.getFirstTitle(cHandle) == ["H1", "T000001"]
# Novel Stats
# ===========
hHandle = theProject.newFile("Chapter", nwItemClass.NOVEL, "a508bb932959c") hHandle = theProject.newFile("Chapter", nwItemClass.NOVEL, "a508bb932959c")
sHandle = theProject.newFile("Scene One", nwItemClass.NOVEL, "a508bb932959c") sHandle = theProject.newFile("Scene One", nwItemClass.NOVEL, "a508bb932959c")
@@ -1145,3 +1146,51 @@ def testCoreIndex_CheckTextCounts(dummyGUI):
theIndex._checkTextCounts() theIndex._checkTextCounts()
# END Test testCoreIndex_CheckTextCounts # END Test testCoreIndex_CheckTextCounts
@pytest.mark.core
def testCoreIndex_CheckFirstTitle(dummyGUI):
"""Test the first title checker.
"""
theProject = NWProject(dummyGUI)
theIndex = NWIndex(theProject, dummyGUI)
# Valid Index
theIndex._firstTitle = {
"53b69b83cdafc": ["H1", "T000001"],
"974e400180a99": ["H0", "T000000"],
}
assert theIndex._checkFirstTitles() is None
# Invalid Handle
theIndex._firstTitle = {
"53b69b83cdafc": ["H1", "T000001"],
"h74e400180a99": ["H0", "T000000"],
}
with pytest.raises(KeyError):
theIndex._checkFirstTitles()
# Wrong Length
theIndex._firstTitle = {
"53b69b83cdafc": ["H1", "T000001"],
"974e400180a99": ["H0", "T000000", "stuff"],
}
with pytest.raises(IndexError):
theIndex._checkFirstTitles()
# Wrong Header
theIndex._firstTitle = {
"53b69b83cdafc": ["H1", "T000001"],
"974e400180a99": ["XX", "T000000"],
}
with pytest.raises(ValueError):
theIndex._checkFirstTitles()
# Wrong Title
theIndex._firstTitle = {
"53b69b83cdafc": ["H1", "T000001"],
"974e400180a99": ["H0", "INVALID"],
}
with pytest.raises(ValueError):
theIndex._checkFirstTitles()
# END Test testCoreIndex_CheckFirstTitle