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 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'

# GET requests
def get_api_data(endpoint, params = {}):
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
26 changes: 20 additions & 6 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_


'''
TODO: Finish this test by...
Expand All @@ -26,21 +26,35 @@ 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

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

for pet in data:
assert pet["status"] == 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, 0])
def test_get_by_id_404(pet_id):
test_endpoint = f"/pets/{pet_id}"

response = api_helpers.get_api_data(test_endpoint)

assert response.status_code == 404

def test_get_by_id_invalid():
response = api_helpers.get_api_data("/pets/abc")
assert response.status_code in [400, 404]
68 changes: 55 additions & 13 deletions test_store.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,58 @@
from jsonschema import validate
import pytest
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
3) Validate the response codes and values
4) Validate the response message "Order and pet status updated successfully"
'''


# Validating PATCH updates order status successfully
def test_patch_order_by_id():
pass
order_id = 1
test_endpoint = f"/store/order/{order_id}"

payload = {"status": "delivered"}

response = api_helpers.patch_api_data(test_endpoint, payload)
assert response.status_code == 200

data = response.json()

assert data["message"] == "Order and pet status updated successfully"
assert data["status"] == "delivered"
assert "id" in data
assert isinstance(data["id"], int)


# Validating API rejects invalid payload
def test_patch_order_invalid_payload():
order_id = 2
test_endpoint = f"/store/order/{order_id}"

payload = {"status": 123}

response = api_helpers.patch_api_data(test_endpoint, payload)
assert response.status_code in [400, 422]


# Validating PATCH on non-existing order returns 404
def test_patch_order_not_found():
order_id = 999
test_endpoint = f"/store/order/{order_id}"

payload = {"status": "delivered"}

response = api_helpers.patch_api_data(test_endpoint, payload)
assert response.status_code == 404


# Validating PATCH persists changes
def test_patch_order_persistence():
order_id = 1
test_endpoint = f"/store/order/{order_id}"

payload = {"status": "processing"}

patch_response = api_helpers.patch_api_data(test_endpoint, payload)
assert patch_response.status_code == 200

get_response = api_helpers.get_api_data(test_endpoint)
assert get_response.status_code == 200

assert get_response.json()["status"] == "processing"