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: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
__pycache__
.pytest_cache
.venv/
.python-version
test_scratch.py
pet_scratch.py
store_scratch.py
Expand Down
4 changes: 3 additions & 1 deletion api_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
base_url = 'http://localhost:5000'

# GET requests
def get_api_data(endpoint, params = {}):
def get_api_data(endpoint, params = None):
if params is None:
params = {}
response = requests.get(f'{base_url}{endpoint}', params=params)
return response

Expand Down
19 changes: 7 additions & 12 deletions 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 @@ -150,19 +150,14 @@ def patch(self, order_id):
if pet is None:
api.abort(404, f"No pet found with ID {pet_id}")

# Update the order status
order['status'] = update_data['status']

# Update the pet's status based on the order's new status
if update_data['status'] == 'pending':
pet['status'] = 'pending'
elif update_data['status'] == 'sold':
pet['status'] = 'sold'
elif update_data['status'] == 'available':
pet['status'] = 'available'
else:
# Validate status before updating
if update_data['status'] not in PET_STATUS:
api.abort(400, f"Invalid status '{update_data['status']}'. Valid statuses are {', '.join(PET_STATUS)}")

# Update the order and pet status
order['status'] = update_data['status']
pet['status'] = update_data['status']


return {"message": "Order and pet status updated successfully"}

Expand Down
14 changes: 14 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[project]
name = "pytest-api-example"
version = "0.1.0"
description = "Sample API tests using pytest against a Flask Petstore API"
requires-python = ">=3.13"
dependencies = [
"flask>=3.1.3",
"flask-restx>=1.3.2",
"jsonschema>=4.26.0",
"pyhamcrest>=2.1.0",
"pytest>=9.0.2",
"pytest-html>=4.2.0",
"requests>=2.33.0",
]
15 changes: 14 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,16 @@
},
}
}

order = {
"type": "object",
"required": ["id", "pet_id"],
"properties": {
"id": {
"type": "string"
},
"pet_id": {
"type": "integer"
},
}
}
15 changes: 10 additions & 5 deletions test_pet.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,26 @@ 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)
# TODO...
assert response.status_code == 200

for pet in response.json():
assert_that(pet['status'], is_(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():
# TODO...
pass
@pytest.mark.parametrize("pet_id", [999, -1])
def test_get_by_id_404(pet_id):
response = api_helpers.get_api_data(f"/pets/{pet_id}")
assert response.status_code == 404
15 changes: 13 additions & 2 deletions test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,16 @@
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 order_id():
"""Create an order for an available pet and return the order ID."""
response = api_helpers.post_api_data("/store/order", {"pet_id": 0})
assert response.status_code == 201
validate(instance=response.json(), schema=schemas.order)
return response.json()["id"]

def test_patch_order_by_id(order_id):
response = api_helpers.patch_api_data(f"/store/order/{order_id}", {"status": "sold"})

assert response.status_code == 200
assert_that(response.json()["message"], is_("Order and pet status updated successfully"))
Loading