Update wizard test coverage

This commit is contained in:
Veronica Berglyd Olsen
2022-05-15 16:31:51 +02:00
parent 1d5c8566d1
commit c91f19f9f7
+94 -56
View File
@@ -21,12 +21,11 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
import pytest import pytest
import os import os
import sys
from tools import getGuiItem from tools import getGuiItem
from PyQt5.QtCore import Qt from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QFileDialog, QWizard, QMessageBox from PyQt5.QtWidgets import QFileDialog, QWizard, QMessageBox, QDialog
from novelwriter.enum import nwItemClass from novelwriter.enum import nwItemClass
from novelwriter.tools.projwizard import ( from novelwriter.tools.projwizard import (
@@ -40,9 +39,9 @@ stepDelay = 20
@pytest.mark.gui @pytest.mark.gui
@pytest.mark.skipif(sys.platform.startswith("darwin"), reason="Not running on Darwin") # @pytest.mark.skipif(sys.platform.startswith("darwin"), reason="Not running on Darwin")
def testToolProjectWizard_Main(qtbot, monkeypatch, nwGUI, nwMinimal): def testToolProjectWizard_Handling(qtbot, monkeypatch, nwGUI, nwMinimal):
"""Test the new project wizard. """Test the launch of the project wizard.
Disabled for macOS because the test segfaults on QWizard.show() Disabled for macOS because the test segfaults on QWizard.show()
""" """
# Block message box # Block message box
@@ -55,58 +54,80 @@ def testToolProjectWizard_Main(qtbot, monkeypatch, nwGUI, nwMinimal):
# New with a project open should cause an error # New with a project open should cause an error
assert nwGUI.openProject(nwMinimal) assert nwGUI.openProject(nwMinimal)
assert not nwGUI.newProject() with monkeypatch.context() as mp:
mp.setattr(nwGUI, "closeProject", lambda *a: False)
assert nwGUI.newProject() is False
# Close project, but call with invalid path # Close project, but call with invalid path
assert nwGUI.closeProject() assert nwGUI.closeProject()
with monkeypatch.context() as mp: with monkeypatch.context() as mp:
mp.setattr(nwGUI, "showNewProjectDialog", lambda *a: None) mp.setattr(nwGUI, "showNewProjectDialog", lambda *a: None)
assert not nwGUI.newProject() assert nwGUI.newProject() is False
# Now, with an empty dictionary # Now, with an empty dictionary
mp.setattr(nwGUI, "showNewProjectDialog", lambda *a: {}) mp.setattr(nwGUI, "showNewProjectDialog", lambda *a: {})
assert not nwGUI.newProject() assert nwGUI.newProject() is False
# Now, with a non-empty folder # Now, with a non-empty folder
mp.setattr(nwGUI, "showNewProjectDialog", lambda *a: {"projPath": nwMinimal}) mp.setattr(nwGUI, "showNewProjectDialog", lambda *a: {"projPath": nwMinimal})
assert not nwGUI.newProject() assert nwGUI.newProject() is False
## ##
# Test the Wizard # Test the Wizard Launching
## ##
monkeypatch.setattr(GuiProjectWizard, "exec_", lambda *a: None)
nwGUI.mainConf.lastPath = " " nwGUI.mainConf.lastPath = " "
monkeypatch.setattr(GuiProjectWizard, "exec_", lambda *a: None)
nwGUI.closeProject() result = nwGUI.showNewProjectDialog()
nwGUI.showNewProjectDialog()
qtbot.waitUntil(lambda: getGuiItem("GuiProjectWizard") is not None, timeout=1000) qtbot.waitUntil(lambda: getGuiItem("GuiProjectWizard") is not None, timeout=1000)
nwWiz = getGuiItem("GuiProjectWizard") nwWiz = getGuiItem("GuiProjectWizard")
assert isinstance(nwWiz, GuiProjectWizard) assert isinstance(nwWiz, GuiProjectWizard)
nwWiz.show() nwWiz.show()
qtbot.wait(stepDelay)
qtbot.mouseClick(nwWiz.button(QWizard.CancelButton), Qt.LeftButton)
assert result is None
with monkeypatch.context() as mp:
mp.setattr(GuiProjectWizard, "result", lambda *a: QDialog.Accepted)
result = nwGUI.showNewProjectDialog()
nwWiz.button(QWizard.CancelButton).click()
assert isinstance(result, dict)
nwWiz.reject()
nwWiz.close()
# qtbot.stopForInteraction()
# END Test testToolProjectWizard_Handling
@pytest.mark.gui
@pytest.mark.parametrize("prjType", ["minimal", "custom1", "custom2", "sample"])
def testToolProjectWizard_Run(qtbot, monkeypatch, nwGUI, fncDir, prjType):
"""Test the new project wizard with a set of selection scenarios.
"""
monkeypatch.setattr(QMessageBox, "question", lambda *a: QMessageBox.Yes)
monkeypatch.setattr(QMessageBox, "critical", lambda *a: QMessageBox.Yes)
monkeypatch.setattr(GuiProjectWizard, "exec_", lambda *a: None)
nwGUI.mainConf.lastPath = " "
nwWiz = GuiProjectWizard(nwGUI)
nwWiz.show()
qtbot.wait(stepDelay) qtbot.wait(stepDelay)
for wStep in range(4):
# This does not actually create the project, it just generates the
# dictionary that defines it.
# Intro Page # Intro Page
# ==========
introPage = nwWiz.currentPage() introPage = nwWiz.currentPage()
assert isinstance(introPage, ProjWizardIntroPage) assert isinstance(introPage, ProjWizardIntroPage)
assert not nwWiz.button(QWizard.NextButton).isEnabled() assert not nwWiz.button(QWizard.NextButton).isEnabled()
qtbot.wait(stepDelay) introPage.projName.setText("Test Wizard")
for c in ("Test Minimal %d" % wStep): introPage.projTitle.setText("My Novel")
qtbot.keyClick(introPage.projName, c, delay=typeDelay) introPage.projAuthors.setPlainText("Jane Doe")
qtbot.wait(stepDelay)
for c in "Minimal Novel":
qtbot.keyClick(introPage.projTitle, c, delay=typeDelay)
qtbot.wait(stepDelay)
for c in "Jane Doe":
qtbot.keyClick(introPage.projAuthors, c, delay=typeDelay)
# Setting projName should activate the button # Setting projName should activate the button
assert nwWiz.button(QWizard.NextButton).isEnabled() assert nwWiz.button(QWizard.NextButton).isEnabled()
@@ -115,24 +136,37 @@ def testToolProjectWizard_Main(qtbot, monkeypatch, nwGUI, nwMinimal):
qtbot.mouseClick(nwWiz.button(QWizard.NextButton), Qt.LeftButton) qtbot.mouseClick(nwWiz.button(QWizard.NextButton), Qt.LeftButton)
# Folder Page # Folder Page
# ===========
storagePage = nwWiz.currentPage() storagePage = nwWiz.currentPage()
assert isinstance(storagePage, ProjWizardFolderPage) assert isinstance(storagePage, ProjWizardFolderPage)
assert not nwWiz.button(QWizard.NextButton).isEnabled() assert not nwWiz.button(QWizard.NextButton).isEnabled()
assert storagePage.errLabel.text() == ""
if wStep == 0: # Set an invalid path
# Check invalid path first, the first time we reach here storagePage.projPath.setText(os.path.join(fncDir, "not", "a", "path"))
monkeypatch.setattr(QFileDialog, "getExistingDirectory", lambda *a, **kw: "") assert not nwWiz.button(QWizard.NextButton).isEnabled()
qtbot.wait(stepDelay) assert storagePage.errLabel.text().startswith("Error")
# Set an existing path
storagePage.projPath.setText(fncDir)
assert not nwWiz.button(QWizard.NextButton).isEnabled()
assert storagePage.errLabel.text().startswith("Error")
# Return a non-result from browse
with monkeypatch.context() as mp:
mp.setattr(QFileDialog, "getExistingDirectory", lambda *a, **k: "")
qtbot.mouseClick(storagePage.browseButton, Qt.LeftButton, delay=100) qtbot.mouseClick(storagePage.browseButton, Qt.LeftButton, delay=100)
assert storagePage.projPath.text() == "" assert storagePage.errLabel.text() == ""
# Then, we always return nwMinimal as path # Let the browse feature handle it
monkeypatch.setattr(QFileDialog, "getExistingDirectory", lambda *a, **kw: nwMinimal) projPath = os.path.join(fncDir, "Test Wizard")
with monkeypatch.context() as mp:
qtbot.wait(stepDelay) mp.setattr(QFileDialog, "getExistingDirectory", lambda *a, **k: fncDir)
qtbot.mouseClick(storagePage.browseButton, Qt.LeftButton, delay=100) qtbot.mouseClick(storagePage.browseButton, Qt.LeftButton, delay=100)
projPath = os.path.join(nwMinimal, "Test Minimal %d" % wStep)
assert storagePage.projPath.text() == projPath assert storagePage.projPath.text() == projPath
assert storagePage.errLabel.text() == ""
# Setting projPath should activate the button # Setting projPath should activate the button
assert nwWiz.button(QWizard.NextButton).isEnabled() assert nwWiz.button(QWizard.NextButton).isEnabled()
@@ -141,25 +175,27 @@ def testToolProjectWizard_Main(qtbot, monkeypatch, nwGUI, nwMinimal):
qtbot.mouseClick(nwWiz.button(QWizard.NextButton), Qt.LeftButton) qtbot.mouseClick(nwWiz.button(QWizard.NextButton), Qt.LeftButton)
# Populate Page # Populate Page
# =============
popPage = nwWiz.currentPage() popPage = nwWiz.currentPage()
assert isinstance(popPage, ProjWizardPopulatePage) assert isinstance(popPage, ProjWizardPopulatePage)
assert nwWiz.button(QWizard.NextButton).isEnabled() assert nwWiz.button(QWizard.NextButton).isEnabled()
qtbot.wait(stepDelay) qtbot.wait(stepDelay)
if wStep == 0: if prjType.startswith("minimal"):
popPage.popMinimal.setChecked(True) popPage.popMinimal.setChecked(True)
elif wStep == 1: elif prjType.startswith("custom"):
popPage.popCustom.setChecked(True) popPage.popCustom.setChecked(True)
elif wStep == 2: elif prjType.startswith("sample"):
popPage.popCustom.setChecked(True)
elif wStep == 3:
popPage.popSample.setChecked(True) popPage.popSample.setChecked(True)
qtbot.wait(stepDelay) qtbot.wait(stepDelay)
qtbot.mouseClick(nwWiz.button(QWizard.NextButton), Qt.LeftButton) qtbot.mouseClick(nwWiz.button(QWizard.NextButton), Qt.LeftButton)
# Custom Page # Custom Page
if wStep == 1 or wStep == 2: # ===========
if prjType.startswith("custom"):
customPage = nwWiz.currentPage() customPage = nwWiz.currentPage()
assert isinstance(customPage, ProjWizardCustomPage) assert isinstance(customPage, ProjWizardCustomPage)
assert nwWiz.button(QWizard.NextButton).isEnabled() assert nwWiz.button(QWizard.NextButton).isEnabled()
@@ -171,7 +207,7 @@ def testToolProjectWizard_Main(qtbot, monkeypatch, nwGUI, nwMinimal):
customPage.addObject.setChecked(True) customPage.addObject.setChecked(True)
customPage.addEntity.setChecked(True) customPage.addEntity.setChecked(True)
if wStep == 2: if prjType == "custom2":
customPage.numChapters.setValue(0) customPage.numChapters.setValue(0)
customPage.numScenes.setValue(10) customPage.numScenes.setValue(10)
customPage.chFolders.setChecked(False) customPage.chFolders.setChecked(False)
@@ -180,20 +216,24 @@ def testToolProjectWizard_Main(qtbot, monkeypatch, nwGUI, nwMinimal):
qtbot.mouseClick(nwWiz.button(QWizard.NextButton), Qt.LeftButton) qtbot.mouseClick(nwWiz.button(QWizard.NextButton), Qt.LeftButton)
# Final Page # Final Page
# ==========
finalPage = nwWiz.currentPage() finalPage = nwWiz.currentPage()
assert isinstance(finalPage, ProjWizardFinalPage) assert isinstance(finalPage, ProjWizardFinalPage)
assert nwWiz.button(QWizard.FinishButton).isEnabled() # But we don't click it assert nwWiz.button(QWizard.FinishButton).isEnabled() # But we don't click it
# Check Data # Check Data
# ==========
projData = nwGUI._assembleProjectWizardData(nwWiz) projData = nwGUI._assembleProjectWizardData(nwWiz)
assert projData["projName"] == "Test Minimal %d" % wStep assert projData["projName"] == "Test Wizard"
assert projData["projTitle"] == "Minimal Novel" assert projData["projTitle"] == "My Novel"
assert projData["projAuthors"] == "Jane Doe" assert projData["projAuthors"] == "Jane Doe"
assert projData["projPath"] == projPath assert projData["projPath"] == projPath
assert projData["popMinimal"] == (wStep == 0) assert projData["popMinimal"] == prjType.startswith("minimal")
assert projData["popCustom"] == (wStep == 1 or wStep == 2) assert projData["popCustom"] == prjType.startswith("custom")
assert projData["popSample"] == (wStep == 3) assert projData["popSample"] == prjType.startswith("sample")
if wStep == 1 or wStep == 2: if prjType.startswith("custom"):
assert projData["addRoots"] == [ assert projData["addRoots"] == [
nwItemClass.PLOT, nwItemClass.PLOT,
nwItemClass.CHARACTER, nwItemClass.CHARACTER,
@@ -202,7 +242,7 @@ def testToolProjectWizard_Main(qtbot, monkeypatch, nwGUI, nwMinimal):
nwItemClass.OBJECT, nwItemClass.OBJECT,
nwItemClass.ENTITY, nwItemClass.ENTITY,
] ]
if wStep == 1: if prjType == "custom1":
assert projData["numChapters"] == 5 assert projData["numChapters"] == 5
assert projData["numScenes"] == 5 assert projData["numScenes"] == 5
assert projData["chFolders"] assert projData["chFolders"]
@@ -216,12 +256,10 @@ def testToolProjectWizard_Main(qtbot, monkeypatch, nwGUI, nwMinimal):
assert projData["numScenes"] == 0 assert projData["numScenes"] == 0
assert not projData["chFolders"] assert not projData["chFolders"]
# Restart the wizard for next iteration # Cleanup
nwWiz.restart()
nwWiz.reject() nwWiz.reject()
nwWiz.close() nwWiz.close()
# qtbot.stopForInteraction() # qtbot.stopForInteraction()
# END Test testToolProjectWizard_Main # END Test testToolProjectWizard_Run