Tighten up the item class (#937)

* Protect the attributes of the NWItem class
* Fix test coverage
* Add a type checker function to the tree class
This commit is contained in:
Veronica Berglyd Olsen
2021-12-31 16:25:20 +01:00
committed by GitHub
parent af3d0d2eb0
commit 6ac9e1aec4
14 changed files with 255 additions and 183 deletions
+2 -2
View File
@@ -433,7 +433,7 @@ def testCoreIndex_ScanText(nwMinimal, mockGUI):
# Page wo/Title
# =============
theProject.projTree[pHandle].itemLayout = nwItemLayout.DOCUMENT
theProject.projTree[pHandle]._layout = nwItemLayout.DOCUMENT
assert theIndex.scanText(pHandle, (
"This is a page with some text on it.\n\n"
))
@@ -446,7 +446,7 @@ def testCoreIndex_ScanText(nwMinimal, mockGUI):
assert theIndex._fileIndex[pHandle]["T000000"]["pCount"] == 1
assert theIndex._fileIndex[pHandle]["T000000"]["synopsis"] == ""
theProject.projTree[pHandle].itemLayout = nwItemLayout.NOTE
theProject.projTree[pHandle]._layout = nwItemLayout.NOTE
assert theIndex.scanText(pHandle, (
"This is a page with some text on it.\n\n"
))
+28 -13
View File
@@ -86,7 +86,7 @@ def testCoreItem_Setters(mockGUI):
assert theItem.itemStatus == "Main"
# Status
theItem.itemClass = nwItemClass.NOVEL
theItem._class = nwItemClass.NOVEL
theItem.setStatus("Nonsense")
assert theItem.itemStatus == "New"
theItem.setStatus("New")
@@ -100,31 +100,31 @@ def testCoreItem_Setters(mockGUI):
# Expanded
theItem.setExpanded(8)
assert not theItem.isExpanded
assert theItem.isExpanded is False
theItem.setExpanded(None)
assert not theItem.isExpanded
assert theItem.isExpanded is False
theItem.setExpanded("None")
assert not theItem.isExpanded
assert theItem.isExpanded is False
theItem.setExpanded("What?")
assert not theItem.isExpanded
assert theItem.isExpanded is False
theItem.setExpanded("True")
assert theItem.isExpanded
assert theItem.isExpanded is True
theItem.setExpanded(True)
assert theItem.isExpanded
assert theItem.isExpanded is True
# Exported
theItem.setExported(8)
assert not theItem.isExported
assert theItem.isExported is False
theItem.setExported(None)
assert not theItem.isExported
assert theItem.isExported is False
theItem.setExported("None")
assert not theItem.isExported
assert theItem.isExported is False
theItem.setExported("What?")
assert not theItem.isExported
assert theItem.isExported is False
theItem.setExported("True")
assert theItem.isExported
assert theItem.isExported is True
theItem.setExported(True)
assert theItem.isExported
assert theItem.isExported is True
# CharCount
theItem.setCharCount(None)
@@ -196,6 +196,21 @@ def testCoreItem_Methods(mockGUI):
theItem.setLayout("NOTE")
assert theItem.describeMe() == "Project Note"
# Representation
# ==============
theItem.setName("New Item")
theItem.setHandle("1234567890abc")
theItem.setParent("4567890abcdef")
assert repr(theItem) == "<NWItem handle=1234567890abc, parent=4567890abcdef, name='New Item'>"
# Truthiness
# ==========
assert bool(theItem) is True
theItem.setHandle(None)
assert bool(theItem) is False
# END Test testCoreItem_Methods
+56 -51
View File
@@ -39,61 +39,61 @@ def mockItems(mockGUI):
theProject = NWProject(mockGUI)
itemA = NWItem(theProject)
itemA.itemName = "Novel"
itemA.itemType = nwItemType.ROOT
itemA.itemClass = nwItemClass.NOVEL
itemA.isExpanded = True
itemA._name = "Novel"
itemA._type = nwItemType.ROOT
itemA._class = nwItemClass.NOVEL
itemA._expanded = True
itemB = NWItem(theProject)
itemB.itemName = "Act One"
itemB.itemType = nwItemType.FOLDER
itemB.itemClass = nwItemClass.NOVEL
itemB.isExpanded = True
itemB._name = "Act One"
itemB._type = nwItemType.FOLDER
itemB._class = nwItemClass.NOVEL
itemB._expanded = True
itemC = NWItem(theProject)
itemC.itemName = "Chapter One"
itemC.itemType = nwItemType.FILE
itemC.itemClass = nwItemClass.NOVEL
itemC.itemLayout = nwItemLayout.DOCUMENT
itemC.charCount = 300
itemC.wordCount = 50
itemC.paraCount = 2
itemC._name = "Chapter One"
itemC._type = nwItemType.FILE
itemC._class = nwItemClass.NOVEL
itemC._layout = nwItemLayout.DOCUMENT
itemC._charCount = 300
itemC._wordCount = 50
itemC._paraCount = 2
itemD = NWItem(theProject)
itemD.itemName = "Scene One"
itemD.itemType = nwItemType.FILE
itemD.itemClass = nwItemClass.NOVEL
itemD.itemLayout = nwItemLayout.DOCUMENT
itemD.charCount = 3000
itemD.wordCount = 500
itemD.paraCount = 20
itemD._name = "Scene One"
itemD._type = nwItemType.FILE
itemD._class = nwItemClass.NOVEL
itemD._layout = nwItemLayout.DOCUMENT
itemD._charCount = 3000
itemD._wordCount = 500
itemD._paraCount = 20
itemE = NWItem(theProject)
itemE.itemName = "Outtakes"
itemE.itemType = nwItemType.ROOT
itemE.itemClass = nwItemClass.ARCHIVE
itemE.isExpanded = False
itemE._name = "Outtakes"
itemE._type = nwItemType.ROOT
itemE._class = nwItemClass.ARCHIVE
itemE._expanded = False
itemF = NWItem(theProject)
itemF.itemName = "Trash"
itemF.itemType = nwItemType.TRASH
itemF.itemClass = nwItemClass.TRASH
itemF.isExpanded = False
itemF._name = "Trash"
itemF._type = nwItemType.TRASH
itemF._class = nwItemClass.TRASH
itemF._expanded = False
itemG = NWItem(theProject)
itemG.itemName = "Characters"
itemG.itemType = nwItemType.ROOT
itemG.itemClass = nwItemClass.CHARACTER
itemG.isExpanded = True
itemG._name = "Characters"
itemG._type = nwItemType.ROOT
itemG._class = nwItemClass.CHARACTER
itemG._expanded = True
itemH = NWItem(theProject)
itemH.itemName = "Jane Doe"
itemH.itemType = nwItemType.FILE
itemH.itemClass = nwItemClass.CHARACTER
itemH.itemLayout = nwItemLayout.NOTE
itemH.charCount = 2000
itemH.wordCount = 400
itemH.paraCount = 16
itemH._name = "Jane Doe"
itemH._type = nwItemType.FILE
itemH._class = nwItemClass.CHARACTER
itemH._layout = nwItemLayout.NOTE
itemH._charCount = 2000
itemH._wordCount = 400
itemH._paraCount = 16
theItems = [
("a000000000001", None, itemA),
@@ -154,20 +154,20 @@ def testCoreTree_BuildTree(mockGUI, mockItems):
# Try to add another trash folder
itemT = NWItem(theProject)
itemT.itemName = "Trash"
itemT.itemType = nwItemType.TRASH
itemT.itemClass = nwItemClass.TRASH
itemT.isExpanded = False
itemT._name = "Trash"
itemT._type = nwItemType.TRASH
itemT._class = nwItemClass.TRASH
itemT._expanded = False
assert not theTree.append("1234567890abc", None, itemT)
assert len(theTree) == len(mockItems)
# Generate handle automatically
itemT = NWItem(theProject)
itemT.itemName = "New File"
itemT.itemType = nwItemType.FILE
itemT.itemClass = nwItemClass.NOVEL
itemT.itemLayout = nwItemLayout.DOCUMENT
itemT._name = "New File"
itemT._type = nwItemType.FILE
itemT._class = nwItemClass.NOVEL
itemT._layout = nwItemLayout.DOCUMENT
assert theTree.append(None, None, itemT)
assert len(theTree) == len(mockItems) + 1
@@ -218,6 +218,11 @@ def testCoreTree_Methods(mockGUI, mockItems):
assert len(theTree) == len(mockItems)
# Chech type
assert theTree.checkType("blabla", nwItemType.FILE) is False
assert theTree.checkType("b000000000001", nwItemType.FILE) is False
assert theTree.checkType("c000000000001", nwItemType.FILE) is True
# Root item lookup
theTree._treeRoots.append("stuff")
assert theTree.findRoot(nwItemClass.WORLD) is None
@@ -243,12 +248,12 @@ def testCoreTree_Methods(mockGUI, mockItems):
]
# Break the folder parent handle
theTree["b000000000001"].itemParent = "stuff"
theTree["b000000000001"]._parent = "stuff"
assert theTree.getItemPath("c000000000001") == [
"c000000000001", "b000000000001"
]
theTree["b000000000001"].itemParent = "a000000000001"
theTree["b000000000001"]._parent = "a000000000001"
assert theTree.getItemPath("c000000000001") == [
"c000000000001", "b000000000001", "a000000000001"
]
+2 -2
View File
@@ -62,9 +62,9 @@ def testDlgItemEditor_Dialog(qtbot, monkeypatch, nwGUI, fncProj):
assert nwGUI.editItem() is False
# Invalid Type
nwGUI.theProject.projTree["0e17daca5f3e1"].itemType = nwItemType.NO_TYPE
nwGUI.theProject.projTree["0e17daca5f3e1"]._type = nwItemType.NO_TYPE
assert nwGUI.editItem() is False
nwGUI.theProject.projTree["0e17daca5f3e1"].itemType = nwItemType.FILE
nwGUI.theProject.projTree["0e17daca5f3e1"]._type = nwItemType.FILE
# Open Properly
assert nwGUI.editItem() is True
+5 -5
View File
@@ -1225,8 +1225,8 @@ def testGuiEditor_WordCounters(qtbot, monkeypatch, caplog, nwGUI, nwMinimal, ips
# Open a document and populate it
sHandle = "8c659a11cd429"
nwGUI.theProject.projTree[sHandle].initCount = 0 # Clear item's count
nwGUI.theProject.projTree[sHandle].wordCount = 0 # Clear item's count
nwGUI.theProject.projTree[sHandle]._initCount = 0 # Clear item's count
nwGUI.theProject.projTree[sHandle]._wordCount = 0 # Clear item's count
assert nwGUI.openDocument(sHandle) is True
qtbot.wait(stepDelay)
@@ -1252,9 +1252,9 @@ def testGuiEditor_WordCounters(qtbot, monkeypatch, caplog, nwGUI, nwMinimal, ips
nwGUI.docEditor.wCounterDoc.run()
# nwGUI.docEditor._updateDocCounts(cC, wC, pC)
qtbot.wait(stepDelay)
assert nwGUI.theProject.projTree[sHandle].charCount == cC
assert nwGUI.theProject.projTree[sHandle].wordCount == wC
assert nwGUI.theProject.projTree[sHandle].paraCount == pC
assert nwGUI.theProject.projTree[sHandle]._charCount == cC
assert nwGUI.theProject.projTree[sHandle]._wordCount == wC
assert nwGUI.theProject.projTree[sHandle]._paraCount == pC
assert nwGUI.docEditor.docFooter.wordsText.text() == f"Words: {wC} (+{wC})"
# Select all text
+1 -1
View File
@@ -223,7 +223,7 @@ def testGuiProjTree_TreeItems(qtbot, caplog, monkeypatch, nwGUI, nwMinimal):
# Add new file after one that has no parent handle
chItem = nwTree._getTreeItem("44cb730c42048")
nwTree.setCurrentItem(chItem, QItemSelectionModel.Current)
nwTree.theProject.projTree["44cb730c42048"].itemParent = None
nwTree.theProject.projTree["44cb730c42048"]._parent = None
assert not nwTree.newTreeItem(nwItemType.FILE, nwItemClass.NOVEL)
nwTree.clearSelection()