Ejemplo n.º 1
0
class TestMessageBuilder(unittest.TestCase):
    def setUp(self):
        self.builder = MessageBuilder(msg_fmts)

    def testInstantiaion(self):
        pass  # instantiation is covered in the setup. Just want a simple pass here.

    def testDefinitionLoading(self):
        GET_ADDR = self.builder.GET_ADDR
        self.assertTrue(isinstance(GET_ADDR, type))

    def testInvalidDefinition(self):
        with self.assertRaises(InvalidFieldException):
            self.builder.build_message_class("INVALID_DEF",
                                             invalid_def["INVALID_DEF"])

    def testMessageProduction_Normal(self):
        wrt_req_1 = self.builder.build_message("x001600089999999900000000")

    def testMessageProduction_MultipleMatches(self):
        with self.assertRaises(MultipleMatchingMessageDefinitionsException):
            wrt_req_1 = self.builder.build_message("x00150004FFFFFFFF")
    def testAllMessageProductionMethods(self):
        builder = MessageBuilder(msg_fmts)

        data = "x001600089999999900000000"
        wrt_req_1 = builder.build_message(data)
        wrt_req_2 = builder.WRITE_REGISTER_REQUEST.from_data(data)
        wrt_req_3 = builder.WRITE_REGISTER_REQUEST(addr="x99999999",
                                                   data="x00000000")

        self.assertTrue(isinstance(wrt_req_1, builder.WRITE_REGISTER_REQUEST))
        self.assertTrue(isinstance(wrt_req_2, builder.WRITE_REGISTER_REQUEST))
        self.assertTrue(isinstance(wrt_req_3, builder.WRITE_REGISTER_REQUEST))

        self.assertEqual(wrt_req_1, wrt_req_2)
        self.assertEqual(wrt_req_2, wrt_req_3)
        self.assertEqual(wrt_req_3, wrt_req_1)
    def testInvalidMessageProduction(self):
        builder = MessageBuilder(msg_fmts)

        with self.assertRaises(InvalidDataFormatException):
            wrt_req_1 = builder.build_message("x0014000899999999000000001")