예제 #1
0
 def test_cycle_indirectly(self):
     records = [
         Record(5, 2),
         Record(3, 2),
         Record(2, 6),
         Record(4, 1),
         Record(1, 0),
         Record(0, 0),
         Record(6, 3)
     ]
     # Cycle caused by Record 2 with parent_id(6) greater than record_id(2)
     with self.assertRaisesWithMessage(ValueError):
         build_tree(records)
예제 #2
0
 def test_cycle_directly(self):
     records = [
         Record(5, 2),
         Record(3, 2),
         Record(2, 2),
         Record(4, 1),
         Record(1, 0),
         Record(0, 0),
         Record(6, 3)
     ]
     # Cycle caused by Record 2 with parent_id pointing to itself
     with self.assertRaisesWithMessage(ValueError):
         build_tree(records)
예제 #3
0
    def test_three_nodes_in_reverse_order(self):
        records = [Record(2, 0), Record(1, 0), Record(0, 0)]
        root = build_tree(records)

        self.assert_node_is_branch(root, node_id=0, children_count=2)
        self.assert_node_is_leaf(root.children[0], node_id=1)
        self.assert_node_is_leaf(root.children[1], node_id=2)
예제 #4
0
    def test_more_than_two_children(self):
        records = [Record(0, 0), Record(1, 0), Record(2, 0), Record(3, 0)]
        root = build_tree(records)

        self.assert_node_is_branch(root, node_id=0, children_count=3)
        self.assert_node_is_leaf(root.children[0], node_id=1)
        self.assert_node_is_leaf(root.children[1], node_id=2)
        self.assert_node_is_leaf(root.children[2], node_id=3)
예제 #5
0
    def test_unbalanced_tree(self):
        records = [
            Record(0, 0),
            Record(1, 0),
            Record(2, 0),
            Record(3, 1),
            Record(4, 1),
            Record(5, 1),
            Record(6, 2),
        ]
        root = build_tree(records)

        self.assert_node_is_branch(root, 0, 2)
        self.assert_node_is_branch(root.children[0], 1, 3)
        self.assert_node_is_branch(root.children[1], 2, 1)
        self.assert_node_is_leaf(root.children[0].children[0], 3)
        self.assert_node_is_leaf(root.children[0].children[1], 4)
        self.assert_node_is_leaf(root.children[0].children[2], 5)
        self.assert_node_is_leaf(root.children[1].children[0], 6)
예제 #6
0
 def test_no_root_node(self):
     records = [Record(1, 0), Record(2, 0)]
     # Record with record_id 0 (root) is missing
     with self.assertRaisesWithMessage(ValueError):
         build_tree(records)
예제 #7
0
 def test_root_node_has_parent(self):
     records = [Record(0, 1), Record(1, 0)]
     # Root parent_id should be equal to record_id(0)
     with self.assertRaisesWithMessage(ValueError):
         build_tree(records)
예제 #8
0
    def test_one_node(self):
        records = [Record(0, 0)]
        root = build_tree(records)

        self.assert_node_is_leaf(root, node_id=0)
예제 #9
0
 def test_higher_id_parent_of_lower_id(self):
     records = [Record(0, 0), Record(2, 0), Record(1, 2)]
     # Record 1 have parent_id(2) greater than record_id(1)
     with self.assertRaisesWithMessage(ValueError):
         build_tree(records)
예제 #10
0
 def test_empty_list_input(self):
     records = []
     root = build_tree(records)
     self.assertIsNone(root)
예제 #11
0
 def test_non_continuous(self):
     records = [Record(2, 0), Record(4, 2), Record(1, 0), Record(0, 0)]
     # Record with record_id 3 is missing
     with self.assertRaisesWithMessage(ValueError):
         build_tree(records)