
import ctypes

from pytibrv.api import *
from pytibrv.status import *
from pytibrv.msg import *
import unittest

class MsgTest(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        status = tibrv_Open()
        assert TIBRV_OK == status, tibrvStatus_GetText(status)

    @classmethod
    def tearDownClass(cls):
        tibrv_Close()

    def test_new(self):

        status, msg = tibrvMsg_Create()
        self.assertEqual(TIBRV_OK, status, tibrvStatus_GetText(status))

        status, sz = tibrvMsg_ConvertToString(msg)
        self.assertEqual(TIBRV_OK, status, tibrvStatus_GetText(status))

        self.assertEqual("{}", sz)

        status = tibrvMsg_Destroy(msg)
        self.assertEqual(TIBRV_OK, status, tibrvStatus_GetText(status))

    def test_copy(self):

        status, msg = tibrvMsg_Create()
        self.assertEqual(TIBRV_OK, status, tibrvStatus_GetText(status))

        status = tibrvMsg_UpdateString(msg, 'A', 'TEST')
        self.assertEqual(TIBRV_OK, status, tibrvStatus_GetText(status))

        status, msg2 = tibrvMsg_CreateCopy(msg)
        self.assertEqual(TIBRV_OK, status, tibrvStatus_GetText(status))

        status, sz = tibrvMsg_ConvertToString(msg)
        self.assertEqual(TIBRV_OK, status, tibrvStatus_GetText(status))

        status, sz2 = tibrvMsg_ConvertToString(msg2)
        self.assertEqual(TIBRV_OK, status, tibrvStatus_GetText(status))

        self.assertEqual(sz, sz2)

        status = tibrvMsg_Destroy(msg)
        self.assertEqual(TIBRV_OK, status, tibrvStatus_GetText(status))

        status = tibrvMsg_Destroy(msg2)
        self.assertEqual(TIBRV_OK, status, tibrvStatus_GetText(status))

    def test_invalid(self):

        status, msg = tibrvMsg_Create()
        self.assertEqual(TIBRV_OK, status, tibrvStatus_GetText(status))

        status = tibrvMsg_Destroy(msg)
        self.assertEqual(TIBRV_OK, status, tibrvStatus_GetText(status))

        # construct by invalid msg id, which just destroyed
        status = tibrvMsg_SetSendSubject(msg, 'TEST')
        self.assertEqual(TIBRV_INVALID_MSG, status, tibrvStatus_GetText(status))

        status = tibrvMsg_Destroy(msg)
        self.assertEqual(TIBRV_INVALID_MSG, status, tibrvStatus_GetText(status))


        # assign random msg id, ex: 12345
        # DONT TRY IT, SEGMENT FAULT
        #
        #status = tibrvMsg_Destroy(12345)

    def test_subject(self):
        status, msg = tibrvMsg_Create()
        self.assertEqual(TIBRV_OK, status, tibrvStatus_GetText(status))

        status = tibrvMsg_SetSendSubject(msg, 'TEST')
        self.assertEqual(TIBRV_OK, status, tibrvStatus_GetText(status))

        status, subj = tibrvMsg_GetSendSubject(msg)
        self.assertEqual(TIBRV_OK, status, tibrvStatus_GetText(status))

        self.assertEqual('TEST', subj)

        status = tibrvMsg_SetReplySubject(msg, 'TEST2')
        self.assertEqual(TIBRV_OK, status, tibrvStatus_GetText(status))

        status, subj = tibrvMsg_GetReplySubject(msg)
        self.assertEqual(TIBRV_OK, status, tibrvStatus_GetText(status))
        self.assertEqual('TEST2', subj)

        status = tibrvMsg_Destroy(msg)
        self.assertEqual(TIBRV_OK, status, tibrvStatus_GetText(status))


    def test_get(self):
        status, msg = tibrvMsg_Create()
        self.assertEqual(TIBRV_OK, status, tibrvStatus_GetText(status))

        status = tibrvMsg_UpdateI8(msg, 'I8', 0xFFFF)