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
34 changes: 33 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,35 @@
},
}
}

order = {
"type": "object",
"required": ["pet_id"],
"properties": {
"id": {

"type": "string"

},
"pet_id": {

"type": "integer"

},
"status": {

"type": "string",
"enum": ["pending", "available", "sold"]

},
"complete": {

"type": "boolean"

}

},

"additionalProperties": True

}
34 changes: 27 additions & 7 deletions test_pet.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ def test_pet_schema():

response = api_helpers.get_api_data(test_endpoint)

assert response.status_code == 200
assert response.status_code == 200, f"Expected 200, got {response.status_code}"

# Validate the response schema against the defined schema in schemas.py
validate(instance=response.json(), schema=schemas.pet)

# Assert: Schema Validation
response_json = response.json()
validate(instance=response_json, schema=schemas.pet)

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

pets = response.json()
for pet in pets:
validate(instance=pet, schema=schemas.pet)
assert_that(pet.get("status"), is_(status))


'''
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", [ 0.5, 0.323, "Test_abc", "TestPass" ])
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

try:
response_data = response.json()
if isinstance(response_data, dict):
error_msg = response_data.get("message", "")
assert "Pet not found" in error_msg or f"Pet with ID {pet_id}" in error_msg
except ValueError:
pytest.fail(f"API request failed! with 404 error")
55 changes: 52 additions & 3 deletions test_store.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from jsonschema import validate
from jsonschema import validate, exceptions
from app import PET_TYPE import pytest
import pytest
import schemas
import random
import uuid
import api_helpers
from hamcrest import assert_that, contains_string, is_

Expand All @@ -12,5 +15,51 @@
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 create_pet():
endpoint = "/pets/"
params = {
"id" : uuid.uuid4().int,
"name" : str(uuid.uuid4())[:6],
"type": random.choice(PET_TYPE),
"status": "available"
}

response = api_helpers.post_api_data(endpoint, params)
pet = response.json()
return pet

@pytest.fixture()
def create_order(create_pet):
endpoint = "/store/order"
params = {
"pet_id": create_pet["id"]
}
response = api_helpers.post_api_data(endpoint, params)
order = response.json()
return order



@pytest.mark.parametrize("status", ['available', 'sold', 'pending'])
def test_patch_order_by_id(create_order, status):
test_endpoint = f'/store/order/{create_order["id"]}'
params = {
"status": status
}

response = api_helpers.patch_api_data(test_endpoint, params)

assert response.status_code == 200
assert response.headers["Content-Type"] == "application/json"

updated_order = response.json()

try:
validate(instance=create_order, schema=schemas.order)
except exceptions.ValidationError as e:
pytest.fail(f' Order schema validation that has failed {create_order["id"]}. {e.message}')

assert updated_order["message"] == "Order and pet status updated successfully"