diff --git a/bugs b/bugs new file mode 100644 index 00000000..406d039d --- /dev/null +++ b/bugs @@ -0,0 +1,16 @@ +Bug 1: Broken error message formatting in /pets/findByStatus +In the app, this line is wrong: + api.abort(400, 'Invalid pet status {status}') +It should be: + api.abort(400, f"Invalid pet status {status}") +Right now it returns the literal text {status} instead of the actual value. + + +Bug 2: PATCH assumes "status" always exists +This line: + order['status'] = update_data['status'] +Will raise a server error if the client sends {}. The API should validate that field before using it. + + +Bug 3: No GET endpoint for order lookup +You can create and patch orders, but there is no endpoint to fetch an order by ID. That limits test coverage and makes verification indirect. \ No newline at end of file diff --git a/schemas.py b/schemas.py index 946cb6cc..971bef5e 100644 --- a/schemas.py +++ b/schemas.py @@ -1,12 +1,12 @@ pet = { "type": "object", - "required": ["name", "type"], + "required": ["id", "name", "type", "status"], "properties": { "id": { "type": "integer" }, "name": { - "type": "integer" + "type": "string" }, "type": { "type": "string", @@ -16,5 +16,31 @@ "type": "string", "enum": ["available", "sold", "pending"] }, - } + }, + "additionalProperties": False } + +order = { + "type": "object", + "required": ["id", "pet_id"], + "properties": { + "id": { + "type": "string" + }, + "pet_id": { + "type": "integer" + } + }, + "additionalProperties": False +} + +message_response = { + "type": "object", + "required": ["message"], + "properties": { + "message": { + "type": "string" + } + }, + "additionalProperties": False +} \ No newline at end of file diff --git a/test_pet.py b/test_pet.py index e2156781..8a50de67 100644 --- a/test_pet.py +++ b/test_pet.py @@ -2,13 +2,12 @@ import pytest import schemas import api_helpers -from hamcrest import assert_that, contains_string, is_ +from hamcrest import assert_that,is_ ''' -TODO: Finish this test by... -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 +Fixed: 1) Schema failure by correcting schemas.pet ''' + def test_pet_schema(): test_endpoint = "/pets/1" @@ -20,13 +19,13 @@ def test_pet_schema(): validate(instance=response.json(), schema=schemas.pet) ''' -TODO: Finish this test by... +Fixed: 1) Extending the parameterization to include all available statuses 2) Validate the appropriate response code 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 +33,25 @@ def test_find_by_status_200(status): } response = api_helpers.get_api_data(test_endpoint, params) - # TODO... + assert response.status_code == 200 + + response_json = response.json() + assert isinstance(response_json, list) + for pet in response_json: + validate(instance=pet, schema=schemas.pet) + assert_that(pet["status"], is_(status)) + ''' -TODO: Finish this test by... +Fixedf: 1) Testing and validating the appropriate 404 response for /pets/{pet_id} -2) Parameterizing the test for any edge cases +2) Parameterized for some safe Invalid ids ''' -def test_get_by_id_404(): - # TODO... - pass \ No newline at end of file +@pytest.mark.parametrize("pet_id", [999, 123456, 9999999]) +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 + assert_that(response.json()["message"], is_(f"Pet with ID {pet_id} not found")) \ No newline at end of file diff --git a/test_store.py b/test_store.py index 186bd792..83af6d1f 100644 --- a/test_store.py +++ b/test_store.py @@ -2,15 +2,66 @@ import pytest import schemas import api_helpers -from hamcrest import assert_that, contains_string, is_ +from hamcrest import assert_that, is_ +import uuid + + +def _create_unique_pet(): + unique_pet_id = int(str(uuid.uuid4().int)[:6]) + + pet_payload = { + "id": unique_pet_id, + "name": f"pet-{unique_pet_id}", + "type": "dog", + "status": "available" + } + + create_pet_response = api_helpers.post_api_data("/pets/", pet_payload) + assert create_pet_response.status_code == 201 + validate(instance=create_pet_response.json(), schema=schemas.pet) + + return pet_payload + ''' -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" +Fixed: +1) Created a function to test PATCH /store/order/{order_id} +2) Uses unique pet data for each run +3) Validates response codes and values +4) Validates response message ''' def test_patch_order_by_id(): - pass + # Create unique available pet + pet_payload = _create_unique_pet() + pet_id = pet_payload["id"] + + # Place order + order_payload = { + "pet_id": pet_id + } + + create_order_response = api_helpers.post_api_data("/store/order", order_payload) + assert create_order_response.status_code == 201 + validate(instance=create_order_response.json(), schema=schemas.order) + + order_id = create_order_response.json()["id"] + + # Patch order status to sold + patch_payload = { + "status": "sold" + } + + patch_response = api_helpers.patch_api_data(f"/store/order/{order_id}", patch_payload) + + assert patch_response.status_code == 200 + validate(instance=patch_response.json(), schema=schemas.message_response) + assert_that( + patch_response.json()["message"], + is_("Order and pet status updated successfully") + ) + + # Validate pet status actually changed + get_pet_response = api_helpers.get_api_data(f"/pets/{pet_id}") + assert get_pet_response.status_code == 200 + validate(instance=get_pet_response.json(), schema=schemas.pet) + assert_that(get_pet_response.json()["status"], is_("sold")) \ No newline at end of file