Skip to content

Commit

Permalink
adding test-main.py and merging prince's new db.py file
Browse files Browse the repository at this point in the history
  • Loading branch information
sed19015 committed Apr 18, 2025
1 parent f1973c4 commit f696da3
Showing 1 changed file with 215 additions and 0 deletions.
215 changes: 215 additions & 0 deletions backend/test-main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
import pytest
import json
from main import app, ANIMALS, USERS

@pytest.fixture
def client():
app.config['TESTING'] = True
with app.test_client() as client:
# Reset data before each test
global ANIMALS, USERS
ANIMALS = [
{
"id": 1,
"name": "Luna",
"species": "Dog",
"breed": "Labrador Mix",
"age": 2,
"personality": "Playful and energetic",
"image_path": "/images/luna.jpg",
"adoption_status": "Available"
},
{
"id": 2,
"name": "Oliver",
"species": "Cat",
"breed": "Tabby",
"age": 4,
"personality": "Independent but affectionate",
"image_path": "/images/oliver.jpg",
"adoption_status": "Available"
},
{
"id": 3,
"name": "Max",
"species": "Dog",
"breed": "German Shepherd",
"age": 3,
"personality": "Loyal and intelligent",
"image_path": "/images/max.jpg",
"adoption_status": "Available"
}
]
USERS = [
{
"id": 1,
"username": "admin",
"password": "admin123",
"email": "admin@example.com"
}
]
yield client

def test_homepage(client):
"""Test that the homepage returns a 200 status code."""
response = client.get('/')
assert response.status_code == 200
assert b'Animal Shelter API' in response.data

def test_register_user(client):
"""Test user registration with valid data."""
response = client.post('/api/register',
json={
'username': 'testuser',
'password': 'password123',
'email': 'test@example.com'
})
assert response.status_code == 201
data = json.loads(response.data)
assert 'Registration successful' in data['message']
assert data['user_id'] == 2 # Should be the second user

def test_register_duplicate_username(client):
"""Test registration with a duplicate username."""
# First registration
client.post('/api/register',
json={
'username': 'testuser',
'password': 'password123',
'email': 'test@example.com'
})

# Attempt to register with same username
response = client.post('/api/register',
json={
'username': 'testuser',
'password': 'different',
'email': 'different@example.com'
})
assert response.status_code == 409
data = json.loads(response.data)
assert 'Username already exists' in data['error']

def test_register_missing_fields(client):
"""Test registration with missing fields."""
response = client.post('/api/register',
json={
'username': 'testuser',
# Missing password and email
})
assert response.status_code == 400
data = json.loads(response.data)
assert 'All fields are required' in data['error']

def test_login_success(client):
"""Test successful login."""
response = client.post('/api/login',
json={
'username': 'admin',
'password': 'admin123'
})
assert response.status_code == 200
data = json.loads(response.data)
assert 'Login successful' in data['message']
assert data['username'] == 'admin'

def test_login_incorrect_password(client):
"""Test login with incorrect password."""
response = client.post('/api/login',
json={
'username': 'admin',
'password': 'wrong_password'
})
assert response.status_code == 401
data = json.loads(response.data)
assert 'Invalid username or password' in data['error']

def test_login_nonexistent_user(client):
"""Test login with a username that doesn't exist."""
response = client.post('/api/login',
json={
'username': 'nonexistent',
'password': 'anything'
})
assert response.status_code == 401
data = json.loads(response.data)
assert 'Invalid username or password' in data['error']

def test_get_animals(client):
"""Test getting all available animals."""
response = client.get('/api/animals')
assert response.status_code == 200
data = json.loads(response.data)
assert len(data) == 3
assert data[0]['name'] == 'Luna'
assert data[1]['name'] == 'Oliver'
assert data[2]['name'] == 'Max'

def test_get_animal_details(client):
"""Test getting details for a specific animal."""
response = client.get('/api/animals/2')
assert response.status_code == 200
data = json.loads(response.data)
assert data['name'] == 'Oliver'
assert data['species'] == 'Cat'
assert data['breed'] == 'Tabby'

def test_get_nonexistent_animal(client):
"""Test getting details for an animal that doesn't exist."""
response = client.get('/api/animals/999')
assert response.status_code == 404
data = json.loads(response.data)
assert 'Animal not found' in data['error']

def test_adopt_animal(client):
"""Test adopting an animal."""
response = client.post('/api/animals/1/adopt',
json={
'user_id': 1
})
assert response.status_code == 200
data = json.loads(response.data)
assert 'Adoption successful' in data['message']
assert data['animal']['adoption_status'] == 'Adopted'

# Check that the animal is no longer available
response = client.get('/api/animals')
assert response.status_code == 200
data = json.loads(response.data)
assert len(data) == 2 # Only 2 animals should now be available

def test_adopt_already_adopted_animal(client):
"""Test trying to adopt an animal that's already adopted."""
# First adoption
client.post('/api/animals/1/adopt', json={'user_id': 1})

# Attempt second adoption
response = client.post('/api/animals/1/adopt', json={'user_id': 1})
assert response.status_code == 400
data = json.loads(response.data)
assert 'Animal not available for adoption' in data['error']

def test_animal_search(client):
"""Test searching for animals with filters."""
# Search by species
response = client.get('/api/animals/search?species=Dog')
assert response.status_code == 200
data = json.loads(response.data)
assert len(data) == 2
assert all(animal['species'] == 'Dog' for animal in data)

# Search by age range
response = client.get('/api/animals/search?min_age=3&max_age=4')
assert response.status_code == 200
data = json.loads(response.data)
assert len(data) == 2
assert all(3 <= animal['age'] <= 4 for animal in data)

# Combined search
response = client.get('/api/animals/search?species=Cat&min_age=3')
assert response.status_code == 200
data = json.loads(response.data)
assert len(data) == 1
assert data[0]['name'] == 'Oliver'
assert data[0]['species'] == 'Cat'
assert data[0]['age'] == 4

0 comments on commit f696da3

Please sign in to comment.