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
Empty file added output.json
Empty file.
20 changes: 19 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,21 @@
},
}
}

order = {
"type": "object",
"required": ["pet_id"],
"properties": {
"id": {
"type": "string"
},
"pet_id": {
"type": "integer"
},
"status": {
"type": "string",
"enum": ["available", "sold", "pending"]
},
}
}

28 changes: 23 additions & 5 deletions test_pet.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,39 @@ 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 = {
"status": status
}

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

assert response.status_code == 200

# Parse JSON body and ensure it is formatted as a list
pet_data = response.json()
assert isinstance(pet_data, list)

# Validate pet has a status
# Validate that status is in list of acceptable statuses
for individual_pet in pet_data:
assert 'status' in individual_pet
assert individual_pet['status'] in status

# Validate the response schema for each pet
validate(instance=individual_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", [9999999, -1, 0])
def test_get_by_id_404(pet_id):
test_endpoint = "/pets/{pet_id}"

# Validate correct status code received
response = api_helpers.get_api_data(test_endpoint)
assert response.status_code == 404
72 changes: 70 additions & 2 deletions test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import pytest
import schemas
import api_helpers
import requests
import random
from hamcrest import assert_that, contains_string, is_

'''
Expand All @@ -12,5 +14,71 @@
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_setup():
# Create random pet id
pet_id = random.randint(1, 100)
pet_payload = {
"id": pet_id,
"name": "snowball",
"type": "cat",
"status": "available"
}

# Post pet with the pet payload and verify success
pet_response = requests.post("http://localhost:5000/pets/", json=pet_payload)
assert pet_response.status_code in (200, 201)

order_payload = {
"pet_id": pet_id,
}
# Post order with order payload and verify success
order_response = api_helpers.post_api_data("/store/order", order_payload)
assert order_response.status_code in (200, 201)

# Yield order_id
order = order_response.json()
order_id = order["id"]

yield order_id

# Clean up previous pet/order ids
requests.delete(f"http://localhost:5000/pets/{pet_id}")
requests.delete(f"http://localhost:5000/store/order/{order_id}")

@pytest.mark.parametrize("status", ["available", "pending", "sold"])
def test_patch_order_by_id_200(order_setup, status):
endpoint = f"/store/order/{order_setup}"
new_payload = {"status": status}

# Patch new status data
response = api_helpers.patch_api_data(endpoint, new_payload)
assert response.status_code == 200, f"Expected 200, got {response.status_code}"

# Verify success message received
patch_response = response.json()
assert "Order and pet status updated successfully" in patch_response["message"]

def test_patch_order_by_id_400(order_setup):
endpoint = f"/store/order/{order_setup}"

# Provide invalid status
bad_payload = {"status": "not_available"}

response = api_helpers.patch_api_data(endpoint, bad_payload)

# Verify error code 400 is received
assert response.status_code == 400, f"Expected 400, got {response.status_code}"

def test_patch_order_by_id_404():
# Provide integer id instead of string
bad_id = 0
endpoint = f"/store/order/{bad_id}"

payload = {"status": "available"}

response = api_helpers.patch_api_data(endpoint, payload)

# Verify error code 404 is received
assert response.status_code == 404, f"Expected 404, got {response.status_code}"