Improve the setOrder method in the NWTree class, and improve tests
This commit is contained in:
@@ -331,20 +331,15 @@ class NWTree:
|
|||||||
def setOrder(self, newOrder):
|
def setOrder(self, newOrder):
|
||||||
"""Reorders the tree based on a list of items.
|
"""Reorders the tree based on a list of items.
|
||||||
"""
|
"""
|
||||||
tmpOrder = []
|
tmpOrder = [tHandle for tHandle in newOrder if tHandle in self._projTree]
|
||||||
|
if not (len(tmpOrder) == len(newOrder) == len(self._treeOrder)):
|
||||||
# Add all known elements to a new temp list
|
# Something is wrong, so let's debug it
|
||||||
for tHandle in newOrder:
|
for tHandle in newOrder:
|
||||||
if tHandle in self._projTree:
|
if tHandle not in self._projTree:
|
||||||
tmpOrder.append(tHandle)
|
logger.error("Handle '%s' in new tree order is not in old order", tHandle)
|
||||||
else:
|
for tHandle in self._treeOrder:
|
||||||
logger.error("Handle '%s' in new tree order is not in project tree", tHandle)
|
if tHandle not in tmpOrder:
|
||||||
|
logger.warning("Handle '%s' in old tree order is not in new order", tHandle)
|
||||||
# Do a reverse lookup to check for items that will be lost
|
|
||||||
# This is mainly for debugging purposes
|
|
||||||
for tHandle in self._treeOrder:
|
|
||||||
if tHandle not in tmpOrder:
|
|
||||||
logger.warning("Handle '%s' in old tree order is not in new tree order", tHandle)
|
|
||||||
|
|
||||||
# Save the temp list
|
# Save the temp list
|
||||||
self._treeOrder = tmpOrder
|
self._treeOrder = tmpOrder
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ import random
|
|||||||
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
from mock import causeOSError
|
||||||
from tools import readFile
|
from tools import readFile
|
||||||
|
|
||||||
from novelwriter.enum import nwItemClass, nwItemType, nwItemLayout
|
from novelwriter.enum import nwItemClass, nwItemType, nwItemLayout
|
||||||
@@ -230,6 +231,36 @@ def testCoreTree_BuildTree(mockGUI, mockItems):
|
|||||||
# END Test testCoreTree_BuildTree
|
# END Test testCoreTree_BuildTree
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.core
|
||||||
|
def testCoreTree_PackUnpack(mockGUI, mockItems):
|
||||||
|
"""Test packing and unpacking data.
|
||||||
|
"""
|
||||||
|
theProject = NWProject(mockGUI)
|
||||||
|
theTree = NWTree(theProject)
|
||||||
|
|
||||||
|
aHandles = []
|
||||||
|
for tHandle, pHandle, nwItem in mockItems:
|
||||||
|
aHandles.append(tHandle)
|
||||||
|
theTree.append(tHandle, pHandle, nwItem)
|
||||||
|
theTree.updateItemData(tHandle)
|
||||||
|
|
||||||
|
assert len(theTree) == len(mockItems)
|
||||||
|
|
||||||
|
# Pack
|
||||||
|
tree = theTree.pack()
|
||||||
|
for i, (tHandle, pHandle, nwItem) in enumerate(mockItems):
|
||||||
|
assert tree[i]["itemAttr"]["handle"] == tHandle
|
||||||
|
|
||||||
|
# Unpack
|
||||||
|
theTree.clear()
|
||||||
|
assert len(theTree) == 0
|
||||||
|
assert theTree.handles() == []
|
||||||
|
assert theTree.unpack(tree) is True
|
||||||
|
assert theTree.handles() == aHandles
|
||||||
|
|
||||||
|
# END Test testCoreTree_PackUnpack
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.core
|
@pytest.mark.core
|
||||||
def testCoreTree_Methods(mockGUI, mockItems):
|
def testCoreTree_Methods(mockGUI, mockItems):
|
||||||
"""Test various class methods.
|
"""Test various class methods.
|
||||||
@@ -272,6 +303,13 @@ def testCoreTree_Methods(mockGUI, mockItems):
|
|||||||
assert theTree.findRoot(nwItemClass.NOVEL) == "a000000000001"
|
assert theTree.findRoot(nwItemClass.NOVEL) == "a000000000001"
|
||||||
assert theTree.findRoot(nwItemClass.CHARACTER) == "a000000000004"
|
assert theTree.findRoot(nwItemClass.CHARACTER) == "a000000000004"
|
||||||
|
|
||||||
|
# Iter roots
|
||||||
|
roots = list(theTree.iterRoots(None))
|
||||||
|
assert roots[0][0] == "a000000000001"
|
||||||
|
assert roots[1][0] == "a000000000002"
|
||||||
|
assert roots[2][0] == "a000000000003"
|
||||||
|
assert roots[3][0] == "a000000000004"
|
||||||
|
|
||||||
# Add a fake item to root and check that it can handle it
|
# Add a fake item to root and check that it can handle it
|
||||||
theTree._treeRoots["0000000000000"] = NWItem(theProject)
|
theTree._treeRoots["0000000000000"] = NWItem(theProject)
|
||||||
assert theTree.findRoot(nwItemClass.WORLD) is None
|
assert theTree.findRoot(nwItemClass.WORLD) is None
|
||||||
@@ -363,7 +401,7 @@ def testCoreTree_Stats(mockGUI, mockItems):
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.core
|
@pytest.mark.core
|
||||||
def testCoreTree_Reorder(mockGUI, mockItems):
|
def testCoreTree_Reorder(caplog, mockGUI, mockItems):
|
||||||
"""Test changing tree order.
|
"""Test changing tree order.
|
||||||
"""
|
"""
|
||||||
theProject = NWProject(mockGUI)
|
theProject = NWProject(mockGUI)
|
||||||
@@ -384,12 +422,16 @@ def testCoreTree_Reorder(mockGUI, mockItems):
|
|||||||
theTree.setOrder(bHandle)
|
theTree.setOrder(bHandle)
|
||||||
assert theTree.handles() == bHandle
|
assert theTree.handles() == bHandle
|
||||||
|
|
||||||
|
caplog.clear()
|
||||||
theTree.setOrder(bHandle + ["stuff"])
|
theTree.setOrder(bHandle + ["stuff"])
|
||||||
assert theTree.handles() == bHandle
|
assert theTree.handles() == bHandle
|
||||||
|
assert "Handle 'stuff' in new tree order is not in old order" in caplog.text
|
||||||
|
|
||||||
|
caplog.clear()
|
||||||
theTree._treeOrder.append("stuff")
|
theTree._treeOrder.append("stuff")
|
||||||
theTree.setOrder(bHandle)
|
theTree.setOrder(bHandle)
|
||||||
assert theTree.handles() == bHandle
|
assert theTree.handles() == bHandle
|
||||||
|
assert "Handle 'stuff' in old tree order is not in new order" in caplog.text
|
||||||
|
|
||||||
# END Test testCoreTree_Reorder
|
# END Test testCoreTree_Reorder
|
||||||
|
|
||||||
@@ -421,6 +463,11 @@ def testCoreTree_ToCFile(monkeypatch, mockGUI, mockItems, tmpPath):
|
|||||||
theProject._storage._runtimePath = None
|
theProject._storage._runtimePath = None
|
||||||
assert theTree.writeToCFile() is False
|
assert theTree.writeToCFile() is False
|
||||||
|
|
||||||
|
theProject._storage._runtimePath = tmpPath
|
||||||
|
with monkeypatch.context() as mp:
|
||||||
|
mp.setattr("builtins.open", causeOSError)
|
||||||
|
assert theTree.writeToCFile() is False
|
||||||
|
|
||||||
theProject._storage._runtimePath = tmpPath
|
theProject._storage._runtimePath = tmpPath
|
||||||
assert theTree.writeToCFile() is True
|
assert theTree.writeToCFile() is True
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user