From 6a1dc8b5ae6b077d0e9adb7b6708cc3c1bb07fa8 Mon Sep 17 00:00:00 2001
From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com>
Date: Mon, 30 Nov 2020 22:28:01 +0100
Subject: [PATCH] New test for NWItem class
---
nw/core/item.py | 14 +-
tests/conftest.py | 271 +++++++++++-----------
tests/{test_item.py => test_core_item.py} | 190 +++++++++++----
3 files changed, 300 insertions(+), 175 deletions(-)
rename tests/{test_item.py => test_core_item.py} (65%)
diff --git a/nw/core/item.py b/nw/core/item.py
index 6793af39..ead16ab8 100644
--- a/nw/core/item.py
+++ b/nw/core/item.py
@@ -96,20 +96,21 @@ class NWItem():
return False
if "handle" in xItem.attrib:
- self.itemHandle = xItem.attrib["handle"]
+ self.setHandle(xItem.attrib["handle"])
else:
logger.error("XML item entry does not have a handle")
return False
if "parent" in xItem.attrib:
- self.itemParent = xItem.attrib["parent"]
+ self.setParent(xItem.attrib["parent"])
+
+ if "order" in xItem.attrib:
+ self.setOrder(xItem.attrib["order"])
retStatus = True
for xValue in xItem:
if xValue.tag == "name":
self.setName(xValue.text)
- elif xValue.tag == "order":
- self.setOrder(xValue.text)
elif xValue.tag == "type":
self.setType(xValue.text)
elif xValue.tag == "class":
@@ -156,7 +157,10 @@ class NWItem():
def setName(self, theName):
"""Set the item name.
"""
- self.itemName = theName.strip()
+ if isinstance(theName, str):
+ self.itemName = theName.strip()
+ else:
+ self.itemName = ""
return
def setHandle(self, theHandle):
diff --git a/tests/conftest.py b/tests/conftest.py
index b8cc2fef..97aafd7d 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -9,7 +9,7 @@ import os
from nwdummy import DummyMain
-from PyQt5.QtWidgets import QMessageBox
+# from PyQt5.QtWidgets import QMessageBox
sys.path.insert(1, os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)))
@@ -20,7 +20,7 @@ from nw.config import Config # noqa: E402
##
@pytest.fixture(scope="session")
-def nwTemp():
+def tmpDir():
"""A temporary folder for the test session. This folder is
presistent after the test so that the status of generated files can
be checked. The folder is instead cleared before a new test session.
@@ -33,170 +33,179 @@ def nwTemp():
os.mkdir(tempDir)
return tempDir
-@pytest.fixture(scope="session")
-def nwRef():
- """The folder where all the reference files are stored for verifying
- the results of tests.
- """
- testDir = os.path.dirname(__file__)
- refDir = os.path.join(testDir, "reference")
- return refDir
+# @pytest.fixture(scope="session")
+# def nwRef():
+# """The folder where all the reference files are stored for verifying
+# the results of tests.
+# """
+# testDir = os.path.dirname(__file__)
+# refDir = os.path.join(testDir, "reference")
+# return refDir
##
# novelWriter Objects
##
+# @pytest.fixture(scope="session")
+# def tmpConf(nwTemp):
+# """Create a temporary novelWriter configuration object.
+# """
+# theConf = Config()
+# theConf.initConfig(nwTemp, nwTemp)
+# theConf.setLastPath("")
+# return theConf
+
@pytest.fixture(scope="session")
-def tmpConf(nwTemp):
+def tmpConf(tmpDir):
"""Create a temporary novelWriter configuration object.
"""
theConf = Config()
- theConf.initConfig(nwTemp, nwTemp)
+ theConf.initConfig(tmpDir, tmpDir)
theConf.setLastPath("")
return theConf
-@pytest.fixture(scope="session")
-def nwConf(nwRef, nwTemp):
- """Temporary novelWriter configuration used for the dummy instance
- of novelWriter's main GUI.
- """
- theConf = Config()
- theConf.initConfig(nwRef, nwTemp)
- return theConf
+# @pytest.fixture(scope="session")
+# def nwConf(nwRef, nwTemp):
+# """Temporary novelWriter configuration used for the dummy instance
+# of novelWriter's main GUI.
+# """
+# theConf = Config()
+# theConf.initConfig(nwRef, nwTemp)
+# return theConf
@pytest.fixture(scope="session")
-def nwDummy(nwRef, nwTemp, nwConf):
+def dummyGUI(tmpConf):
"""Create a dummy instance of novelWriter's main GUI class.
"""
theDummy = DummyMain()
- theDummy.mainConf = nwConf
+ theDummy.mainConf = tmpConf
return theDummy
##
# Temporary Test Folders
##
-@pytest.fixture(scope="session")
-def nwTempProj(nwTemp):
- """A temporary folder for project tests.
- """
- projDir = os.path.join(nwTemp, "proj")
- if not os.path.isdir(projDir):
- os.mkdir(projDir)
- return projDir
+# @pytest.fixture(scope="session")
+# def nwTempProj(nwTemp):
+# """A temporary folder for project tests.
+# """
+# projDir = os.path.join(nwTemp, "proj")
+# if not os.path.isdir(projDir):
+# os.mkdir(projDir)
+# return projDir
-@pytest.fixture(scope="session")
-def nwTempGUI(nwTemp):
- """A temporary folder for GUI tests.
- """
- guiDir = os.path.join(nwTemp, "gui")
- if not os.path.isdir(guiDir):
- os.mkdir(guiDir)
- return guiDir
+# @pytest.fixture(scope="session")
+# def nwTempGUI(nwTemp):
+# """A temporary folder for GUI tests.
+# """
+# guiDir = os.path.join(nwTemp, "gui")
+# if not os.path.isdir(guiDir):
+# os.mkdir(guiDir)
+# return guiDir
-@pytest.fixture(scope="session")
-def nwTempBuild(nwTemp):
- """A temporary folder for build tests.
- """
- buildDir = os.path.join(nwTemp, "build")
- if not os.path.isdir(buildDir):
- os.mkdir(buildDir)
- return buildDir
+# @pytest.fixture(scope="session")
+# def nwTempBuild(nwTemp):
+# """A temporary folder for build tests.
+# """
+# buildDir = os.path.join(nwTemp, "build")
+# if not os.path.isdir(buildDir):
+# os.mkdir(buildDir)
+# return buildDir
-@pytest.fixture(scope="function")
-def nwFuncTemp(nwTemp):
- """A temporary folder for a single test function.
- """
- funcDir = os.path.join(nwTemp, "ftemp")
- if os.path.isdir(funcDir):
- shutil.rmtree(funcDir)
- if not os.path.isdir(funcDir):
- os.mkdir(funcDir)
- yield funcDir
- if os.path.isdir(funcDir):
- shutil.rmtree(funcDir)
- return
+# @pytest.fixture(scope="function")
+# def nwFuncTemp(nwTemp):
+# """A temporary folder for a single test function.
+# """
+# funcDir = os.path.join(nwTemp, "ftemp")
+# if os.path.isdir(funcDir):
+# shutil.rmtree(funcDir)
+# if not os.path.isdir(funcDir):
+# os.mkdir(funcDir)
+# yield funcDir
+# if os.path.isdir(funcDir):
+# shutil.rmtree(funcDir)
+# return
##
# Temp Folders for Projects
##
-@pytest.fixture(scope="function")
-def nwMinimal(nwTemp):
- """A minimal novelWriter example project.
- """
- testDir = os.path.dirname(__file__)
- minimalStore = os.path.join(testDir, "minimal")
- minimalDir = os.path.join(nwTemp, "minimal")
- if os.path.isdir(minimalDir):
- shutil.rmtree(minimalDir)
- shutil.copytree(minimalStore, minimalDir)
- cacheDir = os.path.join(minimalDir, "cache")
- if os.path.isdir(cacheDir):
- shutil.rmtree(cacheDir)
- metaDir = os.path.join(minimalDir, "meta")
- if os.path.isdir(metaDir):
- shutil.rmtree(metaDir)
- yield minimalDir
- if os.path.isdir(minimalDir):
- shutil.rmtree(minimalDir)
- return
+# @pytest.fixture(scope="function")
+# def nwMinimal(nwTemp):
+# """A minimal novelWriter example project.
+# """
+# testDir = os.path.dirname(__file__)
+# minimalStore = os.path.join(testDir, "minimal")
+# minimalDir = os.path.join(nwTemp, "minimal")
+# if os.path.isdir(minimalDir):
+# shutil.rmtree(minimalDir)
+# shutil.copytree(minimalStore, minimalDir)
+# cacheDir = os.path.join(minimalDir, "cache")
+# if os.path.isdir(cacheDir):
+# shutil.rmtree(cacheDir)
+# metaDir = os.path.join(minimalDir, "meta")
+# if os.path.isdir(metaDir):
+# shutil.rmtree(metaDir)
+# yield minimalDir
+# if os.path.isdir(minimalDir):
+# shutil.rmtree(minimalDir)
+# return
-@pytest.fixture(scope="function")
-def nwLipsum(nwTemp):
- """A medium sized novelWriter example project with a lot of Lorem
- Ipsum dummy text.
- """
- testDir = os.path.dirname(__file__)
- lipsumStore = os.path.join(testDir, "lipsum")
- lipsumDir = os.path.join(nwTemp, "lipsum")
- if os.path.isdir(lipsumDir):
- shutil.rmtree(lipsumDir)
- shutil.copytree(lipsumStore, lipsumDir)
- cacheDir = os.path.join(lipsumDir, "cache")
- if os.path.isdir(cacheDir):
- shutil.rmtree(cacheDir)
- metaDir = os.path.join(lipsumDir, "meta")
- if os.path.isdir(metaDir):
- shutil.rmtree(metaDir)
- yield lipsumDir
- if os.path.isdir(lipsumDir):
- shutil.rmtree(lipsumDir)
- return
+# @pytest.fixture(scope="function")
+# def nwLipsum(nwTemp):
+# """A medium sized novelWriter example project with a lot of Lorem
+# Ipsum dummy text.
+# """
+# testDir = os.path.dirname(__file__)
+# lipsumStore = os.path.join(testDir, "lipsum")
+# lipsumDir = os.path.join(nwTemp, "lipsum")
+# if os.path.isdir(lipsumDir):
+# shutil.rmtree(lipsumDir)
+# shutil.copytree(lipsumStore, lipsumDir)
+# cacheDir = os.path.join(lipsumDir, "cache")
+# if os.path.isdir(cacheDir):
+# shutil.rmtree(cacheDir)
+# metaDir = os.path.join(lipsumDir, "meta")
+# if os.path.isdir(metaDir):
+# shutil.rmtree(metaDir)
+# yield lipsumDir
+# if os.path.isdir(lipsumDir):
+# shutil.rmtree(lipsumDir)
+# return
-@pytest.fixture(scope="function")
-def nwOldProj(nwTemp):
- """A minimal movelWriter project using the old folder structure.
- """
- testDir = os.path.dirname(__file__)
- oldProjStore = os.path.join(testDir, "oldproj")
- oldProjDir = os.path.join(nwTemp, "oldproj")
- if os.path.isdir(oldProjDir):
- shutil.rmtree(oldProjDir)
- shutil.copytree(oldProjStore, oldProjDir)
- yield oldProjDir
- if os.path.isdir(oldProjDir):
- shutil.rmtree(oldProjDir)
- return
+# @pytest.fixture(scope="function")
+# def nwOldProj(nwTemp):
+# """A minimal movelWriter project using the old folder structure.
+# """
+# testDir = os.path.dirname(__file__)
+# oldProjStore = os.path.join(testDir, "oldproj")
+# oldProjDir = os.path.join(nwTemp, "oldproj")
+# if os.path.isdir(oldProjDir):
+# shutil.rmtree(oldProjDir)
+# shutil.copytree(oldProjStore, oldProjDir)
+# yield oldProjDir
+# if os.path.isdir(oldProjDir):
+# shutil.rmtree(oldProjDir)
+# return
##
# Monkey Patch Dialogs
##
-@pytest.fixture(scope="function")
-def yesToAll(monkeypatch):
- """Make the message boxes/questions always say yes.
- """
- monkeypatch.setattr(
- QMessageBox, "question", lambda *args, **kwargs: QMessageBox.Yes
- )
- monkeypatch.setattr(
- QMessageBox, "information", lambda *args, **kwargs: QMessageBox.Yes
- )
- monkeypatch.setattr(
- QMessageBox, "warning", lambda *args, **kwargs: QMessageBox.Yes
- )
- monkeypatch.setattr(
- QMessageBox, "critical", lambda *args, **kwargs: QMessageBox.Yes
- )
- return
+# @pytest.fixture(scope="function")
+# def yesToAll(monkeypatch):
+# """Make the message boxes/questions always say yes.
+# """
+# monkeypatch.setattr(
+# QMessageBox, "question", lambda *args, **kwargs: QMessageBox.Yes
+# )
+# monkeypatch.setattr(
+# QMessageBox, "information", lambda *args, **kwargs: QMessageBox.Yes
+# )
+# monkeypatch.setattr(
+# QMessageBox, "warning", lambda *args, **kwargs: QMessageBox.Yes
+# )
+# monkeypatch.setattr(
+# QMessageBox, "critical", lambda *args, **kwargs: QMessageBox.Yes
+# )
+# return
diff --git a/tests/test_item.py b/tests/test_core_item.py
similarity index 65%
rename from tests/test_item.py
rename to tests/test_core_item.py
index 95aa8470..a144b898 100644
--- a/tests/test_item.py
+++ b/tests/test_core_item.py
@@ -9,10 +9,11 @@ from lxml import etree
from nw.core.project import NWProject, NWItem
from nw.constants import nwItemClass, nwItemType, nwItemLayout
-@pytest.mark.project
-def testItemSettersSimple(nwDummy):
-
- theProject = NWProject(nwDummy)
+@pytest.mark.core
+def testCoreItemSetters(dummyGUI):
+ """Test all the simple setter classes for the NWItem class.
+ """
+ theProject = NWProject(dummyGUI)
theItem = NWItem(theProject)
# Name
@@ -20,12 +21,16 @@ def testItemSettersSimple(nwDummy):
assert theItem.itemName == "A Name"
theItem.setName("\t A Name ")
assert theItem.itemName == "A Name"
+ theItem.setName(123)
+ assert theItem.itemName == ""
# Handle
theItem.setHandle(123)
assert theItem.itemHandle is None
theItem.setHandle("0123456789abcdef")
assert theItem.itemHandle is None
+ theItem.setHandle("0123456789abg")
+ assert theItem.itemHandle is None
theItem.setHandle("0123456789abc")
assert theItem.itemHandle == "0123456789abc"
@@ -36,6 +41,8 @@ def testItemSettersSimple(nwDummy):
assert theItem.itemParent is None
theItem.setParent("0123456789abcdef")
assert theItem.itemParent is None
+ theItem.setParent("0123456789abg")
+ assert theItem.itemParent is None
theItem.setParent("0123456789abc")
assert theItem.itemParent == "0123456789abc"
@@ -59,6 +66,19 @@ def testItemSettersSimple(nwDummy):
theItem.setStatus("Main")
assert theItem.itemStatus == "Main"
+ # Importance
+ theItem.itemClass = nwItemClass.NOVEL
+ theItem.setStatus("Nonsense")
+ assert theItem.itemStatus == "New"
+ theItem.setStatus("New")
+ assert theItem.itemStatus == "New"
+ theItem.setStatus("Note")
+ assert theItem.itemStatus == "Note"
+ theItem.setStatus("Draft")
+ assert theItem.itemStatus == "Draft"
+ theItem.setStatus("Finished")
+ assert theItem.itemStatus == "Finished"
+
# Expanded
theItem.setExpanded(8)
assert not theItem.isExpanded
@@ -73,6 +93,20 @@ def testItemSettersSimple(nwDummy):
theItem.setExpanded(True)
assert theItem.isExpanded
+ # Exported
+ theItem.setExported(8)
+ assert not theItem.isExported
+ theItem.setExported(None)
+ assert not theItem.isExported
+ theItem.setExported("None")
+ assert not theItem.isExported
+ theItem.setExported("What?")
+ assert not theItem.isExported
+ theItem.setExported("True")
+ assert theItem.isExported
+ theItem.setExported(True)
+ assert theItem.isExported
+
# CharCount
theItem.setCharCount(None)
assert theItem.charCount == 0
@@ -105,10 +139,47 @@ def testItemSettersSimple(nwDummy):
theItem.setCursorPos(1)
assert theItem.cursorPos == 1
-@pytest.mark.project
-def testItemClassSetter(nwDummy):
+ # Initial Count
+ theItem.setWordCount(234)
+ theItem.saveInitialCount()
+ assert theItem.initCount == 234
- theProject = NWProject(nwDummy)
+# END Test testCoreItemSetters
+
+@pytest.mark.core
+def testCoreItemTypeSetter(dummyGUI):
+ """Test the setter for all the nwItemType values for the NWItem
+ class.
+ """
+ theProject = NWProject(dummyGUI)
+ theItem = NWItem(theProject)
+
+ # Type
+ theItem.setType(None)
+ assert theItem.itemType == nwItemType.NO_TYPE
+ theItem.setType("NONSENSE")
+ assert theItem.itemType == nwItemType.NO_TYPE
+ theItem.setType("NO_TYPE")
+ assert theItem.itemType == nwItemType.NO_TYPE
+ theItem.setType("ROOT")
+ assert theItem.itemType == nwItemType.ROOT
+ theItem.setType("FOLDER")
+ assert theItem.itemType == nwItemType.FOLDER
+ theItem.setType("FILE")
+ assert theItem.itemType == nwItemType.FILE
+ theItem.setType("TRASH")
+ assert theItem.itemType == nwItemType.TRASH
+ theItem.setType(nwItemType.ROOT)
+ assert theItem.itemType == nwItemType.ROOT
+
+# END Test testCoreItemTypeSetter
+
+@pytest.mark.core
+def testCoreItemClassSetter(dummyGUI):
+ """Test the setter for all the nwItemClass values for the NWItem
+ class.
+ """
+ theProject = NWProject(dummyGUI)
theItem = NWItem(theProject)
# Class
@@ -138,33 +209,17 @@ def testItemClassSetter(nwDummy):
assert theItem.itemClass == nwItemClass.ARCHIVE
theItem.setClass("TRASH")
assert theItem.itemClass == nwItemClass.TRASH
+ theItem.setClass(nwItemClass.NOVEL)
+ assert theItem.itemClass == nwItemClass.NOVEL
-@pytest.mark.project
-def testItemTypeSetter(nwDummy):
+# END Test testCoreItemClassSetter
- theProject = NWProject(nwDummy)
- theItem = NWItem(theProject)
-
- # Type
- theItem.setType(None)
- assert theItem.itemType == nwItemType.NO_TYPE
- theItem.setType("NONSENSE")
- assert theItem.itemType == nwItemType.NO_TYPE
- theItem.setType("NO_TYPE")
- assert theItem.itemType == nwItemType.NO_TYPE
- theItem.setType("ROOT")
- assert theItem.itemType == nwItemType.ROOT
- theItem.setType("FOLDER")
- assert theItem.itemType == nwItemType.FOLDER
- theItem.setType("FILE")
- assert theItem.itemType == nwItemType.FILE
- theItem.setType("TRASH")
- assert theItem.itemType == nwItemType.TRASH
-
-@pytest.mark.project
-def testItemLayoutSetter(nwDummy):
-
- theProject = NWProject(nwDummy)
+@pytest.mark.core
+def testCoreItemLayoutSetter(dummyGUI):
+ """Test the setter for all the nwItemLayout values for the NWItem
+ class.
+ """
+ theProject = NWProject(dummyGUI)
theItem = NWItem(theProject)
# Layout
@@ -190,14 +245,22 @@ def testItemLayoutSetter(nwDummy):
assert theItem.itemLayout == nwItemLayout.SCENE
theItem.setLayout("NOTE")
assert theItem.itemLayout == nwItemLayout.NOTE
+ theItem.setLayout(nwItemLayout.NOTE)
+ assert theItem.itemLayout == nwItemLayout.NOTE
-@pytest.mark.project
-def testItemXMLPackUnpack(nwDummy):
+# END Test testCoreItemLayoutSetter
- theProject = NWProject(nwDummy)
- theItem = NWItem(theProject)
+@pytest.mark.core
+def testCoreItemXMLPackUnpack(dummyGUI):
+ """Test packing and unpacking XML objects for the NWItem class.
+ """
+ theProject = NWProject(dummyGUI)
nwXML = etree.Element("novelWriterXML")
+ # File
+ # ====
+
+ theItem = NWItem(theProject)
theItem.setHandle("0123456789abc")
theItem.setParent("0123456789abc")
theItem.setOrder(1)
@@ -206,7 +269,7 @@ def testItemXMLPackUnpack(nwDummy):
theItem.setType("FILE")
theItem.setStatus("Main")
theItem.setLayout("NOTE")
- theItem.setExpanded(True)
+ theItem.setExported(False)
theItem.setParaCount(3)
theItem.setWordCount(5)
theItem.setCharCount(7)
@@ -219,17 +282,18 @@ def testItemXMLPackUnpack(nwDummy):
b""
b"- "
b"A NameFILENOVELNew"
- b"TrueNOTE7"
+ b"FalseNOTE7"
b"5311
"
b""
)
# Unpack
+ theItem = NWItem(theProject)
assert theItem.unpackXML(xContent[0])
assert theItem.itemHandle == "0123456789abc"
assert theItem.itemParent == "0123456789abc"
assert theItem.itemOrder == 1
- assert theItem.isExpanded
+ assert theItem.isExported is False
assert theItem.paraCount == 3
assert theItem.wordCount == 5
assert theItem.charCount == 7
@@ -238,6 +302,52 @@ def testItemXMLPackUnpack(nwDummy):
assert theItem.itemType == nwItemType.FILE
assert theItem.itemLayout == nwItemLayout.NOTE
+ # Folder
+ # ======
+
+ theItem = NWItem(theProject)
+ theItem.setHandle("0123456789abc")
+ theItem.setParent("0123456789abc")
+ theItem.setOrder(1)
+ theItem.setName("A Name")
+ theItem.setClass("NOVEL")
+ theItem.setType("FOLDER")
+ theItem.setStatus("Main")
+ theItem.setLayout("NOTE")
+ theItem.setExpanded(True)
+ theItem.setExported(False)
+ theItem.setParaCount(3)
+ theItem.setWordCount(5)
+ theItem.setCharCount(7)
+ theItem.setCursorPos(11)
+
+ # Pack
+ xContent = etree.SubElement(nwXML, "content")
+ theItem.packXML(xContent)
+ assert etree.tostring(xContent, pretty_print=False, encoding="utf-8") == (
+ b""
+ b"- "
+ b"A NameFOLDERNOVELNew"
+ b"True
"
+ b""
+ )
+
+ # Unpack
+ theItem = NWItem(theProject)
+ assert theItem.unpackXML(xContent[0])
+ assert theItem.itemHandle == "0123456789abc"
+ assert theItem.itemParent == "0123456789abc"
+ assert theItem.itemOrder == 1
+ assert theItem.isExpanded is True
+ assert theItem.isExported is True
+ assert theItem.paraCount == 0
+ assert theItem.wordCount == 0
+ assert theItem.charCount == 0
+ assert theItem.cursorPos == 0
+ assert theItem.itemClass == nwItemClass.NOVEL
+ assert theItem.itemType == nwItemType.FOLDER
+ assert theItem.itemLayout == nwItemLayout.NO_LAYOUT
+
# Errors
## Not an Item
@@ -268,3 +378,5 @@ def testItemXMLPackUnpack(nwDummy):
assert etree.tostring(xDummy, pretty_print=False, encoding="utf-8") == (
b""
)
+
+# END Test testCoreItemXMLPackUnpack