import unittest
from modelverse_state.main import ModelverseState

class TestDeleteNode(unittest.TestCase):
    def setUp(self):
        self.mvs = ModelverseState()

    def test_delete_node_no_exists(self):
        n = self.mvs.delete_node(-1)
        assert n == None

    def test_delete_node_no_value(self):
        a = self.mvs.create_node()
        assert a != None

        n = self.mvs.delete_node(a)
        assert n == None

    def test_delete_node_value(self):
        a = self.mvs.create_nodevalue(1)
        assert a != None

        d = self.mvs.read_value(a)
        assert d == 1

        n = self.mvs.delete_node(a)
        assert n == None

        d = self.mvs.read_value(a)
        assert d == None
        self.assertEquals(d, None)

    def test_delete_node_edge(self):
        a = self.mvs.create_node()
        b = self.mvs.create_node()
        c = self.mvs.create_edge(a, b)
        assert a != None
        assert b != None
        assert c != None

        n = self.mvs.delete_node(c)
        assert n == None

    def test_delete_node_remove_edge_outgoing(self):
        a = self.mvs.create_nodevalue(1)
        b = self.mvs.create_node()
        c = self.mvs.create_edge(a, b)
        assert a != None
        assert b != None
        assert c != None

        n = self.mvs.delete_node(a)
        assert n == None

        d = self.mvs.read_value(a)
        assert d == None

        s, t = self.mvs.read_edge(c)
        assert s == None
        assert t == None

        d = self.mvs.read_outgoing(b)
        assert d != None
        assert set(d) == set([])

    def test_delete_node_remove_edge_incoming(self):
        a = self.mvs.create_nodevalue(1)
        b = self.mvs.create_node()
        c = self.mvs.create_edge(b, a)
        assert a != None
        assert b != None
        assert c != None

        n = self.mvs.delete_node(a)
        assert n == None

        d = self.mvs.read_value(a)
        assert d == None

        s, t = self.mvs.read_edge(c)
        assert s == None
        assert t == None

        d = self.mvs.read_outgoing(b)
        assert d != None
        assert set(d) == set([])

    def test_delete_node_remove_edge_both(self):
        a = self.mvs.create_nodevalue(1)
        b = self.mvs.create_node()
        c = self.mvs.create_edge(a, b)
        e = self.mvs.create_node()
        f = self.mvs.create_edge(e, a)
        assert a != None
        assert b != None
        assert c != None
        assert e != None
        assert f != None

        n = self.mvs.delete_node(a)
        assert n == None

        d = self.mvs.read_value(a)
        assert d == None

        s, t = self.mvs.read_edge(c)
        assert s == None
        assert t == None

        d = self.mvs.read_incoming(b)
        assert d != None
        assert set(d) == set([])

        s, t = self.mvs.read_edge(f)
        assert s == None
        assert t == None

        d = self.mvs.read_outgoing(e)
        assert d != None
        assert set(d) == set([])

    def test_delete_node_remove_edge_recursive(self):
        a = self.mvs.create_nodevalue(1)
        b = self.mvs.create_node()
        c = self.mvs.create_edge(a, b)
        d = self.mvs.create_edge(c, b)
        assert a != None
        assert b != None
        assert c != None
        assert d != None

        n = self.mvs.delete_node(a)
        assert n == None

        d = self.mvs.read_value(a)
        assert d == None

        s, t = self.mvs.read_edge(c)
        assert s == None
        assert t == None

        s, t = self.mvs.read_edge(d)
        assert s == None
        assert t == None

        d = self.mvs.read_outgoing(b)
        assert d != None
        assert set(d) == set([])

    def test_delete_node_remove_edge_recursive_deep(self):
        a = self.mvs.create_node()
        b = self.mvs.create_node()
        c = self.mvs.create_node()
        d = self.mvs.create_edge(a, b)
        e = self.mvs.create_edge(d, c)
        f = self.mvs.create_node()
        g = self.mvs.create_edge(f, e)
        h = self.mvs.create_edge(b, c)
        assert a != None
        assert b != None
        assert c != None
        assert d != None
        assert e != None
        assert f != None
        assert g != None
        assert h != None

        n = self.mvs.delete_node(a)
        assert n == None

        l = self.mvs.read_outgoing(a)
        assert l == None

        l = self.mvs.read_incoming(a)
        assert l == None

        l = self.mvs.read_outgoing(b)
        assert l != None
        assert set(l) == set([h])

        l = self.mvs.read_incoming(b)
        assert l != None
        assert set(l) == set([])

        l = self.mvs.read_outgoing(c)
        assert l != None
        assert set(l) == set([])

        l = self.mvs.read_incoming(c)
        assert l != None
        assert set(l) == set([h])

        s, t = self.mvs.read_edge(d)
        assert s == None
        assert t == None

        s, t = self.mvs.read_edge(e)
        assert s == None
        assert t == None

        s, t = self.mvs.read_edge(g)
        assert s == None
        assert t == None

        l = self.mvs.read_outgoing(f)
        assert l != None
        assert set(l) == set([])

        l = self.mvs.read_incoming(f)
        assert l != None
        assert set(l) == set([])

        s, t = self.mvs.read_edge(h)
        assert s == b
        assert t == c
