New test for NWItem class

This commit is contained in:
Veronica K. B. Olsen
2020-11-30 22:28:01 +01:00
parent 78c738c5ab
commit 6a1dc8b5ae
3 changed files with 300 additions and 175 deletions
+9 -5
View File
@@ -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):
+140 -131
View File
@@ -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
+151 -39
View File
@@ -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"<content>"
b"<item handle=\"0123456789abc\" order=\"1\" parent=\"0123456789abc\">"
b"<name>A Name</name><type>FILE</type><class>NOVEL</class><status>New</status>"
b"<exported>True</exported><layout>NOTE</layout><charCount>7</charCount>"
b"<exported>False</exported><layout>NOTE</layout><charCount>7</charCount>"
b"<wordCount>5</wordCount><paraCount>3</paraCount><cursorPos>11</cursorPos></item>"
b"</content>"
)
# 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"<content>"
b"<item handle=\"0123456789abc\" order=\"1\" parent=\"0123456789abc\">"
b"<name>A Name</name><type>FOLDER</type><class>NOVEL</class><status>New</status>"
b"<expanded>True</expanded></item>"
b"</content>"
)
# 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"<group/>"
)
# END Test testCoreItemXMLPackUnpack