Update tests

This commit is contained in:
Veronica Berglyd Olsen
2024-05-27 23:36:53 +02:00
parent b33bf954bb
commit 087a9cdd2e
6 changed files with 68 additions and 31 deletions
+7 -6
View File
@@ -23,10 +23,11 @@ from __future__ import annotations
import pytest
from PyQt5.QtCore import QItemSelectionModel
from PyQt5.QtWidgets import QDialog, QListWidgetItem
from PyQt5.QtWidgets import QListWidgetItem
from novelwriter.dialogs.editlabel import GuiEditLabel
from novelwriter.dialogs.quotes import GuiQuoteSelect
from novelwriter.types import QtAccepted, QtRejected
@pytest.mark.gui
@@ -47,17 +48,17 @@ def testDlgOther_QuoteSelect(qtbot, monkeypatch, nwGUI):
assert nwQuot.previewLabel.text() == lastItem
nwQuot.accept()
assert nwQuot.result() == QDialog.DialogCode.Accepted
assert nwQuot.result() == QtAccepted
assert nwQuot.selectedQuote == lastItem
nwQuot.close()
# Test Class Method
with monkeypatch.context() as mp:
mp.setattr(GuiQuoteSelect, "result", lambda *a: QDialog.DialogCode.Accepted)
mp.setattr(GuiQuoteSelect, "result", lambda *a: QtAccepted)
assert GuiQuoteSelect.getQuote(nwGUI, current="X") == ("X", True)
with monkeypatch.context() as mp:
mp.setattr(GuiQuoteSelect, "result", lambda *a: QDialog.DialogCode.Rejected)
mp.setattr(GuiQuoteSelect, "result", lambda *a: QtRejected)
assert GuiQuoteSelect.getQuote(nwGUI, current="X") == ("X", False)
# qtbot.stop()
@@ -69,13 +70,13 @@ def testDlgOther_EditLabel(qtbot, monkeypatch):
monkeypatch.setattr(GuiEditLabel, "exec", lambda *a: None)
with monkeypatch.context() as mp:
mp.setattr(GuiEditLabel, "result", lambda *a: QDialog.DialogCode.Accepted)
mp.setattr(GuiEditLabel, "result", lambda *a: QtAccepted)
newLabel, dlgOk = GuiEditLabel.getLabel(None, text="Hello World") # type: ignore
assert dlgOk is True
assert newLabel == "Hello World"
with monkeypatch.context() as mp:
mp.setattr(GuiEditLabel, "result", lambda *a: QDialog.DialogCode.Rejected)
mp.setattr(GuiEditLabel, "result", lambda *a: QtRejected)
newLabel, dlgOk = GuiEditLabel.getLabel(None, text="Hello World") # type: ignore
assert dlgOk is False
assert newLabel == "Hello World"
+27 -8
View File
@@ -25,16 +25,15 @@ import pytest
from PyQt5.QtCore import Qt
from novelwriter.dialogs.docmerge import GuiDocMerge
from novelwriter.types import QtUserRole
from novelwriter.types import QtAccepted, QtRejected, QtUserRole
from tests.tools import C, buildTestProject
@pytest.mark.gui
def testDlgMerge_Main(qtbot, nwGUI, projPath, mockRnd):
"""Test the merge documents tool.
"""
# Create a new project
def testDlgMerge_Main(qtbot, monkeypatch, nwGUI, projPath, mockRnd):
"""Test the merge documents tool."""
monkeypatch.setattr(GuiDocMerge, "exec", lambda *a: None)
buildTestProject(nwGUI, projPath)
# Check that the dialog kan handle invalid items
@@ -54,13 +53,16 @@ def testDlgMerge_Main(qtbot, nwGUI, projPath, mockRnd):
itemOne = nwMerge.listBox.item(0)
itemTwo = nwMerge.listBox.item(1)
assert itemOne is not None
assert itemTwo is not None
assert itemOne.data(QtUserRole) == C.hChapterDoc
assert itemTwo.data(QtUserRole) == C.hSceneDoc
assert itemOne.checkState() == Qt.CheckState.Checked
assert itemTwo.checkState() == Qt.CheckState.Checked
data = nwMerge.getData()
data = nwMerge.data()
assert data["sHandle"] == C.hChapterDir
assert data["origItems"] == [C.hChapterDir, C.hChapterDoc, C.hSceneDoc]
assert data["moveToTrash"] is False
@@ -70,7 +72,7 @@ def testDlgMerge_Main(qtbot, nwGUI, projPath, mockRnd):
itemTwo.setCheckState(Qt.CheckState.Unchecked)
nwMerge.trashSwitch.setChecked(True)
data = nwMerge.getData()
data = nwMerge.data()
assert data["sHandle"] == C.hChapterDir
assert data["origItems"] == [C.hChapterDir, C.hChapterDoc, C.hSceneDoc]
assert data["moveToTrash"] is True
@@ -79,10 +81,27 @@ def testDlgMerge_Main(qtbot, nwGUI, projPath, mockRnd):
# Restore default values
nwMerge._resetList()
data = nwMerge.getData()
data = nwMerge.data()
assert data["sHandle"] == C.hChapterDir
assert data["origItems"] == [C.hChapterDir, C.hChapterDoc, C.hSceneDoc]
assert data["moveToTrash"] is True
assert data["finalItems"] == [C.hChapterDoc, C.hSceneDoc]
# Test Class Method
with monkeypatch.context() as mp:
mp.setattr(GuiDocMerge, "result", lambda *a: QtAccepted)
data, status = GuiDocMerge.getData(
nwGUI, C.hChapterDir, [C.hChapterDir, C.hChapterDoc, C.hSceneDoc]
)
assert data["sHandle"] == C.hChapterDir
assert status is True
with monkeypatch.context() as mp:
mp.setattr(GuiDocMerge, "result", lambda *a: QtRejected)
data, status = GuiDocMerge.getData(
nwGUI, C.hChapterDir, [C.hChapterDir, C.hChapterDoc, C.hSceneDoc]
)
assert data["sHandle"] == C.hChapterDir
assert status is False
# qtbot.stop()
+18 -4
View File
@@ -25,6 +25,7 @@ import pytest
from novelwriter import SHARED
from novelwriter.dialogs.docsplit import GuiDocSplit
from novelwriter.dialogs.editlabel import GuiEditLabel
from novelwriter.types import QtAccepted, QtRejected
from tests.tools import C, buildTestProject
@@ -33,8 +34,7 @@ from tests.tools import C, buildTestProject
def testDlgSplit_Main(qtbot, monkeypatch, nwGUI, projPath, mockRnd):
"""Test the split document tool."""
monkeypatch.setattr(GuiEditLabel, "getLabel", lambda *a, text: (text, True))
# Create a new project
monkeypatch.setattr(GuiDocSplit, "exec", lambda *a: None)
buildTestProject(nwGUI, projPath)
project = SHARED.project
@@ -74,7 +74,7 @@ def testDlgSplit_Main(qtbot, monkeypatch, nwGUI, projPath, mockRnd):
nwSplit.splitLevel.setCurrentIndex(3)
assert nwSplit.listBox.count() == 12
data, text = nwSplit.getData()
data, text = nwSplit.data()
assert text == docText.splitlines()
assert data["sHandle"] == hSplitDoc
assert data["spLevel"] == 4
@@ -97,5 +97,19 @@ def testDlgSplit_Main(qtbot, monkeypatch, nwGUI, projPath, mockRnd):
nwSplit._loadContent(C.hNovelRoot)
assert nwSplit.listBox.count() == 0
nwSplit.reject()
# Test Class Method
with monkeypatch.context() as mp:
mp.setattr(GuiDocSplit, "result", lambda *a: QtAccepted)
data, text, status = GuiDocSplit.getData(nwGUI, hSplitDoc)
assert data["sHandle"] == hSplitDoc
assert text == docText.splitlines()
assert status is True
with monkeypatch.context() as mp:
mp.setattr(GuiDocSplit, "result", lambda *a: QtRejected)
data, text, status = GuiDocSplit.getData(nwGUI, hSplitDoc)
assert data["sHandle"] == hSplitDoc
assert text == docText.splitlines()
assert status is False
# qtbot.stop()
@@ -23,13 +23,13 @@ from __future__ import annotations
import pytest
from PyQt5.QtGui import QColor
from PyQt5.QtWidgets import QAction, QColorDialog, QDialog
from PyQt5.QtWidgets import QAction, QColorDialog
from novelwriter import CONFIG, SHARED
from novelwriter.dialogs.editlabel import GuiEditLabel
from novelwriter.dialogs.projectsettings import GuiProjectSettings
from novelwriter.enum import nwItemType, nwStatusShape
from novelwriter.types import QtMouseLeft
from novelwriter.types import QtAccepted, QtMouseLeft
from tests.tools import C, buildTestProject
@@ -43,7 +43,7 @@ def testDlgProjSettings_Dialog(qtbot, monkeypatch, nwGUI):
"""
# Block the GUI blocking thread
monkeypatch.setattr(GuiProjectSettings, "exec", lambda *a: None)
monkeypatch.setattr(GuiProjectSettings, "result", lambda *a: QDialog.DialogCode.Accepted)
monkeypatch.setattr(GuiProjectSettings, "result", lambda *a: QtAccepted)
# Check that we cannot open when there is no project
nwGUI.mainMenu.aProjectSettings.activate(QAction.Trigger)
+3 -2
View File
@@ -23,11 +23,12 @@ from __future__ import annotations
import pytest
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QAction, QDialog, QFileDialog
from PyQt5.QtWidgets import QAction, QFileDialog
from novelwriter import SHARED
from novelwriter.core.spellcheck import UserDictionary
from novelwriter.dialogs.wordlist import GuiWordList
from novelwriter.types import QtAccepted
from tests.mocked import causeOSError
from tests.tools import buildTestProject
@@ -39,7 +40,7 @@ def testDlgWordList_Dialog(qtbot, monkeypatch, nwGUI, fncPath, projPath):
buildTestProject(nwGUI, projPath)
monkeypatch.setattr(GuiWordList, "exec", lambda *a: None)
monkeypatch.setattr(GuiWordList, "result", lambda *a: QDialog.DialogCode.Accepted)
monkeypatch.setattr(GuiWordList, "result", lambda *a: QtAccepted)
monkeypatch.setattr(GuiWordList, "accept", lambda *a: None)
# Open project