"""传输层与错误解析的单元测试(不联网)。""" from __future__ import annotations import unittest from qtimport.errors import Category, QtError, extract_exception_message from qtimport.transport import Transport WCF_ERROR_PAGE = """\ufeff

Request Error

The server encountered an error processing the request. The exception message is 'Could not find stored procedure 'QuickTeam.usp_Nope'.'. See server logs for more details.

""" class DecodeTest(unittest.TestCase): def test_plain_json(self) -> None: self.assertEqual(Transport.decode('[{"A":1}]'), [{"A": 1}]) def test_double_encoded(self) -> None: raw = '"[{\\"WorkSheetId\\": 225072}]"' self.assertEqual(Transport.decode(raw)[0]["WorkSheetId"], 225072) def test_triple_encoded(self) -> None: raw = '"\\"[{\\\\\\"A\\\\\\": 1}]\\""' self.assertEqual(Transport.decode(raw), [{"A": 1}]) def test_scalar_string(self) -> None: self.assertEqual(Transport.decode('"0"'), 0) def test_not_json(self) -> None: self.assertEqual(Transport.decode(""), "") class ErrorTest(unittest.TestCase): def test_extract_message(self) -> None: message = extract_exception_message(WCF_ERROR_PAGE) self.assertIn("Could not find stored procedure", message) def test_missing_procedure_not_retryable(self) -> None: error = QtError.from_http(400, WCF_ERROR_PAGE) self.assertEqual(error.category, Category.SP_MISSING) self.assertFalse(error.retryable) def test_param_error(self) -> None: body = "

The exception message is 'Procedure or function usp_X has too many arguments " body += "specified.'. See server logs

" self.assertEqual(QtError.from_http(400, body).category, Category.PARAM) def test_server_error_retryable(self) -> None: error = QtError.from_http(503, "

Service Unavailable

") self.assertTrue(error.retryable) def test_html_unescaped(self) -> None: body = "

The exception message is 'Parameter 'UserId' is missing.'. See server logs

" self.assertIn("UserId", extract_exception_message(body)) if __name__ == "__main__": unittest.main()