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
2 changes: 1 addition & 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
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
27 changes: 23 additions & 4 deletions test_pet.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,40 @@ 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", "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)

assert response.status_code == 200
data = response.json()
assert isinstance(data, list)

for pet in data:
validate(instance=pet, schema=schemas.pet)
assert pet["status"] == status
# 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
@pytest.mark.parametrize("pet_id", [999999, -1, 123456789])
def test_get_by_id_404(pet_id):
response = api_helpers.get_api_data(f"/pets/{pet_id}")
assert response.status_code == 404

# Check if response contains JSON; if not, fall back to text
try:
body = response.json()
assert "message" in body
assert_that(body["message"], contains_string(str(pet_id)))
except ValueError: # JSONDecodeError inherits from ValueError
# API returned HTML instead of JSON
body_text = response.text.lower()
assert "not found" in body_text, f"Unexpected 404 body: {body_text}"
48 changes: 46 additions & 2 deletions test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,49 @@
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 fresh_available_pet_id():
import time
new_id = int(10_000 + (time.time() % 100_000))
new_pet = {
"id": new_id,
"name": f"tester-{new_id}",
"type": "dog",
"status": "available",
}
resp = api_helpers.post_api_data("/pets/", new_pet)

assert resp.status_code in (201, 409)
return new_id

def test_patch_order_by_id(fresh_available_pet_id):

post_resp = api_helpers.post_api_data("/store/order", {"pet_id": fresh_available_pet_id})
assert post_resp.status_code == 201
order = post_resp.json()
assert "id" in order
order_id = order["id"]


patch_resp = api_helpers.patch_api_data(f"/store/order/{order_id}", {"status": "sold"})
assert patch_resp.status_code == 200
assert patch_resp.json().get("message") == "Order and pet status updated successfully"


pet_resp = api_helpers.get_api_data(f"/pets/{fresh_available_pet_id}")
assert pet_resp.status_code == 200
assert pet_resp.json()["status"] == "sold"

def test_patch_order_invalid_status(fresh_available_pet_id):

post_resp = api_helpers.post_api_data("/store/order", {"pet_id": fresh_available_pet_id})
assert post_resp.status_code == 201
order_id = post_resp.json()["id"]


bad = api_helpers.patch_api_data(f"/store/order/{order_id}", {"status": "bogus"})
assert bad.status_code == 400

def test_patch_order_not_found():
resp = api_helpers.patch_api_data("/store/order/does-not-exist", {"status": "sold"})
assert resp.status_code == 404