From 319c0b8fa75cfe737f0e001a2434e3ed2fd561d4 Mon Sep 17 00:00:00 2001 From: bryantmadisonh Date: Thu, 6 Nov 2025 19:15:31 -0500 Subject: [PATCH] assessment_v1 Completed TODOs --- output.json | 0 schemas.py | 20 +++++++++++++- test_pet.py | 28 ++++++++++++++++---- test_store.py | 72 +++++++++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 112 insertions(+), 8 deletions(-) create mode 100644 output.json diff --git a/output.json b/output.json new file mode 100644 index 0000000..e69de29 diff --git a/schemas.py b/schemas.py index 946cb6c..63604c6 100644 --- a/schemas.py +++ b/schemas.py @@ -6,7 +6,7 @@ "type": "integer" }, "name": { - "type": "integer" + "type": "string" }, "type": { "type": "string", @@ -18,3 +18,21 @@ }, } } + +order = { + "type": "object", + "required": ["pet_id"], + "properties": { + "id": { + "type": "string" + }, + "pet_id": { + "type": "integer" + }, + "status": { + "type": "string", + "enum": ["available", "sold", "pending"] + }, + } +} + diff --git a/test_pet.py b/test_pet.py index e215678..f4f22c7 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,31 @@ def test_find_by_status_200(status): } response = api_helpers.get_api_data(test_endpoint, params) - # TODO... + + assert response.status_code == 200 + + # Parse JSON body and ensure it is formatted as a list + pet_data = response.json() + assert isinstance(pet_data, list) + + # Validate pet has a status + # Validate that status is in list of acceptable statuses + for individual_pet in pet_data: + assert 'status' in individual_pet + assert individual_pet['status'] in status + + # Validate the response schema for each pet + validate(instance=individual_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("pet_id", [9999999, -1, 0]) +def test_get_by_id_404(pet_id): + test_endpoint = "/pets/{pet_id}" + + # Validate correct status code received + response = api_helpers.get_api_data(test_endpoint) + assert response.status_code == 404 \ No newline at end of file diff --git a/test_store.py b/test_store.py index 186bd79..001529c 100644 --- a/test_store.py +++ b/test_store.py @@ -2,6 +2,8 @@ import pytest import schemas import api_helpers +import requests +import random from hamcrest import assert_that, contains_string, is_ ''' @@ -12,5 +14,71 @@ 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 order_setup(): + # Create random pet id + pet_id = random.randint(1, 100) + pet_payload = { + "id": pet_id, + "name": "snowball", + "type": "cat", + "status": "available" + } + + # Post pet with the pet payload and verify success + pet_response = requests.post("http://localhost:5000/pets/", json=pet_payload) + assert pet_response.status_code in (200, 201) + + order_payload = { + "pet_id": pet_id, + } + # Post order with order payload and verify success + order_response = api_helpers.post_api_data("/store/order", order_payload) + assert order_response.status_code in (200, 201) + + # Yield order_id + order = order_response.json() + order_id = order["id"] + + yield order_id + + # Clean up previous pet/order ids + requests.delete(f"http://localhost:5000/pets/{pet_id}") + requests.delete(f"http://localhost:5000/store/order/{order_id}") + +@pytest.mark.parametrize("status", ["available", "pending", "sold"]) +def test_patch_order_by_id_200(order_setup, status): + endpoint = f"/store/order/{order_setup}" + new_payload = {"status": status} + + # Patch new status data + response = api_helpers.patch_api_data(endpoint, new_payload) + assert response.status_code == 200, f"Expected 200, got {response.status_code}" + + # Verify success message received + patch_response = response.json() + assert "Order and pet status updated successfully" in patch_response["message"] + +def test_patch_order_by_id_400(order_setup): + endpoint = f"/store/order/{order_setup}" + + # Provide invalid status + bad_payload = {"status": "not_available"} + + response = api_helpers.patch_api_data(endpoint, bad_payload) + + # Verify error code 400 is received + assert response.status_code == 400, f"Expected 400, got {response.status_code}" + +def test_patch_order_by_id_404(): + # Provide integer id instead of string + bad_id = 0 + endpoint = f"/store/order/{bad_id}" + + payload = {"status": "available"} + + response = api_helpers.patch_api_data(endpoint, payload) + + # Verify error code 404 is received + assert response.status_code == 404, f"Expected 404, got {response.status_code}" +