Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type": "integer"
},
"name": {
"type": "integer"
"type": "string"
},
"type": {
"type": "string",
Expand All @@ -18,3 +18,19 @@
},
}
}
#Added Order schema for validation
order = {
"type": "object",
"required": ["message"],
"properties": {
"id": {
"type": "string"
},
"pet_id": {
"type": "integer"
},
"message": {
"type": "string"
}
}
}
52 changes: 29 additions & 23 deletions test_pet.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
import api_helpers
from hamcrest import assert_that, contains_string, 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
'''

def test_pet_schema():
test_endpoint = "/pets/1"

Expand All @@ -19,28 +15,38 @@ def test_pet_schema():
# Validate the response schema against the defined schema in schemas.py
validate(instance=response.json(), schema=schemas.pet)

'''
TODO: Finish this test by...
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", "pending", "sold"]) #adjusted and added more statuses
def test_find_by_status_200(status):
test_endpoint = "/pets/findByStatus"
params = {
"status": status
}

response = api_helpers.get_api_data(test_endpoint, params)
# TODO...

'''
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
#validates the response code
assert response.status_code == 200, f"Expected status code 200, got {response.status_code}"

pets = response.json()
assert isinstance(pets, list), "Response is not a list of pets"

for pet in pets:
assert pet.get("status") == status, f"Expected status {status}, got {pet.get('status')}"
validate(instance=pet, schema=schemas.pet)

#checks for a 404 response
@pytest.mark.parametrize("invalid_id", [-1, 999999, "invalid_id"])
def test_get_by_id_404(invalid_id):
test_endpoint = f"/pets/{invalid_id}"

response = api_helpers.get_api_data(test_endpoint)
assert response.status_code == 404, f"Expected status code 404, got {response.status_code}"

if response.headers.get("Content-Type", "").startswith("application/json"):
try:
error_data = response.json()
if error_data:
assert "message" in error_data, "Error response should contain 'message'"
assert_that(error_data["message"], contains_string("not found"))
except ValueError:
pass
65 changes: 55 additions & 10 deletions test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,60 @@
import pytest
import schemas
import api_helpers
import random
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"
'''
def test_patch_order_by_id():
pass
@pytest.fixture
def new_order():
# Unique test data using random ID
pet_id = random.randint(1, 1000)
pet_data = {
"id": pet_id,
"name": "ranger",
"status": "available",
"type": "dog"
}

pet_response = api_helpers.post_api_data("/pets", pet_data)
assert pet_response.status_code == 201, f"Failed to create pet, got {pet_response.status_code}"

order_data = {
"pet_id": pet_id,
"quantity": 1,
"shipDate": "2025-08-06T00:00:00Z",
"status": "placed",
"complete": False
}

order_response = api_helpers.post_api_data("/store/order", order_data)
assert order_response.status_code == 201, f"Failed to create order, got {order_response.status_code}"

order_json = order_response.json()
assert "id" in order_json, "Order response did not contain 'id'"

return order_json


def test_patch_order_by_id(new_order):
# Testing PATCH endpoint
order_id = new_order["id"]
test_endpoint = f"/store/order/{order_id}"

patch_data = {
"status": "available"
}

response = api_helpers.patch_api_data(test_endpoint, patch_data)

# Validate response code
assert response.status_code == 200, f"Expected 200 OK, got {response.status_code}"

response_data = response.json()

# Validate with 'Order' schema (optional)
if hasattr(schemas, 'order'):
validate(instance=response_data, schema=schemas.order)

# Validate success message
assert response_data.get("message") == "Order and pet status updated successfully", \
f"Unexpected message: {response_data.get('message')}"