def test_serial_stream(self): message = { "id": 5, "value": { "value_a_name": 15.0, "value_b_name": 26.5, "value_c_name": 13.3 }, } stream = get_serial_stream(message) self.assertEqual( stream, b'{"id": 5, "value": {"value_a_name": 15.0, "value_b_name": 26.5, "value_c_name": 13.3}}\n', )
def test_send(self, mock_port_list, mock_serial): """ tests the send method """ port = ListPortInfo() port.vid = "vid" port.pid = "pid" port.manufacturer = "Microsoft" port.serial_number = "456" port.interface = "usb" port.device = "usb" mock_port_list.return_value = [port] test_payload = {"value": "value"} output = get_serial_stream(test_payload) with patch.dict( os.environ, { "LOG_DIRECTORY": self.temp_dir.path, "RADIO_TRANSMITTER_PORT": "usb", "LOG_FILE": "logger.txt", "TRANSCEIVER_BAUDRATE": "9600", "TRANSCEIVER_TIMEOUT": "1", }, ): with LogCapture() as capture: transciever = Transceiver(log_file_name="LOG_FILE") mock_serial_sender = MagicMock() transciever.serial = mock_serial_sender transciever.send(test_payload) mock_serial_sender.write.assert_called_with(output) capture.check( ("LOG_FILE", "INFO", "Port device found: usb"), ("LOG_FILE", "INFO", "Opening serial on: usb"), ("LOG_FILE", "INFO", "sending"), ("LOG_FILE", "INFO", "{'value': 'value'}"), )
def send(self, payload): self.logging.info("sending") self.serial.write(get_serial_stream(payload)) self.logging.info(payload)