Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vkbo committed May 27, 2024
1 parent b33bf95 commit 087a9cd
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 31 deletions.
13 changes: 7 additions & 6 deletions tests/test_dialogs/test_dlg_dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
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
Expand All @@ -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()
Expand All @@ -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"
35 changes: 27 additions & 8 deletions tests/test_dialogs/test_dlg_docmerge.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,15 @@
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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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()
22 changes: 18 additions & 4 deletions tests/test_dialogs/test_dlg_docsplit.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
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

Expand All @@ -33,8 +34,7 @@
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
Expand Down Expand Up @@ -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
Expand All @@ -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()
6 changes: 3 additions & 3 deletions tests/test_dialogs/test_dlg_projectsettings.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
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

Expand All @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions tests/test_dialogs/test_dlg_wordlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@
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
Expand All @@ -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
Expand Down
18 changes: 10 additions & 8 deletions tests/test_gui/test_gui_projtree.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

from PyQt5.QtCore import QEvent, QMimeData, QPoint, Qt, QTimer
from PyQt5.QtGui import QDragEnterEvent, QDragMoveEvent, QDropEvent, QMouseEvent
from PyQt5.QtWidgets import QDialog, QMenu, QMessageBox, QTreeWidget, QTreeWidgetItem
from PyQt5.QtWidgets import QMenu, QMessageBox, QTreeWidget, QTreeWidgetItem

from novelwriter import CONFIG, SHARED
from novelwriter.core.item import NWItem
Expand All @@ -37,7 +37,7 @@
from novelwriter.enum import nwItemClass, nwItemLayout, nwItemType, nwWidget
from novelwriter.gui.projtree import GuiProjectTree, GuiProjectView, _TreeContextMenu
from novelwriter.guimain import GuiMain
from novelwriter.types import QtModeNone, QtMouseLeft, QtMouseMiddle
from novelwriter.types import QtAccepted, QtModeNone, QtMouseLeft, QtMouseMiddle, QtRejected

from tests.mocked import causeOSError
from tests.tools import C, buildTestProject
Expand Down Expand Up @@ -529,8 +529,9 @@ def testGuiProjTree_MergeDocuments(qtbot, monkeypatch, nwGUI, projPath, mockRnd,

monkeypatch.setattr(GuiDocMerge, "__init__", lambda *a: None)
monkeypatch.setattr(GuiDocMerge, "exec", lambda *a: None)
monkeypatch.setattr(GuiDocMerge, "result", lambda *a: QDialog.DialogCode.Accepted)
monkeypatch.setattr(GuiDocMerge, "getData", lambda *a: mergeData)
monkeypatch.setattr(GuiDocMerge, "deleteLater", lambda *a: None)
monkeypatch.setattr(GuiDocMerge, "result", lambda *a: QtAccepted)
monkeypatch.setattr(GuiDocMerge, "data", lambda *a: mergeData)

buildTestProject(nwGUI, projPath)

Expand Down Expand Up @@ -585,7 +586,7 @@ def testGuiProjTree_MergeDocuments(qtbot, monkeypatch, nwGUI, projPath, mockRnd,

# User cancels merge
with monkeypatch.context() as mp:
mp.setattr(GuiDocMerge, "result", lambda *a: QDialog.DialogCode.Rejected)
mp.setattr(GuiDocMerge, "result", lambda *a: QtRejected)
assert projTree._mergeDocuments(hChapter1, True) is False

# The merge goes through
Expand Down Expand Up @@ -628,8 +629,9 @@ def testGuiProjTree_SplitDocument(qtbot, monkeypatch, nwGUI, projPath, mockRnd,

monkeypatch.setattr(GuiDocSplit, "__init__", lambda *a: None)
monkeypatch.setattr(GuiDocSplit, "exec", lambda *a: None)
monkeypatch.setattr(GuiDocSplit, "result", lambda *a: QDialog.DialogCode.Accepted)
monkeypatch.setattr(GuiDocSplit, "getData", lambda *a: (splitData, splitText))
monkeypatch.setattr(GuiDocSplit, "deleteLater", lambda *a: None)
monkeypatch.setattr(GuiDocSplit, "result", lambda *a: QtAccepted)
monkeypatch.setattr(GuiDocSplit, "data", lambda *a: (splitData, splitText))

# Create a project
buildTestProject(nwGUI, projPath)
Expand Down Expand Up @@ -722,7 +724,7 @@ def testGuiProjTree_SplitDocument(qtbot, monkeypatch, nwGUI, projPath, mockRnd,

# Cancelled by user
with monkeypatch.context() as mp:
mp.setattr(GuiDocSplit, "result", lambda *a: QDialog.DialogCode.Rejected)
mp.setattr(GuiDocSplit, "result", lambda *a: QtRejected)
assert projTree._splitDocument(hSplitDoc) is False

# qtbot.stop()
Expand Down

0 comments on commit 087a9cd

Please sign in to comment.