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
10 changes: 10 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions .idea/pytest-api-example.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion api_helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import requests

base_url = 'http://localhost:5000'
base_url = 'http://127.0.0.1:5000' # I used the loopback address instead of localhost since I am getting a 403 status code

# GET requests
def get_api_data(endpoint, params = {}):
Expand Down
4 changes: 4 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
api.add_namespace(store_ns)

# In-memory data storage
# I added id = 3 just to test the sold status
pets = [
{
'id': 0, 'name': 'snowball', 'type': 'cat', 'status': 'available'
Expand All @@ -44,6 +45,9 @@
},
{
'id': 2, 'name': 'flippy', 'type': 'fish', 'status': 'available'
},
{
'id': 3, 'name': 'vernon', 'type': 'fish', 'status': 'sold'
}
]

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" # I changed this from integer to string. I am getting this error: jsonschema.exceptions.ValidationError: 'ranger' is not of type 'integer'
},
"type": {
"type": "string",
Expand Down
32 changes: 27 additions & 5 deletions test_pet.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pytest
import schemas
import api_helpers
from hamcrest import assert_that, contains_string, is_
from hamcrest import assert_that, contains_string, is_, equal_to, any_of

'''
TODO: Finish this test by...
Expand All @@ -14,10 +14,14 @@ def test_pet_schema():

response = api_helpers.get_api_data(test_endpoint)

assert response.status_code == 200
if response.status_code in [403, 404]:
pytest.fail("Forbidden. Server is blocking the request")

assert response.status_code == 200
# Validate the response schema against the defined schema in schemas.py
# vernon = response.json()
validate(instance=response.json(), schema=schemas.pet)
# print(vernon)

'''
TODO: Finish this test by...
Expand All @@ -26,7 +30,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", "pending", "sold"])
def test_find_by_status_200(status):
test_endpoint = "/pets/findByStatus"
params = {
Expand All @@ -35,12 +39,30 @@ def test_find_by_status_200(status):

response = api_helpers.get_api_data(test_endpoint, params)
# TODO...
assert response.status_code == 200
pets = response.json()
for pet in pets:
assert_that(pet["status"], is_(equal_to(status)))
validate(instance=pet, schema=schemas.pet)
print(pets)

'''
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", [4, 1000, -1, "vernon", 0.01, "v"])
def test_get_by_id_404(pet_id):
# TODO...
pass
test_endpoint = "/pets/" + str(pet_id)
params = {
"pet_id": pet_id
}
response = api_helpers.get_api_data(test_endpoint, params)
assert response.status_code == 404
assert_that(response.text, any_of(
contains_string(f"Pet with ID {pet_id} not found"),
contains_string(f"404 Not Found")
))
# print(response)
37 changes: 34 additions & 3 deletions test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pytest
import schemas
import api_helpers
from hamcrest import assert_that, contains_string, is_
from hamcrest import assert_that, contains_string, is_, any_of, equal_to

'''
TODO: Finish this test by...
Expand All @@ -12,5 +12,36 @@
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 post_order(request):
test_endpoint = "/store/order"
params = {
"pet_id": request.param,
}
response = api_helpers.post_api_data(test_endpoint, params)
if response.status_code != 201:
pytest.skip(f"Pet with ID {params["pet_id"]} is not available for order")
return response.json()

@pytest.mark.parametrize("post_order, target_status", [
(0, "available"), #pet_id, expectd status
(1, "pending"),
(2, "available"),
(3, "sold"),
], indirect=["post_order"])
def test_patch_order_by_id(post_order, target_status):
order_id = post_order["id"]
pet_id = post_order["pet_id"]
test_endpoint = f"/store/order/{order_id}"
params = {
"status": target_status,
}
response = api_helpers.patch_api_data(test_endpoint, params)
assert response.status_code == 200
assert_that(response.json()["message"], equal_to("Order and pet status updated successfully"))

test_endpoint = f"/pets/{pet_id}"
check_pet_applied = api_helpers.get_api_data(test_endpoint)
assert_that(check_pet_applied.json()["status"], equal_to(target_status))