def test_set_frequency(self): """Test set_frequency method.""" expecetd_frequency = 50 self.speaker.frequency = expecetd_frequency expected_values = (50, 0) sent_messages = [] while self.conn.send_list: sent_messages.append(self.conn.send_list.pop()) self.assertTrue( parse_message( 0x03, 0, -1, (Speaker.FREQUENCY, None, self.speaker.prop_samp_freq, None) ) in sent_messages ) self.assertTrue( parse_message( 0x03, 0, -1, (Speaker.VOLUME, None, self.speaker.prop_samp_freq, None) ) in sent_messages ) self.assertTrue( parse_message( 0x04, 16, -1, parse_data(expected_values, 'float') ) in sent_messages )
def test_set_text(self): """Test set_text method.""" mock_text = "abcd" self.display.text = mock_text clear_message = parse_message(0x04, 21, -1, (0, 0)) text_message = parse_message(0x04, 17, -1, parse_data(mock_text + '\0', 'string')) self.assertEqual(self.conn.send_list[0], clear_message) self.assertEqual(self.conn.send_list[1], text_message)
def test_on(self): """Test on method.""" expected_color = (100, 100, 100) self.led.rgb = expected_color set_message = parse_message(0x04, 16, -1, parse_data(expected_color, 'int')) sent_messages = [] while self.conn.send_list: sent_messages.append(self.conn.send_list.pop()) self.assertTrue(set_message in sent_messages)
def test_set_blue(self): """Test blue method.""" expected_color = (0, 0, 20) set_message = parse_message(0x04, 16, -1, parse_data(expected_color, 'int')) sent_messages = [] self.led.blue = 20 while self.conn.send_list: sent_messages.append(self.conn.send_list.pop()) self.assertTrue(set_message in sent_messages)
def test_set_rgb(self): """Test set_rgb method with user-defined inputs.""" expected_color = (10, 20, 100) self.led.rgb = expected_color set_message = parse_message(0x04, 16, -1, parse_data(expected_color, 'int')) sent_messages = [] while self.conn.send_list: sent_messages.append(self.conn.send_list.pop()) self.assertTrue(set_message in sent_messages)
def test_show_variable(self): """Test set_variable method.""" mock_variable = 123 mock_position = 5 self.display.show_variable(mock_variable, mock_position, mock_position) self.assertEqual( self.conn.send_list[0], parse_message( 0x04, 22, -1, parse_data((mock_variable, mock_position, mock_position), 'display_var')))
def test_set_speed(self): """Test set_speed method.""" expected_values = first_speed_value, second_speed_value = 50, 50 self.motor.speed = expected_values sent_messages = [] while self.conn.send_list: sent_messages.append(self.conn.send_list.pop()) self.assertTrue( parse_message(0x03, 0, -1, (Motor.FIRST_SPEED, None, self.motor.prop_samp_freq, None)) in sent_messages) self.assertTrue( parse_message(0x03, 0, -1, (Motor.SECOND_SPEED, None, self.motor.prop_samp_freq, None)) in sent_messages) self.assertTrue( parse_message( 0x04, 19, -1, parse_data(( 0, 1, first_speed_value), 'int')) in sent_messages) self.assertTrue( parse_message( 0x04, 19, -1, parse_data(( 1, 1, second_speed_value), 'int')) in sent_messages)
def __parse_set_message(destination_id: int, property_type: int, property_values: Tuple, property_data_type: int) -> List[str]: """Generate set_property json serialized message :param destination_id: Id of the destination module :type destination_id: int :param property_type: Property Type :type property_type: int :param property_values: Property values :type property_values: Tuple :param property_data_type: Property Data Type :type property_data_type: int :return: List of json messages :rtype: List[str] """ data_list = [] if property_data_type == OutputModule.INT: data_list.append(parse_data(property_values, 'int')) elif property_data_type == OutputModule.FLOAT: data_list.append(parse_data(property_values, 'float')) elif property_data_type == OutputModule.STRING: for i in range(0, len(property_values), 8): chunk = str(property_values)[i:i + 8] data_list.append(parse_data(chunk, 'string')) elif property_data_type == OutputModule.RAW: data_list.append(parse_data(property_values, 'raw')) elif property_data_type == OutputModule.DISPLAY_VAR: data_list.append(parse_data(property_values, 'display_var')) else: raise RuntimeError("Not supported property data type.") messages = [] for data in data_list: messages.append( parse_message(0x04, property_type, destination_id, data)) return messages
def test_set_second_degree(self): """Test set_second_degree method.""" second_degree_value = 50 self.motor.second_degree = second_degree_value sent_messages = [] while self.conn.send_list: sent_messages.append(self.conn.send_list.pop()) self.assertTrue( parse_message(0x03, 0, -1, (Motor.SECOND_DEGREE, None, self.motor.prop_samp_freq, None)) in sent_messages) self.assertTrue( parse_message( 0x04, 19, -1, parse_data(( 1, 2, second_degree_value), 'int')) in sent_messages)