diff --git a/backend/main.py b/backend/main.py index c76f5db..cc08ed8 100644 --- a/backend/main.py +++ b/backend/main.py @@ -28,52 +28,53 @@ def init_db(): create_tables(conn) pets = [ - ( - "Buddy", - "Dog", - "Golden Retriever", - 3, - "Friendly", - "/images/buddy.jpg", - "Available", - ), - ( - "Whiskers", - "Cat", - "Siamese", - 2, - "Independent", - "/images/whiskers.jpg", - "Available", - ), - ( - "Rex", - "Dog", - "German Shepherd", - 4, - "Protective", - "/images/rex.jpg", - "Available", - ), - ( - "Max", - "Dog", - "Labrador Retriever", - 5, - "Playful", - "/images/max.jpg", - "Adopted", - ), - ( - "Mittens", - "Cat", - "Persian", - 1, - "Lazy", - "/images/mittens.jpg", - "Available", - ), - ("Bella", "Dog", "Bulldog", 2, "Gentle", "/images/bella.jpg", "Available"), + ("Buddy", + "Dog", + "Golden Retriever", + 3, + "Friendly", + "/images/buddy.jpg", + "Available", + ), + ("Whiskers", + "Cat", + "Siamese", + 2, + "Independent", + "/images/whiskers.jpg", + "Available", + ), + ("Rex", + "Dog", + "German Shepherd", + 4, + "Protective", + "/images/rex.jpg", + "Available", + ), + ("Max", + "Dog", + "Labrador Retriever", + 5, + "Playful", + "/images/max.jpg", + "Adopted", + ), + ("Mittens", + "Cat", + "Persian", + 1, + "Lazy", + "/images/mittens.jpg", + "Available", + ), + ("Bella", + "Dog", + "Bulldog", + 2, + "Gentle", + "/images/bella.jpg", + "Available"), ] cursor = conn.cursor() @@ -143,16 +144,19 @@ def add_pet(): for field in required_fields: if field not in pet_data: - return jsonify({"error": f"Missing required field: {field}"}), 400 + return jsonify( + {"error": f"Missing required field: {field}"}), 400 if not (0 < pet_data["age"] < 100): # adding validation - return jsonify({"error": "Invalid age. Must be between 1 and 99"}), 400 + return jsonify( + {"error": "Invalid age. Must be between 1 and 99"}), 400 conn = get_db() cursor = conn.cursor() # Check for duplicate pet name - cursor.execute("SELECT * FROM animals WHERE name = ?", (pet_data["name"],)) + cursor.execute("SELECT * FROM animals WHERE name = ?", + (pet_data["name"],)) existing = cursor.fetchone() if existing: return jsonify({"error": "Pet already exists"}), 409 diff --git a/backend/pets.py b/backend/pets.py index 9fc318c..47c9dae 100644 --- a/backend/pets.py +++ b/backend/pets.py @@ -126,7 +126,8 @@ def add_pet(pet_data): required_fields = ["name", "breed", "age", "temperament"] for field in required_fields: if field not in pet_data: - return jsonify({"error": f"Missing required field: {field}"}), 400 + return jsonify( + {"error": f"Missing required field: {field}"}), 400 # Generate a new ID (in a real app, the database would handle this) new_id = max(pet["id"] for pet in PETS) + 1 if PETS else 1 @@ -174,7 +175,8 @@ def update_pet(pet_id, pet_data): """ try: # Find the pet with the given ID - pet_index = next((i for i, p in enumerate(PETS) if p["id"] == pet_id), None) + pet_index = next( + (i for i, p in enumerate(PETS) if p["id"] == pet_id), None) if pet_index is None: return jsonify({"error": "Pet not found"}), 404 @@ -209,7 +211,8 @@ def delete_pet(pet_id): """ try: # Find the pet with the given ID - pet_index = next((i for i, p in enumerate(PETS) if p["id"] == pet_id), None) + pet_index = next( + (i for i, p in enumerate(PETS) if p["id"] == pet_id), None) if pet_index is None: return jsonify({"error": "Pet not found"}), 404 diff --git a/backend/test_main.py b/backend/test_main.py index 1c7b925..3d19653 100644 --- a/backend/test_main.py +++ b/backend/test_main.py @@ -56,7 +56,13 @@ def setup_test_database(): # Add test data cursor.execute( "INSERT INTO animals (name, species, breed, age, personality, image_path, adoption_status) VALUES (?, ?, ?, ?, ?, ?, ?)", - ("TestDog", "Dog", "Mixed", 2, "Friendly", "/images/test.jpg", "Available"), + ("TestDog", + "Dog", + "Mixed", + 2, + "Friendly", + "/images/test.jpg", + "Available"), ) conn.commit() @@ -106,8 +112,10 @@ def test_login_user(client): # Test login with incorrect password response = client.post( - "/api/login", json={"username": "testuser", "password": "WrongPassword"} - ) + "/api/login", + json={ + "username": "testuser", + "password": "WrongPassword"}) assert response.status_code == 401 diff --git a/backend/tests/__pycache__/test_api.cpython-311-pytest-8.3.4.pyc b/backend/tests/__pycache__/test_api.cpython-311-pytest-8.3.4.pyc index e97211f..57e1ca4 100644 Binary files a/backend/tests/__pycache__/test_api.cpython-311-pytest-8.3.4.pyc and b/backend/tests/__pycache__/test_api.cpython-311-pytest-8.3.4.pyc differ diff --git a/backend/tests/test_api.py b/backend/tests/test_api.py index 6e01c49..e86176d 100644 --- a/backend/tests/test_api.py +++ b/backend/tests/test_api.py @@ -1,18 +1,22 @@ -import requests +"""This module contains tests for the pet adoption API.""" + import sqlite3 import os +import requests BASE_URL = "http://127.0.0.1:5000" DB_PATH = os.path.join(os.path.dirname(__file__), "../animal_shelter.db") def test_get_all_pets(): - response = requests.get(f"{BASE_URL}/api/pets") + """Test case to verify retrieving all pets.""" + response = requests.get(f"{BASE_URL}/api/pets", timeout=10) # Adding timeout assert response.status_code == 200 assert isinstance(response.json(), list) def test_add_pet_boundary_age(): + """Test case to verify adding a pet with a boundary age.""" new_pet = { "name": "Oldy", "species": "Turtle", @@ -21,13 +25,13 @@ def test_add_pet_boundary_age(): "personality": "Wise elder", "image_path": "/images/oldy.jpg", } - response = requests.post(f"{BASE_URL}/api/pets", json=new_pet) + response = requests.post(f"{BASE_URL}/api/pets", json=new_pet, timeout=10) # Adding timeout - # reject the pet + # Reject the pet assert response.status_code == 400 assert "Invalid age" in response.text - # Pet will notbe in the database + # Pet will not be in the database conn = sqlite3.connect(DB_PATH) conn.row_factory = sqlite3.Row cursor = conn.cursor() @@ -39,6 +43,7 @@ def test_add_pet_boundary_age(): def test_add_pet_invalid_age(): + """Test case to verify adding a pet with an invalid high age.""" pet = { "name": "Overaged", "species": "Turtle", @@ -47,11 +52,13 @@ def test_add_pet_invalid_age(): "personality": "Ancient wisdom", "image_path": "/images/old.jpg", } - response = requests.post(f"{BASE_URL}/api/pets", json=pet) - assert response.status_code in [400, 422] # Expect rejection or handle accordingly + response = requests.post(f"{BASE_URL}/api/pets", json=pet, timeout=10) # Adding timeout + # Expect rejection or handle accordingly + assert response.status_code in [400, 422] def test_add_pet_missing_name(): + """Test case to verify adding a pet with a missing name field.""" pet = { # "name" is missing "species": "Dog", @@ -60,13 +67,14 @@ def test_add_pet_missing_name(): "personality": "Playful", "image_path": "/images/no_name.jpg", } - response = requests.post(f"{BASE_URL}/api/pets", json=pet) + response = requests.post(f"{BASE_URL}/api/pets", json=pet, timeout=10) # Adding timeout assert response.status_code == 400 assert "Missing required field: name" in response.text def test_duplicate_pet_submission(): - # first, removing any existing "Shadow" + """Test case to verify duplicate pet submission is blocked.""" + # First, removing any existing "Shadow" conn = sqlite3.connect(DB_PATH) cursor = conn.cursor() cursor.execute("DELETE FROM animals WHERE name = ?", ("Shadow",)) @@ -83,10 +91,10 @@ def test_duplicate_pet_submission(): } # First submit (should succeed) - response1 = requests.post(f"{BASE_URL}/api/pets", json=pet) + response1 = requests.post(f"{BASE_URL}/api/pets", json=pet, timeout=10) # Adding timeout assert response1.status_code == 201 # Second submit (should be blocked) - response2 = requests.post(f"{BASE_URL}/api/pets", json=pet) + response2 = requests.post(f"{BASE_URL}/api/pets", json=pet, timeout=10) # Adding timeout assert response2.status_code == 409 - assert "already exists" in response2.text + assert "already exists" in response2.text \ No newline at end of file