diff --git a/tests/test_index.py b/tests/test_index.py index ff3e9260..084a9d39 100644 --- a/tests/test_index.py +++ b/tests/test_index.py @@ -6,11 +6,12 @@ import pytest from nw.core.project import NWProject from nw.core.index import NWIndex -from nw.constants import nwItemClass +from nw.constants import nwItemClass, nwItemLayout @pytest.mark.project def testIndexScanThis(nwMinimal, nwDummy): - + """Test the tag scanner function scanThis. + """ theProject = NWProject(nwDummy) theProject.projTree.setSeed(42) assert theProject.openProject(nwMinimal) @@ -58,7 +59,8 @@ def testIndexScanThis(nwMinimal, nwDummy): @pytest.mark.project def testIndexCheckThese(nwMinimal, nwDummy): - + """Test the tag checker function checkThese. + """ theProject = NWProject(nwDummy) theProject.projTree.setSeed(42) assert theProject.openProject(nwMinimal) @@ -92,8 +94,184 @@ def testIndexCheckThese(nwMinimal, nwDummy): assert theProject.closeProject() @pytest.mark.project -def testIndexMeta(nwMinimal, nwDummy): +def testIndexScanText(nwMinimal, nwDummy): + """Check the index data extraction functions. + """ + theProject = NWProject(nwDummy) + theProject.projTree.setSeed(42) + assert theProject.openProject(nwMinimal) + theIndex = NWIndex(theProject, nwDummy) + + # Some items for fail to scan tests + dHandle = theProject.newFolder("Folder", nwItemClass.NOVEL, "a508bb932959c") + xHandle = theProject.newFile("No Layout", nwItemClass.NOVEL, "a508bb932959c") + xItem = theProject.projTree[xHandle] + xItem.setLayout(nwItemLayout.NO_LAYOUT) + + # Check invalid data + assert not theIndex.scanText(None, "Hello World!") + assert not theIndex.scanText(dHandle, "Hello World!") + assert not theIndex.scanText(xHandle, "Hello World!") + + xItem.setLayout(nwItemLayout.SCENE) + xItem.setParent(None) + assert not theIndex.scanText(xHandle, "Hello World!") + + # Create the trash folder + tHandle = theProject.trashFolder() + assert theProject.projTree[tHandle] is not None + xItem.setParent(tHandle) + assert not theIndex.scanText(xHandle, "Hello World!") + + # Create the archive root + aHandle = theProject.newRoot("Outtakes", nwItemClass.ARCHIVE) + assert theProject.projTree[aHandle] is not None + xItem.setParent(aHandle) + assert not theIndex.scanText(xHandle, "Hello World!") + + # Make some usable items + nHandle = theProject.newFile("Hello", nwItemClass.NOVEL, "a508bb932959c") + cHandle = theProject.newFile("Jane", nwItemClass.CHARACTER, "afb3043c7b2b3") + sHandle = theProject.newFile("Scene", nwItemClass.NOVEL, "a508bb932959c") + + # Index correct text + assert theIndex.scanText(cHandle, ( + "# Jane Smith\n" + "@tag: Jane\n" + )) + assert theIndex.scanText(nHandle, ( + "# Hello World!\n" + "@pov: Jane\n" + "@char: Jane\n\n" + "% this is a comment\n\n" + "This is a story about Jane Smith.\n\n" + "Well, not really.\n" + )) + assert str(theIndex.tagIndex) == "{'Jane': [2, '%s', 'CHARACTER', 'T000001']}" % cHandle + assert theIndex.novelIndex[nHandle]["T000001"]["title"] == "Hello World!" + + # Check that title sections are indexed properly + assert theIndex.scanText(nHandle, ( + "# Title One\n\n" + "% synopsis: Synopsis One.\n\n" + "Paragraph One.\n\n" + "## Title Two\n\n" + "% synopsis: Synopsis Two.\n\n" + "Paragraph Two.\n\n" + "### Title Three\n\n" + "% synopsis: Synopsis Three.\n\n" + "Paragraph Three.\n\n" + "#### Title Four\n\n" + "% synopsis: Synopsis Four.\n\n" + "Paragraph Four.\n\n" + "##### Title Five\n\n" # Not interpreted as a title, the hashes is counted as a word + "Paragraph Five.\n\n" + )) + assert theIndex.refIndex[nHandle].get("T000000", None) is not None # Always there + assert theIndex.refIndex[nHandle].get("T000001", None) is not None # Heading 1 + assert theIndex.refIndex[nHandle].get("T000002", None) is None + assert theIndex.refIndex[nHandle].get("T000003", None) is None + assert theIndex.refIndex[nHandle].get("T000004", None) is None + assert theIndex.refIndex[nHandle].get("T000005", None) is None + assert theIndex.refIndex[nHandle].get("T000006", None) is None + assert theIndex.refIndex[nHandle].get("T000007", None) is not None # Heading 2 + assert theIndex.refIndex[nHandle].get("T000008", None) is None + assert theIndex.refIndex[nHandle].get("T000009", None) is None + assert theIndex.refIndex[nHandle].get("T000010", None) is None + assert theIndex.refIndex[nHandle].get("T000011", None) is None + assert theIndex.refIndex[nHandle].get("T000012", None) is None + assert theIndex.refIndex[nHandle].get("T000013", None) is not None # Heading 3 + assert theIndex.refIndex[nHandle].get("T000014", None) is None + assert theIndex.refIndex[nHandle].get("T000015", None) is None + assert theIndex.refIndex[nHandle].get("T000016", None) is None + assert theIndex.refIndex[nHandle].get("T000017", None) is None + assert theIndex.refIndex[nHandle].get("T000018", None) is None + assert theIndex.refIndex[nHandle].get("T000019", None) is not None # Heading 4 + assert theIndex.refIndex[nHandle].get("T000020", None) is None + assert theIndex.refIndex[nHandle].get("T000021", None) is None + assert theIndex.refIndex[nHandle].get("T000022", None) is None + assert theIndex.refIndex[nHandle].get("T000023", None) is None + assert theIndex.refIndex[nHandle].get("T000024", None) is None + assert theIndex.refIndex[nHandle].get("T000025", None) is None + assert theIndex.refIndex[nHandle].get("T000026", None) is None + + assert theIndex.novelIndex[nHandle]["T000001"]["level"] == "H1" + assert theIndex.novelIndex[nHandle]["T000007"]["level"] == "H2" + assert theIndex.novelIndex[nHandle]["T000013"]["level"] == "H3" + assert theIndex.novelIndex[nHandle]["T000019"]["level"] == "H4" + + assert theIndex.novelIndex[nHandle]["T000001"]["title"] == "Title One" + assert theIndex.novelIndex[nHandle]["T000007"]["title"] == "Title Two" + assert theIndex.novelIndex[nHandle]["T000013"]["title"] == "Title Three" + assert theIndex.novelIndex[nHandle]["T000019"]["title"] == "Title Four" + + assert theIndex.novelIndex[nHandle]["T000001"]["layout"] == "SCENE" + assert theIndex.novelIndex[nHandle]["T000007"]["layout"] == "SCENE" + assert theIndex.novelIndex[nHandle]["T000013"]["layout"] == "SCENE" + assert theIndex.novelIndex[nHandle]["T000019"]["layout"] == "SCENE" + + assert theIndex.novelIndex[nHandle]["T000001"]["synopsis"] == "Synopsis One." + assert theIndex.novelIndex[nHandle]["T000007"]["synopsis"] == "Synopsis Two." + assert theIndex.novelIndex[nHandle]["T000013"]["synopsis"] == "Synopsis Three." + assert theIndex.novelIndex[nHandle]["T000019"]["synopsis"] == "Synopsis Four." + + assert theIndex.novelIndex[nHandle]["T000001"]["cCount"] == 23 + assert theIndex.novelIndex[nHandle]["T000007"]["cCount"] == 23 + assert theIndex.novelIndex[nHandle]["T000013"]["cCount"] == 27 + assert theIndex.novelIndex[nHandle]["T000019"]["cCount"] == 56 + + assert theIndex.novelIndex[nHandle]["T000001"]["wCount"] == 4 + assert theIndex.novelIndex[nHandle]["T000007"]["wCount"] == 4 + assert theIndex.novelIndex[nHandle]["T000013"]["wCount"] == 4 + assert theIndex.novelIndex[nHandle]["T000019"]["wCount"] == 9 + + assert theIndex.novelIndex[nHandle]["T000001"]["pCount"] == 1 + assert theIndex.novelIndex[nHandle]["T000007"]["pCount"] == 1 + assert theIndex.novelIndex[nHandle]["T000013"]["pCount"] == 1 + assert theIndex.novelIndex[nHandle]["T000019"]["pCount"] == 3 + + assert theIndex.scanText(cHandle, ( + "# Title One\n\n" + "@tag: One\n\n" + "% synopsis: Synopsis One.\n\n" + "Paragraph One.\n\n" + )) + assert theIndex.refIndex[cHandle].get("T000000", None) is not None + assert theIndex.refIndex[cHandle].get("T000001", None) is not None + assert theIndex.refIndex[cHandle].get("T000002", None) is None + assert theIndex.refIndex[cHandle].get("T000003", None) is None + assert theIndex.refIndex[cHandle].get("T000004", None) is None + assert theIndex.refIndex[cHandle].get("T000005", None) is None + assert theIndex.refIndex[cHandle].get("T000006", None) is None + assert theIndex.refIndex[cHandle].get("T000007", None) is None + + assert theIndex.noteIndex[cHandle]["T000001"]["level"] == "H1" + assert theIndex.noteIndex[cHandle]["T000001"]["title"] == "Title One" + assert theIndex.noteIndex[cHandle]["T000001"]["layout"] == "NOTE" + assert theIndex.noteIndex[cHandle]["T000001"]["synopsis"] == "Synopsis One." + assert theIndex.noteIndex[cHandle]["T000001"]["cCount"] == 23 + assert theIndex.noteIndex[cHandle]["T000001"]["wCount"] == 4 + assert theIndex.noteIndex[cHandle]["T000001"]["pCount"] == 1 + + assert theIndex.scanText(sHandle, ( + "# Title One\n\n" + "@pov: One\n\n" # Valid + "@char: Two\n\n" # Invalid tag + "@:\n\n" # Invalid line + "% synopsis: Synopsis One.\n\n" + "Paragraph One.\n\n" + )) + assert str(theIndex.refIndex[sHandle]["T000001"]["tags"]) == ( + "[[3, '@pov', 'One'], [5, '@char', 'Two']]" + ) + + assert theProject.closeProject() + +@pytest.mark.project +def testIndexExtractData(nwMinimal, nwDummy): + """Check the index data extraction functions. + """ theProject = NWProject(nwDummy) theProject.projTree.setSeed(42) assert theProject.openProject(nwMinimal) @@ -109,16 +287,11 @@ def testIndexMeta(nwMinimal, nwDummy): assert theIndex.scanText(nHandle, ( "# Hello World!\n" "@pov: Jane\n" - "@char: Jane\n" - "\n" - "% this is a comment\n" - "\n" - "This is a story about Jane Smith.\n" - "\n" + "@char: Jane\n\n" + "% this is a comment\n\n" + "This is a story about Jane Smith.\n\n" "Well, not really.\n" )) - assert str(theIndex.tagIndex) == "{'Jane': [2, '%s', 'CHARACTER', 'T000001']}" % cHandle - assert theIndex.novelIndex[nHandle]["T000001"]["title"] == "Hello World!" # The novel structure should contain the pointer to the novel file header assert str(theIndex.getNovelStructure()) == "['%s:T000001']" % nHandle @@ -129,6 +302,10 @@ def testIndexMeta(nwMinimal, nwDummy): assert wC == 12 # Words in text and title only assert pC == 2 # Paragraphs in text only + ## + # getReferences + ## + # Look up an ivalid handle theRefs = theIndex.getReferences("Not a handle") assert theRefs["@pov"] == [] @@ -139,30 +316,41 @@ def testIndexMeta(nwMinimal, nwDummy): assert str(theRefs["@pov"]) == "['Jane']" assert str(theRefs["@char"]) == "['Jane']" + ## + # getBackReferenceList + ## + + # None handle should return an empty dict + assert theIndex.getBackReferenceList(None) == {} + # The character file should have a record of the reference from the novel file theRefs = theIndex.getBackReferenceList(cHandle) assert str(theRefs) == "{'%s': 'T000001'}" % nHandle + ## + # getTagSource + ## + + assert theIndex.getTagSource("Jane") == (cHandle, 2, "T000001") + assert theIndex.getTagSource("John") == (None, 0, "T000000") + + ## + # getCounts for whole text and sections + ## + # Get section counts for a novel file assert theIndex.scanText(nHandle, ( "# Hello World!\n" "@pov: Jane\n" - "@char: Jane\n" - "\n" - "% this is a comment\n" - "\n" - "This is a story about Jane Smith.\n" - "\n" - "Well, not really.\n" - "\n" + "@char: Jane\n\n" + "% this is a comment\n\n" + "This is a story about Jane Smith.\n\n" + "Well, not really.\n\n" "# Hello World!\n" "@pov: Jane\n" - "@char: Jane\n" - "\n" - "% this is a comment\n" - "\n" - "This is a story about Jane Smith.\n" - "\n" + "@char: Jane\n\n" + "% this is a comment\n\n" + "This is a story about Jane Smith.\n\n" "Well, not really.\n" )) # Whole document @@ -187,22 +375,15 @@ def testIndexMeta(nwMinimal, nwDummy): assert theIndex.scanText(cHandle, ( "# Hello World!\n" "@pov: Jane\n" - "@char: Jane\n" - "\n" - "% this is a comment\n" - "\n" - "This is a story about Jane Smith.\n" - "\n" - "Well, not really.\n" - "\n" + "@char: Jane\n\n" + "% this is a comment\n\n" + "This is a story about Jane Smith.\n\n" + "Well, not really.\n\n" "# Hello World!\n" "@pov: Jane\n" - "@char: Jane\n" - "\n" - "% this is a comment\n" - "\n" - "This is a story about Jane Smith.\n" - "\n" + "@char: Jane\n\n" + "% this is a comment\n\n" + "This is a story about Jane Smith.\n\n" "Well, not really.\n" )) # Whole document