import unittest
from modelverse_state.main import ModelverseState

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

    def test_delete_edge_no_exists(self):
        n = self.mvs.delete_edge(1)
        assert n == None

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

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

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

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

    def test_delete_edge_normal(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_edge(c)
        assert n == None

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

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

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

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

    def test_delete_edge_remove_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_edge(c)
        assert n == None

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

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

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

        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

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

    def test_delete_edge_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_edge(d)
        assert n == None

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

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

        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

    def test_delete_edge_remove_edge_recursive_steps(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_edge(g)
        assert n == None

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

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

        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([d])

        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, e])

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

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

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

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

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

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

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

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

        l = self.mvs.read_incoming(g)
        assert l == 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

        n = self.mvs.delete_edge(e)
        assert n == None

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

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

        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([d])

        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 == a
        assert t == b

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

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

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

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

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

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

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

        l = self.mvs.read_incoming(g)
        assert l == 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

        n = self.mvs.delete_edge(d)
        assert n == None

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

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

        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

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

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

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

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

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

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

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

        l = self.mvs.read_incoming(g)
        assert l == 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
