Fix tests and update coverage

This commit is contained in:
Veronica Berglyd Olsen
2025-04-18 19:20:56 +02:00
parent b60ce68fcd
commit b75ec3677c
3 changed files with 174 additions and 48 deletions
+14 -17
View File
@@ -659,23 +659,20 @@ class GuiProjectTree(QTreeView):
nLevel = 0 nLevel = 0
sHandle, sPos = SHARED.project.tree.pickParent(node, nLevel, nNote) sHandle, sPos = SHARED.project.tree.pickParent(node, nLevel, nNote)
if not sHandle: if sHandle:
SHARED.error(self.tr("Did not find anywhere to add the file or folder!")) newLabel, dlgOk = GuiEditLabel.getLabel(self, text=newLabel)
return if dlgOk:
# Add the file or folder
newLabel, dlgOk = GuiEditLabel.getLabel(self, text=newLabel) if itemType == nwItemType.FILE:
if dlgOk: if tHandle := SHARED.project.newFile(newLabel, sHandle, sPos):
# Add the file or folder if copyDoc:
if itemType == nwItemType.FILE: SHARED.project.copyFileContent(tHandle, copyDoc)
if tHandle := SHARED.project.newFile(newLabel, sHandle, sPos): elif hLevel > 0:
if copyDoc: SHARED.project.writeNewFile(tHandle, hLevel, not nNote)
SHARED.project.copyFileContent(tHandle, copyDoc) SHARED.project.index.reIndexHandle(tHandle)
elif hLevel > 0: SHARED.project.tree.refreshItems([tHandle])
SHARED.project.writeNewFile(tHandle, hLevel, not nNote) else:
SHARED.project.index.reIndexHandle(tHandle) tHandle = SHARED.project.newFolder(newLabel, sHandle, sPos)
SHARED.project.tree.refreshItems([tHandle])
else:
tHandle = SHARED.project.newFolder(newLabel, sHandle, sPos)
# Select the new item automatically # Select the new item automatically
if tHandle: if tHandle:
+108
View File
@@ -211,6 +211,114 @@ def testCoreTree_ManipulateTree(mockGUI, mockItems):
] ]
@pytest.mark.core
def testCoreTree_PickParent(mockGUI, mockItems):
"""Check the parent item picker."""
project = NWProject()
tree = NWTree(project)
assert len(tree) == 0
# Case 1: Root and Folder
# =======================
hNovelRoot = tree.create("Novel", None, nwItemType.ROOT, nwItemClass.NOVEL)
nNovelRoot = tree.nodes[hNovelRoot]
# Add a folder
sHandle, sPos = tree.pickParent(nNovelRoot, 0, False)
assert sHandle == hNovelRoot
assert sPos == 0
hNovelFolder = tree.create("Folder", sHandle, nwItemType.FOLDER, pos=sPos)
assert hNovelFolder is not None
nNovelFolder = tree.nodes[hNovelFolder]
assert nNovelFolder.item.itemClass == nwItemClass.NOVEL
# Add a partition
sHandle, sPos = tree.pickParent(nNovelRoot, 1, False)
assert sHandle == hNovelRoot
assert sPos == 1
hPartOne = tree.create("Part One", sHandle, nwItemType.FILE, pos=sPos)
assert hPartOne is not None
nPartOne = tree.nodes[hPartOne]
assert nPartOne.item.itemClass == nwItemClass.NOVEL
nPartOne.item.setMainHeading("H1")
# Folders behave identical to root
sHandle, sPos = tree.pickParent(nNovelFolder, 0, False)
assert sHandle == hNovelFolder
assert sPos == 0
# Case 2: Documents of Same Level
# ===============================
# Add a chapter under Part One
hChapterOne = tree.create("Chapter One", hPartOne, nwItemType.FILE, pos=sPos)
assert hChapterOne is not None
nChapterOne = tree.nodes[hChapterOne]
assert nChapterOne.item.itemParent == hPartOne
nChapterOne.item.setMainHeading("H2")
# Add a chapter next to Chapter One -> Sibling (default behaviour)
sHandle, sPos = tree.pickParent(nChapterOne, 2, False)
assert sHandle == hPartOne
assert sPos == 1
hChapterTwo = tree.create("Chapter Two", sHandle, nwItemType.FILE, pos=sPos)
assert hChapterTwo is not None
nChapterTwo = tree.nodes[hChapterTwo]
assert nChapterTwo.item.itemClass == nwItemClass.NOVEL
nChapterTwo.item.setMainHeading("H2")
# Add a new part next to Chapter Two -> Sibling to parent (second if condition)
sHandle, sPos = tree.pickParent(nChapterTwo, 1, False)
assert sHandle == hNovelRoot
assert sPos == 2
# Case 3: Documents of Deeper Level
# =================================
# Add chapter directly to Part One with existing chapters -> Added as child (third if)
sHandle, sPos = tree.pickParent(nPartOne, 2, False)
assert sHandle == hPartOne
assert sPos == 2
# But adding a new part becomes a sibling
sHandle, sPos = tree.pickParent(nPartOne, 1, False)
assert sHandle == hNovelRoot
assert sPos == 2
# Case 4: Notes
# =============
hCharRoot = tree.create("Characters", None, nwItemType.ROOT, nwItemClass.CHARACTER)
nCharRoot = tree.nodes[hCharRoot]
# Add a note at root level
hNoteOne = tree.create("Note One", hCharRoot, nwItemType.FILE, pos=sPos)
assert hNoteOne is not None
nNoteOne = tree.nodes[hNoteOne]
assert nNoteOne.item.itemClass == nwItemClass.CHARACTER
nNoteOne.item.setMainHeading("H1")
assert nCharRoot.childCount() == 1
# Adding a new note is a sibling
sHandle, sPos = tree.pickParent(nNoteOne, 1, True)
assert sHandle == hCharRoot
assert sPos == 1
# Add a child note to Note One
hNoteTwo = tree.create("Note Two", hNoteOne, nwItemType.FILE, pos=sPos)
assert hNoteTwo is not None
nNoteTwo = tree.nodes[hNoteTwo]
assert nNoteTwo.item.itemClass == nwItemClass.CHARACTER
nNoteTwo.item.setMainHeading("H1")
assert nNoteOne.childCount() == 1
# Adding a new note now is a child
sHandle, sPos = tree.pickParent(nNoteOne, 1, True)
assert sHandle == hNoteOne
assert sPos == 1
@pytest.mark.core @pytest.mark.core
def testCoreTree_ItemMethods(monkeypatch, mockGUI, mockItems): def testCoreTree_ItemMethods(monkeypatch, mockGUI, mockItems):
"""Check the item methods of the tree.""" """Check the item methods of the tree."""
+52 -31
View File
@@ -134,7 +134,7 @@ def testGuiProjTree_NewTreeItem(qtbot, caplog, monkeypatch, nwGUI, projPath, moc
# Add a new file in the new folder # Add a new file in the new folder
hNewFile = "0000000000013" hNewFile = "0000000000013"
projView.setSelectedHandle(hNewFolder, doScroll=True) projView.setSelectedHandle(hNewFolder, doScroll=True)
projTree.newTreeItem(nwItemType.FILE) projTree.newTreeItem(nwItemType.FILE, hLevel=0)
assert hNewFile in tree assert hNewFile in tree
item = tree[hNewFile] item = tree[hNewFile]
assert item is not None assert item is not None
@@ -148,9 +148,28 @@ def testGuiProjTree_NewTreeItem(qtbot, caplog, monkeypatch, nwGUI, projPath, moc
"Objects", "Trash", "Objects", "Trash",
] ]
# Add a new chapter next to the other new file # Add a new partition next to the other new file
hNewChapter = "0000000000014" hNewPart = "0000000000014"
projView.setSelectedHandle(hNewFile, doScroll=True) projView.setSelectedHandle(hNewFile, doScroll=True)
projTree.newTreeItem(nwItemType.FILE, hLevel=1)
assert hNewPart in tree
item = tree[hNewPart]
assert item is not None
assert item.itemName == "New Part"
assert item.itemParent == hNewFolder
assert item.itemRoot == C.hNovelRoot
assert item.itemClass == nwItemClass.NOVEL
assert nwGUI.openDocument(hNewPart)
assert nwGUI.docEditor.getText() == "# New Part\n\n"
assert [n.item.itemName for n in tree.model.root.allChildren()] == [
"Novel", "Title Page", "New Folder", "New Chapter", "New Scene",
"New Folder", "New Document", "New Part", "Plot", "Characters",
"Locations", "Objects", "Trash",
]
# Add a new chapter next to the other new file
hNewChapter = "0000000000015"
projView.setSelectedHandle(hNewPart, doScroll=True)
projTree.newTreeItem(nwItemType.FILE, hLevel=2) projTree.newTreeItem(nwItemType.FILE, hLevel=2)
assert hNewChapter in tree assert hNewChapter in tree
item = tree[hNewChapter] item = tree[hNewChapter]
@@ -163,12 +182,12 @@ def testGuiProjTree_NewTreeItem(qtbot, caplog, monkeypatch, nwGUI, projPath, moc
assert nwGUI.docEditor.getText() == "## New Chapter\n\n" assert nwGUI.docEditor.getText() == "## New Chapter\n\n"
assert [n.item.itemName for n in tree.model.root.allChildren()] == [ assert [n.item.itemName for n in tree.model.root.allChildren()] == [
"Novel", "Title Page", "New Folder", "New Chapter", "New Scene", "Novel", "Title Page", "New Folder", "New Chapter", "New Scene",
"New Folder", "New Document", "New Chapter", "Plot", "Characters", "New Folder", "New Document", "New Part", "New Chapter", "Plot",
"Locations", "Objects", "Trash", "Characters", "Locations", "Objects", "Trash",
] ]
# Add a new scene next to the other new file # Add a new scene next to the other new file
hNewScene = "0000000000015" hNewScene = "0000000000016"
projView.setSelectedHandle(hNewChapter, doScroll=True) projView.setSelectedHandle(hNewChapter, doScroll=True)
projTree.newTreeItem(nwItemType.FILE, hLevel=3) projTree.newTreeItem(nwItemType.FILE, hLevel=3)
assert hNewScene in tree assert hNewScene in tree
@@ -182,8 +201,8 @@ def testGuiProjTree_NewTreeItem(qtbot, caplog, monkeypatch, nwGUI, projPath, moc
assert nwGUI.docEditor.getText() == "### New Scene\n\n" assert nwGUI.docEditor.getText() == "### New Scene\n\n"
assert [n.item.itemName for n in tree.model.root.allChildren()] == [ assert [n.item.itemName for n in tree.model.root.allChildren()] == [
"Novel", "Title Page", "New Folder", "New Chapter", "New Scene", "Novel", "Title Page", "New Folder", "New Chapter", "New Scene",
"New Folder", "New Document", "New Chapter", "New Scene", "Plot", "New Folder", "New Document", "New Part", "New Chapter", "New Scene",
"Characters", "Locations", "Objects", "Trash", "Plot", "Characters", "Locations", "Objects", "Trash",
] ]
# Add a new scene with the content copied from the previous # Add a new scene with the content copied from the previous
@@ -191,7 +210,7 @@ def testGuiProjTree_NewTreeItem(qtbot, caplog, monkeypatch, nwGUI, projPath, moc
nwGUI.docEditor.setPlainText("### New Scene\n\nWith Stuff\n\n") nwGUI.docEditor.setPlainText("### New Scene\n\nWith Stuff\n\n")
nwGUI.saveDocument() nwGUI.saveDocument()
hNewSceneCopy = "0000000000016" hNewSceneCopy = "0000000000017"
projView.setSelectedHandle(hNewScene, doScroll=True) projView.setSelectedHandle(hNewScene, doScroll=True)
projTree.newTreeItem(nwItemType.FILE, copyDoc=hNewScene) projTree.newTreeItem(nwItemType.FILE, copyDoc=hNewScene)
assert hNewSceneCopy in tree assert hNewSceneCopy in tree
@@ -205,12 +224,12 @@ def testGuiProjTree_NewTreeItem(qtbot, caplog, monkeypatch, nwGUI, projPath, moc
assert nwGUI.docEditor.getText() == "### New Scene\n\nWith Stuff\n\n" assert nwGUI.docEditor.getText() == "### New Scene\n\nWith Stuff\n\n"
assert [n.item.itemName for n in tree.model.root.allChildren()] == [ assert [n.item.itemName for n in tree.model.root.allChildren()] == [
"Novel", "Title Page", "New Folder", "New Chapter", "New Scene", "Novel", "Title Page", "New Folder", "New Chapter", "New Scene",
"New Folder", "New Document", "New Chapter", "New Scene", "New Scene", "New Folder", "New Document", "New Part", "New Chapter", "New Scene",
"Plot", "Characters", "Locations", "Objects", "Trash", "New Scene", "Plot", "Characters", "Locations", "Objects", "Trash",
] ]
# Add a new file to the characters folder # Add a new file to the characters folder
hNewCharacter = "0000000000017" hNewCharacter = "0000000000018"
projView.setSelectedHandle(C.hCharRoot, doScroll=True) projView.setSelectedHandle(C.hCharRoot, doScroll=True)
projTree.newTreeItem(nwItemType.FILE, hLevel=1, isNote=True) projTree.newTreeItem(nwItemType.FILE, hLevel=1, isNote=True)
assert hNewCharacter in tree assert hNewCharacter in tree
@@ -224,8 +243,9 @@ def testGuiProjTree_NewTreeItem(qtbot, caplog, monkeypatch, nwGUI, projPath, moc
assert nwGUI.docEditor.getText() == "# New Note\n\n" assert nwGUI.docEditor.getText() == "# New Note\n\n"
assert [n.item.itemName for n in tree.model.root.allChildren()] == [ assert [n.item.itemName for n in tree.model.root.allChildren()] == [
"Novel", "Title Page", "New Folder", "New Chapter", "New Scene", "Novel", "Title Page", "New Folder", "New Chapter", "New Scene",
"New Folder", "New Document", "New Chapter", "New Scene", "New Scene", "New Folder", "New Document", "New Part", "New Chapter", "New Scene",
"Plot", "Characters", "New Note", "Locations", "Objects", "Trash", "New Scene", "Plot", "Characters", "New Note", "Locations",
"Objects", "Trash",
] ]
# Cancel during creation # Cancel during creation
@@ -235,15 +255,16 @@ def testGuiProjTree_NewTreeItem(qtbot, caplog, monkeypatch, nwGUI, projPath, moc
projTree.newTreeItem(nwItemType.FILE) projTree.newTreeItem(nwItemType.FILE)
assert [n.item.itemName for n in tree.model.root.allChildren()] == [ assert [n.item.itemName for n in tree.model.root.allChildren()] == [
"Novel", "Title Page", "New Folder", "New Chapter", "New Scene", "Novel", "Title Page", "New Folder", "New Chapter", "New Scene",
"New Folder", "New Document", "New Chapter", "New Scene", "New Scene", "New Folder", "New Document", "New Part", "New Chapter", "New Scene",
"Plot", "Characters", "New Note", "Locations", "Objects", "Trash", "New Scene", "Plot", "Characters", "New Note", "Locations",
"Objects", "Trash",
] ]
# From Template # From Template
# ============= # =============
# Create template folder # Create template folder
hTemplateRoot = "0000000000018" hTemplateRoot = "0000000000019"
projView.setSelectedHandle(hObjectRoot) projView.setSelectedHandle(hObjectRoot)
projTree.newTreeItem(nwItemType.ROOT, nwItemClass.TEMPLATE) projTree.newTreeItem(nwItemType.ROOT, nwItemClass.TEMPLATE)
assert hTemplateRoot in tree assert hTemplateRoot in tree
@@ -255,13 +276,13 @@ def testGuiProjTree_NewTreeItem(qtbot, caplog, monkeypatch, nwGUI, projPath, moc
assert item.itemClass == nwItemClass.TEMPLATE assert item.itemClass == nwItemClass.TEMPLATE
assert [n.item.itemName for n in tree.model.root.allChildren()] == [ assert [n.item.itemName for n in tree.model.root.allChildren()] == [
"Novel", "Title Page", "New Folder", "New Chapter", "New Scene", "Novel", "Title Page", "New Folder", "New Chapter", "New Scene",
"New Folder", "New Document", "New Chapter", "New Scene", "New Scene", "New Folder", "New Document", "New Part", "New Chapter", "New Scene",
"Plot", "Characters", "New Note", "Locations", "Objects", "Templates", "New Scene", "Plot", "Characters", "New Note", "Locations",
"Trash", "Objects", "Templates", "Trash",
] ]
# Create scene template # Create scene template
hSceneTemplate = "0000000000019" hSceneTemplate = "000000000001a"
projView.setSelectedHandle(hTemplateRoot, doScroll=True) projView.setSelectedHandle(hTemplateRoot, doScroll=True)
projTree.newTreeItem(nwItemType.FILE, hLevel=3) projTree.newTreeItem(nwItemType.FILE, hLevel=3)
assert hSceneTemplate in tree assert hSceneTemplate in tree
@@ -278,13 +299,13 @@ def testGuiProjTree_NewTreeItem(qtbot, caplog, monkeypatch, nwGUI, projPath, moc
nwGUI.saveDocument() nwGUI.saveDocument()
assert [n.item.itemName for n in tree.model.root.allChildren()] == [ assert [n.item.itemName for n in tree.model.root.allChildren()] == [
"Novel", "Title Page", "New Folder", "New Chapter", "New Scene", "Novel", "Title Page", "New Folder", "New Chapter", "New Scene",
"New Folder", "New Document", "New Chapter", "New Scene", "New Scene", "New Folder", "New Document", "New Part", "New Chapter", "New Scene",
"Plot", "Characters", "New Note", "Locations", "Objects", "Templates", "New Scene", "Plot", "Characters", "New Note", "Locations",
"New Scene Template", "Trash", "Objects", "Templates", "New Scene Template", "Trash",
] ]
# Create from template # Create from template
hNewFromTemplate = "000000000001a" hNewFromTemplate = "000000000001b"
projView.setSelectedHandle(hNewSceneCopy, doScroll=True) projView.setSelectedHandle(hNewSceneCopy, doScroll=True)
projView.createFileFromTemplate(hSceneTemplate) projView.createFileFromTemplate(hSceneTemplate)
assert hNewFromTemplate in tree assert hNewFromTemplate in tree
@@ -298,9 +319,9 @@ def testGuiProjTree_NewTreeItem(qtbot, caplog, monkeypatch, nwGUI, projPath, moc
assert nwGUI.docEditor.getText() == "### New Scene Template\n\nWith Stuff\n\n" assert nwGUI.docEditor.getText() == "### New Scene Template\n\nWith Stuff\n\n"
assert [n.item.itemName for n in tree.model.root.allChildren()] == [ assert [n.item.itemName for n in tree.model.root.allChildren()] == [
"Novel", "Title Page", "New Folder", "New Chapter", "New Scene", "Novel", "Title Page", "New Folder", "New Chapter", "New Scene",
"New Folder", "New Document", "New Chapter", "New Scene", "New Scene", "New Folder", "New Document", "New Part", "New Chapter", "New Scene",
"New Scene Template", "Plot", "Characters", "New Note", "Locations", "New Scene", "New Scene Template", "Plot", "Characters", "New Note",
"Objects", "Templates", "New Scene Template", "Trash", "Locations", "Objects", "Templates", "New Scene Template", "Trash",
] ]
# Rename Item # Rename Item
@@ -489,7 +510,7 @@ def testGuiProjTree_MouseClicks(qtbot, monkeypatch, nwGUI, projPath, mockRnd):
# Single click emits a signal # Single click emits a signal
with qtbot.waitSignal(projView.selectedItemChanged) as signal: with qtbot.waitSignal(projView.selectedItemChanged) as signal:
projTree._onSingleClick(model.indexFromHandle(C.hNovelRoot)) projTree._onSelectionChange(model.indexFromHandle(C.hNovelRoot), QModelIndex())
assert signal.args[0] == C.hNovelRoot assert signal.args[0] == C.hNovelRoot
# Double click on folder expands/collapses it # Double click on folder expands/collapses it
@@ -1065,7 +1086,7 @@ def testGuiProjTree_ContextMenu(qtbot, monkeypatch, nwGUI, projPath, mockRnd):
# Handles for new objects # Handles for new objects
hCharNote = "0000000000011" hCharNote = "0000000000011"
hNovelNote = "0000000000012" hNovelNote = "0000000000012"
hTrashDoc = "0000000000013" hTrashDoc = "0000000000013"
hSubNote = "0000000000014" hSubNote = "0000000000014"
hNewFolderOne = "0000000000015" hNewFolderOne = "0000000000015"
hNewFolderTwo = "0000000000017" hNewFolderTwo = "0000000000017"
@@ -1090,7 +1111,7 @@ def testGuiProjTree_ContextMenu(qtbot, monkeypatch, nwGUI, projPath, mockRnd):
assert [n.item.itemName for n in tree.model.root.allChildren()] == [ assert [n.item.itemName for n in tree.model.root.allChildren()] == [
"Novel", "Title Page", "New Folder", "New Chapter", "New Scene", "Novel", "Title Page", "New Folder", "New Chapter", "New Scene",
"New Note", "SubNote", "Plot", "Characters", "New Note", "Locations", "New Note", "SubNote", "Plot", "Characters", "New Note", "Locations",
"Trash", "New Document", "Trash", "New Part",
] ]
# Pop the menu in various positions and check for success # Pop the menu in various positions and check for success