From 06d39ea35269b60a7b31cadcc27fc7d1b79af657 Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Sat, 21 May 2022 17:33:58 +0200 Subject: [PATCH] Make tests not depend on minimal project settings --- .../coreProject_NewFile_nwProject.nwx | 5 +- .../coreProject_NewMinimal_nwProject.nwx | 14 ++--- .../coreProject_NewRoot_nwProject.nwx | 5 +- .../guiEditor_Main_Final_nwProject.nwx | 13 ++--- .../guiEditor_Main_Initial_nwProject.nwx | 13 ++--- .../guiProjSettings_Dialog_nwProject.nwx | 10 ++-- tests/test_core/test_core_project.py | 10 ++-- tests/test_dialogs/test_dlg_docmerge.py | 4 +- tests/test_dialogs/test_dlg_docsplit.py | 4 +- tests/test_dialogs/test_dlg_itemeditor.py | 10 ++-- tests/test_dialogs/test_dlg_projsettings.py | 7 +-- tests/test_gui/test_gui_guimain.py | 14 +++-- tests/test_gui/test_gui_mainmenu.py | 4 +- tests/test_gui/test_gui_projtree.py | 8 +-- tests/test_gui/test_gui_statusbar.py | 8 +-- tests/test_tools/test_tools_lipsum.py | 4 +- tests/test_tools/test_tools_writingstats.py | 4 +- tests/tools.py | 54 +++++++++++++++++++ 18 files changed, 124 insertions(+), 67 deletions(-) diff --git a/tests/reference/coreProject_NewFile_nwProject.nwx b/tests/reference/coreProject_NewFile_nwProject.nwx index d253ebcc..19a2f4bf 100644 --- a/tests/reference/coreProject_NewFile_nwProject.nwx +++ b/tests/reference/coreProject_NewFile_nwProject.nwx @@ -1,8 +1,9 @@ - + New Project - + New Novel + Jane Doe 2 1 0 diff --git a/tests/reference/coreProject_NewMinimal_nwProject.nwx b/tests/reference/coreProject_NewMinimal_nwProject.nwx index a6711a84..68e08580 100644 --- a/tests/reference/coreProject_NewMinimal_nwProject.nwx +++ b/tests/reference/coreProject_NewMinimal_nwProject.nwx @@ -1,5 +1,5 @@ - + New Project @@ -27,7 +27,7 @@
- New + New Note Draft Finished @@ -39,7 +39,7 @@ Main - + Novel @@ -60,15 +60,11 @@ Title Page - - - New Chapter - - + New Chapter - + New Scene diff --git a/tests/reference/coreProject_NewRoot_nwProject.nwx b/tests/reference/coreProject_NewRoot_nwProject.nwx index 2ab62301..02930a4e 100644 --- a/tests/reference/coreProject_NewRoot_nwProject.nwx +++ b/tests/reference/coreProject_NewRoot_nwProject.nwx @@ -1,8 +1,9 @@ - + New Project - + New Novel + Jane Doe 2 1 0 diff --git a/tests/reference/guiEditor_Main_Final_nwProject.nwx b/tests/reference/guiEditor_Main_Final_nwProject.nwx index c4ba847c..a47d454d 100644 --- a/tests/reference/guiEditor_Main_Final_nwProject.nwx +++ b/tests/reference/guiEditor_Main_Final_nwProject.nwx @@ -1,9 +1,10 @@ - + New Project - - 5 + New Novel + Jane Doe + 4 2 3 @@ -15,8 +16,8 @@ True 000000000000f None - 126 - 99 + 129 + 102 27 @@ -45,7 +46,7 @@ Novel - + Title Page diff --git a/tests/reference/guiEditor_Main_Initial_nwProject.nwx b/tests/reference/guiEditor_Main_Initial_nwProject.nwx index 1a5fd5be..03313570 100644 --- a/tests/reference/guiEditor_Main_Initial_nwProject.nwx +++ b/tests/reference/guiEditor_Main_Initial_nwProject.nwx @@ -1,9 +1,10 @@ - + New Project - - 3 + New Novel + Jane Doe + 2 1 0 @@ -15,8 +16,8 @@ True None None - 6 - 6 + 9 + 9 0 @@ -45,7 +46,7 @@ Novel - + Title Page diff --git a/tests/reference/guiProjSettings_Dialog_nwProject.nwx b/tests/reference/guiProjSettings_Dialog_nwProject.nwx index 326c63e5..f12c1dc2 100644 --- a/tests/reference/guiProjSettings_Dialog_nwProject.nwx +++ b/tests/reference/guiProjSettings_Dialog_nwProject.nwx @@ -1,11 +1,11 @@ - + Project Name Project Title Jane Doe John Doh - 2 + 1 1 0 @@ -17,8 +17,8 @@ True None None - 6 - 6 + 9 + 9 0 B @@ -51,7 +51,7 @@ Novel - + Title Page diff --git a/tests/test_core/test_core_project.py b/tests/test_core/test_core_project.py index 5162e0b9..df0638fd 100644 --- a/tests/test_core/test_core_project.py +++ b/tests/test_core/test_core_project.py @@ -26,7 +26,7 @@ from shutil import copyfile from zipfile import ZipFile from lxml import etree -from tools import cmpFiles, writeFile, readFile +from tools import cmpFiles, writeFile, readFile, buildTestProject from mock import causeOSError from novelwriter.core.project import NWProject @@ -252,8 +252,8 @@ def testCoreProject_NewRoot(fncDir, outDir, refDir, mockGUI, mockRnd): compFile = os.path.join(refDir, "coreProject_NewRoot_nwProject.nwx") theProject = NWProject(mockGUI) + buildTestProject(theProject, fncDir) - assert theProject.newProject({"projPath": fncDir}) is True assert theProject.setProjectPath(fncDir) is True assert theProject.saveProject() is True assert theProject.closeProject() is True @@ -288,8 +288,8 @@ def testCoreProject_NewFile(fncDir, outDir, refDir, mockGUI, mockRnd): compFile = os.path.join(refDir, "coreProject_NewFile_nwProject.nwx") theProject = NWProject(mockGUI) + buildTestProject(theProject, fncDir) - assert theProject.newProject({"projPath": fncDir}) is True assert theProject.setProjectPath(fncDir) is True assert theProject.saveProject() is True assert theProject.closeProject() is True @@ -671,7 +671,7 @@ def testCoreProject_StatusImport(mockGUI, fncDir, mockRnd): """Test the status and importance flag handling. """ theProject = NWProject(mockGUI) - assert theProject.newProject({"projPath": fncDir}) is True + buildTestProject(theProject, fncDir) statusKeys = ["s000008", "s000009", "s00000a", "s00000b"] importKeys = ["i00000c", "i00000d", "i00000e", "i00000f"] @@ -792,7 +792,7 @@ def testCoreProject_Methods(monkeypatch, mockGUI, tmpDir, fncDir, mockRnd): """Test other project class methods and functions. """ theProject = NWProject(mockGUI) - assert theProject.newProject({"projPath": fncDir}) is True + buildTestProject(theProject, fncDir) # Setting project path assert theProject.setProjectPath(None) diff --git a/tests/test_dialogs/test_dlg_docmerge.py b/tests/test_dialogs/test_dlg_docmerge.py index f635be4a..a82c77bd 100644 --- a/tests/test_dialogs/test_dlg_docmerge.py +++ b/tests/test_dialogs/test_dlg_docmerge.py @@ -23,7 +23,7 @@ import os import pytest from mock import causeOSError -from tools import getGuiItem, readFile, writeFile +from tools import getGuiItem, readFile, writeFile, buildTestProject from PyQt5.QtWidgets import QAction, QMessageBox, QDialog @@ -41,7 +41,7 @@ def testDlgMerge_Main(qtbot, monkeypatch, nwGUI, fncProj, mockRnd): monkeypatch.setattr(QMessageBox, "critical", lambda *a: QMessageBox.Ok) # Create a new project - assert nwGUI.newProject({"projPath": fncProj}) + buildTestProject(nwGUI, fncProj) # Handles for new objects hNovelRoot = "0000000000008" diff --git a/tests/test_dialogs/test_dlg_docsplit.py b/tests/test_dialogs/test_dlg_docsplit.py index 19988c0b..90e3375d 100644 --- a/tests/test_dialogs/test_dlg_docsplit.py +++ b/tests/test_dialogs/test_dlg_docsplit.py @@ -23,7 +23,7 @@ import os import pytest from mock import causeOSError -from tools import getGuiItem, readFile, writeFile +from tools import getGuiItem, readFile, writeFile, buildTestProject from PyQt5.QtWidgets import QAction, QMessageBox, QDialog @@ -42,7 +42,7 @@ def testDlgSplit_Main(qtbot, monkeypatch, nwGUI, fncProj, mockRnd): monkeypatch.setattr(QMessageBox, "critical", lambda *a: QMessageBox.Ok) # Create a new project - assert nwGUI.newProject({"projPath": fncProj}) is True + buildTestProject(nwGUI, fncProj) # Handles for new objects hNovelRoot = "0000000000008" diff --git a/tests/test_dialogs/test_dlg_itemeditor.py b/tests/test_dialogs/test_dlg_itemeditor.py index ebd71d6c..c221138e 100644 --- a/tests/test_dialogs/test_dlg_itemeditor.py +++ b/tests/test_dialogs/test_dlg_itemeditor.py @@ -21,7 +21,7 @@ along with this program. If not, see . import pytest -from tools import getGuiItem +from tools import getGuiItem, buildTestProject from PyQt5.QtWidgets import QAction, QDialog, QMessageBox @@ -48,7 +48,7 @@ def testDlgItemEditor_Dialog(qtbot, monkeypatch, nwGUI, fncProj, mockRnd): assert nwGUI.editItem() is False # Create and Open Project - assert nwGUI.newProject({"projPath": fncProj}) + buildTestProject(nwGUI, fncProj) tHandle = "000000000000f" # No Selection @@ -99,7 +99,7 @@ def testDlgItemEditor_Novel(qtbot, monkeypatch, nwGUI, fncProj, mockRnd): monkeypatch.setattr(GuiProjectTree, "hasFocus", lambda *a: True) # Create Project and Open Document - assert nwGUI.newProject({"projPath": fncProj}) + buildTestProject(nwGUI, fncProj) tHandle = "000000000000f" assert nwGUI.theProject.statusItems.name(statusKeys[0]) == "New" @@ -156,7 +156,7 @@ def testDlgItemEditor_Note(qtbot, monkeypatch, nwGUI, fncProj, mockRnd): monkeypatch.setattr(GuiProjectTree, "hasFocus", lambda *a: True) # Create Project and Open Document - assert nwGUI.newProject({"projPath": fncProj}) + buildTestProject(nwGUI, fncProj) assert nwGUI.theProject.statusItems.name(statusKeys[0]) == "New" assert nwGUI.theProject.statusItems.name(statusKeys[1]) == "Note" assert nwGUI.theProject.importItems.name(importKeys[0]) == "New" @@ -209,7 +209,7 @@ def testDlgItemEditor_Folder(qtbot, monkeypatch, nwGUI, fncProj, mockRnd): monkeypatch.setattr(GuiProjectTree, "hasFocus", lambda *a: True) # Create Project and Open Document - assert nwGUI.newProject({"projPath": fncProj}) + buildTestProject(nwGUI, fncProj) # Edit a Folder itemEdit = GuiItemEditor(nwGUI, "000000000000d") diff --git a/tests/test_dialogs/test_dlg_projsettings.py b/tests/test_dialogs/test_dlg_projsettings.py index 54827cfd..f193194a 100644 --- a/tests/test_dialogs/test_dlg_projsettings.py +++ b/tests/test_dialogs/test_dlg_projsettings.py @@ -23,7 +23,7 @@ import os import pytest from shutil import copyfile -from tools import cmpFiles, getGuiItem +from tools import cmpFiles, getGuiItem, buildTestProject from PyQt5.QtGui import QColor from PyQt5.QtCore import Qt @@ -57,7 +57,7 @@ def testDlgProjSettings_Dialog( assert getGuiItem("GuiProjectSettings") is None # Create new project - assert nwGUI.newProject({"projPath": fncProj}) + buildTestProject(nwGUI, fncProj) nwGUI.mainConf.backupPath = fncDir nwGUI.theProject.setSpellLang("en") @@ -81,7 +81,7 @@ def testDlgProjSettings_Dialog( # ============ assert projEdit.tabMain.editName.text() == "New Project" - assert projEdit.tabMain.editTitle.text() == "" + assert projEdit.tabMain.editTitle.text() == "New Novel" assert projEdit.tabMain.editAuthors.toPlainText() == "Jane Smith\nJohn Smith" assert projEdit.tabMain.spellLang.currentData() == "en" assert projEdit.tabMain.doBackup.isChecked() is False @@ -90,6 +90,7 @@ def testDlgProjSettings_Dialog( projEdit.tabMain.editName.setText("") for c in "Project Name": qtbot.keyClick(projEdit.tabMain.editName, c, delay=typeDelay) + projEdit.tabMain.editTitle.setText("") for c in "Project Title": qtbot.keyClick(projEdit.tabMain.editTitle, c, delay=typeDelay) diff --git a/tests/test_gui/test_gui_guimain.py b/tests/test_gui/test_gui_guimain.py index ddb4f09b..04d43d7f 100644 --- a/tests/test_gui/test_gui_guimain.py +++ b/tests/test_gui/test_gui_guimain.py @@ -23,7 +23,7 @@ import os import pytest from shutil import copyfile -from tools import cmpFiles +from tools import cmpFiles, buildTestProject from PyQt5.QtCore import Qt from PyQt5.QtWidgets import QMessageBox, QDialog @@ -76,9 +76,7 @@ def testGuiMain_ProjectTreeItems(qtbot, monkeypatch, nwGUI, fncProj, mockRnd): """ monkeypatch.setattr(QMessageBox, "question", lambda *a: QMessageBox.Yes) - assert nwGUI.newProject({"projPath": fncProj}) is True - assert nwGUI.saveProject() is True - # assert False + buildTestProject(nwGUI, fncProj) sHandle = "000000000000f" assert nwGUI.openSelectedItem() is False @@ -139,7 +137,7 @@ def testGuiMain_Editing(qtbot, monkeypatch, nwGUI, fncProj, refDir, outDir, mock monkeypatch.setattr(GuiDocEditor, "hasFocus", lambda *a: True) # Create new, save, close project - assert nwGUI.newProject({"projPath": fncProj}) + buildTestProject(nwGUI, fncProj) assert nwGUI.saveProject() assert nwGUI.closeProject() @@ -178,9 +176,9 @@ def testGuiMain_Editing(qtbot, monkeypatch, nwGUI, fncProj, refDir, outDir, mock assert nwGUI.theProject.projMeta == os.path.join(fncProj, "meta") assert nwGUI.theProject.projFile == "nwProject.nwx" assert nwGUI.theProject.projName == "New Project" - assert nwGUI.theProject.bookTitle == "" - assert len(nwGUI.theProject.bookAuthors) == 0 - assert not nwGUI.theProject.spellCheck + assert nwGUI.theProject.bookTitle == "New Novel" + assert len(nwGUI.theProject.bookAuthors) == 1 + assert nwGUI.theProject.spellCheck is False # Check that tree items have been created assert nwGUI.treeView._getTreeItem("0000000000008") is not None diff --git a/tests/test_gui/test_gui_mainmenu.py b/tests/test_gui/test_gui_mainmenu.py index 815c8566..801abd68 100644 --- a/tests/test_gui/test_gui_mainmenu.py +++ b/tests/test_gui/test_gui_mainmenu.py @@ -26,7 +26,7 @@ from PyQt5.QtCore import Qt from PyQt5.QtGui import QTextCursor, QTextBlock from PyQt5.QtWidgets import QAction, QFileDialog, QMessageBox -from tools import writeFile +from tools import writeFile, buildTestProject from novelwriter.gui.doceditor import GuiDocEditor from novelwriter.enum import nwDocAction, nwDocInsert @@ -465,7 +465,7 @@ def testGuiMenu_Insert(qtbot, monkeypatch, nwGUI, fncDir, fncProj, mockRnd): monkeypatch.setattr(QMessageBox, "question", lambda *a: QMessageBox.Yes) monkeypatch.setattr(QMessageBox, "critical", lambda *a: QMessageBox.Yes) - assert nwGUI.newProject({"projPath": fncProj}) + buildTestProject(nwGUI, fncProj) assert nwGUI.treeView._getTreeItem("000000000000f") is not None assert nwGUI.openDocument("000000000000f") is True diff --git a/tests/test_gui/test_gui_projtree.py b/tests/test_gui/test_gui_projtree.py index af851138..5d3354f9 100644 --- a/tests/test_gui/test_gui_projtree.py +++ b/tests/test_gui/test_gui_projtree.py @@ -22,6 +22,8 @@ along with this program. If not, see . import pytest import os +from tools import buildTestProject + from PyQt5.QtWidgets import QAction, QMessageBox from novelwriter.guimain import GuiMain @@ -47,7 +49,7 @@ def testGuiProjTree_NewItems(qtbot, caplog, monkeypatch, nwGUI, fncDir, mockRnd) # Create a project prjDir = os.path.join(fncDir, "project") - assert nwGUI.newProject({"projPath": prjDir}) is True + buildTestProject(nwGUI, prjDir) # No itemType set nwTree.clearSelection() @@ -155,7 +157,7 @@ def testGuiProjTree_MoveItems(qtbot, monkeypatch, nwGUI, fncDir, mockRnd): # Create a project prjDir = os.path.join(fncDir, "project") - assert nwGUI.newProject({"projPath": prjDir}) is True + buildTestProject(nwGUI, prjDir) # Move Documents # ============== @@ -283,7 +285,7 @@ def testGuiProjTree_DeleteItems(qtbot, caplog, monkeypatch, nwGUI, fncDir, mockR # Create a project prjDir = os.path.join(fncDir, "project") - assert nwGUI.newProject({"projPath": prjDir}) is True + buildTestProject(nwGUI, prjDir) # Try emptying the trash already now, when there is no trash folder assert nwTree.emptyTrash() is False diff --git a/tests/test_gui/test_gui_statusbar.py b/tests/test_gui/test_gui_statusbar.py index 8993a2e8..51e9c70b 100644 --- a/tests/test_gui/test_gui_statusbar.py +++ b/tests/test_gui/test_gui_statusbar.py @@ -22,6 +22,8 @@ along with this program. If not, see . import time import pytest +from tools import buildTestProject + from PyQt5.QtWidgets import QMessageBox from novelwriter.core import NWDoc @@ -34,7 +36,7 @@ def testGuiStatusBar_Main(qtbot, monkeypatch, nwGUI, fncProj, mockRnd): """ monkeypatch.setattr(QMessageBox, "question", lambda *a: QMessageBox.Yes) - assert nwGUI.newProject({"projPath": fncProj}) is True + buildTestProject(nwGUI, fncProj) cHandle = nwGUI.theProject.newFile("A Note", "000000000000a") newDoc = NWDoc(nwGUI.theProject, cHandle) newDoc.writeDocument("# A Note\n\n") @@ -89,10 +91,10 @@ def testGuiStatusBar_Main(qtbot, monkeypatch, nwGUI, fncProj, mockRnd): # Project Stats nwGUI.statusBar.mainConf.incNotesWCount = False nwGUI._updateStatusWordCount() - assert nwGUI.statusBar.statsText.text() == "Words: 6 (+6)" + assert nwGUI.statusBar.statsText.text() == "Words: 9 (+9)" nwGUI.statusBar.mainConf.incNotesWCount = True nwGUI._updateStatusWordCount() - assert nwGUI.statusBar.statsText.text() == "Words: 8 (+8)" + assert nwGUI.statusBar.statsText.text() == "Words: 11 (+11)" # qtbot.stopForInteraction() diff --git a/tests/test_tools/test_tools_lipsum.py b/tests/test_tools/test_tools_lipsum.py index 7d6c08f9..629deb56 100644 --- a/tests/test_tools/test_tools_lipsum.py +++ b/tests/test_tools/test_tools_lipsum.py @@ -21,7 +21,7 @@ along with this program. If not, see . import pytest -from tools import getGuiItem +from tools import getGuiItem, buildTestProject from PyQt5.QtWidgets import QAction, QMessageBox @@ -40,7 +40,7 @@ def testToolLipsum_Main(qtbot, monkeypatch, nwGUI, fncProj, mockRnd): assert getGuiItem("GuiLipsum") is None # Create a new project - assert nwGUI.newProject({"projPath": fncProj}) is True + buildTestProject(nwGUI, fncProj) assert nwGUI.openDocument("000000000000f") is True assert len(nwGUI.docEditor.getText()) == 15 diff --git a/tests/test_tools/test_tools_writingstats.py b/tests/test_tools/test_tools_writingstats.py index cffef9e6..b7358b20 100644 --- a/tests/test_tools/test_tools_writingstats.py +++ b/tests/test_tools/test_tools_writingstats.py @@ -23,8 +23,8 @@ import pytest import json import os -from tools import getGuiItem, writeFile from mock import causeOSError +from tools import getGuiItem, writeFile, buildTestProject from PyQt5.QtCore import Qt from PyQt5.QtWidgets import QAction, QFileDialog, QMessageBox @@ -48,7 +48,7 @@ def testToolWritingStats_Main(qtbot, monkeypatch, nwGUI, fncDir, fncProj): monkeypatch.setattr(QMessageBox, "critical", lambda *a: QMessageBox.Yes) # Create a project to work on - assert nwGUI.newProject({"projPath": fncProj}) + buildTestProject(nwGUI, fncProj) qtbot.wait(100) assert nwGUI.saveProject() sessFile = os.path.join(fncProj, "meta", nwFiles.SESS_STATS) diff --git a/tests/tools.py b/tests/tools.py index bd417aae..a8e2c774 100644 --- a/tests/tools.py +++ b/tests/tools.py @@ -20,6 +20,7 @@ along with this program. If not, see . """ import os +import time import shutil from PyQt5.QtWidgets import qApp @@ -119,3 +120,56 @@ def cleanProject(projPath): os.unlink(tocFile) return + + +def buildTestProject(theObject, projPath): + """Build a standard test project in projPath using theProject + object as the parent. + """ + from novelwriter.enum import nwItemClass + from novelwriter.core import NWProject, NWDoc + + if isinstance(theObject, NWProject): + theGUI = None + theProject = theObject + else: + theGUI = theObject + theProject = theObject.theProject + + theProject.clearProject() + theProject.setProjectPath(projPath, newProject=True) + theProject.setProjectName("New Project") + theProject.setBookTitle("New Novel") + theProject.setBookAuthors("Jane Doe") + + # Creating a minimal project with a few root folders and a + # single chapter folder with a single file. + xHandle = {} + xHandle[1] = theProject.newRoot(theProject.tr("Novel"), nwItemClass.NOVEL) + xHandle[2] = theProject.newRoot(theProject.tr("Plot"), nwItemClass.PLOT) + xHandle[3] = theProject.newRoot(theProject.tr("Characters"), nwItemClass.CHARACTER) + xHandle[4] = theProject.newRoot(theProject.tr("World"), nwItemClass.WORLD) + xHandle[5] = theProject.newFile(theProject.tr("Title Page"), xHandle[1]) + xHandle[6] = theProject.newFolder(theProject.tr("New Chapter"), xHandle[1]) + xHandle[7] = theProject.newFile(theProject.tr("New Chapter"), xHandle[6]) + xHandle[8] = theProject.newFile(theProject.tr("New Scene"), xHandle[6]) + + aDoc = NWDoc(theProject, xHandle[5]) + aDoc.writeDocument("#! New Novel\n\n>> By Jane DOe <<\n") + + aDoc = NWDoc(theProject, xHandle[7]) + aDoc.writeDocument("## %s\n\n" % theProject.tr("New Chapter")) + + aDoc = NWDoc(theProject, xHandle[8]) + aDoc.writeDocument("### %s\n\n" % theProject.tr("New Scene")) + + theProject.projOpened = time.time() + theProject.setProjectChanged(True) + theProject.saveProject(autoSave=True) + + if theGUI is not None: + theGUI.hasProject = True + theGUI.rebuildTrees() + theGUI.rebuildIndex(beQuiet=True) + + return