def test_200_invalid_json(self, mock_request, logger_debug, logger_warning): """Given a valid response but invalid json, JSONDecodeError is raised.""" mock_request.get(board_database._BOARD_API, text="some text") with self.assertRaises(board_database.ResponseJSONError): board_database.get_online_board_data() self.assertTrue("Invalid JSON" in str(logger_warning.call_args), "Invalid JSON should be mentioned") self.assertTrue("some text" in str(logger_debug.call_args), "Message content should be in the debug message")
def test_404(self, mock_request, logger_debug, logger_warning): """Given a 404 error code, TargetAPIError is raised.""" mock_request.get(board_database._BOARD_API, status_code=404, text="Not Found") with self.assertRaises(board_database.BoardAPIError): board_database.get_online_board_data() self.assertTrue("404" in str(logger_warning.call_args), "HTTP status code should be mentioned") self.assertTrue("Not Found" in str(logger_debug.call_args), "Message content should be in the debug message")
def test_401(self, mock_request, logger_debug, logger_warning): """Given a 401 error code, BoardAPIError is raised.""" mock_request.get(board_database._BOARD_API, status_code=401, text="Who are you?") with self.assertRaises(board_database.BoardAPIError): board_database.get_online_board_data() self.assertTrue("MBED_API_AUTH_TOKEN" in str(logger_warning.call_args), "Auth token should be mentioned") self.assertTrue("Who are you?" in str(logger_debug.call_args), "Message content should be in the debug message")
def test_200_no_data_field(self, caplog, requests_mock): """Given a valid response but no data field, ResponseJSONError is raised.""" caplog.set_level(logging.DEBUG) requests_mock.get(board_database._BOARD_API, json={"notdata": [], "stillnotdata": {}}) with pytest.raises(board_database.ResponseJSONError): board_database.get_online_board_data() assert any( x for x in caplog.records if x.levelno == logging.WARNING and "missing the 'data' field" in x.msg ), "Data field should be mentioned" assert any( x for x in caplog.records if x.levelno == logging.DEBUG and "notdata, stillnotdata" in x.msg ), "JSON keys from message should be in the debug message"
def test_200_invalid_json(self, caplog, requests_mock): """Given a valid response but invalid json, JSONDecodeError is raised.""" caplog.set_level(logging.DEBUG) requests_mock.get(board_database._BOARD_API, text="some text") with pytest.raises(board_database.ResponseJSONError): board_database.get_online_board_data() assert any( x for x in caplog.records if x.levelno == logging.WARNING and "Invalid JSON" in x.msg ), "Invalid JSON should be mentioned" assert any( x for x in caplog.records if x.levelno == logging.DEBUG and "some text" in x.msg ), "Message content should be in the debug message"
def test_404(self, caplog, requests_mock): """Given a 404 error code, TargetAPIError is raised.""" caplog.set_level(logging.DEBUG) requests_mock.get(board_database._BOARD_API, status_code=404, text="Not Found") with pytest.raises(board_database.BoardAPIError): board_database.get_online_board_data() assert any( x for x in caplog.records if x.levelno == logging.WARNING and "404" in x.msg ), "HTTP status code should be mentioned" assert any( x for x in caplog.records if x.levelno == logging.DEBUG and "Not Found" in x.msg ), "Message content should be in the debug message"
def test_401(self, caplog, requests_mock): """Given a 401 error code, BoardAPIError is raised.""" caplog.set_level(logging.DEBUG) requests_mock.get(board_database._BOARD_API, status_code=401, text="Who are you?") with pytest.raises(board_database.BoardAPIError): board_database.get_online_board_data() assert any( x for x in caplog.records if x.levelno == logging.WARNING and "MBED_API_AUTH_TOKEN" in x.msg ), "Auth token should be mentioned" assert any( x for x in caplog.records if x.levelno == logging.DEBUG and "Who are you?" in x.msg ), "Message content should be in the debug message"
def test_200_value_data(self, mock_request): """Given a valid response, target data is set from the returned json.""" mock_request.get(board_database._BOARD_API, json={"data": 42}) board_data = board_database.get_online_board_data() self.assertEqual( 42, board_data, "Target data should match the contents of the target API data")
def test_200_no_data_field(self, mock_request, logger_debug, logger_warning): """Given a valid response but no data field, ResponseJSONError is raised.""" mock_request.get(board_database._BOARD_API, json={ "notdata": [], "stillnotdata": {} }) with self.assertRaises(board_database.ResponseJSONError): board_database.get_online_board_data() self.assertTrue( "missing the 'data' field" in str(logger_warning.call_args), "Data field should be mentioned") self.assertTrue( "notdata, stillnotdata" in str(logger_debug.call_args), "JSON keys from message should be in the debug message", )
def from_online_database(cls) -> "Boards": """Initialise with the online board database. Raises: BoardDatabaseError: Could not retrieve data from the board database. """ return cls( Board.from_online_board_entry(b) for b in board_database.get_online_board_data())
def test_200_value_data(self, requests_mock): """Given a valid response, target data is set from the returned json.""" requests_mock.get(board_database._BOARD_API, json={"data": 42}) board_data = board_database.get_online_board_data() assert 42 == board_data, "Target data should match the contents of the target API data"