forked from automationExamples/pytest-api-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_store.py
More file actions
69 lines (58 loc) · 2.21 KB
/
test_store.py
File metadata and controls
69 lines (58 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from jsonschema import validate
import pytest
import schemas
import api_helpers
from hamcrest import assert_that, contains_string, equal_to, 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"
'''
'''
Fixture to create a unique order for each test run
'''
@pytest.fixture
def create_order():
# First, find an available pet or use pet_id 2
pets_response = api_helpers.get_api_data("/pets/")
available_pets = [p for p in pets_response.json() if p['status'] == 'available']
# If no available pets, create a new one
if not available_pets:
new_pet = {
"id": 999,
"name": "test_pet",
"type": "dog",
"status": "available"
}
pet_create_response = api_helpers.post_api_data("/pets/", new_pet)
pet_id = 999
else:
pet_id = available_pets[0]['id']
# Create an order for the available pet
order_data = {
"pet_id": pet_id
}
response = api_helpers.post_api_data("/store/order", order_data)
assert response.status_code == 201
order = response.json()
# Validate order schema
validate(instance=order, schema=schemas.order)
return order['id'], pet_id
def test_patch_order_by_id(create_order):
order_id, pet_id = create_order
payload = {
"status": "sold" # Valid status from PET_STATUS enum
}
test_endpoint = f"/store/order/{order_id}"
response = api_helpers.patch_api_data(test_endpoint, payload)
# Validate response code
assert response.status_code == 200
# Validate response message
data = response.json()
assert_that(data.get("message"), equal_to("Order and pet status updated successfully"))
# Validate the order was updated (optional - verify by getting the pet status)
pet_response = api_helpers.get_api_data(f"/pets/{pet_id}")
assert pet_response.json()["status"] == "sold"