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 app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
api = Api(app, version='1.0', title='Petstore API',
description='A simple Petstore API')

# Enums
# Enumscls
PET_STATUS = ['available', 'sold', 'pending']
PET_TYPE = ['cat', 'dog', 'fish']

Expand Down
11 changes: 9 additions & 2 deletions schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"type": "integer"
},
"name": {
"type": "integer"
},
"type": "string"
}, # fix: was integer
"type": {
"type": "string",
"enum": ["cat", "dog", "fish"]
Expand All @@ -18,3 +18,10 @@
},
}
}
order_patch_success = {
"type": "object",
"required": ["message"],
"properties": {
"message": {"type": "string"}
}
}
28 changes: 20 additions & 8 deletions test_pet.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,33 @@ 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
}
params = {"status": status}

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

assert response.status_code == 200
body = response.json()
assert isinstance(body, list)

for pet in body:
assert_that(pet["status"], is_(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", [3, 999, 2147483647])
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
body = response.json()
assert_that(body["message"], contains_string("not found"))
68 changes: 66 additions & 2 deletions test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,69 @@
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 created_order():
# 1) Get all pets and compute a unique ID
all_pets_response = api_helpers.get_api_data("/pets/")
assert all_pets_response.status_code == 200
all_pets = all_pets_response.json()

next_id = max([p["id"] for p in all_pets], default=-1) + 1

# 2) Create a fresh AVAILABLE pet for this test run
new_pet = {
"id": next_id,
"name": f"qa_pet_{next_id}",
"type": "dog",
"status": "available"
}

create_pet_response = api_helpers.post_api_data("/pets/", new_pet)
assert create_pet_response.status_code == 201

# 3) Create order for that fresh pet
create_order_response = api_helpers.post_api_data("/store/order", {"pet_id": next_id})
assert create_order_response.status_code == 201

order = create_order_response.json()
assert "id" in order
assert order["pet_id"] == next_id

return {"order_id": order["id"], "pet_id": next_id}
@pytest.mark.parametrize("new_status", ["pending", "sold", "available"])
def test_patch_order_by_id(created_order, new_status):
order_id = created_order["order_id"]
pet_id = created_order["pet_id"]

# PATCH /store/order/{order_id}
patch_response = api_helpers.patch_api_data(
f"/store/order/{order_id}",
{"status": new_status}
)

# Validate response code + message
assert patch_response.status_code == 200
patch_body = patch_response.json()
assert_that(
patch_body["message"],
is_("Order and pet status updated successfully")
)

# Validate values by checking the linked pet status is updated
pet_response = api_helpers.get_api_data(f"/pets/{pet_id}")
assert pet_response.status_code == 200

pet_body = pet_response.json()
assert_that(pet_body["status"], is_(new_status))
validate(instance=pet_body, schema=schemas.pet)
def test_patch_order_by_id_404():
patch_response = api_helpers.patch_api_data(
"/store/order/not-a-real-order-id",
{"status": "sold"}
)

patch_body = patch_response.json()

assert patch_response.status_code == 404
assert_that(patch_body["message"], contains_string("Order not found"))
validate(instance=patch_body, schema=schemas.order_patch_success)