From 22aede4ccc49356bc3f20cb74d43f0dbbd4c45cd Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Sat, 22 Feb 2025 21:40:37 +0800 Subject: [PATCH 1/2] tests/unit: Fix exception verification in json2code_test.py Previously, exception verification code was placed after the function call within assertRaises(), making it unreachable. This caused the test to pass without actually verifying the exception details. Move the verification code outside the assertRaises() block by capturing the exception first. This ensures the test properly validates both the exception type and its attributes. Example: Before: ```py with self.assertRaises(ValueError): function_call() self.assertEqual(exc.message, "expected message") # Never executed ``` After: ```py with self.assertRaises(ValueError) as e: function_call() self.assertEqual(e.exception.message, "expected message") # Executed ``` Signed-off-by: Kefu Chai --- tests/unit/json2code_test.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/unit/json2code_test.py b/tests/unit/json2code_test.py index 46c912bb76..6689c0c4d5 100755 --- a/tests/unit/json2code_test.py +++ b/tests/unit/json2code_test.py @@ -79,11 +79,11 @@ def test_missing_path_param(self): with self.assertRaises(urllib.error.HTTPError) as e: with urllib.request.urlopen(url): pass - ex = e.exception - self.assertEqual(ex.code, 404) - response = json.loads(ex.read().decode('utf-8')) - self.assertEqual(response['message'], 'Not found') - self.assertEqual(response['code'], 404) + ex = e.exception + self.assertEqual(ex.code, 404) + response = json.loads(ex.read().decode('utf-8')) + self.assertEqual(response['message'], 'Not found') + self.assertEqual(response['code'], 404) if __name__ == '__main__': From 26c287ffede4ce95270138652f804f5313eb18a6 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Sat, 22 Feb 2025 21:34:27 +0800 Subject: [PATCH 2/2] tests/unit: Use http.HTTPStatus constants instead of raw status codes Replace hardcoded HTTP status code 404 with http.HTTPStatus.NOT_FOUND to improve code readability and maintainability. Using symbolic constants makes the test's intent clearer and helps prevent typos. Before: self.assertEqual(response.status_code, 404) After: self.assertEqual(response.status_code, http.HTTPStatus.NOT_FOUND) Signed-off-by: Kefu Chai --- tests/unit/json2code_test.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/unit/json2code_test.py b/tests/unit/json2code_test.py index 6689c0c4d5..05560b0bb4 100755 --- a/tests/unit/json2code_test.py +++ b/tests/unit/json2code_test.py @@ -27,6 +27,7 @@ import unittest import urllib.request import urllib.parse +from http import HTTPStatus class TestJson2Code(unittest.TestCase): @@ -80,10 +81,10 @@ def test_missing_path_param(self): with urllib.request.urlopen(url): pass ex = e.exception - self.assertEqual(ex.code, 404) + self.assertEqual(ex.code, HTTPStatus.NOT_FOUND) response = json.loads(ex.read().decode('utf-8')) self.assertEqual(response['message'], 'Not found') - self.assertEqual(response['code'], 404) + self.assertEqual(response['code'], HTTPStatus.NOT_FOUND) if __name__ == '__main__':