Skip to content

Commit

Permalink
Committing local changes before pulling
Browse files Browse the repository at this point in the history
  • Loading branch information
pdr21001 committed Apr 26, 2025
1 parent dc0a095 commit 796a8ff
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 67 deletions.
102 changes: 53 additions & 49 deletions backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions backend/pets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
14 changes: 11 additions & 3 deletions backend/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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


Expand Down
Binary file modified backend/tests/__pycache__/test_api.cpython-311-pytest-8.3.4.pyc
Binary file not shown.
32 changes: 20 additions & 12 deletions backend/tests/test_api.py
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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()
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",))
Expand All @@ -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

0 comments on commit 796a8ff

Please sign in to comment.