Add test coverage of new function in Document class

This commit is contained in:
Veronica Berglyd Olsen
2023-07-20 21:14:10 +02:00
parent def846ce4b
commit 4e448134fc
+6 -2
View File
@@ -31,8 +31,7 @@ from novelwriter.core.document import NWDocument
@pytest.mark.core @pytest.mark.core
def testCoreDocument_LoadSave(monkeypatch, mockGUI, fncPath, mockRnd): def testCoreDocument_LoadSave(monkeypatch, mockGUI, fncPath, mockRnd):
"""Test loading and saving a document with the NWDocument class. """Test loading and saving a document with the NWDocument class."""
"""
theProject = NWProject(mockGUI) theProject = NWProject(mockGUI)
mockRnd.reset() mockRnd.reset()
buildTestProject(theProject, fncPath) buildTestProject(theProject, fncPath)
@@ -44,27 +43,32 @@ def testCoreDocument_LoadSave(monkeypatch, mockGUI, fncPath, mockRnd):
theDoc = NWDocument(theProject, "stuff") theDoc = NWDocument(theProject, "stuff")
assert bool(theDoc) is False assert bool(theDoc) is False
assert theDoc.readDocument() is None assert theDoc.readDocument() is None
assert theDoc.fileExists() is False
# Non-existent handle # Non-existent handle
theDoc = NWDocument(theProject, C.hInvalid) theDoc = NWDocument(theProject, C.hInvalid)
assert theDoc.readDocument() is None assert theDoc.readDocument() is None
assert theDoc._currHash is None assert theDoc._currHash is None
assert theDoc.fileExists() is False
# No content path # No content path
with monkeypatch.context() as mp: with monkeypatch.context() as mp:
mp.setattr("novelwriter.core.storage.NWStorage.contentPath", property(lambda *a: None)) mp.setattr("novelwriter.core.storage.NWStorage.contentPath", property(lambda *a: None))
theDoc = NWDocument(theProject, C.hSceneDoc) theDoc = NWDocument(theProject, C.hSceneDoc)
assert theDoc.readDocument() is None assert theDoc.readDocument() is None
assert theDoc.fileExists() is False
# Cause open() to fail while loading # Cause open() to fail while loading
with monkeypatch.context() as mp: with monkeypatch.context() as mp:
mp.setattr("builtins.open", causeOSError) mp.setattr("builtins.open", causeOSError)
theDoc = NWDocument(theProject, C.hSceneDoc) theDoc = NWDocument(theProject, C.hSceneDoc)
assert theDoc.fileExists() is True
assert theDoc.readDocument() is None assert theDoc.readDocument() is None
assert theDoc.getError() == "OSError: Mock OSError" assert theDoc.getError() == "OSError: Mock OSError"
# Load the text # Load the text
theDoc = NWDocument(theProject, C.hSceneDoc) theDoc = NWDocument(theProject, C.hSceneDoc)
assert theDoc.fileExists() is True
assert theDoc.readDocument() == "### New Scene\n\n" assert theDoc.readDocument() == "### New Scene\n\n"
# Try to open a new (non-existent) file # Try to open a new (non-existent) file