From 0c977d0f91c584da5fa97e3891219608bbbc31c4 Mon Sep 17 00:00:00 2001 From: Naveen V <94vnaveen@gmail.com> Date: Mon, 17 Nov 2025 19:19:59 -0500 Subject: [PATCH] fixed schema and updated test cases --- schemas.py | 23 ++++++++++++++++++++++- test_pet.py | 32 +++++++++++++++++++++++++++----- test_store.py | 45 +++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 92 insertions(+), 8 deletions(-) diff --git a/schemas.py b/schemas.py index 946cb6c..99fbc29 100644 --- a/schemas.py +++ b/schemas.py @@ -6,7 +6,7 @@ "type": "integer" }, "name": { - "type": "integer" + "type": "string" }, "type": { "type": "string", @@ -18,3 +18,24 @@ }, } } + +order = { + "type": "object", + "required": ["pet_id"], + "properties": { + "id": { + "type": "string" + }, + "pet_id": { + "type": "integer" + }, + "status": { + "type": "string", + "enum": ["pending", "sold", "available"] + }, + "complete": { + "type": "boolean" + } + }, + "additionalProperties": True +} diff --git a/test_pet.py b/test_pet.py index e215678..fab4a28 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", "sold", "pending"]) def test_find_by_status_200(status): test_endpoint = "/pets/findByStatus" params = { @@ -34,13 +34,35 @@ def test_find_by_status_200(status): } response = api_helpers.get_api_data(test_endpoint, params) - # TODO... + + # Validate response code + assert response.status_code == 200 + + # Validate each pet's status and schema + for pet in response.json(): + assert pet["status"] == status + validate(instance=pet, schema=schemas.pet) + assert_that(pet.get("status"), is_(status)) ''' TODO: Finish this test by... 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", [5]) +def test_get_by_id_404(pet_id): + test_endpoint = f"/pets/{pet_id}" + + response = api_helpers.get_api_data(test_endpoint) + + # Validate response code + assert response.status_code == 404 + + # Validate error message + try: + response_body = response.json() + if(isinstance(response_body, dict)): + error_message = response_body.get("message", "") + assert "Pet not found" in error_message or f"Pet with ID {pet_id}" in error_message + except Exception: + pytest.fail(f"404: request failed to return valid JSON response") \ No newline at end of file diff --git a/test_store.py b/test_store.py index 186bd79..a44e3e7 100644 --- a/test_store.py +++ b/test_store.py @@ -2,6 +2,9 @@ import pytest import schemas import api_helpers +import uuid +import random +from app import PET_TYPE from hamcrest import assert_that, contains_string, is_ ''' @@ -12,5 +15,43 @@ 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 create_pet(): + endpoint = "/pets/" + params = { + "id" : uuid.uuid4().int, + "name" : str(uuid.uuid4())[:6], + "type": random.choice(PET_TYPE), + "status": "available" + } + + response = api_helpers.post_api_data(endpoint, params) + pet = response.json() + return pet + +@pytest.fixture +def create_order(create_pet): + # Create a new order to be used in the test + endpoint = "/store/order" + request_body = { + "pet_id": create_pet["id"], + } + + response = api_helpers.post_api_data(endpoint, request_body) + validate(instance=response.json(), schema=schemas.order) + return response.json() + + +@pytest.mark.parametrize("status", ['available', 'sold']) +def test_patch_order_by_id(status, create_order): + endpoint = f"/store/order/{create_order["id"]}" + request_body = { + "status": status + } + + response = api_helpers.patch_api_data(endpoint, request_body) + print(response.json()) + + assert_that(response.status_code, is_(200)) + assert_that(response.json().get("message"), is_("Order and pet status updated successfully")) \ No newline at end of file