import unittest
from modelverse_state.main import ModelverseState

from modelverse_state import status

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

    def test_delete_edge_no_exists(self):
        n, r = self.mvs.delete_edge(1)
        self.assertEquals(r, status.FAIL_DE_UNKNOWN)

    def test_delete_edge_node(self):
        a, r = self.mvs.create_node()
        self.assertEquals(r, status.SUCCESS)

        n, r = self.mvs.delete_edge(a)
        self.assertEquals(r, status.FAIL_DE_UNKNOWN)

    def test_delete_edge_nodevalue(self):
        a, r = self.mvs.create_nodevalue(1)
        self.assertEquals(r, status.SUCCESS)

        n, r = self.mvs.delete_edge(a)
        self.assertEquals(r, status.FAIL_DE_UNKNOWN)

    def test_delete_edge_normal(self):
        a, r = self.mvs.create_nodevalue(1)
        self.assertEquals(r, status.SUCCESS)
        b, r = self.mvs.create_node()
        self.assertEquals(r, status.SUCCESS)
        c, r = self.mvs.create_edge(a, b)
        self.assertEquals(r, status.SUCCESS)

        n, r = self.mvs.delete_edge(c)
        self.assertEquals(r, status.SUCCESS)

        l, r = self.mvs.read_outgoing(a)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(len(l), 0)
        self.assertEquals(set(l), set())

        l, r = self.mvs.read_incoming(a)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(len(l), 0)
        self.assertEquals(set(l), set())

        l, r = self.mvs.read_outgoing(b)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(len(l), 0)
        self.assertEquals(set(l), set())

        l, r = self.mvs.read_incoming(b)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(len(l), 0)
        self.assertEquals(set(l), set())

    def test_delete_edge_remove_recursive(self):
        a, r = self.mvs.create_nodevalue(1)
        self.assertEquals(r, status.SUCCESS)
        b, r = self.mvs.create_node()
        self.assertEquals(r, status.SUCCESS)
        c, r = self.mvs.create_edge(a, b)
        self.assertEquals(r, status.SUCCESS)
        d, r = self.mvs.create_edge(c, b)
        self.assertEquals(r, status.SUCCESS)

        n, r = self.mvs.delete_edge(c)
        self.assertEquals(r, status.SUCCESS)

        l, r = self.mvs.read_value(a)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(l, 1)

        l, r = self.mvs.read_outgoing(a)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(len(l), 0)
        self.assertEquals(set(l), set())

        l, r = self.mvs.read_incoming(a)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(len(l), 0)
        self.assertEquals(set(l), set())

        v, r = self.mvs.read_edge(c)
        s, t = v
        self.assertEquals(r, status.FAIL_RE_UNKNOWN)
        self.assertEquals(s, None)
        self.assertEquals(t, None)

        v, r = self.mvs.read_edge(d)
        s, t = v
        self.assertEquals(r, status.FAIL_RE_UNKNOWN)
        self.assertEquals(s, None)
        self.assertEquals(t, None)

        l, r = self.mvs.read_outgoing(b)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(len(l), 0)
        self.assertEquals(set(l), set())

    def test_delete_edge_remove_edge_recursive_deep(self):
        a, r = self.mvs.create_node()
        self.assertEquals(r, status.SUCCESS)
        b, r = self.mvs.create_node()
        self.assertEquals(r, status.SUCCESS)
        c, r = self.mvs.create_node()
        self.assertEquals(r, status.SUCCESS)
        d, r = self.mvs.create_edge(a, b)
        self.assertEquals(r, status.SUCCESS)
        e, r = self.mvs.create_edge(d, c)
        self.assertEquals(r, status.SUCCESS)
        f, r = self.mvs.create_node()
        self.assertEquals(r, status.SUCCESS)
        g, r = self.mvs.create_edge(f, e)
        self.assertEquals(r, status.SUCCESS)
        h, r = self.mvs.create_edge(b, c)
        self.assertEquals(r, status.SUCCESS)

        n, r = self.mvs.delete_edge(d)
        self.assertEquals(r, status.SUCCESS)

        l, r = self.mvs.read_outgoing(a)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(set(l), set())

        l, r = self.mvs.read_incoming(a)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(set(l), set())

        l, r = self.mvs.read_outgoing(b)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(set(l), set([h]))

        l, r = self.mvs.read_incoming(b)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(set(l), set())

        l, r = self.mvs.read_outgoing(c)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(set(l), set())

        l, r = self.mvs.read_incoming(c)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(set(l), set([h]))

        v, r = self.mvs.read_edge(d)
        s, t = v
        self.assertEquals(r, status.FAIL_RE_UNKNOWN)
        self.assertEquals(s, None)
        self.assertEquals(t, None)

        v, r = self.mvs.read_edge(e)
        s, t = v
        self.assertEquals(r, status.FAIL_RE_UNKNOWN)
        self.assertEquals(s, None)
        self.assertEquals(t, None)

        v, r = self.mvs.read_edge(g)
        s, t = v
        self.assertEquals(r, status.FAIL_RE_UNKNOWN)
        self.assertEquals(s, None)
        self.assertEquals(t, None)

        l, r = self.mvs.read_outgoing(f)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(set(l), set())

        l, r = self.mvs.read_incoming(f)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(set(l), set())

        v, r = self.mvs.read_edge(h)
        s, t = v
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(s, b)
        self.assertEquals(t, c)

    def test_delete_edge_remove_edge_recursive_steps(self):
        a, r = self.mvs.create_node()
        self.assertEquals(r, status.SUCCESS)
        b, r = self.mvs.create_node()
        self.assertEquals(r, status.SUCCESS)
        c, r = self.mvs.create_node()
        self.assertEquals(r, status.SUCCESS)
        d, r = self.mvs.create_edge(a, b)
        self.assertEquals(r, status.SUCCESS)
        e, r = self.mvs.create_edge(d, c)
        self.assertEquals(r, status.SUCCESS)
        f, r = self.mvs.create_node()
        self.assertEquals(r, status.SUCCESS)
        g, r = self.mvs.create_edge(f, e)
        self.assertEquals(r, status.SUCCESS)
        h, r = self.mvs.create_edge(b, c)
        self.assertEquals(r, status.SUCCESS)

        n, r = self.mvs.delete_edge(g)
        self.assertEquals(r, status.SUCCESS)

        l, r = self.mvs.read_outgoing(a)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(set(l), set([d]))

        l, r = self.mvs.read_incoming(a)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(set(l), set())

        l, r = self.mvs.read_outgoing(b)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(set(l), set([h]))

        l, r = self.mvs.read_incoming(b)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(set(l), set([d]))

        l, r = self.mvs.read_outgoing(c)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(set(l), set())

        l, r = self.mvs.read_incoming(c)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(set(l), set([h, e]))

        v, r = self.mvs.read_edge(d)
        s, t = v
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(s, a)
        self.assertEquals(t, b)

        l, r = self.mvs.read_outgoing(d)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(set(l), set([e]))

        l, r = self.mvs.read_incoming(d)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(set(l), set())

        v, r = self.mvs.read_edge(e)
        s, t = v
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(s, d)
        self.assertEquals(t, c)

        l, r = self.mvs.read_outgoing(e)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(set(l), set())

        l, r = self.mvs.read_incoming(e)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(set(l), set())

        v, r = self.mvs.read_edge(g)
        s, t = v
        self.assertEquals(r, status.FAIL_RE_UNKNOWN)
        self.assertEquals(s, None)
        self.assertEquals(t, None)

        l, r = self.mvs.read_outgoing(g)
        self.assertEquals(r, status.FAIL_RO_UNKNOWN)
        self.assertEquals(l, None)

        l, r = self.mvs.read_incoming(g)
        self.assertEquals(r, status.FAIL_RI_UNKNOWN)
        self.assertEquals(l, None)

        l, r = self.mvs.read_outgoing(f)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(set(l), set())

        l, r = self.mvs.read_incoming(f)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(set(l), set())

        v, r = self.mvs.read_edge(h)
        s, t = v
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(s, b)
        self.assertEquals(t, c)

        n, r = self.mvs.delete_edge(e)
        self.assertEquals(r, status.SUCCESS)

        l, r = self.mvs.read_outgoing(a)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(set(l), set([d]))

        l, r = self.mvs.read_incoming(a)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(set(l), set())

        l, r = self.mvs.read_outgoing(b)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(set(l), set([h]))

        l, r = self.mvs.read_incoming(b)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(set(l), set([d]))

        l, r = self.mvs.read_outgoing(c)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(set(l), set())

        l, r = self.mvs.read_incoming(c)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(set(l), set([h]))

        v, r = self.mvs.read_edge(d)
        s, t = v
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(s, a)
        self.assertEquals(t, b)

        l, r = self.mvs.read_outgoing(d)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(set(l), set())

        l, r = self.mvs.read_incoming(d)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(set(l), set())

        v, r = self.mvs.read_edge(e)
        s, t = v
        self.assertEquals(r, status.FAIL_RE_UNKNOWN)
        self.assertEquals(s, None)
        self.assertEquals(t, None)

        l, r = self.mvs.read_outgoing(e)
        self.assertEquals(r, status.FAIL_RO_UNKNOWN)
        self.assertEquals(l, None)

        l, r = self.mvs.read_incoming(e)
        self.assertEquals(r, status.FAIL_RI_UNKNOWN)
        self.assertEquals(l, None)

        v, r = self.mvs.read_edge(g)
        s, t = v
        self.assertEquals(r, status.FAIL_RE_UNKNOWN)
        self.assertEquals(s, None)
        self.assertEquals(t, None)

        l, r = self.mvs.read_outgoing(g)
        self.assertEquals(r, status.FAIL_RO_UNKNOWN)
        self.assertEquals(l, None)

        l, r = self.mvs.read_incoming(g)
        self.assertEquals(r, status.FAIL_RI_UNKNOWN)
        self.assertEquals(l, None)

        l, r = self.mvs.read_outgoing(f)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(set(l), set())

        l, r = self.mvs.read_incoming(f)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(set(l), set())

        v, r = self.mvs.read_edge(h)
        s, t = v
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(s, b)
        self.assertEquals(t, c)

        n, r = self.mvs.delete_edge(d)
        self.assertEquals(r, status.SUCCESS)

        l, r = self.mvs.read_outgoing(a)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(set(l), set())

        l, r = self.mvs.read_incoming(a)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(set(l), set())

        l, r = self.mvs.read_outgoing(b)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(set(l), set([h]))

        l, r = self.mvs.read_incoming(b)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(set(l), set())

        l, r = self.mvs.read_outgoing(c)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(set(l), set())

        l, r = self.mvs.read_incoming(c)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(set(l), set([h]))

        v, r = self.mvs.read_edge(d)
        s, t = v
        self.assertEquals(r, status.FAIL_RE_UNKNOWN)
        self.assertEquals(s, None)
        self.assertEquals(t, None)

        l, r = self.mvs.read_outgoing(d)
        self.assertEquals(r, status.FAIL_RO_UNKNOWN)
        self.assertEquals(l, None)

        l, r = self.mvs.read_incoming(d)
        self.assertEquals(r, status.FAIL_RI_UNKNOWN)
        self.assertEquals(l, None)

        v, r = self.mvs.read_edge(e)
        s, t = v
        self.assertEquals(r, status.FAIL_RE_UNKNOWN)
        self.assertEquals(s, None)
        self.assertEquals(t, None)

        l, r = self.mvs.read_outgoing(e)
        self.assertEquals(r, status.FAIL_RO_UNKNOWN)
        self.assertEquals(l, None)

        l, r = self.mvs.read_incoming(e)
        self.assertEquals(r, status.FAIL_RI_UNKNOWN)
        self.assertEquals(l, None)

        v, r = self.mvs.read_edge(g)
        s, t = v
        self.assertEquals(r, status.FAIL_RE_UNKNOWN)
        self.assertEquals(s, None)
        self.assertEquals(t, None)

        l, r = self.mvs.read_outgoing(g)
        self.assertEquals(r, status.FAIL_RO_UNKNOWN)
        self.assertEquals(l, None)

        l, r = self.mvs.read_incoming(g)
        self.assertEquals(r, status.FAIL_RI_UNKNOWN)
        self.assertEquals(l, None)

        l, r = self.mvs.read_outgoing(f)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(set(l), set())

        l, r = self.mvs.read_incoming(f)
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(set(l), set())

        v, r = self.mvs.read_edge(h)
        s, t = v
        self.assertEquals(r, status.SUCCESS)
        self.assertEquals(s, b)
        self.assertEquals(t, c)
