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
20 changes: 19 additions & 1 deletion schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"]
}
}
}
35 changes: 30 additions & 5 deletions test_pet.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,55 @@ 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
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")])
# 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 = {
"status": 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
# 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
58 changes: 56 additions & 2 deletions test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -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