Fix test coverage of wordlist dialog

This commit is contained in:
Veronica Berglyd Olsen
2023-06-13 21:10:11 +02:00
parent 8d38f27eee
commit f33ba03a2f
17 changed files with 52 additions and 60 deletions
+4 -8
View File
@@ -61,8 +61,7 @@ def resetConfigVars():
@pytest.fixture(scope="session", autouse=True)
def sessionFixture():
"""A session wide fixture to set up the test environment.
"""
"""A session wide fixture to set up the test environment."""
if _TMP_ROOT.exists():
shutil.rmtree(_TMP_ROOT)
_TMP_ROOT.mkdir()
@@ -111,8 +110,7 @@ def tstPaths():
@pytest.fixture(scope="function")
def fncPath():
"""A temporary folder for a single test function.
"""
"""A temporary folder for a single test function."""
fncPath = _TMP_ROOT / "function"
if fncPath.is_dir():
shutil.rmtree(fncPath)
@@ -139,16 +137,14 @@ def projPath(fncPath):
@pytest.fixture(scope="function")
def mockGUI():
"""Create a mock instance of novelWriter's main GUI class.
"""
"""Create a mock instance of novelWriter's main GUI class."""
theGui = MockGuiMain()
return theGui
@pytest.fixture(scope="function")
def nwGUI(qtbot, monkeypatch, functionFixture):
"""Create an instance of the novelWriter GUI.
"""
"""Create an instance of the novelWriter GUI."""
monkeypatch.setattr(QMessageBox, "warning", lambda *a: QMessageBox.Ok)
monkeypatch.setattr(QMessageBox, "critical", lambda *a: QMessageBox.Ok)
monkeypatch.setattr(QMessageBox, "information", lambda *a: QMessageBox.Ok)
+29 -30
View File
@@ -24,9 +24,9 @@ import pytest
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QDialog, QAction
from tools import buildTestProject, writeFile, readFile, getGuiItem
from mocked import causeOSError
from tools import buildTestProject, getGuiItem
from novelwriter.core.spellcheck import UserDictionary
from novelwriter.dialogs.wordlist import GuiWordList
@@ -42,7 +42,6 @@ def testDlgWordList_Dialog(qtbot, monkeypatch, nwGUI, projPath):
# Open project
nwGUI.openProject(projPath)
dictFile = projPath / "meta" / "wordlist.txt"
# Load the dialog
nwGUI.mainMenu.aEditWordList.activate(QAction.Trigger)
@@ -56,15 +55,15 @@ def testDlgWordList_Dialog(qtbot, monkeypatch, nwGUI, projPath):
assert wList.listBox.count() == 0
# Add words
writeFile(dictFile, (
"word_a\n"
"word_c\n"
"word_g\n"
" \n" # Should be ignored
"word_f\n"
"word_b\n"
))
assert wList._loadWordList()
userDict = UserDictionary(nwGUI.theProject)
userDict.add("word_a")
userDict.add("word_c")
userDict.add("word_g")
userDict.add("word_f")
userDict.add("word_b")
userDict.save()
wList._loadWordList()
# Check that the content was loaded
assert wList.listBox.item(0).text() == "word_a"
@@ -72,18 +71,22 @@ def testDlgWordList_Dialog(qtbot, monkeypatch, nwGUI, projPath):
assert wList.listBox.item(2).text() == "word_c"
assert wList.listBox.item(3).text() == "word_f"
assert wList.listBox.item(4).text() == "word_g"
assert wList.listBox.count() == 5
# Add a blank word
# Add a blank word, which is ignored
wList.newEntry.setText(" ")
assert not wList._doAdd()
wList._doAdd()
assert wList.listBox.count() == 5
# Add an existing word
# Add an existing word, which is ignored
wList.newEntry.setText("word_c")
assert not wList._doAdd()
wList._doAdd()
assert wList.listBox.count() == 5
# Add a new word
wList.newEntry.setText("word_d")
assert wList._doAdd()
wList._doAdd()
assert wList.listBox.count() == 6
# Check that the content now
assert wList.listBox.item(0).text() == "word_a"
@@ -95,7 +98,7 @@ def testDlgWordList_Dialog(qtbot, monkeypatch, nwGUI, projPath):
# Delete a word
wList.newEntry.setText("delete_me")
assert wList._doAdd()
wList._doAdd()
assert wList.listBox.item(0).text() == "delete_me"
delItem = wList.listBox.findItems("delete_me", Qt.MatchExactly)[0]
@@ -107,18 +110,14 @@ def testDlgWordList_Dialog(qtbot, monkeypatch, nwGUI, projPath):
# Save files
assert wList._doSave()
assert readFile(dictFile) == (
"word_a\n"
"word_b\n"
"word_c\n"
"word_d\n"
"word_f\n"
"word_g\n"
)
# Save again and make it fail
monkeypatch.setattr("builtins.open", causeOSError)
assert not wList._doSave()
userDict.load()
assert len(list(userDict)) == 6
assert "word_a" in userDict
assert "word_b" in userDict
assert "word_c" in userDict
assert "word_d" in userDict
assert "word_f" in userDict
assert "word_g" in userDict
# qtbot.stop()
wList._doClose()