diff --git a/report.html b/report.html new file mode 100644 index 00000000..417b624c --- /dev/null +++ b/report.html @@ -0,0 +1,773 @@ + + + + + report.html + + + +

report.html

+

Report generated on 25-Mar-2026 at 21:16:21 by pytest-html + v4.2.0

+
+

Environment

+
+
+ + + + + +
+
+

Summary

+
+
+

8 tests took 192 ms.

+

(Un)check the boxes to filter the results.

+
+ +
+
+
+
+ + 0 Failed, + + 8 Passed, + + 0 Skipped, + + 0 Expected failures, + + 0 Unexpected passes, + + 0 Errors, + + 0 Reruns + + 0 Retried, +
+
+  /  +
+
+
+
+
+
+
+
+ + + + + + + + + +
ResultTestDurationLinks
+ + + \ No newline at end of file diff --git a/schemas.py b/schemas.py index 946cb6cc..760be600 100644 --- a/schemas.py +++ b/schemas.py @@ -6,7 +6,7 @@ "type": "integer" }, "name": { - "type": "integer" + "type": "string" }, "type": { "type": "string", diff --git a/test_pet.py b/test_pet.py index e2156781..bff4813a 100644 --- a/test_pet.py +++ b/test_pet.py @@ -13,34 +13,52 @@ def test_pet_schema(): test_endpoint = "/pets/1" response = api_helpers.get_api_data(test_endpoint) - + print('response',response) + + print("Full URL:", f"{api_helpers.base_url}{test_endpoint}") + print("Status Code:", response.status_code) + + assert response.status_code == 200 - - # Validate the response schema against the defined schema in schemas.py + print(response.json()) validate(instance=response.json(), schema=schemas.pet) -''' -TODO: Finish this test by... -1) Extending the parameterization to include all available statuses -2) Validate the appropriate response code -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")]) +VALID_STATUSES = ["available", "pending", "sold"] + +@pytest.mark.parametrize("status", VALID_STATUSES) 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... + response = api_helpers.get_api_data(test_endpoint, params=params) + + assert response.status_code == 200, f"Expected 200, got {response.status_code}" + + pets_list = response.json() + + for pet in pets_list: + + validate(instance=pet, schema=schemas.pet) + + assert pet["status"] == status, f"Expectedd status '{status}', got '{pet['status']}'" + +INVALID_PET_IDS = [-1, 999, 123456789] + +@pytest.mark.parametrize("pet_id", INVALID_PET_IDS) +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, f"Expected 404 for pet_id {pet_id}, got {response.status_code}" + + + try: + data = response.json() + assert "error" in data or "message" in data, f"No error/message in response for pet_id {pet_id}" + + except ValueError: + pass -''' -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 \ No newline at end of file diff --git a/test_store.py b/test_store.py index 186bd792..c76ab114 100644 --- a/test_store.py +++ b/test_store.py @@ -1,16 +1,62 @@ -from jsonschema import validate import pytest -import schemas import api_helpers +import schemas +import random 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" -''' -def test_patch_order_by_id(): - pass + + + + +@pytest.fixture +def create_order(): + """Create a unique test order before each test and return its data.""" + + + response = api_helpers.get_api_data("/pets/") + assert response.status_code == 200, f"Failed to get the list of pets : {response.text}" + response_body = response.json() + id_list = [item["id"] for item in response_body] + + pet_names = ["snowball", "ranger", "flippy", "arnold", "buddy", "bella"] + pet_types = ["cat", "dog", "fish", "doggy", "hamster", "bird"] + + name = random.choice(pet_names) + pet_type = random.choice(pet_types) + + while True: + order_id = random.randint(10000, 99999) + if order_id not in id_list: + break + + pet_data = { + "id": order_id, + "name": name, + "type": pet_type, + "status": "available" + } + #import pdb;pdb.set_trace() + response = api_helpers.post_api_data("/pets/", data=pet_data) + assert response.status_code == 201, f"Failed to create an order entry: {response.text}" + + order_data = { + "pet_id": order_id + } + # Create order via POST API + response = api_helpers.post_api_data("/store/order", data=order_data) + assert response.status_code == 201, f"Failed to create order: {response.text}" + + # Provide the order data to the test + yield response.json() + + +def test_patch_order_by_id(create_order): + order_id = create_order["id"] + payload={"status" : "sold"} + url = f"/store/order/{order_id}" + response = api_helpers.patch_api_data(url, data=payload) + assert response.status_code == 200 + output = response.json() + assert output['message'] == "Order and pet status updated successfully" + +