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
13 changes: 12 additions & 1 deletion schemas.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
''' Noticed the name in the schema was incorrectly marked as an integer'''
pet = {
"type": "object",
"required": ["name", "type"],
Expand All @@ -6,7 +7,7 @@
"type": "integer"
},
"name": {
"type": "integer"
"type": "string"
},
"type": {
"type": "string",
Expand All @@ -18,3 +19,13 @@
},
}
}

order = {
"type": "object",
"properties": {
"message": {
"type": "string"
}
},
"required": ["message"]
}
37 changes: 28 additions & 9 deletions test_pet.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
from hamcrest import assert_that, contains_string, is_

'''
TODO: Finish this test by...
1) Troubleshooting and fixing the test failure
The purpose of this test is to validate the response matches the expected schema defined in schemas.py
'''
def test_pet_schema():
Expand All @@ -20,27 +18,48 @@ def test_pet_schema():
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")])
@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
pets = response.json()
assert isinstance(pets, list), "Validate the output is a list"
for pet in pets:
assert_that(pet["status"], is_(status),
f"Pet {pet.get('id')} has incorrect status")
valid_statuses = ["available", "sold", "pending"]
for pet in pets:
# Validate the status property is one of the valid status options
assert pet["status"] in valid_statuses, \
f"Pet {pet.get('id')} has invalid status: {pet.get('status')}"
# validate each item returns in the expected schema
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("id", ["9999999999", ".9", "-1", "abc", "@#$%"])
def test_get_by_id_404(id):
params = {
"id": id
}
test_endpoint = "/pets/{id}"
response = api_helpers.get_api_data(test_endpoint)
assert response.status_code == 404

'''
Noticed the swagger doc had that the response for a 404 would be "Pet not found", but the actual response was different.
assert_that(response.text, contains_string("Pet not found"))
'''
47 changes: 42 additions & 5 deletions test_store.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,53 @@
from urllib import response
from jsonschema import validate
import pytest
import random
import schemas
import api_helpers
from hamcrest import assert_that, contains_string, is_

'''
TODO: Finish this test by...
1) Creating a function to test the PATCH request /store/order/{order_id}
2) *Optional* Consider using @pytest.fixture to create unique test data for each run
2) *Optional* Consider creating an 'Order' model in schemas.py and validating it in the test
2) Used @pytest.fixture to create unique test data for each run
2) Created an 'Order' model in schemas.py and validating it in the test
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

'''
Created a fixture that will create a new pet and order for the patch call so that the test is reusable.
If there was a delete call, I would include that to wrap at the end of the test
'''
@pytest.fixture
def order_data():
pet_endpoint = "/pets/"
petPayload = {
"id": random.randint(1, 10000),
"name": "string",
"type": "cat",
"status": "available"
}
api_helpers.post_api_data(pet_endpoint, petPayload)
pet_id = petPayload.get("id")

post_endpoint = "/store/order"
payload = {
"pet_id": pet_id
}
response = api_helpers.post_api_data(post_endpoint, payload)
data = response.json()

order_id = data.get("id")
return order_id

@pytest.mark.parametrize("status", ["available", "sold", "pending"])
def test_patch_order_by_id(order_data, status):
test_endpoint = f"/store/order/{order_data}"
payload = {
"status": status
}
response = api_helpers.patch_api_data(test_endpoint, payload)
assert response.status_code == 200
''' created a schema for the outputted message and validated the response against it.'''
validate(instance=response.json(), schema=schemas.order)
assert_that(response.json().get("message"), contains_string("Order and pet status updated successfully"))