From 854984c452050e28d5173269ad45070b3c76948f Mon Sep 17 00:00:00 2001 From: ramven912-cpu Date: Fri, 14 Nov 2025 13:59:45 -0500 Subject: [PATCH 1/4] Update comments for clarity in app.py --- app.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app.py b/app.py index 1925371..8aadd8c 100644 --- a/app.py +++ b/app.py @@ -52,7 +52,7 @@ ''' Pet Namespace ''' -# Get a list of all pets +# Get a list of all pets from the store @pet_ns.route('/') @@ -103,7 +103,7 @@ def get(self): filtered_pets = [pet for pet in pets if pet['status'] == status] return filtered_pets -# Store Namespace +# Store Namespace -ref @store_ns.route('/order') class OrderResource(Resource): @store_ns.doc('place_order') @@ -148,12 +148,12 @@ def patch(self, order_id): pet = next((pet for pet in pets if pet['id'] == pet_id), None) if pet is None: - api.abort(404, f"No pet found with ID {pet_id}") + api.abort(404, f"No pets found with ID {pet_id}") - # Update the order status + # Updating the order status order['status'] = update_data['status'] - # Update the pet's status based on the order's new status + # Updat the pet's status based on the order's new status if update_data['status'] == 'pending': pet['status'] = 'pending' elif update_data['status'] == 'sold': From aa52d179e4bf58d373fadf6ee350c5cb6dc54431 Mon Sep 17 00:00:00 2001 From: ramven912-cpu Date: Sun, 16 Nov 2025 20:11:00 -0500 Subject: [PATCH 2/4] order added --- schemas.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/schemas.py b/schemas.py index 946cb6c..8c80ed4 100644 --- a/schemas.py +++ b/schemas.py @@ -6,7 +6,7 @@ "type": "integer" }, "name": { - "type": "integer" + "type": "string" }, "type": { "type": "string", @@ -18,3 +18,16 @@ }, } } + +order = { + "type": "object", + "required": ["pet_id"], + "properties": { + "id": { + "type": "string" + }, + "pet_id": { + "type": "integer" + } + } +} From c51e544ed42442f2e4a4530ff5d0ab7243247c82 Mon Sep 17 00:00:00 2001 From: ramven912-cpu Date: Sun, 16 Nov 2025 20:12:03 -0500 Subject: [PATCH 3/4] pet tests with parameterization and validation Refactor tests to include additional status cases and validate responses. --- test_pet.py | 37 ++++++++++++++----------------------- 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/test_pet.py b/test_pet.py index e215678..d8eb7c2 100644 --- a/test_pet.py +++ b/test_pet.py @@ -4,11 +4,6 @@ import api_helpers from hamcrest import assert_that, contains_string, is_ -''' -TODO: Finish this test by... -1) Troubleshooting and fixing the test failure -The purpose of this test is to validate the response matches the expected schema defined in schemas.py -''' def test_pet_schema(): test_endpoint = "/pets/1" @@ -19,14 +14,7 @@ def test_pet_schema(): # Validate the response schema against the defined schema in schemas.py validate(instance=response.json(), schema=schemas.pet) -''' -TODO: Finish this test by... -1) Extending the parameterization to include all available statuses -2) Validate the appropriate response code -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 +22,16 @@ def test_find_by_status_200(status): } response = api_helpers.get_api_data(test_endpoint, params) - # TODO... - -''' -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 + + assert response.status_code == 200 + + for pet in response.json(): + validate(instance=pet, schema=schemas.pet) + assert pet['status'] == status + +@pytest.mark.parametrize("pet_id", [-1, 1.5, 9999999999, "ranger", True, " "]) +def test_get_by_id_404(pet_id): + test_endpoint = f"/pets/{pet_id}" + response = api_helpers.get_api_data(test_endpoint) + + assert response.status_code == 404 From 2bf3faed0c663365f8673d1913a55f7566998cae Mon Sep 17 00:00:00 2001 From: ramven912-cpu Date: Sun, 16 Nov 2025 20:12:44 -0500 Subject: [PATCH 4/4] Implement tests for order status updates Add tests for PATCH request to update order status --- test_store.py | 70 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 60 insertions(+), 10 deletions(-) diff --git a/test_store.py b/test_store.py index 186bd79..4aef43f 100644 --- a/test_store.py +++ b/test_store.py @@ -4,13 +4,63 @@ import api_helpers from hamcrest import assert_that, contains_string, is_ -''' -TODO: Finish this test by... -1) Creating a function to test the PATCH request /store/order/{order_id} -2) *Optional* Consider using @pytest.fixture to create unique test data for each run -2) *Optional* Consider creating an 'Order' model in schemas.py and validating it in the test -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 new_pet(): + all_pets = api_helpers.get_api_data("/pets/") + + # ASSUMPTION: pet_id = len(pets) will always be a free index. + pet_data = { + "id": len(all_pets.json()), + "name": "Test Cat", + "type": "cat", + "status": "available" + } + pet_response = api_helpers.post_api_data("/pets/", pet_data) + + assert pet_response.status_code == 201 + return pet_response.json() + +@pytest.fixture +def new_order(new_pet): + pet_data = { + "pet_id": new_pet["id"] + } + order_response = api_helpers.post_api_data("/store/order", pet_data) + + assert order_response.status_code == 201 + validate(instance=order_response.json(), schema=schemas.order) + return order_response.json() + +test_patch_parameters = [ + (None, "available", 200), + (None, "sold", 200), + (None, "pending", 200), + + (None, 0, 400), + (None, -1, 400), + (None, "", 400), + + ("None", "available", 404), + (" ", "sold", 404), + (0, "available", 404), + ("e0862a12-17a3-49eb-8f69-a5d8997f8349", "pending", 404), +] +@pytest.mark.parametrize("order_id,status,response_code", test_patch_parameters) +def test_patch_order_by_id(new_order, order_id, status, response_code): + if order_id is None: + order_id = new_order['id'] + + test_endpoint = f"/store/order/{order_id}" + data = {'status': status} + + response = api_helpers.patch_api_data(test_endpoint, data) + + assert response.status_code == response_code + expected_message = None + if response_code == 200: + expected_message = "Order and pet status updated successfully" + elif response_code == 400: + expected_message = f"Invalid status '{status}'. Valid statuses are available, sold, pending" + else: + expected_message = "Order not found" + assert expected_message in response.json()['message']