diff --git a/api_helpers.py b/api_helpers.py index 62f6f0db..3e82f4ba 100644 --- a/api_helpers.py +++ b/api_helpers.py @@ -1,6 +1,6 @@ import requests -base_url = 'http://localhost:5000' +base_url = 'http://127.0.0.1:5000' # GET requests def get_api_data(endpoint, params = {}): diff --git a/schemas.py b/schemas.py index 946cb6cc..760be600 100644 --- a/schemas.py +++ b/schemas.py @@ -6,7 +6,7 @@ "type": "integer" }, "name": { - "type": "integer" + "type": "string" }, "type": { "type": "string", diff --git a/test_pet.py b/test_pet.py index e2156781..679fe099 100644 --- a/test_pet.py +++ b/test_pet.py @@ -2,7 +2,7 @@ import pytest import schemas import api_helpers -from hamcrest import assert_that, contains_string, is_ + ''' TODO: Finish this test by... @@ -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", "pending", "sold"]) def test_find_by_status_200(status): test_endpoint = "/pets/findByStatus" params = { @@ -34,13 +34,27 @@ def test_find_by_status_200(status): } response = api_helpers.get_api_data(test_endpoint, params) - # TODO... + assert response.status_code == 200 + + data= response.json() + assert isinstance(data,list) + for pet in data: + assert pet["status"] == status + 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("pet_id", [999, -1, 0]) +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 + +def test_get_by_id_invalid(): + response = api_helpers.get_api_data("/pets/abc") + assert response.status_code in [400, 404] diff --git a/test_store.py b/test_store.py index 186bd792..b0b3e653 100644 --- a/test_store.py +++ b/test_store.py @@ -1,16 +1,58 @@ -from jsonschema import validate import pytest -import schemas 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" -''' + + +# Validating PATCH updates order status successfully def test_patch_order_by_id(): - pass + order_id = 1 + test_endpoint = f"/store/order/{order_id}" + + payload = {"status": "delivered"} + + response = api_helpers.patch_api_data(test_endpoint, payload) + assert response.status_code == 200 + + data = response.json() + + assert data["message"] == "Order and pet status updated successfully" + assert data["status"] == "delivered" + assert "id" in data + assert isinstance(data["id"], int) + + +# Validating API rejects invalid payload +def test_patch_order_invalid_payload(): + order_id = 2 + test_endpoint = f"/store/order/{order_id}" + + payload = {"status": 123} + + response = api_helpers.patch_api_data(test_endpoint, payload) + assert response.status_code in [400, 422] + + +# Validating PATCH on non-existing order returns 404 +def test_patch_order_not_found(): + order_id = 999 + test_endpoint = f"/store/order/{order_id}" + + payload = {"status": "delivered"} + + response = api_helpers.patch_api_data(test_endpoint, payload) + assert response.status_code == 404 + + +# Validating PATCH persists changes +def test_patch_order_persistence(): + order_id = 1 + test_endpoint = f"/store/order/{order_id}" + + payload = {"status": "processing"} + + patch_response = api_helpers.patch_api_data(test_endpoint, payload) + assert patch_response.status_code == 200 + + get_response = api_helpers.get_api_data(test_endpoint) + assert get_response.status_code == 200 + + assert get_response.json()["status"] == "processing" \ No newline at end of file