def test_interface(self):
        """
        insure that the interface is added to the
        extra_items list if it is present in port
        """
        port = ListPortInfo()
        port.interface = "123interface"

        response = extra_info(port)
        self.assertTrue(port.interface in response)
Beispiel #2
0
    def test_is_serial_usb_manufacturer_match(self, mock_port_list,
                                              mock_serial):
        """
        Tests the __init__ function where the port manufacturer
        is not empty and doesn't match the supplied port in RADIO_TRANSMITTER_PORT
        """

        port, port2 = ListPortInfo(), ListPortInfo()
        port.vid = "foo"
        port.pid = "bar"
        port.manufacturer = "Microsoft"
        port.serial_number = "456"
        port.interface = "usb"
        port.device = "usb"

        port2.vid = "foo"
        port2.pid = "bar"
        port2.manufacturer = "Apple"
        port2.serial_number = "123"
        port2.interface = "bluetooth"
        port2.device = "usb2"

        mock_port_list.return_value = [port, port2]

        with patch.dict(
                os.environ,
            {
                "LOG_DIRECTORY": self.temp_dir.path,
                "RADIO_TRANSMITTER_PORT": "usb2",
                "LOG_FILE": "logger.txt",
                "TRANSCEIVER_BAUDRATE": "9600",
                "TRANSCEIVER_TIMEOUT": "1",
            },
        ):
            transciever = Transceiver(log_file_name="LOG_FILE")

            self.assertTrue(transciever.logging is not None)
            self.assertTrue(transciever.logging.name == "LOG_FILE")
            self.assertIsInstance(transciever.logging, Logger)

            self.assertTrue(transciever.port == "usb2")
            self.assertTrue(transciever.port_vid == "foo")
            self.assertTrue(transciever.port_pid == "bar")
            self.assertTrue(transciever.port_vendor == "Apple")
            self.assertTrue(transciever.port_intf == "bluetooth")
            self.assertTrue(transciever.port_serial_number == "123")

            mock_serial.assert_called_with(
                port="usb2",
                baudrate=self.baudrate,
                parity=self.parity,
                stopbits=self.stopbits,
                bytesize=self.bytesize,
                timeout=self.timeout,
            )
    def test_get_port_empty(self, port_mocks):

        port = ListPortInfo()
        port.vid = None
        port.pid = None
        port.manufacturer = None
        port.serial_number = None
        port.interface = None
        port.device = "usb"

        port_mocks.return_value = [port]
        self.assertIsNone(get_port())
    def test_get_port_match(self, port_mocks):

        port = ListPortInfo()
        port.vid = "vid"
        port.pid = None
        port.manufacturer = None
        port.serial_number = None
        port.interface = None
        port.device = "usb"

        port_mocks.return_value = [port]
        self.assertTrue("port found" in get_port())
Beispiel #5
0
    def test_is_serial_usb_no_vid(self, mock_port_list, mock_serial):
        """
        Tests the __init__ function where the port vid
        is empty
        """

        port = ListPortInfo()
        port.vid = None
        port.pid = None
        port.manufacturer = None
        port.serial_number = None
        port.interface = None
        port.device = "usb"

        mock_port_list.return_value = [port]

        with patch.dict(
                os.environ,
            {
                "LOG_DIRECTORY": self.temp_dir.path,
                "RADIO_TRANSMITTER_PORT": "",
                "LOG_FILE": "logger.txt",
                "TRANSCEIVER_BAUDRATE": "9600",
                "TRANSCEIVER_TIMEOUT": "1",
            },
        ):
            transciever = Transceiver(log_file_name="LOG_FILE")

            self.assertTrue(transciever.logging is not None)
            self.assertTrue(transciever.logging.name == "LOG_FILE")
            self.assertIsInstance(transciever.logging, Logger)

            self.assertTrue(transciever.port == "")
            self.assertIsNone(transciever.port_vid)
            self.assertIsNone(transciever.port_pid)
            self.assertIsNone(transciever.port_vendor)
            self.assertIsNone(transciever.port_intf)
            self.assertIsNone(transciever.port_serial_number)

            mock_serial.assert_called_with(
                port="",
                baudrate=self.baudrate,
                parity=self.parity,
                stopbits=self.stopbits,
                bytesize=self.bytesize,
                timeout=self.timeout,
            )
Beispiel #6
0
    def test_listen_exception(self, mock_port_list, mock_serial, mock_json):
        """
        tests the listen method with invalid input
        """
        port = ListPortInfo()
        port.vid = "vid"
        port.pid = "pid"
        port.manufacturer = "Microsoft"
        port.serial_number = "456"
        port.interface = "usb"
        port.device = "usb"

        mock_json.side_effect = Exception("ex")

        mock_port_list.return_value = [port]

        test_input = "{'value': 'value'}"
        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:
                transceiver = Transceiver(log_file_name="LOG_FILE")

                mock_receiver = MagicMock()
                mock_receiver.readline.return_value.decode.return_value = test_input
                transceiver.serial = mock_receiver

                with self.assertRaises(Exception):
                    transceiver.listen()

                capture.check(
                    ("LOG_FILE", "INFO", "Port device found: usb"),
                    ("LOG_FILE", "INFO", "Opening serial on: usb"),
                    ("LOG_FILE", "ERROR", "error occurred: ex"),
                )
Beispiel #7
0
    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 test_pass(self):
        """
        insure that is_serial_usb returns true if all test cases haven't
        failed
        """
        port, args = ListPortInfo(), Namespace()

        port.vid = "bar"
        port.pid = "foo"
        port.manufacturer = "Apple"
        port.serial_number = "456"
        port.interface = "bar"

        args.vid = None
        args.pid = None
        args.vendor = None
        args.serial = None
        args.intf = None

        response = is_usb_serial(port, args)
        self.assertTrue(response)
    def test_args_intf_not_empty(self):
        """
        checks to make sure that the is_serial_usb function
        exists correctly when the args["serial"] is not empty
        and port["interface"] is none
        """
        port, args = ListPortInfo(), Namespace()

        port.vid = "bar"
        port.pid = "foo"
        port.manufacturer = "Apple"
        port.serial_number = "456"
        port.interface = None

        args.vid = None
        args.pid = None
        args.vendor = None
        args.serial = None
        args.intf = "foo"

        response = is_usb_serial(port, args)
        self.assertFalse(response)