Update tests

This commit is contained in:
Veronica K. B. Olsen
2021-04-25 23:22:05 +02:00
parent 0eb09a2136
commit bd28867804
3 changed files with 28 additions and 28 deletions
+2 -2
View File
@@ -107,7 +107,7 @@ class DummyApp:
# =========================================================================== # # =========================================================================== #
def causeOSError(*args, **kwargs): def causeOSError(*args, **kwargs):
raise OSError raise OSError("OSError")
def causeException(*args, **kwargs): def causeException(*args, **kwargs):
raise Exception raise Exception("Exception")
+24 -22
View File
@@ -26,7 +26,6 @@ import pytest
from dummy import causeOSError from dummy import causeOSError
from nw.core import NWProject, NWDoc from nw.core import NWProject, NWDoc
from nw.core.item import NWItem
from nw.enum import nwItemClass, nwItemLayout from nw.enum import nwItemClass, nwItemLayout
@pytest.mark.core @pytest.mark.core
@@ -37,39 +36,37 @@ def testCoreDocument_LoadSave(monkeypatch, dummyGUI, nwMinimal):
assert theProject.openProject(nwMinimal) assert theProject.openProject(nwMinimal)
assert theProject.projPath == nwMinimal assert theProject.projPath == nwMinimal
theDoc = NWDoc(theProject)
sHandle = "8c659a11cd429" sHandle = "8c659a11cd429"
# Not a valid handle # Not a valid handle
assert theDoc.readDocument("dummy") is None theDoc = NWDoc(theProject, "dummy")
assert theDoc.readDocument() is None
# Non-existent handle # Non-existent handle
assert theDoc.readDocument("0000000000000") is None theDoc = NWDoc(theProject, "0000000000000")
assert theDoc.readDocument() is None
# Cause open() to fail while loading # Cause open() to fail while loading
def dummyOpen(*args, **kwargs):
raise OSError
with monkeypatch.context() as mp: with monkeypatch.context() as mp:
mp.setattr("builtins.open", dummyOpen) mp.setattr("builtins.open", causeOSError)
assert theDoc.readDocument(sHandle) is None theDoc = NWDoc(theProject, sHandle)
assert theDoc.readDocument() is None
assert theDoc.getError() == "OSError"
# Load the text # Load the text
assert theDoc.readDocument(sHandle) == "### New Scene\n\n" theDoc = NWDoc(theProject, sHandle)
assert theDoc.readDocument() == "### New Scene\n\n"
# Try to open a new (non-existent) file # Try to open a new (non-existent) file
nHandle = theProject.projTree.findRoot(nwItemClass.NOVEL) nHandle = theProject.projTree.findRoot(nwItemClass.NOVEL)
assert nHandle is not None assert nHandle is not None
xHandle = theProject.newFile("New File", nwItemClass.NOVEL, nHandle) xHandle = theProject.newFile("New File", nwItemClass.NOVEL, nHandle)
assert theDoc.readDocument(xHandle) == "" theDoc = NWDoc(theProject, xHandle)
assert theDoc.readDocument() == ""
# Check cached item
assert isinstance(theDoc._theItem, NWItem)
assert theDoc.readDocument(xHandle, isOrphan=True) == ""
assert theDoc._theItem is None
# Set handle and save again # Set handle and save again
theText = "### Test File\n\nText ...\n\n" theText = "### Test File\n\nText ...\n\n"
theDoc = NWDoc(theProject, xHandle)
assert theDoc.readDocument(xHandle) == "" assert theDoc.readDocument(xHandle) == ""
assert theDoc.writeDocument(theText) assert theDoc.writeDocument(theText)
@@ -98,22 +95,27 @@ def testCoreDocument_LoadSave(monkeypatch, dummyGUI, nwMinimal):
with monkeypatch.context() as mp: with monkeypatch.context() as mp:
mp.setattr("builtins.open", causeOSError) mp.setattr("builtins.open", causeOSError)
assert not theDoc.writeDocument(theText) assert not theDoc.writeDocument(theText)
assert theDoc.getError() == "OSError"
# Saving with no handle # Saving with no handle
theDoc.clearDocument() theDoc.clearDocument()
assert not theDoc.writeDocument(theText) assert not theDoc.writeDocument(theText)
# Delete the last document # Delete the last document
assert not theDoc.deleteDocument("dummy") theDoc = NWDoc(theProject, "dummy")
assert not theDoc.deleteDocument()
assert os.path.isfile(docPath) assert os.path.isfile(docPath)
# Cause the delete to fail # Cause the delete to fail
with monkeypatch.context() as mp: with monkeypatch.context() as mp:
mp.setattr("os.unlink", causeOSError) mp.setattr("os.unlink", causeOSError)
assert not theDoc.deleteDocument(xHandle) theDoc = NWDoc(theProject, xHandle)
assert not theDoc.deleteDocument()
assert theDoc.getError() == "OSError"
# Make the delete pass # Make the delete pass
assert theDoc.deleteDocument(xHandle) theDoc = NWDoc(theProject, xHandle)
assert theDoc.deleteDocument()
assert not os.path.isfile(docPath) assert not os.path.isfile(docPath)
# END Test testCoreDocument_Load # END Test testCoreDocument_Load
@@ -126,11 +128,11 @@ def testCoreDocument_Methods(monkeypatch, dummyGUI, nwMinimal):
assert theProject.openProject(nwMinimal) assert theProject.openProject(nwMinimal)
assert theProject.projPath == nwMinimal assert theProject.projPath == nwMinimal
theDoc = NWDoc(theProject)
sHandle = "8c659a11cd429" sHandle = "8c659a11cd429"
theDoc = NWDoc(theProject, sHandle)
docPath = os.path.join(nwMinimal, "content", sHandle+".nwd") docPath = os.path.join(nwMinimal, "content", sHandle+".nwd")
assert theDoc.readDocument(sHandle) == "### New Scene\n\n" assert theDoc.readDocument() == "### New Scene\n\n"
# Check location # Check location
assert theDoc.getFileLocation() == docPath assert theDoc.getFileLocation() == docPath
@@ -158,6 +160,6 @@ def testCoreDocument_Methods(monkeypatch, dummyGUI, nwMinimal):
"Text ...\n\n" "Text ...\n\n"
) )
assert theDoc.readDocument(sHandle) == "### Test File\n\nText ...\n\n" assert theDoc.readDocument() == "### Test File\n\nText ...\n\n"
# END Test testCoreDocument_Methods # END Test testCoreDocument_Methods
+2 -4
View File
@@ -137,10 +137,8 @@ def testCoreToken_TextOps(monkeypatch, nwMinimal, dummyGUI):
) )
docTextR = docText.replace("<A>", "this").replace("<B>", "that") docTextR = docText.replace("<A>", "this").replace("<B>", "that")
nDoc = NWDoc(theProject) nDoc = NWDoc(theProject, sHandle)
nDoc.readDocument(sHandle) assert nDoc.writeDocument(docText)
nDoc.writeDocument(docText)
nDoc.clearDocument()
theProject.setAutoReplace({"A": "this", "B": "that"}) theProject.setAutoReplace({"A": "this", "B": "that"})