import unittest

from dbis_btree.BBaum import BplusTree


class TestBPlusTree(unittest.TestCase):
    def test_graph_generation(self):
        btree = BplusTree(2)
        btree.add_node("A", [3, 5])
        btree.add_node("B", [1, 2])
        btree.add_node("C", [3, 4])
        btree.add_node("D", [5, 6])
        btree.add_edge("A", "B", 1)
        btree.add_edge("A", "C", 2)
        btree.add_edge("A", "D", 3)
        self.assertEqual(
            """// dot
digraph btree {
	graph [label="M = 2" nodesep=0.5 splines=line]
	node [fillcolor="#FFFFFF" fontsize=10 height=.05 shape=none style=filled]
	A [label=<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0"><TR> <TD PORT="f0"> </TD> <TD>3</TD> <TD PORT="f1"> </TD> <TD>5</TD> <TD PORT="f2"> </TD> </TR></TABLE>>]
	A:f0 -> B
	A:f1 -> C
	A:f2 -> D
	B [label=<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0"><TR> <TD PORT="f0"> </TD> <TD>1</TD> <TD PORT="f1"> </TD> <TD>2</TD> <TD PORT="f2"> </TD> </TR></TABLE>>]
	C [label=<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0"><TR> <TD PORT="f0"> </TD> <TD>3</TD> <TD PORT="f1"> </TD> <TD>4</TD> <TD PORT="f2"> </TD> </TR></TABLE>>]
	D [label=<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0"><TR> <TD PORT="f0"> </TD> <TD>5</TD> <TD PORT="f1"> </TD> <TD>6</TD> <TD PORT="f2"> </TD> </TR></TABLE>>]
	B:f2 -> C:f0 [constraint=true dir=none style=dashed]
	C:f2 -> D:f0 [constraint=true dir=none style=dashed]
	{
		graph [rank=same]
		B
		C
		D
	}
}
""",  # noqa: E501
            str(btree.graph),
        )

    def test_graph_generation_on_bigger_tree(self):
        # Anfangs-B-plus-Baum
        b = BplusTree(4)  # M_5 wird in der ersten Zelle deklariert und ist schon auf 4 gesetzt.
        b.add_node("A", [20])
        b.add_node("B", [10])
        b.add_node("C", [30])
        b.add_node("D", [5, 6, 7, 8])
        b.add_node("E", [10, 12, 15, 18])
        b.add_node("F", [20, 25, 28, 29])
        b.add_node("G", [30, 32, 35, 38])
        b.add_edge("A", "B", 1)
        b.add_edge("A", "C", 2)
        b.add_edge("B", "D", 1)
        b.add_edge("B", "E", 2)
        b.add_edge("C", "F", 1)
        b.add_edge("C", "G", 2)
        graph = str(b.graph)
        self.assertIn('A [label=<<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0"><TR> <TD PORT="f0">', graph)
        self.assertIn("F:f4 -> G:f0 [constraint=true dir=none style=dashed]", graph)
        self.assertIn("{\n		graph [rank=same]\n		D\n		E\n		F\n		G\n	}", graph)

    def test_generate_copy_text(self):
        btree = BplusTree(4)
        btree.add_node("A", [10, 20])
        btree.add_node("B", [5, 6])
        btree.add_edge("A", "B", 1)
        expected = "b1 = BplusTree(4)\nb1.add_node('A', [10, 20])\nb1.add_edge('A', 'B', 1)\nb1.add_node('B', [5, 6])\n"
        actual = btree.generate_copy_text("b1")
        assert actual == expected


if __name__ == "__main__":
    unittest.main()
