def get_board(matching: Callable) -> Board: """Returns first `mbed_tools.targets.board.Board` for which `matching` is True. Uses database mode configured in the environment. Args: matching: A function which will be called to test matching conditions for each board in database. Raises: UnknownBoard: a board matching the criteria could not be found in the board database. """ database_mode = _get_database_mode() if database_mode == _DatabaseMode.OFFLINE: logger.info("Using the offline database (only) to identify boards.") return Boards.from_offline_database().get_board(matching) if database_mode == _DatabaseMode.ONLINE: logger.info("Using the online database (only) to identify boards.") return Boards.from_online_database().get_board(matching) try: logger.info("Using the offline database to identify boards.") return Boards.from_offline_database().get_board(matching) except UnknownBoard: logger.info( "Unable to identify a board using the offline database, trying the online database." ) try: return Boards.from_online_database().get_board(matching) except BoardDatabaseError: logger.error( "Unable to access the online database to identify a board.") raise UnknownBoard()
def test_get_board_success(self, mocked_get_board_data): """Check a Board can be looked up by arbitrary parameters.""" fake_board_data = [ { "attributes": { "product_code": "0300" } }, { "attributes": { "product_code": "0200" } }, { "attributes": { "product_code": "0100" } }, ] mocked_get_board_data.return_value = fake_board_data boards = Boards.from_online_database() board = boards.get_board(lambda b: b.product_code == "0100") self.assertEqual( board.product_code, "0100", "Board's product code should match the given product code.")
def main(args: argparse.Namespace) -> int: """Main entry point.""" set_log_level(args.verbose) pr_info = PullRequestInfo( repo="ARMMbed/mbed-targets", head_branch=args.head_branch, base_branch=args.base_branch, subject=args.pr_subject, body=args.pr_description, ) try: online_boards = Boards.from_online_database() if BOARD_DATABASE_PATH.exists(): offline_boards = Boards.from_offline_database() added, removed = get_boards_added_or_removed( offline_boards, online_boards) if not (added or removed): logger.info("No changes to commit. Exiting.") return 0 news_file_path = write_news_file_from_boards(added, removed) else: news_file_path = create_news_file( "Offline board database updated.", NewsType.feature) save_board_database(online_boards.json_dump(), BOARD_DATABASE_PATH) git_commit_and_push([BOARD_DATABASE_PATH, news_file_path], pr_info.head_branch, pr_info.subject) raise_github_pr(pr_info) return 0 except ToolsError as tools_error: log_exception(logger, tools_error) return 1
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_get_board_failure(self, mocked_get_board_data): """Check Boards handles queries without a match.""" mocked_get_board_data.return_value = [] boards = Boards.from_online_database() with self.assertRaises(UnknownBoard): boards.get_board(lambda b: b.product_code == "unknown")
def test_membership_test_returns_false_for_non_board(self, mocked_get_board_data): """Tests the __contains__ method was implemented correctly.""" board_data = make_dummy_internal_board_data() mocked_get_board_data.return_value = board_data boards = Boards.from_online_database() self.assertFalse("a" in boards)
def test_board_found_in_boards_membership_test(self, mocked_get_board_data): """Tests the __contains__ method was implemented correctly.""" board_data = make_dummy_internal_board_data() mocked_get_board_data.return_value = board_data boards = Boards.from_online_database() board, *_ = boards self.assertIn(board, boards)
def test_iteration_is_repeatable(self, mocked_get_board_data): """Test Boards is an iterable and not an exhaustible iterator.""" fake_board_data = make_dummy_internal_board_data() mocked_get_board_data.return_value = fake_board_data boards = Boards.from_online_database() tgts_a = [b for b in boards] tgts_b = [b for b in boards] self.assertEqual(tgts_a, tgts_b, "The lists are equal as boards was not exhausted on the first pass.")
def main(args: argparse.Namespace) -> int: """Main entry point.""" set_log_level(args.verbose) try: online_boards = Boards.from_online_database() offline_boards = Boards.from_offline_database() result = compare_databases(offline_boards, online_boards) if not (result.boards_added or result.boards_removed or result.boards_modified): logger.info("No changes to commit. Exiting.") return 0 news_file_text = create_news_file_text_from_result(result) create_news_file(news_file_text, NewsType.feature) save_board_database(online_boards.json_dump(), BOARD_DATABASE_PATH) return 0 except ToolsError as tools_error: log_exception(logger, tools_error) return 1
def test_len_boards(self, mocked_get_board_data): """Test len(Boards()) matches len(board_database).""" board_data = make_dummy_internal_board_data() mocked_get_board_data.return_value = board_data self.assertEqual(len(Boards.from_online_database()), len(board_data))