From 678a31855f56c01d0a86d67ea022950877244100 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Mon, 28 Sep 2020 20:29:52 +0200 Subject: [PATCH] Improved coverage of dialogs tools, and import document feature --- nw/guimain.py | 29 ++++++++++++----------------- tests/conftest.py | 27 --------------------------- tests/test_dialogs.py | 30 ++++++++++++++++++++++++------ tests/test_gui.py | 42 ++++++++++++++++++++++++++++++++++++++++-- tests/test_project.py | 2 +- 5 files changed, 77 insertions(+), 53 deletions(-) diff --git a/nw/guimain.py b/nw/guimain.py index 483f095d..bc8d5e88 100644 --- a/nw/guimain.py +++ b/nw/guimain.py @@ -594,15 +594,12 @@ class GuiMain(QMainWindow): return False if not self.docEditor.isEmpty(): - if self.mainConf.showGUI: - msgBox = QMessageBox() - msgRes = msgBox.question(self, "Import Document", ( - "Importing the file will overwrite the current content of the document. " - "Do you want to proceed?" - )) - if msgRes != QMessageBox.Yes: - return False - else: + msgBox = QMessageBox() + msgRes = msgBox.question(self, "Import Document", ( + "Importing the file will overwrite the current content of the document. " + "Do you want to proceed?" + )) + if msgRes != QMessageBox.Yes: return False self.docEditor.replaceText(theText) @@ -612,18 +609,16 @@ class GuiMain(QMainWindow): def mergeDocuments(self): """Merge multiple documents to one single new document. """ - if self.mainConf.showGUI: - dlgMerge = GuiDocMerge(self, self.theProject) - dlgMerge.exec_() - return True + dlgMerge = GuiDocMerge(self, self.theProject) + dlgMerge.exec_() + return def splitDocument(self): """Split a single document into multiple documents. """ - if self.mainConf.showGUI: - dlgSplit = GuiDocSplit(self, self.theProject) - dlgSplit.exec_() - return True + dlgSplit = GuiDocSplit(self, self.theProject) + dlgSplit.exec_() + return def passDocumentAction(self, theAction): """Pass on document action theAction to the document viewer if diff --git a/tests/conftest.py b/tests/conftest.py index a8945155..b7ef709e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -178,30 +178,3 @@ def nwOldProj(nwTemp): if path.isdir(oldProjDir): shutil.rmtree(oldProjDir) return - -## -# Monkey Patch Dialogs -## - -@pytest.fixture(scope="function") -def mnkQtDialogs(monkeypatch, nwTemp): - """Mock Qt dialog functions to prevent GUI blocking while testing. - """ - monkeypatch.setattr( - QFileDialog, "getExistingDirectory", lambda *args, **kwargs: nwTemp - ) - - monkeypatch.setattr( - QMessageBox, "question", lambda *args, **kwargs: QMessageBox.Yes - ) - monkeypatch.setattr( - QMessageBox, "information", lambda *args, **kwargs: None - ) - monkeypatch.setattr( - QMessageBox, "warning", lambda *args, **kwargs: None - ) - monkeypatch.setattr( - QMessageBox, "critical", lambda *args, **kwargs: None - ) - - return diff --git a/tests/test_dialogs.py b/tests/test_dialogs.py index 13d41afb..717065e6 100644 --- a/tests/test_dialogs.py +++ b/tests/test_dialogs.py @@ -14,7 +14,7 @@ from os import path from PyQt5.QtCore import Qt, QItemSelectionModel from PyQt5.QtWidgets import ( QDialogButtonBox, QTreeWidgetItem, QListWidgetItem, QDialog, QAction, - QMessageBox + QMessageBox, QFileDialog ) from nw.gui import ( @@ -547,7 +547,7 @@ def testBuildTool(qtbot, nwTempBuild, nwLipsum, nwRef, nwTemp): nwGUI.closeMain() @pytest.mark.gui -def testMergeSplitTools(qtbot, nwTempGUI, nwLipsum, nwRef, nwTemp): +def testMergeSplitTools(qtbot, monkeypatch, nwTempGUI, nwLipsum, nwRef, nwTemp): nwGUI = nw.main(["--testmode", "--config=%s" % nwLipsum, "--data=%s" % nwTemp]) qtbot.addWidget(nwGUI) @@ -562,7 +562,13 @@ def testMergeSplitTools(qtbot, nwTempGUI, nwLipsum, nwRef, nwTemp): assert nwGUI.treeView.setSelectedHandle("45e6b01ca35c1") qtbot.wait(stepDelay) - nwMerge = GuiDocMerge(nwGUI, nwGUI.theProject) + monkeypatch.setattr(GuiDocMerge, "exec_", lambda *args: None) + nwGUI.mainMenu.aMergeDocs.activate(QAction.Trigger) + qtbot.waitUntil(lambda: getGuiItem("GuiDocMerge") is not None, timeout=1000) + + nwMerge = getGuiItem("GuiDocMerge") + assert isinstance(nwMerge, GuiDocMerge) + nwMerge.show() qtbot.wait(stepDelay) nwMerge._doMerge() @@ -579,8 +585,16 @@ def testMergeSplitTools(qtbot, nwTempGUI, nwLipsum, nwRef, nwTemp): # Split By Chapter assert nwGUI.treeView.setSelectedHandle("73475cb40a568") qtbot.wait(stepDelay) - nwSplit = GuiDocSplit(nwGUI, nwGUI.theProject) + + monkeypatch.setattr(GuiDocSplit, "exec_", lambda *args: None) + nwGUI.mainMenu.aSplitDoc.activate(QAction.Trigger) + qtbot.waitUntil(lambda: getGuiItem("GuiDocSplit") is not None, timeout=1000) + + nwSplit = getGuiItem("GuiDocSplit") + assert isinstance(nwSplit, GuiDocSplit) + nwSplit.show() qtbot.wait(stepDelay) + nwSplit.splitLevel.setCurrentIndex(1) qtbot.wait(stepDelay) @@ -863,7 +877,7 @@ def testLoadProject(qtbot, monkeypatch, nwMinimal, nwTemp): nwGUI.closeMain() @pytest.mark.gui -def testPreferences(qtbot, monkeypatch, mnkQtDialogs, nwMinimal, nwTemp, nwRef, tmpConf): +def testPreferences(qtbot, monkeypatch, nwMinimal, nwTemp, nwRef, tmpConf): nwGUI = nw.main(["--testmode", "--config=%s" % nwMinimal, "--data=%s" % nwTemp]) qtbot.addWidget(nwGUI) nwGUI.show() @@ -872,6 +886,8 @@ def testPreferences(qtbot, monkeypatch, mnkQtDialogs, nwMinimal, nwTemp, nwRef, assert nwGUI.openProject(nwMinimal) + monkeypatch.setattr(QMessageBox, "information", lambda *args, **kwargs: None) + monkeypatch.setattr(GuiPreferences, "exec_", lambda *args: None) monkeypatch.setattr(GuiPreferences, "result", lambda *args: QDialog.Accepted) nwGUI.mainMenu.aPreferences.activate(QAction.Trigger) @@ -883,6 +899,7 @@ def testPreferences(qtbot, monkeypatch, mnkQtDialogs, nwMinimal, nwTemp, nwRef, # Override Config tmpConf.confPath = nwMinimal + tmpConf.showGUI = False nwGUI.mainConf = tmpConf nwPrefs.mainConf = tmpConf nwPrefs.tabGeneral.mainConf = tmpConf @@ -1047,13 +1064,14 @@ def testQuotesDialog(qtbot, nwMinimal, nwTemp): nwGUI.close() @pytest.mark.gui -def testDialogsOpenClose(qtbot, mnkQtDialogs, nwMinimal, nwTemp): +def testDialogsOpenClose(qtbot, monkeypatch, nwMinimal, nwTemp): nwGUI = nw.main(["--testmode", "--config=%s" % nwMinimal, "--data=%s" % nwTemp, nwMinimal]) qtbot.addWidget(nwGUI) nwGUI.show() qtbot.waitForWindowShown(nwGUI) qtbot.wait(stepDelay) + monkeypatch.setattr(QFileDialog, "getExistingDirectory", lambda *args, **kwargs: nwTemp) assert nwGUI.selectProjectPath() == nwTemp # qtbot.stopForInteraction() diff --git a/tests/test_gui.py b/tests/test_gui.py index 8cf3a4d4..016325b2 100644 --- a/tests/test_gui.py +++ b/tests/test_gui.py @@ -12,7 +12,9 @@ from nwtools import cmpFiles from os import path from PyQt5.QtCore import Qt, QUrl, QPoint, QItemSelectionModel from PyQt5.QtGui import QTextCursor, QColor, QPixmap, QIcon -from PyQt5.QtWidgets import qApp, QAction, QTreeWidgetItem, QStyle +from PyQt5.QtWidgets import ( + qApp, QAction, QTreeWidgetItem, QStyle, QFileDialog, QMessageBox +) from nw.constants import ( nwItemType, nwItemClass, nwUnicode, nwOutline, nwDocAction, nwDocInsert @@ -999,7 +1001,7 @@ def testContextMenu(qtbot, nwLipsum, nwTemp): nwGUI.close() @pytest.mark.gui -def testInsertMenu(qtbot, nwFuncTemp, nwTemp): +def testInsertMenu(qtbot, monkeypatch, nwFuncTemp, nwTemp): nwGUI = nw.main(["--testmode", "--config=%s" % nwFuncTemp, "--data=%s" % nwTemp]) qtbot.addWidget(nwGUI) nwGUI.show() @@ -1085,6 +1087,42 @@ def testInsertMenu(qtbot, nwFuncTemp, nwTemp): assert nwGUI.docEditor.getText() == " " nwGUI.docEditor.clear() + # Insert text from file + nwGUI.closeDocument() + + # First, with no path + monkeypatch.setattr(QFileDialog, "getOpenFileName", lambda *args, **kwards: []) + assert not nwGUI.importDocument() + + # Then with a path, but an invalid one + monkeypatch.setattr(QFileDialog, "getOpenFileName", lambda *args, **kwards: [" "]) + assert not nwGUI.importDocument() + + # Then a valid path, but bot a file that exists + theFile = path.join(nwTemp, "import.txt") + monkeypatch.setattr(QFileDialog, "getOpenFileName", lambda *args, **kwards: [theFile]) + assert not nwGUI.importDocument() + + # Create the file and try again, but with no target document open + with open(theFile, mode="w+", encoding="utf8") as outFile: + outFile.write("Foo") + assert not nwGUI.importDocument() + + # Open the document from before, and add some text to it + nwGUI.openDocument("0e17daca5f3e1") + nwGUI.docEditor.setText("Bar") + assert nwGUI.docEditor.getText() == "Bar" + + # The document isn't empty, so the message box should pop + monkeypatch.setattr(QMessageBox, "question", lambda *args, **kwargs: QMessageBox.No) + assert not nwGUI.importDocument() + assert nwGUI.docEditor.getText() == "Bar" + + # Finally, accept the replaced text, this time we use the menu entry to trigger it + monkeypatch.setattr(QMessageBox, "question", lambda *args, **kwargs: QMessageBox.Yes) + nwGUI.mainMenu.aImportFile.activate(QAction.Trigger) + assert nwGUI.docEditor.getText() == "Foo" + # qtbot.stopForInteraction() nwGUI.closeMain() nwGUI.close() diff --git a/tests/test_project.py b/tests/test_project.py index 7e46b8e6..9ee7324c 100644 --- a/tests/test_project.py +++ b/tests/test_project.py @@ -580,7 +580,7 @@ def testOrphanedFiles(nwDummy, nwLipsum): assert theProject.closeProject() @pytest.mark.project -def testOldProject(nwDummy, nwOldProj, mnkQtDialogs): +def testOldProject(nwDummy, nwOldProj): theProject = NWProject(nwDummy) theProject.mainConf.showGUI = False