From dfaeda5d56a10d04cb010de5553302941d03deba Mon Sep 17 00:00:00 2001 From: deekshithanaga Date: Wed, 22 Oct 2025 20:46:51 -0400 Subject: [PATCH] Fix test_get_by_id_404 JSONDecodeError handling for HTML 404 responses --- app.py | 2 +- schemas.py | 2 +- test_pet.py | 27 +++++++++++++++++++++++---- test_store.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 71 insertions(+), 8 deletions(-) diff --git a/app.py b/app.py index 1925371..d932db3 100644 --- a/app.py +++ b/app.py @@ -98,7 +98,7 @@ def get(self): """Find pets by status""" status = request.args.get('status') if status not in PET_STATUS: - api.abort(400, 'Invalid pet status {status}') + api.abort(400, f'Invalid pet status {status}') if status: filtered_pets = [pet for pet in pets if pet['status'] == status] return filtered_pets diff --git a/schemas.py b/schemas.py index 946cb6c..760be60 100644 --- a/schemas.py +++ b/schemas.py @@ -6,7 +6,7 @@ "type": "integer" }, "name": { - "type": "integer" + "type": "string" }, "type": { "type": "string", diff --git a/test_pet.py b/test_pet.py index e215678..4778bfd 100644 --- a/test_pet.py +++ b/test_pet.py @@ -26,7 +26,7 @@ def test_pet_schema(): 3) Validate the 'status' property in the response is equal to the expected status 4) Validate the schema for each object in the response ''' -@pytest.mark.parametrize("status", [("available")]) +@pytest.mark.parametrize("status", ["available", "pending", "sold"]) def test_find_by_status_200(status): test_endpoint = "/pets/findByStatus" params = { @@ -34,6 +34,14 @@ def test_find_by_status_200(status): } response = api_helpers.get_api_data(test_endpoint, params) + + assert response.status_code == 200 + data = response.json() + assert isinstance(data, list) + + for pet in data: + validate(instance=pet, schema=schemas.pet) + assert pet["status"] == status # TODO... ''' @@ -41,6 +49,17 @@ def test_find_by_status_200(status): 1) Testing and validating the appropriate 404 response for /pets/{pet_id} 2) Parameterizing the test for any edge cases ''' -def test_get_by_id_404(): - # TODO... - pass \ No newline at end of file +@pytest.mark.parametrize("pet_id", [999999, -1, 123456789]) +def test_get_by_id_404(pet_id): + response = api_helpers.get_api_data(f"/pets/{pet_id}") + assert response.status_code == 404 + + # Check if response contains JSON; if not, fall back to text + try: + body = response.json() + assert "message" in body + assert_that(body["message"], contains_string(str(pet_id))) + except ValueError: # JSONDecodeError inherits from ValueError + # API returned HTML instead of JSON + body_text = response.text.lower() + assert "not found" in body_text, f"Unexpected 404 body: {body_text}" diff --git a/test_store.py b/test_store.py index 186bd79..f14a3c0 100644 --- a/test_store.py +++ b/test_store.py @@ -12,5 +12,49 @@ 3) Validate the response codes and values 4) Validate the response message "Order and pet status updated successfully" ''' -def test_patch_order_by_id(): - pass +@pytest.fixture +def fresh_available_pet_id(): + import time + new_id = int(10_000 + (time.time() % 100_000)) + new_pet = { + "id": new_id, + "name": f"tester-{new_id}", + "type": "dog", + "status": "available", + } + resp = api_helpers.post_api_data("/pets/", new_pet) + + assert resp.status_code in (201, 409) + return new_id + +def test_patch_order_by_id(fresh_available_pet_id): + + post_resp = api_helpers.post_api_data("/store/order", {"pet_id": fresh_available_pet_id}) + assert post_resp.status_code == 201 + order = post_resp.json() + assert "id" in order + order_id = order["id"] + + + patch_resp = api_helpers.patch_api_data(f"/store/order/{order_id}", {"status": "sold"}) + assert patch_resp.status_code == 200 + assert patch_resp.json().get("message") == "Order and pet status updated successfully" + + + pet_resp = api_helpers.get_api_data(f"/pets/{fresh_available_pet_id}") + assert pet_resp.status_code == 200 + assert pet_resp.json()["status"] == "sold" + +def test_patch_order_invalid_status(fresh_available_pet_id): + + post_resp = api_helpers.post_api_data("/store/order", {"pet_id": fresh_available_pet_id}) + assert post_resp.status_code == 201 + order_id = post_resp.json()["id"] + + + bad = api_helpers.patch_api_data(f"/store/order/{order_id}", {"status": "bogus"}) + assert bad.status_code == 400 + +def test_patch_order_not_found(): + resp = api_helpers.patch_api_data("/store/order/does-not-exist", {"status": "sold"}) + assert resp.status_code == 404 \ No newline at end of file