Fix doc merge dialog test

This commit is contained in:
Veronica Berglyd Olsen
2022-10-09 22:47:36 +02:00
parent 02dc7f5fe0
commit 428dce3e6e
+43 -125
View File
@@ -19,17 +19,14 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import os
import pytest
from mock import causeOSError
from tools import getGuiItem, readFile, writeFile, buildTestProject
from tools import buildTestProject
from PyQt5.QtWidgets import QAction, QMessageBox
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QMessageBox
from novelwriter.enum import nwItemType, nwWidget
from novelwriter.dialogs import GuiDocMerge, GuiEditLabel
from novelwriter.core.tree import NWTree
from novelwriter.dialogs import GuiDocMerge
@pytest.mark.gui
@@ -39,142 +36,63 @@ def testDlgMerge_Main(qtbot, monkeypatch, nwGUI, fncProj, mockRnd):
# Block message box
monkeypatch.setattr(QMessageBox, "question", lambda *a: QMessageBox.Yes)
monkeypatch.setattr(QMessageBox, "critical", lambda *a: QMessageBox.Ok)
monkeypatch.setattr(GuiEditLabel, "getLabel", lambda *a, text: (text, True))
# Create a new project
buildTestProject(nwGUI, fncProj)
# Handles for new objects
hNovelRoot = "0000000000008"
hInvalid = "0000000000000"
hChapterDir = "000000000000d"
hChapterOne = "000000000000e"
hSceneOne = "000000000000f"
hSceneTwo = "0000000000010"
hSceneThree = "0000000000011"
hSceneFour = "0000000000012"
hMergedDoc = "0000000000023"
hChapterDoc = "000000000000e"
hSceneDoc = "000000000000f"
# Add Project Content
nwGUI.switchFocus(nwWidget.TREE)
nwGUI.projView.projTree.clearSelection()
nwGUI.projView.projTree._getTreeItem(hChapterDir).setSelected(True)
nwGUI.projView.projTree.newTreeItem(nwItemType.FILE)
nwGUI.projView.projTree.newTreeItem(nwItemType.FILE)
nwGUI.projView.projTree.newTreeItem(nwItemType.FILE)
assert nwGUI.saveProject() is True
assert nwGUI.closeProject() is True
tChapterOne = "## Chapter One\n\n% Chapter one comment\n"
tSceneOne = "### Scene One\n\nThere once was a man from Nantucket"
tSceneTwo = "### Scene Two\n\nWho kept all his cash in a bucket."
tSceneThree = "### Scene Three\n\n\tBut his daughter, named Nan, \n\tRan away with a man"
tSceneFour = "### Scene Four\n\nAnd as for the bucket, Nantucket."
contentDir = os.path.join(fncProj, "content")
writeFile(os.path.join(contentDir, hChapterOne+".nwd"), tChapterOne)
writeFile(os.path.join(contentDir, hSceneOne+".nwd"), tSceneOne)
writeFile(os.path.join(contentDir, hSceneTwo+".nwd"), tSceneTwo)
writeFile(os.path.join(contentDir, hSceneThree+".nwd"), tSceneThree)
writeFile(os.path.join(contentDir, hSceneFour+".nwd"), tSceneFour)
assert nwGUI.openProject(fncProj) is True
# Open the Merge tool
nwGUI.switchFocus(nwWidget.TREE)
nwGUI.projView.projTree.clearSelection()
nwGUI.projView.projTree._getTreeItem(hChapterDir).setSelected(True)
monkeypatch.setattr(GuiDocMerge, "exec_", lambda *a: None)
nwGUI.mainMenu.aMergeDocs.activate(QAction.Trigger)
qtbot.waitUntil(lambda: getGuiItem("GuiDocMerge") is not None, timeout=1000)
nwMerge = getGuiItem("GuiDocMerge")
assert isinstance(nwMerge, GuiDocMerge)
# Check that the dialog kan handle invalid items
nwMerge = GuiDocMerge(nwGUI, hInvalid, [hInvalid])
qtbot.addWidget(nwMerge)
nwMerge.show()
qtbot.wait(50)
# Populate List
# =============
nwMerge.listBox.clear()
assert nwMerge.listBox.count() == 0
nwMerge.reject()
# No item selected
nwGUI.projView.projTree.clearSelection()
assert nwMerge._populateList() is False
assert nwMerge.listBox.count() == 0
# Load items from chapter dir
nwMerge = GuiDocMerge(nwGUI, hChapterDir, [hChapterDir, hChapterDoc, hSceneDoc])
qtbot.addWidget(nwMerge)
nwMerge.show()
# Non-existing item
with monkeypatch.context() as mp:
mp.setattr(NWTree, "__getitem__", lambda *a: None)
nwGUI.projView.projTree.clearSelection()
nwGUI.projView.projTree._getTreeItem(hChapterDir).setSelected(True)
assert nwMerge._populateList() is False
assert nwMerge.listBox.count() == 0
assert nwMerge.listBox.count() == 2
# Select a non-folder
nwGUI.projView.projTree.clearSelection()
nwGUI.projView.projTree._getTreeItem(hChapterOne).setSelected(True)
assert nwMerge._populateList() is False
assert nwMerge.listBox.count() == 0
itemOne = nwMerge.listBox.item(0)
itemTwo = nwMerge.listBox.item(1)
# Select the chapter folder
nwGUI.projView.projTree.clearSelection()
nwGUI.projView.projTree._getTreeItem(hChapterDir).setSelected(True)
assert nwMerge._populateList() is True
assert nwMerge.listBox.count() == 5
assert itemOne.data(Qt.UserRole) == hChapterDoc
assert itemTwo.data(Qt.UserRole) == hSceneDoc
# Merge Documents
# ===============
assert itemOne.checkState() == Qt.Checked
assert itemTwo.checkState() == Qt.Checked
# First, a successful merge
with monkeypatch.context() as mp:
mp.setattr(GuiDocMerge, "_doClose", lambda *a: None)
assert nwMerge._doMerge() is True
assert nwGUI.saveProject() is True
mergedFile = os.path.join(contentDir, hMergedDoc+".nwd")
assert os.path.isfile(mergedFile)
assert readFile(mergedFile) == (
"%%%%~name: New Chapter\n"
"%%%%~path: %s/%s\n"
"%%%%~kind: NOVEL/DOCUMENT\n"
"%s\n\n"
"%s\n\n"
"%s\n\n"
"%s\n\n"
"%s\n\n"
) % (
hNovelRoot,
hMergedDoc,
tChapterOne.strip(),
tSceneOne.strip(),
tSceneTwo.strip(),
tSceneThree.strip(),
tSceneFour.strip(),
)
data = nwMerge.getData()
assert data["sHandle"] == hChapterDir
assert data["origItems"] == [hChapterDir, hChapterDoc, hSceneDoc]
assert data["moveToTrash"] is False
assert data["finalItems"] == [hChapterDoc, hSceneDoc]
# OS error
with monkeypatch.context() as mp:
mp.setattr("builtins.open", causeOSError)
assert nwMerge._doMerge() is False
# Uncheck second item and toggle trash switch
itemTwo.setCheckState(Qt.Unchecked)
nwMerge.trashSwitch.setChecked(True)
# Can't find the source item
with monkeypatch.context() as mp:
mp.setattr(NWTree, "__getitem__", lambda *a: None)
assert nwMerge._doMerge() is False
data = nwMerge.getData()
assert data["sHandle"] == hChapterDir
assert data["origItems"] == [hChapterDir, hChapterDoc, hSceneDoc]
assert data["moveToTrash"] is True
assert data["finalItems"] == [hChapterDoc]
# No source handle set
nwMerge.sourceItem = None
assert nwMerge._doMerge() is False
# Restore default values
nwMerge._resetList()
# No documents to merge
nwMerge.listBox.clear()
assert nwMerge._doMerge() is False
data = nwMerge.getData()
assert data["sHandle"] == hChapterDir
assert data["origItems"] == [hChapterDir, hChapterDoc, hSceneDoc]
assert data["moveToTrash"] is True
assert data["finalItems"] == [hChapterDoc, hSceneDoc]
# Close up
nwMerge._doClose()
# qtbot.stopForInteraction()
# qtbot.stop()
# END Test testDlgMerge_Main