def test_news_file_item_text_formatting(self): with mock.patch("ci_scripts.sync_board_database.Path", autospec=True) as mock_path: mock_path().exists.return_value = False text = sync_board_database.create_news_item_text( "New boards added:", Boards([Board.from_online_board_entry(BOARD_1), Board.from_online_board_entry(BOARD_2)]), ) self.assertEqual(text, "New boards added: u-blox NINA-B1, Multitech xDOT", "Text is formatted correctly.")
def create_fake_device( serial_number="8675309", serial_port="/dev/ttyUSB/default", mount_points=(pathlib.Path("/media/you/DISCO"), pathlib.Path("/media/you/NUCLEO")), interface_version="VA3JME", board_type="BoardType", board_name="BoardName", product_code="0786", target_type="TargetType", slug="slug", build_variant=("NS", "S"), mbed_os_support=("mbed1", "mbed2"), mbed_enabled=("baseline", "extended"), ): device_attrs = { "serial_number": serial_number, "serial_port": serial_port, "mount_points": mount_points, "interface_version": interface_version, } board_attrs = { "board_type": board_type, "board_name": board_name, "product_code": product_code, "target_type": target_type, "slug": slug, "build_variant": build_variant, "mbed_os_support": mbed_os_support, "mbed_enabled": mbed_enabled, } return Device(Board(**board_attrs), **device_attrs)
def from_candidate(cls, candidate: CandidateDevice) -> "Device": """Contruct a Device from a CandidateDevice. We try to resolve a board using data files that may be stored on the CandidateDevice. If this fails we set the board to `None` which means we couldn't verify this Device as being an Mbed enabled device. Args: candidate: The CandidateDevice we're using to create the Device. """ device_file_info = read_device_files(candidate.mount_points) try: mbed_board = resolve_board(device_file_info.product_code, device_file_info.online_id, candidate.serial_number) mbed_enabled = True except NoBoardForCandidate: # Create an empty Board to ensure the device is fully populated and rendering is simple mbed_board = Board.from_offline_board_entry({}) mbed_enabled = False except ResolveBoardError: raise DeviceLookupFailed( f"Failed to resolve the board for candidate device {candidate!r}. There was a problem looking up the " "board data in the database.") return Device( serial_port=candidate.serial_port, serial_number=candidate.serial_number, mount_points=candidate.mount_points, mbed_board=mbed_board, mbed_enabled=mbed_enabled, interface_version=device_file_info.interface_details.get( "Version"), )
def test_offline_database_entry(self): """Given an entry from the offline database, a Board is generated with the correct information.""" board = Board.from_offline_board_entry({ "mbed_os_support": ["Mbed OS 5.15"], "mbed_enabled": ["Basic"], "board_type": "B_1", "board_name": "Board 1", "product_code": "P1", "target_type": "platform", "slug": "Le Slug", }) self.assertEqual("B_1", board.board_type) self.assertEqual("Board 1", board.board_name) self.assertEqual(("Mbed OS 5.15", ), board.mbed_os_support) self.assertEqual(("Basic", ), board.mbed_enabled) self.assertEqual("P1", board.product_code) self.assertEqual("platform", board.target_type) self.assertEqual("Le Slug", board.slug) self.assertEqual((), board.build_variant)
def test_json_dump_from_raw_and_filtered_data(self, mocked_get_offline_board_data, mocked_get_online_board_data): raw_board_data = [ {"attributes": {"product_code": "0200", "board": "test"}}, {"attributes": {"product_code": "0100", "board": "test2"}}, ] mocked_get_online_board_data.return_value = raw_board_data boards = [Board.from_online_board_entry(b) for b in raw_board_data] filtered_board_data = [asdict(board) for board in boards] mocked_get_offline_board_data.return_value = filtered_board_data # Boards.from_online_database handles "raw" board entries from the online db boards = Boards.from_online_database() json_str_from_raw = boards.json_dump() t1_raw, t2_raw = boards # Boards.from_offline_database expects the data to have been "filtered" through the Boards interface offline_boards = Boards.from_offline_database() json_str_from_filtered = offline_boards.json_dump() t1_filt, t2_filt = offline_boards self.assertEqual( json_str_from_raw, json.dumps([asdict(t1_raw), asdict(t2_raw)], indent=4), "JSON string should match serialised board __dict__.", ) self.assertEqual(json_str_from_filtered, json.dumps([t1_filt.__dict__, t2_filt.__dict__], indent=4))
def test_online_database_entry(self): online_data = { "type": "target", "id": "1", "attributes": { "features": { "mbed_enabled": ["Advanced"], "mbed_os_support": [ "Mbed OS 5.10", "Mbed OS 5.11", "Mbed OS 5.12", "Mbed OS 5.13", "Mbed OS 5.14", "Mbed OS 5.15", "Mbed OS 5.8", "Mbed OS 5.9", ], "antenna": ["Connector", "Onboard"], "certification": [ "Anatel (Brazil)", "AS/NZS (Australia and New Zealand)", "CE (Europe)", "FCC/CFR (USA)", "IC RSS (Canada)", "ICASA (South Africa)", "KCC (South Korea)", "MIC (Japan)", "NCC (Taiwan)", "RoHS (Europe)", ], "communication": ["Bluetooth & BLE"], "interface_firmware": ["DAPLink", "J-Link"], "target_core": ["Cortex-M4"], "mbed_studio_support": ["Build and run"], }, "board_type": "k64f", "flash_size": 512, "name": "u-blox NINA-B1", "product_code": "0455", "ram_size": 64, "target_type": "module", "hidden": False, "device_name": "nRF52832_xxAA", "slug": "u-blox-nina-b1", }, } board = Board.from_online_board_entry(online_data) self.assertEqual(online_data["attributes"]["board_type"].upper(), board.board_type) self.assertEqual(online_data["attributes"]["name"], board.board_name) self.assertEqual(tuple(online_data["attributes"]["features"]["mbed_os_support"]), board.mbed_os_support) self.assertEqual(tuple(online_data["attributes"]["features"]["mbed_enabled"]), board.mbed_enabled) self.assertEqual(online_data["attributes"]["product_code"], board.product_code) self.assertEqual(online_data["attributes"]["target_type"], board.target_type) self.assertEqual(online_data["attributes"]["slug"], board.slug) self.assertEqual(tuple(), board.build_variant)
def test_empty_database_entry(self): """Given no data, a Board is created with no information.""" board = Board.from_online_board_entry({}) self.assertEqual("", board.board_type) self.assertEqual("", board.board_name) self.assertEqual((), board.mbed_os_support) self.assertEqual((), board.mbed_enabled) self.assertEqual("", board.product_code) self.assertEqual("", board.target_type) self.assertEqual("", board.slug)
def test_empty_values_keys_are_always_present(self): """Asserts that keys are present even if value is None.""" device = Device( mbed_board=Board.from_offline_board_entry({}), serial_number="foo", serial_port=None, mount_points=[], ) output = json.loads(_build_json_output([device])) self.assertIsNone(output[0]["serial_port"])
def make_board( board_type="BoardType", board_name="BoardName", mbed_os_support=None, mbed_enabled=None, product_code="9999", slug="BoardSlug", target_type="TargetType", ): return Board( board_type=board_type, product_code=product_code, board_name=board_name, target_type=target_type, slug=slug, mbed_os_support=mbed_os_support if mbed_os_support else (), mbed_enabled=mbed_enabled if mbed_enabled else (), build_variant=(), )
def add_device(self, candidate_device: CandidateDevice, mbed_board: Optional[Board] = None) -> None: """Add a candidate device and optionally an Mbed Target to the connected devices. Args: candidate_device: a CandidateDevice object containing the device information. mbed_board: a Board object for identified devices, for unidentified devices this will be None. """ new_device = Device( serial_port=candidate_device.serial_port, serial_number=candidate_device.serial_number, mount_points=candidate_device.mount_points, # Create an empty Board to ensure the device is fully populated and rendering is simple mbed_board=mbed_board if mbed_board is not None else Board.from_offline_board_entry({}), ) if mbed_board is None: # Keep a list of devices that could not be identified but are Mbed Boards self.unidentified_devices.append(new_device) else: # Keep a list of devices that have been identified as Mbed Boards self.identified_devices.append(new_device)
def test_displays_unknown_serial_port_value(self): device = Device( mbed_board=Board.from_offline_board_entry({}), serial_number="serial", serial_port=None, mount_points=[pathlib.Path("somepath")], ) output = _build_tabular_output([device]) expected_output = tabulate( [[ "<unknown>", device.serial_number, "<unknown>", "\n".join(map(str, device.mount_points)), "\n".join(_get_build_targets(device.mbed_board)), ]], headers=[ "Board name", "Serial number", "Serial port", "Mount point(s)", "Build target(s)" ], ) self.assertEqual(output, expected_output)
def _make_mbed_boards_for_diff(boards_a, boards_b): return ( Boards(Board.from_online_board_entry(b) for b in boards_a), Boards(Board.from_online_board_entry(b) for b in boards_b), )