import unittest
from modelverse_state.main import ModelverseState

from modelverse_state import status

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

    def test_read_value_different_id_simple(self):
        id1, r1 = self.mvs.create_nodevalue(1)
        id2, r2 = self.mvs.create_nodevalue(2)
        
        self.assertEquals(r1, status.SUCCESS)
        self.assertEquals(r2, status.SUCCESS)

        v1, r1 = self.mvs.read_value(id1)
        v2, r2 = self.mvs.read_value(id2)
        self.assertEquals(r1, status.SUCCESS)
        self.assertEquals(r2, status.SUCCESS)
        self.assertEquals(v1, 1)
        self.assertEquals(v2, 2)

    def test_read_value_integer_ib_negative(self):
        # Just within range
        for i in range(-2**63, -2**63 + 10):
            id1, r = self.mvs.create_nodevalue(i)
            self.assertEquals(r, status.SUCCESS)
            v, r = self.mvs.read_value(id1)
            self.assertEquals(r, status.SUCCESS)
            self.assertEquals(v, i)

    def test_read_value_integer_ib_zero(self):
        # Nicely within range
        for i in range(-10, 10):
            id1, r = self.mvs.create_nodevalue(i)
            self.assertEquals(r, status.SUCCESS)
            v, r = self.mvs.read_value(id1)
            self.assertEquals(r, status.SUCCESS)
            self.assertEquals(v, i)

    def test_read_value_integer_ib_positive(self):
        # Just within range
        for i in range(2**64-10, 2**64):
            id1, r = self.mvs.create_nodevalue(i)
            self.assertEquals(r, status.SUCCESS)
            v, r = self.mvs.read_value(id1)
            self.assertEquals(r, status.SUCCESS)
            self.assertEquals(v, i)

    def test_read_value_boolean(self):
        id1, r1 = self.mvs.create_nodevalue(True)
        id2, r2 = self.mvs.create_nodevalue(False)
        self.assertEquals(r1, status.SUCCESS)
        self.assertEquals(r2, status.SUCCESS)

        v1, r1 = self.mvs.read_value(id1)
        v2, r2 = self.mvs.read_value(id2)
        self.assertEquals(r1, status.SUCCESS)
        self.assertEquals(r2, status.SUCCESS)
        self.assertEquals(v1, True)
        self.assertEquals(v2, False)

    def test_read_nodevalue_boolean_same(self):
        id1, r1 = self.mvs.create_nodevalue(True)
        id2, r2 = self.mvs.create_nodevalue(True)
        self.assertEquals(r1, status.SUCCESS)
        self.assertEquals(r2, status.SUCCESS)

        v1, r1 = self.mvs.read_value(id1)
        v2, r2 = self.mvs.read_value(id2)
        self.assertEquals(r1, status.SUCCESS)
        self.assertEquals(r2, status.SUCCESS)
        self.assertEquals(v1, True)
        self.assertEquals(v2, True)

    def test_read_value_no_exist(self):
        v1, r1 = self.mvs.read_value(100000)
        self.assertEquals(r1, status.FAIL_RV_UNKNOWN)
        self.assertEquals(v1, None)

    def test_read_value_no_value(self):
        id1, r1 = self.mvs.create_node()
        self.assertEquals(r1, status.SUCCESS)

        v1, r1 = self.mvs.read_value(id1)
        self.assertEquals(r1, status.FAIL_RV_NO_VALUE)
        self.assertEquals(v1, None)
