emmental/curds/test_tree.py
Anna Schumaker 53a1e085d4 curds: Move child ID lookups into the ETree
Using the new system keeps us from needing to maintain an external
dictionary of nodes.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
2019-12-13 09:06:04 -05:00

251 lines
8.8 KiB
Python

# Copyright 2019 (c) Anna Schumaker.
from . import notify
from . import tree
import threading
import unittest
class TestETree(unittest.TestCase):
def setUp(self):
notify.register("child-inserted", self.on_child_inserted)
notify.register("first-child", self.on_first_child)
def tearDown(self):
notify.cancel("child-inserted", self.on_child_inserted)
notify.cancel("first-child", self.on_first_child)
def on_child_inserted(self, child, path):
self.cb_child = child
self.cb_path = path
def on_first_child(self, parent, path):
self.cb_first = parent
self.cb_fpath = path
def test_etree_init(self):
etree = tree.ETree("Test Tree", "test-icon")
self.assertEqual(etree.name, "Test Tree")
self.assertEqual(etree.icon, "test-icon")
self.assertEqual( etree.children, [ ])
self.assertEqual( etree.child_ids, { })
self.assertFalse( etree.tree_lock.locked())
self.assertIsNone(etree.parent)
self.assertIsNone(etree.sibling)
def test_etree_init_empty(self):
etree = tree.ETree()
self.assertEqual(etree.icon, "")
self.assertEqual(etree.name, "")
def test_etree_getstate(self):
etree = tree.ETree("Test Tree", "test-icon")
state = etree.__getstate__()
self.assertEqual(state["icon"], "test-icon")
self.assertEqual(state["name"], "Test Tree")
self.assertEqual(state["children"], [ ])
self.assertEqual(state["parent"], None)
self.assertEqual(state["sibling"], None)
self.assertNotIn("child_ids", state)
self.assertNotIn("tree_lock", state)
def test_etree_setstate(self):
etree = tree.ETree()
ctree = tree.ETree()
state = { "icon" : "test-icon", "name" : "Test Tree",
"children" : [ ctree ], "parent" : 2, "sibling" : 3 }
etree.tree_lock = None
ctree.child_ids = { id(1) : 1 }
etree.__setstate__(state)
self.assertEqual(etree.icon, "test-icon")
self.assertEqual(etree.name, "Test Tree")
self.assertEqual(etree.children, [ ctree ])
self.assertEqual(etree.parent, 2)
self.assertEqual(etree.sibling, 3)
self.assertEqual(etree.child_ids, { id(1) : 1, id(ctree) : ctree })
self.assertFalse(etree.tree_lock.locked())
def test_etree_child_index(self):
root = tree.ETree()
a = root.find_child("A", "icon")
b = root.find_child("B", "icon")
c = a.find_child("C", "icon")
self.assertEqual( root.child_index(a), 0)
self.assertEqual( root.child_index(b), 1)
self.assertIsNone(root.child_index(c))
def test_etree_find_child(self):
root = tree.ETree()
b = root.find_child("B", "icon")
self.assertEqual(root.children, [ b ])
self.assertEqual(b.parent, root)
d = root.find_child("D", "icon")
self.assertEqual(root.children, [ b, d ])
self.assertEqual(b.sibling, d)
c = root.find_child("C", "icon")
self.assertEqual(root.children, [ b, c, d ])
self.assertEqual(b.sibling, c)
self.assertEqual(c.sibling, d)
a = root.find_child("A", "icon")
self.assertEqual(root.children, [ a, b, c, d ])
self.assertEqual(a.sibling, b)
self.assertEqual(root.find_child("A", "icon"), a)
self.assertEqual(root.find_child("B", "icon"), b)
self.assertEqual(root.find_child("C", "icon"), c)
self.assertEqual(root.find_child("D", "icon"), d)
self.assertEqual(root.children, [ a, b, c, d ])
self.assertEqual(root.child_ids, { id(a) : a, id(b) : b, id(c) : c, id(d) : d })
def test_etree_get_markup(self):
etree = tree.ETree("Test Tree", "test-icon")
self.assertEqual(etree.get_markup(), "<big>Test Tree</big>")
def test_etree_get_path(self):
root = tree.ETree()
a = root.find_child("A", "icon")
b = a.find_child("B", "icon")
c = b.find_child("C", "icon")
self.assertEqual(root.get_path(), [ ])
self.assertEqual(a.get_path(), [ 0 ])
self.assertEqual(b.get_path(), [ 0, 0 ])
self.assertEqual(c.get_path(), [ 0, 0, 0 ])
def test_etree_insert_child(self):
root = tree.ETree()
a = root.insert_child(tree.ETree("A", "icon"))
self.assertEqual(root.children, [ a ])
self.assertEqual(a.parent, root)
self.assertEqual(self.cb_child, a)
self.assertEqual(self.cb_path, [ 0 ])
self.assertEqual(self.cb_first, root)
self.assertEqual(self.cb_fpath, [ ])
self.cb_first = None
c = root.insert_child(tree.ETree("C", "icon"))
self.assertEqual(root.children, [ a, c ])
self.assertEqual(a.sibling, c)
self.assertEqual(self.cb_child, c)
self.assertEqual(self.cb_path, [ 1 ])
self.assertIsNone(self.cb_first)
b = root.insert_child(tree.ETree("B", "icon"))
self.assertEqual(root.children, [ a, b, c ])
self.assertEqual(a.sibling, b)
self.assertEqual(b.sibling, c)
self.assertEqual(self.cb_child, b)
self.assertEqual(self.cb_path, [ 1 ])
b = root.insert_child(b)
self.assertEqual(root.children, [ a, b, c ])
def test_etree_lookup(self):
root = tree.ETree()
a = root.insert_child(tree.ETree("A", "icon"))
b = root.insert_child(tree.ETree("B", "icon"))
d = root.insert_child(tree.ETree("D", "icon"))
self.assertEqual( root.lookup("A"), a)
self.assertEqual( root.lookup("B"), b)
self.assertIsNone(root.lookup("C"))
self.assertEqual( root.lookup("D"), d)
def test_etree_lookup_byid(self):
root = tree.ETree()
a = root.insert_child(tree.ETree("A", "icon"))
b = a.insert_child(tree.ETree("B", "icon"))
c = b.insert_child(tree.ETree("C", "icon"))
self.assertEqual(root.lookup_byid(id(a)), a)
self.assertEqual(root.lookup_byid(id(b)), b)
self.assertEqual(root.lookup_byid(id(c)), c)
self.assertEqual( a.lookup_byid(id(b)), b)
self.assertEqual( a.lookup_byid(id(c)), c)
self.assertEqual( b.lookup_byid(id(c)), c)
self.assertIsNone(c.lookup_byid(id(a)))
def test_etree_lookup_path(self):
root = tree.ETree()
a = root.find_child("A", "icon")
b = a.find_child("B", "icon")
c = b.find_child("C", "icon")
self.assertEqual( root.lookup_path([ ]), root)
self.assertEqual( root.lookup_path([ 0 ]), a)
self.assertIsNone(root.lookup_path([ 1 ]))
self.assertEqual( root.lookup_path([ 0, 0 ]), b)
self.assertIsNone(root.lookup_path([ 0, 1 ]))
self.assertIsNone(root.lookup_path([ 1, 0 ]))
self.assertEqual( root.lookup_path([ 0, 0, 0 ]), c)
self.assertIsNone(root.lookup_path([ 0, 0, 1 ]))
self.assertIsNone(root.lookup_path([ 0, 1, 0 ]))
self.assertIsNone(root.lookup_path([ 1, 0, 0 ]))
def test_etree_n_children(self):
root = tree.ETree()
self.assertEqual(root.n_children(), 0)
a = root.find_child("A", "icon")
self.assertEqual(root.n_children(), 1)
b = root.find_child("B", "icon")
self.assertEqual(root.n_children(), 2)
def test_etree_next_child(self):
root = tree.ETree()
a = root.find_child("A", "icon")
b = root.find_child("B", "icon")
self.assertIsNone(root.next_child())
self.assertEqual( a.next_child(), b)
self.assertIsNone(b.next_child())
def test_etree_nth_child(self):
root = tree.ETree()
a = root.find_child("A", "icon")
b = root.find_child("B", "icon")
self.assertIsNone(root.nth_child(-1))
self.assertEqual( root.nth_child(0), a)
self.assertEqual( root.nth_child(1), b)
self.assertIsNone(root.nth_child(2))
def test_etree_reset(self):
root = tree.ETree()
a = root.find_child("A", "icon")
b = root.find_child("B", "icon")
root.reset()
self.assertEqual(root.children, [ ])
def test_etree_sort_key(self):
etree = tree.ETree("Test Tree")
self.assertEqual(etree.sort_key(), [ "test", "tree" ])
etree.name = "Test - Tree!"
self.assertEqual(etree.sort_key(), [ "test", "tree" ])
self.assertEqual(etree.sort_key("Emmental Tree"), [ "test", "tree" ])
def test_etree_walk(self):
root = tree.ETree()
a = root.find_child("A", "icon")
b = a.find_child("B", "icon")
c = a.find_child("C", "icon")
d = b.find_child("D", "icon")
vals = [ a, b, d, c ]
for i, n in enumerate(root.walk()):
self.assertEqual(n, vals[i])
self.assertEqual(i, 3)
for i, n in enumerate(a.walk()):
self.assertEqual(n, vals[i])
self.assertEqual(i, 3)