import unittest
from modelverse_state.main import ModelverseState

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

    def test_read_incoming_node_none(self):
        b = self.mvs.create_node()
        assert b != None

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

    def test_read_incoming_node_one(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

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

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

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

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

        l = self.mvs.read_incoming(b)
        assert l != None
        assert set(l) == set([c, d, e])

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

        f = self.mvs.create_node()
        assert f != None

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

        l = self.mvs.read_incoming(b)
        assert l != None
        assert set(l) == set([c, d, e])

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

    def test_read_incoming_edge_none(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

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

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

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

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

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

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

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

        l = self.mvs.read_incoming(c)
        assert l != None
        assert set(l) == set([d, e, f])

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

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

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

    def test_read_incoming_nodevalue_none(self):
        b = self.mvs.create_nodevalue(1)
        assert b != None

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

    def test_read_incoming_nodevalue_one(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

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

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

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

        l = self.mvs.read_incoming(a)
        assert l != None
        assert set(l) == set([c, d, e])

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

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

        f = self.mvs.create_nodevalue(1)
        assert f != None

        l = self.mvs.read_incoming(a)
        assert l != None
        assert set(l) == set([c, d, e])

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

        l = self.mvs.read_incoming(f)
        assert l != None
        assert set(l) == set([])
    
    def test_read_incoming_node_deleted(self):
        b = self.mvs.create_node()
        assert b != None

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

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

    def test_read_incoming_nodevalue_deleted(self):
        b = self.mvs.create_nodevalue(1)
        assert b != None

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

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

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

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