diff --git a/schemas.py b/schemas.py index 946cb6cc..105ae4ea 100644 --- a/schemas.py +++ b/schemas.py @@ -6,7 +6,8 @@ "type": "integer" }, "name": { - "type": "integer" + # BUG FIX: was "integer", should be "string" — caused test_pet_schema to fail + "type": "string" }, "type": { "type": "string", @@ -18,3 +19,20 @@ }, } } + +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 e2156781..449d929f 100644 --- a/test_pet.py +++ b/test_pet.py @@ -17,8 +17,10 @@ def test_pet_schema(): assert response.status_code == 200 # Validate the response schema against the defined schema in schemas.py + # BUG FIX: schemas.py had "name" typed as "integer" instead of "string" — fixed in schemas.py validate(instance=response.json(), schema=schemas.pet) + ''' TODO: Finish this test by... 1) Extending the parameterization to include all available statuses @@ -26,7 +28,8 @@ 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")]) +# TODO 1: Extended parameterization to cover all three valid pet statuses +@pytest.mark.parametrize("status", [("available"), ("pending"), ("sold")]) def test_find_by_status_200(status): test_endpoint = "/pets/findByStatus" params = { @@ -34,13 +37,35 @@ def test_find_by_status_200(status): } response = api_helpers.get_api_data(test_endpoint, params) - # TODO... + + # TODO 2: Validate the appropriate response code + assert response.status_code == 200 + + pets = response.json() + + for pet in pets: + # TODO 3: Validate the 'status' property in the response is equal to the expected status + assert_that(pet['status'], is_(status)) + + # TODO 4: Validate the schema for each object in the response + 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 +# TODO 2: Parameterized with edge cases — large ID, negative ID, and zero (which is a valid pet, kept for contrast) +@pytest.mark.parametrize("pet_id", [ + 9999, # non-existent ID + -1, # negative ID (edge case) + 99999999 # very large ID (edge case) +]) +def test_get_by_id_404(pet_id): + # TODO 1: Testing and validating the appropriate 404 response for /pets/{pet_id} + test_endpoint = f"/pets/{pet_id}" + + response = api_helpers.get_api_data(test_endpoint) + + assert response.status_code == 404 diff --git a/test_store.py b/test_store.py index 186bd792..bb5410c0 100644 --- a/test_store.py +++ b/test_store.py @@ -12,5 +12,59 @@ 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 + +# TODO 2 (Optional): Fixture to POST a fresh order before each test run, ensuring unique test data +@pytest.fixture +def created_order(): + """ + Creates a fresh order against an available pet before each test. + Uses pet ID 2 (flippy the fish) which starts as 'available'. + After the test, there is no teardown needed as in-memory state resets on server restart. + """ + order_endpoint = "/store/order" + order_payload = { + "pet_id": 2 # 'flippy' starts as 'available' + } + response = api_helpers.post_api_data(order_endpoint, order_payload) + + # Ensure the order was created successfully before running the test + assert response.status_code == 201, f"Fixture failed to create order: {response.json()}" + + # TODO 2 (Optional): Validate the order response against the Order schema defined in schemas.py + validate(instance=response.json(), schema=schemas.order) + + return response.json() + + +# TODO 1: Test the PATCH /store/order/{order_id} endpoint +@pytest.mark.parametrize("new_status", ["sold", "available", "pending"]) +def test_patch_order_by_id(created_order, new_status): + order_id = created_order["id"] + patch_endpoint = f"/store/order/{order_id}" + patch_payload = { + "status": new_status + } + + response = api_helpers.patch_api_data(patch_endpoint, patch_payload) + + # TODO 3: Validate the response code — PATCH returns 200 on success + assert response.status_code == 200 + + # TODO 4: Validate the response message confirms the update + response_json = response.json() + assert_that(response_json["message"], is_("Order and pet status updated successfully")) + + +def test_patch_order_not_found(): + """ + Validates that patching a non-existent order returns 404. + """ + fake_order_id = "00000000-0000-0000-0000-000000000000" + patch_endpoint = f"/store/order/{fake_order_id}" + patch_payload = { + "status": "sold" + } + + response = api_helpers.patch_api_data(patch_endpoint, patch_payload) + + assert response.status_code == 404