def test_filled_ipc_msg_valid(): # Instantiate an empty Message the_msg = IpcMessage() # Check that empty message is not valid assert_false(the_msg.is_valid()) # Set the message type and Value msg_type = "cmd" the_msg.set_msg_type(msg_type) msg_val = "reset" the_msg.set_msg_val(msg_val) # Check that the message is now valid assert_true(the_msg.is_valid())
def test_empty_ipc_msg_invalid(): # Instantiate an empty message the_msg = IpcMessage() # Check that the message is not valid assert_false(the_msg.is_valid())
def test_valid_ipc_msg_from_string(): # Instantiate a valid message from a JSON string json_str = """ {\"msg_type\":\"cmd\", \"msg_val\":\"status\", \"timestamp\" : \"2015-01-27T15:26:01.123456\", \"params\" : { \"paramInt\" : 1234, \"paramStr\" : \"testParam\", \"paramDouble\" : 3.1415 } } """ # Instantiate a valid message from the JSON string the_msg = IpcMessage(from_str=json_str) # Check the message is indeed valid assert_true(the_msg.is_valid()) # Check that all attributes are as expected assert_equals(the_msg.get_msg_type(), "cmd") assert_equals(the_msg.get_msg_val(), "status") assert_equals(the_msg.get_msg_timestamp(), "2015-01-27T15:26:01.123456") # Check that all parameters are as expected assert_equals(the_msg.get_param("paramInt"), 1234) assert_equals(the_msg.get_param("paramStr"), "testParam") assert_equals(the_msg.get_param("paramDouble"), 3.1415) # Check valid message throws an exception on missing parameter with assert_raises(IpcMessageException) as cm: missingParam = the_msg.get_param("missingParam") ex = cm.exception assert_equals(ex.msg, 'Missing parameter missingParam') # Check valid message can fall back to default value if parameter missing defaultParamValue = 90210 assert_equals(the_msg.get_param("missingParam", defaultParamValue), defaultParamValue)