From 9e3afec2e45d59b86211fbd8c2dc8fcc334a59d6 Mon Sep 17 00:00:00 2001 From: b0shy Date: Fri, 31 Oct 2025 18:43:35 -0400 Subject: [PATCH] Completed Ally Interview Assessment - Bashar Shabani --- bug_notes.txt | 9 +++++++++ schemas.py | 2 +- test_pet.py | 26 ++++++++++++++++++++++---- test_store.py | 23 ++++++++++++++++++++--- 4 files changed, 52 insertions(+), 8 deletions(-) create mode 100644 bug_notes.txt diff --git a/bug_notes.txt b/bug_notes.txt new file mode 100644 index 0000000..aa4502f --- /dev/null +++ b/bug_notes.txt @@ -0,0 +1,9 @@ +Bug Notes + +1. In schemas.py, the “name” field was set as an integer, but the API actually returns it as a string (for example, "ranger"). I updated the schema to match the API. + +2. The /pets/{id} endpoint returns inconsistent 404 responses. Some invalid IDs return a proper JSON message, while others (like -1) return an HTML page. I added a content-type check in the test to handle both cases. + +3. The /pets/findByStatus endpoint has a small issue in its error message — it uses '{status}' literally instead of showing the actual invalid status value. + +4. The app uses in-memory data, so creating or updating an order changes the pet status for future tests. It doesn’t break functionality but can cause inconsistent results across runs. \ No newline at end of file diff --git a/schemas.py b/schemas.py index 946cb6c..7455b07 100644 --- a/schemas.py +++ b/schemas.py @@ -6,7 +6,7 @@ "type": "integer" }, "name": { - "type": "integer" + "type": "string" #The type for name was originally set to integer, corrected it to be set to string }, "type": { "type": "string", diff --git a/test_pet.py b/test_pet.py index e215678..f7f2f31 100644 --- a/test_pet.py +++ b/test_pet.py @@ -17,6 +17,8 @@ def test_pet_schema(): assert response.status_code == 200 # Validate the response schema against the defined schema in schemas.py + #1) Troubleshot and fixed the test failur in schemas.py + #1) The issue was that the type for the name property was set integer when it needed to be set to string to match the data it was receiving validate(instance=response.json(), schema=schemas.pet) ''' @@ -26,7 +28,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"]) #Extended the parameterization to also include the statuses sold and pending def test_find_by_status_200(status): test_endpoint = "/pets/findByStatus" params = { @@ -35,12 +37,28 @@ def test_find_by_status_200(status): response = api_helpers.get_api_data(test_endpoint, params) # TODO... - + assert response.status_code == 200 #Added a status code checker to validate that 200 was returned + + pets_list = response.json() #Storing response.json() in a variable so we don't keep calling it every iteration + + for pet in pets_list: #For loop to go through each pet in the response + assert pet["status"] == status #Added a check for the status property in the response to validate that it is equal to the expected status + validate(instance = pet, schema = schemas.pet) #Validates the schema for each object in the response, checks for things llike missing fields and wrong types + ''' 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(): +@pytest.mark.parametrize("bad_id", [9999, -1]) #Two edge cases - testing for ID's that should not exist +def test_get_by_id_404(bad_id): # TODO... - pass \ No newline at end of file + test_endpoint = f"/pets/{bad_id}" #Generate the URL using the missing ID + response = api_helpers.get_api_data(test_endpoint) #Request send for the ID + assert response.status_code == 404 #Added a status code check to validate that 404 was returned + + content_type = response.headers.get("Content-Type", "") #This checks what content type the response is and stores it in the variable** + if "application/json" in content_type: #If statement to check that the content type is json** **Added this because the test was failing and reason why is because with ID "-1" the HTTP 404 that was returned in HTML instead of JSON + body = response.json() #Get the response back that explains what went wrong + assert "message" in body #Added a check for the body to validate that it includes a message + assert_that(body["message"], contains_string("Pet with ID")) #Validates that it mentions the missing pet \ No newline at end of file diff --git a/test_store.py b/test_store.py index 186bd79..708ecaf 100644 --- a/test_store.py +++ b/test_store.py @@ -7,10 +7,27 @@ ''' 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) *Optional* Consider using @pytest.fixture to create unique test data for each run **I was not able to complete +2) *Optional* Consider creating an 'Order' model in schemas.py and validating it in the test **I was not able to complete 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 + create_body = {"pet_id": 0} #Store the pet_id 0 from the in-memory list in app.py, is currently "available" + create_response = api_helpers.post_api_data("/store/order", create_body) #Calls POST /store/order to place an order + assert create_response.status_code == 201 #Checks the order was created + order_id = create_response.json()["id"] #Store the new order ID so we can patch it + + patch_body = {"status": "sold"} #Updating the order status to "sold" + patch_response = api_helpers.patch_api_data(f"/store/order/{order_id}", patch_body) #Send PATCH request + assert patch_response.status_code == 200 #Checks that the PATCH passed + patch_json = patch_response.json() #Store the PATCH response body + assert patch_json["message"] == "Order and pet status updated successfully" #Check that the message text matches the requirement + + pet_check_response = api_helpers.get_api_data(f"/pets/{create_body['pet_id']}") #Stores the pet after the update + assert pet_check_response.status_code == 200 #Checks that the pet lookup passed + assert pet_check_response.json()["status"] == "sold" #Checks that the pet status is now "sold" + + bad_patch_body = {"status": "pizza"} #Tests to see how it handles an invalid status like "pizza" + bad_patch_response = api_helpers.patch_api_data(f"/store/order/{order_id}", bad_patch_body) #Test sending a bad PATCH + assert bad_patch_response.status_code == 400 #Check that the API returns 400 for invalid status \ No newline at end of file