From bd2886780440ca8a7d10a453a7b81b6673efd7cf Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Sun, 25 Apr 2021 23:22:05 +0200 Subject: [PATCH] Update tests --- tests/dummy.py | 4 +-- tests/test_core/test_core_document.py | 46 ++++++++++++++------------ tests/test_core/test_core_tokenizer.py | 6 ++-- 3 files changed, 28 insertions(+), 28 deletions(-) diff --git a/tests/dummy.py b/tests/dummy.py index 653fca58..14f2ef91 100644 --- a/tests/dummy.py +++ b/tests/dummy.py @@ -107,7 +107,7 @@ class DummyApp: # =========================================================================== # def causeOSError(*args, **kwargs): - raise OSError + raise OSError("OSError") def causeException(*args, **kwargs): - raise Exception + raise Exception("Exception") diff --git a/tests/test_core/test_core_document.py b/tests/test_core/test_core_document.py index 056b212e..d44b3a08 100644 --- a/tests/test_core/test_core_document.py +++ b/tests/test_core/test_core_document.py @@ -26,7 +26,6 @@ import pytest from dummy import causeOSError from nw.core import NWProject, NWDoc -from nw.core.item import NWItem from nw.enum import nwItemClass, nwItemLayout @pytest.mark.core @@ -37,39 +36,37 @@ def testCoreDocument_LoadSave(monkeypatch, dummyGUI, nwMinimal): assert theProject.openProject(nwMinimal) assert theProject.projPath == nwMinimal - theDoc = NWDoc(theProject) sHandle = "8c659a11cd429" # Not a valid handle - assert theDoc.readDocument("dummy") is None + theDoc = NWDoc(theProject, "dummy") + assert theDoc.readDocument() is None # Non-existent handle - assert theDoc.readDocument("0000000000000") is None + theDoc = NWDoc(theProject, "0000000000000") + assert theDoc.readDocument() is None # Cause open() to fail while loading - def dummyOpen(*args, **kwargs): - raise OSError - with monkeypatch.context() as mp: - mp.setattr("builtins.open", dummyOpen) - assert theDoc.readDocument(sHandle) is None + mp.setattr("builtins.open", causeOSError) + theDoc = NWDoc(theProject, sHandle) + assert theDoc.readDocument() is None + assert theDoc.getError() == "OSError" # 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 nHandle = theProject.projTree.findRoot(nwItemClass.NOVEL) assert nHandle is not None xHandle = theProject.newFile("New File", nwItemClass.NOVEL, nHandle) - assert theDoc.readDocument(xHandle) == "" - - # Check cached item - assert isinstance(theDoc._theItem, NWItem) - assert theDoc.readDocument(xHandle, isOrphan=True) == "" - assert theDoc._theItem is None + theDoc = NWDoc(theProject, xHandle) + assert theDoc.readDocument() == "" # Set handle and save again theText = "### Test File\n\nText ...\n\n" + theDoc = NWDoc(theProject, xHandle) assert theDoc.readDocument(xHandle) == "" assert theDoc.writeDocument(theText) @@ -98,22 +95,27 @@ def testCoreDocument_LoadSave(monkeypatch, dummyGUI, nwMinimal): with monkeypatch.context() as mp: mp.setattr("builtins.open", causeOSError) assert not theDoc.writeDocument(theText) + assert theDoc.getError() == "OSError" # Saving with no handle theDoc.clearDocument() assert not theDoc.writeDocument(theText) # Delete the last document - assert not theDoc.deleteDocument("dummy") + theDoc = NWDoc(theProject, "dummy") + assert not theDoc.deleteDocument() assert os.path.isfile(docPath) # Cause the delete to fail with monkeypatch.context() as mp: 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 - assert theDoc.deleteDocument(xHandle) + theDoc = NWDoc(theProject, xHandle) + assert theDoc.deleteDocument() assert not os.path.isfile(docPath) # END Test testCoreDocument_Load @@ -126,11 +128,11 @@ def testCoreDocument_Methods(monkeypatch, dummyGUI, nwMinimal): assert theProject.openProject(nwMinimal) assert theProject.projPath == nwMinimal - theDoc = NWDoc(theProject) sHandle = "8c659a11cd429" + theDoc = NWDoc(theProject, sHandle) 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 assert theDoc.getFileLocation() == docPath @@ -158,6 +160,6 @@ def testCoreDocument_Methods(monkeypatch, dummyGUI, nwMinimal): "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 diff --git a/tests/test_core/test_core_tokenizer.py b/tests/test_core/test_core_tokenizer.py index 6d13faee..e766450b 100644 --- a/tests/test_core/test_core_tokenizer.py +++ b/tests/test_core/test_core_tokenizer.py @@ -137,10 +137,8 @@ def testCoreToken_TextOps(monkeypatch, nwMinimal, dummyGUI): ) docTextR = docText.replace("", "this").replace("", "that") - nDoc = NWDoc(theProject) - nDoc.readDocument(sHandle) - nDoc.writeDocument(docText) - nDoc.clearDocument() + nDoc = NWDoc(theProject, sHandle) + assert nDoc.writeDocument(docText) theProject.setAutoReplace({"A": "this", "B": "that"})