Пример #1
0
    def test_Parent_NoDoubleSet(self):
        parent = KeyedNode("parent")
        child = KeyedNode("child")
        child.set_parent(parent)

        imposter = KeyedNode("imposter")
        self.assertRaises(MultipleParentsNotSupported,
                          lambda: child.set_parent(imposter))

        self.assertEqual(child.get_parent(), parent)
Пример #2
0
    def test_RootNode_NoLoops(self):
        grandParent = KeyedNode("grand pa")
        parent = KeyedNode("pa")
        child = KeyedNode("me")
        child.set_parent(parent)
        parent.set_parent(grandParent)

        self.assertEqual(child.get_root_node(), grandParent)
        self.assertEqual(parent.get_root_node(), grandParent)
        self.assertEqual(grandParent.get_root_node(), grandParent)
Пример #3
0
    def test_Children_SingleChild(self):
        grandParent = KeyedNode("grand pa")
        parent = KeyedNode("pa")
        child = KeyedNode("me")
        child.set_parent(parent)
        parent.set_parent(grandParent)

        self.assertListEqual(child.get_child_nodes(), [])
        self.assertListEqual(parent.get_child_nodes(), [child])
        self.assertListEqual(grandParent.get_child_nodes(), [parent])
Пример #4
0
    def test_Depth_NoLoops(self):
        grandParent = KeyedNode("grand pa")
        parent = KeyedNode("pa")
        child = KeyedNode("me")
        child.set_parent(parent)
        parent.set_parent(grandParent)

        self.assertEqual(child.get_depth(), 2)
        self.assertEqual(parent.get_depth(), 1)
        self.assertEqual(grandParent.get_depth(), 0)
Пример #5
0
    def test_Ancestory_NoLoops(self):
        grandParent = KeyedNode("grand pa")
        parent = KeyedNode("pa")
        child = KeyedNode("me")
        child.set_parent(parent)
        parent.set_parent(grandParent)

        self.assertEqual(child.get_ancestry(), [parent, grandParent])
        self.assertEqual(parent.get_ancestry(), [grandParent])
        self.assertEqual(grandParent.get_ancestry(), [])
Пример #6
0
    def test_RootNode_LoopException(self):
        grandParent = KeyedNode("grand pa")
        parent = KeyedNode("pa")
        child = KeyedNode("me")
        child.set_parent(parent)
        parent.set_parent(grandParent)
        grandParent.set_parent(child)

        self.assertRaises(TreeHasLoop, lambda: child.get_root_node())
        self.assertRaises(TreeHasLoop, lambda: parent.get_root_node())
        self.assertRaises(TreeHasLoop, lambda: grandParent.get_root_node())
Пример #7
0
    def test_Children_MultipleChildren(self):
        grandParent = KeyedNode("grand pa")

        parent = KeyedNode("pa")
        aunt = KeyedNode("aunt")
        uncle = KeyedNode("uncle")

        me = KeyedNode("me")
        brother = KeyedNode("bro")
        sister = KeyedNode("si")

        me.set_parent(parent)
        brother.set_parent(parent)
        sister.set_parent(parent)

        parent.set_parent(grandParent)
        aunt.set_parent(grandParent)
        uncle.set_parent(grandParent)

        self.assertListEqual(me.get_child_nodes(), [])
        self.assertListEqual(parent.get_child_nodes(), [me, brother, sister])
        self.assertListEqual(grandParent.get_child_nodes(),
                             [parent, aunt, uncle])
Пример #8
0
    def test_Parent_NoSetSelf(self):
        child = KeyedNode("child")

        self.assertRaises(TreeHasLoop, lambda: child.set_parent(child))
Пример #9
0
    def test_Parent_CanSet(self):
        parent = KeyedNode("parent")
        child = KeyedNode("child")
        child.set_parent(parent)

        self.assertEqual(child.get_parent(), parent)
Пример #10
0
    def test_Traverse_MultipleChildren(self):
        calledBack = {}

        def callback(depth, node):
            if depth not in calledBack:
                calledBack[depth] = []
            calledBack[depth].append(node)

        grandParent = KeyedNode("grand pa")

        parent = KeyedNode("pa")
        aunt = KeyedNode("aunt")
        uncle = KeyedNode("uncle")

        me = KeyedNode("me")
        brother = KeyedNode("bro")
        sister = KeyedNode("si")

        cousin_aunt = KeyedNode("cousin (aunt)")
        cousin_aunt.set_parent(aunt)
        cousin_uncle = KeyedNode("cousin (uncle)")
        cousin_uncle.set_parent(uncle)

        me.set_parent(parent)
        brother.set_parent(parent)
        sister.set_parent(parent)

        parent.set_parent(grandParent)
        aunt.set_parent(grandParent)
        uncle.set_parent(grandParent)
        expected = {
            1: [parent, aunt, uncle],
            2: [me, brother, sister, cousin_aunt, cousin_uncle]
        }

        grandParent.traverse(callback)
        self.assertDictEqual(calledBack, expected)