diff --git a/schemas.py b/schemas.py index 946cb6c..429661c 100644 --- a/schemas.py +++ b/schemas.py @@ -6,7 +6,7 @@ "type": "integer" }, "name": { - "type": "integer" + "type": "string" }, "type": { "type": "string", @@ -18,3 +18,13 @@ }, } } + +order = { + "type": "object", + "required": [ "id","pet_id"], + "properties": { + "id": {"type": "string"}, + "pet_id": {"type": ["string", "integer"]}, + "status": {"type": "string", "enum": ["available", "sold", "pending"]} + } +} diff --git a/test_pet.py b/test_pet.py index e215678..71591a8 100644 --- a/test_pet.py +++ b/test_pet.py @@ -2,11 +2,12 @@ import pytest import schemas import api_helpers -from hamcrest import assert_that, contains_string, is_ +from hamcrest import assert_that, contains_string, is_, equal_to + ''' TODO: Finish this test by... -1) Troubleshooting and fixing the test failure +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(): @@ -26,7 +27,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 = { @@ -35,12 +36,22 @@ 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) + for pet in pets: + # Validate the 'status' property + assert_that (pet['status'], equal_to(status)) + # Validate the schema for each object + 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", [999999, "nonexistent", "#@", "abc123", "abc$", None]) +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 diff --git a/test_store.py b/test_store.py index 186bd79..b2f9207 100644 --- a/test_store.py +++ b/test_store.py @@ -2,6 +2,7 @@ import pytest import schemas import api_helpers +import random from hamcrest import assert_that, contains_string, is_ ''' @@ -12,5 +13,85 @@ 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 + +''' +Test to verify the PATCH request for updating an order by ID. +Here, I have used the fixture get_new_order_id to create a new order and get the order ID. +This test will update the order status to "pending" and validate the response.''' +def test_patch_order_by_id_with_fixture(get_new_order_id): + # Prepare input payload for patch request + order_id = get_new_order_id # Order ID from the fixture + status = "pending" # Status here can be "pending", "sold", or "available" + patch_data = { + "status": status + } + + # Patch request to update the order status + test_endpoint = f"/store/order/{order_id}" + response = api_helpers.patch_api_data(test_endpoint, patch_data) + resp_json = response.json() + + # Validate the response status code for 200 with message containing "Order and pet status updated successfully" + assert response.status_code == 200 + assert_that(resp_json["message"], contains_string("Order and pet status updated successfully")) + + # Validate the order object if present in response + if "order" in resp_json: + order = resp_json["order"] + assert_that(order["id"], is_(order_id)) + assert_that(order["status"], is_(status)) + + +'''Test to verify cases when order is not found. +Here, I have hard Coded 1 as it has pending status. +Might create fixture for pending pets if required.''' +def test_patch_order_by_id_error(): + patch_data = { + "status": "delivered" + } + test_endpoint = f"/store/order/1" #Hard Coded 1 as it has pending pending. Might create fixture for pending pets if required. + response = api_helpers.patch_api_data(test_endpoint, patch_data) + resp_json = response.json() + #print(resp_json["message"]) + #Validate the response status code for 404 with message containing "Order not found. You have requested" + assert response.status_code == 404 + assert_that(resp_json["message"], contains_string("Order not found")) + + +''' +Fixture to create a new order with a unique pet_id. +This fixture will create a new pet and then create an order for that pet. +This will ensure that the order_id is unique for each test run.''' +@pytest.fixture +def get_new_order_id(): + # Create new pet with unique pet_id + pet_id = random.randint(1, 1000) + pet_data = { + "id": pet_id, + "name": f"TestPet{pet_id}", + "type": "dog", + "status": "available" + } + test_endpoint = "/pets/" + response = api_helpers.post_api_data(test_endpoint, pet_data) + resp_json = response.json() + assert response.status_code == 201 + assert resp_json["id"] == pet_id + + #Create an order for the newly created pet + order_data = { + "pet_id": pet_id, + "quantity": 1, + "status": "available", + "shipDate": "2023-10-01T00:00:00Z", + "complete": True + } + test_endpoint = "/store/order" + order_response = api_helpers.post_api_data(test_endpoint, order_data) + order_resp_json = order_response.json() + assert order_response.status_code == 201 + assert order_resp_json["pet_id"] == pet_id + # Validate the order schema + validate(instance=order_resp_json, schema=schemas.order) + print(f"Created order with ID: {resp_json['id']} for pet ID: {pet_id}") + return order_resp_json["id"]