Problem details test case 51/14251/2
authorSwaraj Kumar <swaraj.kumar@samsung.com>
Wed, 12 Mar 2025 12:54:15 +0000 (18:24 +0530)
committerSwaraj Kumar <swaraj.kumar@samsung.com>
Mon, 17 Mar 2025 05:56:13 +0000 (05:56 +0000)
Change-Id: Ifc069d399a2c12e632b109e65a78b0533e8e7742
Signed-off-by: Swaraj Kumar <swaraj.kumar@samsung.com>
tests/test_problemdetails.py

index c44d5eb..dad3fa4 100644 (file)
@@ -20,3 +20,33 @@ def test_problem_details_initialization():
     assert problem.title == "Bad Request"
     assert problem.detail == "Invalid input data"
 
+def test_problem_details_to_dict():
+    """
+    Test that ProblemDetails generates the correct dictionary representation.
+    """
+    problem = ProblemDetails(404, "Not Found", "The requested resource does not exist.")
+    expected_dict = {
+        "title": "Not Found",
+        "status": 404,
+        "detail": "The requested resource does not exist."
+    }
+    assert problem.to_dict() == expected_dict
+
+def test_problem_details_to_json(test_app):
+    """
+    Test that ProblemDetails generates the correct Flask JSON response.
+    """
+    problem = ProblemDetails(500, "Internal Server Error", "Something went wrong")
+    with Flask(__name__).test_request_context():
+        response, status, headers = problem.to_json()
+    assert status == 500
+    assert headers["Content-Type"] == "application/problem+json"
+    # Convert response data to JSON and compare
+    response_data = json.loads(response.get_data(as_text=True))
+    expected_json = {
+        "title": "Internal Server Error",
+        "status": 500,
+        "detail": "Something went wrong"
+    }
+    assert response_data == expected_json
+