Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync backend-team with master #316

Merged
merged 8 commits into from Apr 7, 2020
38 changes: 19 additions & 19 deletions hardware/CommunicationsPi/find_port.py
Expand Up @@ -6,44 +6,44 @@


def is_usb_serial(port, args):
if port["vid"] is None:
if port.vid is None:
return False
if not args.get("vid") is None:
if port["vid"] != args.get("vid"):
if hasattr(args, "vid") and args.vid is not None:
if port.vid is not args.vid:
return False
if not args.get("pid") is None:
if port.get("pid") != args.get("pid"):
if hasattr(args, "pid") and args.pid is not None:
if port.pid is not args.pid:
return False
if not args.get("vendor") is None:
if not port["manufacturer"].startswith(args.get("vendor")):
if hasattr(args, "vendor") and args.vendor is not None:
if not port.manufacturer.startswith(args.vendor):
return False
if not args.get("serial") is None:
if not port["serial_number"].startswith(args.get("serial")):
if hasattr(args, "serial") and args.serial is not None:
if not port.serial_number.startswith(args.serial):
return False
if not args.get("intf") is None:
if port["interface"] is None or not args.get("intf") in port.get("interface"):
if hasattr(args, "intf") and args.intf is not None:
if port.interface is None or args.intf not in port.interface:
return False
return True


def extra_info(port):
extra_items = []
if port.get("manufacturer"):
extra_items.append("vendor '{}'".format(port["manufacturer"]))
if port.get("serial_number"):
extra_items.append("serial '{}'".format(port["serial_number"]))
if port.get("interface"):
extra_items.append("intf '{}'".format(port["interface"]))
if port.manufacturer:
extra_items.append("vendor '{}'".format(port.manufacturer))
if port.serial_number:
extra_items.append("serial '{}'".format(port.serial_number))
if port.interface:
extra_items.append("intf '{}'".format(port.interface))
if extra_items:
return " with " + " ".join(extra_items)
return ""


def get_port():
for port in serial.tools.list_ports.comports():
if is_usb_serial(port, {}):
if is_usb_serial(port, None):
print(port)
print(port["device"])
print(port.device)
return "port found"
return

Expand Down
180 changes: 114 additions & 66 deletions hardware/tests/test_find_port.py
@@ -1,6 +1,8 @@
from django.test import SimpleTestCase

from unittest import mock
from serial.tools.list_ports_common import ListPortInfo
from argparse import Namespace

from ..CommunicationsPi.find_port import is_usb_serial, extra_info, get_port

Expand All @@ -15,8 +17,10 @@ def test_port_vid_empty(self):
insures that the is_serial_usb function retunrs false
if the port.vid param is empty
"""
port = dict.fromkeys(["vid"])
args = {}
port = ListPortInfo()
port.vid = None
args = Namespace()
args.vid = None

response = is_usb_serial(port, args)
self.assertFalse(response)
Expand All @@ -27,8 +31,10 @@ def test_args_vid_not_empty(self):
exists correctly when the args["vid"] is not empty
and port["vid"] doesn't equal args["vid"]
"""
port = {"vid": "foo"}
args = {"vid": "bar"}
port = ListPortInfo()
port.vid = "foo"
args = Namespace()
args.vid = "bar"
response = is_usb_serial(port, args)
self.assertFalse(response)

Expand All @@ -38,8 +44,14 @@ def test_args_pid_not_empty(self):
exists correctly when the args["pid"] is not empty
and port["[id"] doesn't equal args["vid"]
"""
port = {"vid": "bar", "pid": "foo"}
args = {"vid": None, "pid": "bar"}
port = ListPortInfo()
args = Namespace()
port.vid = "bar"
port.pid = "foo"

args.vid = None
args.pid = "bar"

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

Expand All @@ -49,8 +61,15 @@ def test_args_vendor_not_empty(self):
exists correctly when the args["vendor"] is not empty
and port["manufacturer"] doesn't start with args["vendor"]
"""
port = {"vid": "bar", "pid": "foo", "manufacturer": "Apple"}
args = {"vid": None, "pid": None, "vendor": "Microsoft"}
port, args = ListPortInfo(), Namespace()
port.vid = "bar"
port.pid = "foo"
port.manufacturer = "Apple"

args.vid = None
args.pid = None
args.vendor = "Microsoft"

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

Expand All @@ -60,13 +79,18 @@ def test_args_serial_not_empty(self):
exists correctly when the args["serial"] is not empty
and port["serial_number"] doesn't start with args["serial"]
"""
port = {
"vid": "bar",
"pid": "foo",
"manufacturer": "Apple",
"serial_number": "456",
}
args = {"vid": None, "pid": None, "vendor": None, "serial": "123"}
port, args = ListPortInfo(), Namespace()

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

args.vid = None
args.pid = None
args.vendor = None
args.serial = "123"

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

Expand All @@ -76,14 +100,20 @@ def test_args_intf_not_empty(self):
exists correctly when the args["serial"] is not empty
and port["interface"] is none
"""
port = {
"vid": "bar",
"pid": "foo",
"manufacturer": "Apple",
"serial_number": "456",
"interface": None,
}
args = {"vid": None, "pid": None, "vendor": None, "serial": None, "intf": "foo"}
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)

Expand All @@ -93,14 +123,20 @@ def test_args_intf_not_empty_interface_not_empty(self):
exists correctly when the args["serial"] is not empty
and port["interface"] is different than args["serial"]
"""
port = {
"vid": "bar",
"pid": "foo",
"manufacturer": "Apple",
"serial_number": "456",
"interface": "bar",
}
args = {"vid": None, "pid": None, "vendor": None, "serial": None, "intf": "foo"}
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 = "foo"

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

Expand All @@ -109,14 +145,20 @@ def test_pass(self):
insure that is_serial_usb returns true if all test cases haven't
failed
"""
port = {
"vid": "bar",
"pid": "foo",
"manufacturer": "Apple",
"serial_number": "456",
"interface": "bar",
}
args = {"vid": None, "pid": None, "vendor": None, "serial": None, "intf": None}
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)

Expand All @@ -127,64 +169,70 @@ def test_manufacturer(self):
insure that the manufacturer is added to the
extra_items list if it is present in port
"""
port = {"manufacturer": "Microsoft"}
port = ListPortInfo()
port.manufacturer = "Microsoft"

response = extra_info(port)
self.assertTrue(port["manufacturer"] in response)
self.assertTrue(port.manufacturer in response)

def test_no_matches(self):
"""
insure that extra_info returns the empty string if
none of the keys match
"""
port = {"foo": "bar"}
port = ListPortInfo()
port.foo = "bar"

self.assertTrue(extra_info(port) == "")

def test_serial_number(self):
"""
insure that the serial_number is added to the
extra_items list if it is present in port
"""
port = {"serial_number": "123"}
port = ListPortInfo()
port.serial_number = "123"

response = extra_info(port)
self.assertTrue(port["serial_number"] in response)
self.assertTrue(port.serial_number in response)

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

response = extra_info(port)
self.assertTrue(port["interface"] in response)
self.assertTrue(port.interface in response)


class GetPortTests(SimpleTestCase):
@mock.patch("serial.tools.list_ports.comports")
def test_get_port_match(self, port_mocks):

port_mocks.return_value = [
{
"vid": "vid",
"pid": None,
"manufacturer": None,
"serial_number": None,
"interface": None,
"device": "usb",
}
]
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())

@mock.patch("serial.tools.list_ports.comports")
def test_get_port_empty(self, port_mocks):

port_mocks.return_value = [
{
"vid": None,
"pid": None,
"manufacturer": None,
"serial_number": None,
"interface": None,
"device": "usb",
}
]
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())