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
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,24 @@ pytest -v --html=report.html
### Tasks
- [ ] Extend and fix the 3 tests from [test_pet.py](test_pet.py#1). There are TODO instructions for each test listed in the file
- [ ] Create the PATCH test for [test_store.py](test_store.py#1). There are TODO instructions for test along with optional tasks
- [ ] Take note of any bugs you may have found
- [ ] Take note of any bugs you may have found

Bugs Found in the current implementation:
1. Incorrect data type in the pet schema definition(schemas.py, line 9)
The name field in the pet schema was incorrectly typed as "integer" when it should be "string".
Since pet names like "snowball" and "ranger" are text values, this caused the JSON schema validation in test_pet_schema to fail consistently.

2. Missing f-string prefix on error message(app.py, line 101)
The error message for invalid pet status was written as a plain string 'Invalid pet status {status}' instead of an f-string. As a result, the literal text {status} was being returned to the client rather than the actual status value provided in the request.

3. No input validation on PATCH request body(app.py, line 154)
The order update endpoint directly accessed update_data['status'] without first checking whether the field existed in the request body.If a client sends a PATCH request without a status field, this causes an unhandled KeyError crash instead of returning a clean 400 Bad Request response.

4. Incomplete parameterization in status filter test(test_pet.py, line 29)
The test_find_by_status_200 test was only parametrized with "available", leaving "sold" and "pending" completely untested. All three statuses defined in the application should be covered to ensure the filter endpoint behaves correctly across all valid inputs.

5. Wrong string in 404 assertion (test_pet.py, line 54)
The assertion was checking for "not_found" with an underscore, while the actual API error message returns "not found" with a space. This mismatch meant the assertion would never pass regardless of the response.

6. Invalid edge case in 404 test and unimplemented test body(test_pet.py, line 49)
The test_get_by_id_404 test included -1 as a parametrized value, but Flask's<int:pet_id> URL converter only matches positive integers. Negative values are rejected at the routing level before reaching the application logic, resulting in an unexpected response format that does not match the assertion. The test body was also left unimplemented with just pass.
4 changes: 3 additions & 1 deletion app.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def get(self):
"""Find pets by status"""
status = request.args.get('status')
if status not in PET_STATUS:
api.abort(400, 'Invalid pet status {status}')
api.abort(400, f'Invalid pet status {status}')
if status:
filtered_pets = [pet for pet in pets if pet['status'] == status]
return filtered_pets
Expand Down Expand Up @@ -151,6 +151,8 @@ def patch(self, order_id):
api.abort(404, f"No pet found with ID {pet_id}")

# Update the order status
if not update_data or 'status' not in update_data:
api.abort(400, "Missing required field: status")
order['status'] = update_data['status']

# Update the pet's status based on the order's new status
Expand Down
2 changes: 1 addition & 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 Down
17 changes: 14 additions & 3 deletions test_pet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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", "sold", "pending"])
def test_find_by_status_200(status):
test_endpoint = "/pets/findByStatus"
params = {
Expand All @@ -35,12 +35,23 @@ def test_find_by_status_200(status):

response = api_helpers.get_api_data(test_endpoint, params)
# TODO...
assert response.status_code ==200
assert isinstance(response.json(),list)
for pet in response.json():
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():
@pytest.mark.parametrize("pet_id", [9999, 999, 100])
def test_get_by_id_404(pet_id):
# TODO...
pass
response = api_helpers.get_api_data(f"/pets/{pet_id}")
assert response.status_code == 404
assert_that(
response.json().get("message", ""),
contains_string("not found")
)
19 changes: 17 additions & 2 deletions test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,20 @@
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 created_order():
response = api_helpers.post_api_data("/store/order", {"pet_id": 0})
assert response.status_code == 201
return response.json()

def test_patch_order_by_id(created_order):
order_id = created_order["id"]
response = api_helpers.patch_api_data(
f"/store/order/{order_id}",
{"status": "sold"}
)
assert response.status_code == 200
assert_that(
response.json().get("message", ""),
contains_string("Order and pet status updated successfully")
)