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
sHandle, sPos = SHARED.project.tree.pickParent(node, nLevel, nNote)
if not sHandle:
SHARED.error(self.tr("Did not find anywhere to add the file or folder!"))
return
newLabel, dlgOk = GuiEditLabel.getLabel(self, text=newLabel)
if dlgOk:
# Add the file or folder
if itemType == nwItemType.FILE:
if tHandle := SHARED.project.newFile(newLabel, sHandle, sPos):
if copyDoc:
SHARED.project.copyFileContent(tHandle, copyDoc)
elif hLevel > 0:
SHARED.project.writeNewFile(tHandle, hLevel, not nNote)
SHARED.project.index.reIndexHandle(tHandle)
SHARED.project.tree.refreshItems([tHandle])
else:
tHandle = SHARED.project.newFolder(newLabel, sHandle, sPos)
if sHandle:
newLabel, dlgOk = GuiEditLabel.getLabel(self, text=newLabel)
if dlgOk:
# Add the file or folder
if itemType == nwItemType.FILE:
if tHandle := SHARED.project.newFile(newLabel, sHandle, sPos):
if copyDoc:
SHARED.project.copyFileContent(tHandle, copyDoc)
elif hLevel > 0:
SHARED.project.writeNewFile(tHandle, hLevel, not nNote)
SHARED.project.index.reIndexHandle(tHandle)
SHARED.project.tree.refreshItems([tHandle])
else:
tHandle = SHARED.project.newFolder(newLabel, sHandle, sPos)
# Select the new item automatically
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
def testCoreTree_ItemMethods(monkeypatch, mockGUI, mockItems):
"""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
hNewFile = "0000000000013"
projView.setSelectedHandle(hNewFolder, doScroll=True)
projTree.newTreeItem(nwItemType.FILE)
projTree.newTreeItem(nwItemType.FILE, hLevel=0)
assert hNewFile in tree
item = tree[hNewFile]
assert item is not None
@@ -148,9 +148,28 @@ def testGuiProjTree_NewTreeItem(qtbot, caplog, monkeypatch, nwGUI, projPath, moc
"Objects", "Trash",
]
# Add a new chapter next to the other new file
hNewChapter = "0000000000014"
# Add a new partition next to the other new file
hNewPart = "0000000000014"
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)
assert hNewChapter in tree
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 [n.item.itemName for n in tree.model.root.allChildren()] == [
"Novel", "Title Page", "New Folder", "New Chapter", "New Scene",
"New Folder", "New Document", "New Chapter", "Plot", "Characters",
"Locations", "Objects", "Trash",
"New Folder", "New Document", "New Part", "New Chapter", "Plot",
"Characters", "Locations", "Objects", "Trash",
]
# Add a new scene next to the other new file
hNewScene = "0000000000015"
hNewScene = "0000000000016"
projView.setSelectedHandle(hNewChapter, doScroll=True)
projTree.newTreeItem(nwItemType.FILE, hLevel=3)
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 [n.item.itemName for n in tree.model.root.allChildren()] == [
"Novel", "Title Page", "New Folder", "New Chapter", "New Scene",
"New Folder", "New Document", "New Chapter", "New Scene", "Plot",
"Characters", "Locations", "Objects", "Trash",
"New Folder", "New Document", "New Part", "New Chapter", "New Scene",
"Plot", "Characters", "Locations", "Objects", "Trash",
]
# 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.saveDocument()
hNewSceneCopy = "0000000000016"
hNewSceneCopy = "0000000000017"
projView.setSelectedHandle(hNewScene, doScroll=True)
projTree.newTreeItem(nwItemType.FILE, copyDoc=hNewScene)
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 [n.item.itemName for n in tree.model.root.allChildren()] == [
"Novel", "Title Page", "New Folder", "New Chapter", "New Scene",
"New Folder", "New Document", "New Chapter", "New Scene", "New Scene",
"Plot", "Characters", "Locations", "Objects", "Trash",
"New Folder", "New Document", "New Part", "New Chapter", "New Scene",
"New Scene", "Plot", "Characters", "Locations", "Objects", "Trash",
]
# Add a new file to the characters folder
hNewCharacter = "0000000000017"
hNewCharacter = "0000000000018"
projView.setSelectedHandle(C.hCharRoot, doScroll=True)
projTree.newTreeItem(nwItemType.FILE, hLevel=1, isNote=True)
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 [n.item.itemName for n in tree.model.root.allChildren()] == [
"Novel", "Title Page", "New Folder", "New Chapter", "New Scene",
"New Folder", "New Document", "New Chapter", "New Scene", "New Scene",
"Plot", "Characters", "New Note", "Locations", "Objects", "Trash",
"New Folder", "New Document", "New Part", "New Chapter", "New Scene",
"New Scene", "Plot", "Characters", "New Note", "Locations",
"Objects", "Trash",
]
# Cancel during creation
@@ -235,15 +255,16 @@ def testGuiProjTree_NewTreeItem(qtbot, caplog, monkeypatch, nwGUI, projPath, moc
projTree.newTreeItem(nwItemType.FILE)
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 Chapter", "New Scene", "New Scene",
"Plot", "Characters", "New Note", "Locations", "Objects", "Trash",
"New Folder", "New Document", "New Part", "New Chapter", "New Scene",
"New Scene", "Plot", "Characters", "New Note", "Locations",
"Objects", "Trash",
]
# From Template
# =============
# Create template folder
hTemplateRoot = "0000000000018"
hTemplateRoot = "0000000000019"
projView.setSelectedHandle(hObjectRoot)
projTree.newTreeItem(nwItemType.ROOT, nwItemClass.TEMPLATE)
assert hTemplateRoot in tree
@@ -255,13 +276,13 @@ def testGuiProjTree_NewTreeItem(qtbot, caplog, monkeypatch, nwGUI, projPath, moc
assert item.itemClass == nwItemClass.TEMPLATE
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 Chapter", "New Scene", "New Scene",
"Plot", "Characters", "New Note", "Locations", "Objects", "Templates",
"Trash",
"New Folder", "New Document", "New Part", "New Chapter", "New Scene",
"New Scene", "Plot", "Characters", "New Note", "Locations",
"Objects", "Templates", "Trash",
]
# Create scene template
hSceneTemplate = "0000000000019"
hSceneTemplate = "000000000001a"
projView.setSelectedHandle(hTemplateRoot, doScroll=True)
projTree.newTreeItem(nwItemType.FILE, hLevel=3)
assert hSceneTemplate in tree
@@ -278,13 +299,13 @@ def testGuiProjTree_NewTreeItem(qtbot, caplog, monkeypatch, nwGUI, projPath, moc
nwGUI.saveDocument()
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 Chapter", "New Scene", "New Scene",
"Plot", "Characters", "New Note", "Locations", "Objects", "Templates",
"New Scene Template", "Trash",
"New Folder", "New Document", "New Part", "New Chapter", "New Scene",
"New Scene", "Plot", "Characters", "New Note", "Locations",
"Objects", "Templates", "New Scene Template", "Trash",
]
# Create from template
hNewFromTemplate = "000000000001a"
hNewFromTemplate = "000000000001b"
projView.setSelectedHandle(hNewSceneCopy, doScroll=True)
projView.createFileFromTemplate(hSceneTemplate)
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 [n.item.itemName for n in tree.model.root.allChildren()] == [
"Novel", "Title Page", "New Folder", "New Chapter", "New Scene",
"New Folder", "New Document", "New Chapter", "New Scene", "New Scene",
"New Scene Template", "Plot", "Characters", "New Note", "Locations",
"Objects", "Templates", "New Scene Template", "Trash",
"New Folder", "New Document", "New Part", "New Chapter", "New Scene",
"New Scene", "New Scene Template", "Plot", "Characters", "New Note",
"Locations", "Objects", "Templates", "New Scene Template", "Trash",
]
# Rename Item
@@ -489,7 +510,7 @@ def testGuiProjTree_MouseClicks(qtbot, monkeypatch, nwGUI, projPath, mockRnd):
# Single click emits a 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
# Double click on folder expands/collapses it
@@ -1065,7 +1086,7 @@ def testGuiProjTree_ContextMenu(qtbot, monkeypatch, nwGUI, projPath, mockRnd):
# Handles for new objects
hCharNote = "0000000000011"
hNovelNote = "0000000000012"
hTrashDoc = "0000000000013"
hTrashDoc = "0000000000013"
hSubNote = "0000000000014"
hNewFolderOne = "0000000000015"
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()] == [
"Novel", "Title Page", "New Folder", "New Chapter", "New Scene",
"New Note", "SubNote", "Plot", "Characters", "New Note", "Locations",
"Trash", "New Document",
"Trash", "New Part",
]
# Pop the menu in various positions and check for success