From b291396447d00656ccc8bad019944517e5a82879 Mon Sep 17 00:00:00 2001 From: Eric Goodman Date: Wed, 14 Jan 2026 17:54:55 -0500 Subject: [PATCH 1/2] Eric Goodman updates to tests. --- schemas.py | 13 ++++++++++++- test_pet.py | 33 ++++++++++++++++++++++++++++----- test_store.py | 42 ++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 80 insertions(+), 8 deletions(-) diff --git a/schemas.py b/schemas.py index 946cb6c..c3db661 100644 --- a/schemas.py +++ b/schemas.py @@ -1,3 +1,4 @@ +''' Noticed the name in the schema was incorrectly marked as an integer''' pet = { "type": "object", "required": ["name", "type"], @@ -6,7 +7,7 @@ "type": "integer" }, "name": { - "type": "integer" + "type": "string" }, "type": { "type": "string", @@ -18,3 +19,13 @@ }, } } + +order = { + "type": "object", + "properties": { + "message": { + "type": "string" + } + }, + "required": ["message"] +} \ No newline at end of file diff --git a/test_pet.py b/test_pet.py index e215678..d7e6a33 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,36 @@ def test_find_by_status_200(status): } response = api_helpers.get_api_data(test_endpoint, params) - # TODO... + assert response.status_code == 200 + pets = response.json() + assert isinstance(pets, list), "Validate the output is a list" + for pet in pets: + assert_that(pet["status"], is_(status), + f"Pet {pet.get('id')} has incorrect status") + valid_statuses = ["available", "sold", "pending"] + for pet in pets: + # Validate the status property is one of the valid status options + assert pet["status"] in valid_statuses, \ + f"Pet {pet.get('id')} has invalid status: {pet.get('status')}" + # validate each item returns in the expected schema + validate(instance=pet, schema=schemas.pet) + ''' 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("id", ["9999999999", ".9", "-1", "abc", "@#$%"]) +def test_get_by_id_404(id): + params = { + "id": id + } + test_endpoint = "/pets/{id}" + response = api_helpers.get_api_data(test_endpoint) + assert response.status_code == 404 + + ''' + Noticed the swagger doc had that the response for a 404 would be "Pet not found", but the actual response was different. + assert_that(response.text, contains_string("Pet not found")) + ''' \ No newline at end of file diff --git a/test_store.py b/test_store.py index 186bd79..e9eb79d 100644 --- a/test_store.py +++ b/test_store.py @@ -1,5 +1,7 @@ +from urllib import response from jsonschema import validate import pytest +import random import schemas import api_helpers from hamcrest import assert_that, contains_string, is_ @@ -12,5 +14,41 @@ 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 + +''' +Created a fixture that will create a new pet and order for the patch call so that the test is reusable. +If there was a delete call, I would include that to wrap at the end of the test +''' +@pytest.fixture +def order_data(): + pet_endpoint = "/pets/" + petPayload = { + "id": random.randint(1, 10000), + "name": "string", + "type": "cat", + "status": "available" + } + api_helpers.post_api_data(pet_endpoint, petPayload) + pet_id = petPayload.get("id") + + post_endpoint = "/store/order" + payload = { + "pet_id": pet_id + } + response = api_helpers.post_api_data(post_endpoint, payload) + data = response.json() + + order_id = data.get("id") + return order_id + +@pytest.mark.parametrize("status", ["available", "sold", "pending"]) +def test_patch_order_by_id(order_data, status): + test_endpoint = f"/store/order/{order_data}" + payload = { + "status": status + } + response = api_helpers.patch_api_data(test_endpoint, payload) + assert response.status_code == 200 + ''' created a schema for the outputted message and validated the response against it.''' + validate(instance=response.json(), schema=schemas.order) + assert_that(response.json().get("message"), contains_string("Order and pet status updated successfully")) From ae147e4c33bcba167c60bcd7e385538e0817c7f9 Mon Sep 17 00:00:00 2001 From: Eric Goodman Date: Wed, 14 Jan 2026 17:59:07 -0500 Subject: [PATCH 2/2] check off TODOs --- test_pet.py | 4 ---- test_store.py | 5 ++--- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/test_pet.py b/test_pet.py index d7e6a33..cc97833 100644 --- a/test_pet.py +++ b/test_pet.py @@ -5,8 +5,6 @@ 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(): @@ -20,7 +18,6 @@ def test_pet_schema(): 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 @@ -50,7 +47,6 @@ def test_find_by_status_200(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 ''' diff --git a/test_store.py b/test_store.py index e9eb79d..f979cdf 100644 --- a/test_store.py +++ b/test_store.py @@ -7,10 +7,9 @@ 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 +2) Used @pytest.fixture to create unique test data for each run +2) Created 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" '''