Skip to content

Commit

Permalink
Finish merge from origin/main
Browse files Browse the repository at this point in the history
  • Loading branch information
ris20010 committed Apr 25, 2025
2 parents 885057c + 237b43a commit 2ec1f74
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 240 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/backend-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ on:

jobs:
lint:
runs-on: ubuntu-latest
runs-on: self-hosted

steps:
- name: Checkout repository
Expand All @@ -28,15 +28,15 @@ jobs:
run: |
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
pip install -r backend/requirements.txt
- name: Run pylint on Python code
run: |
source venv/bin/activate
pylint backend/main.py backend/db.py backend/init_db.py # Add more files as needed
test:
runs-on: ubuntu-latest
runs-on: self-hosted
needs: lint # Ensure this job runs after linting

steps:
Expand All @@ -52,7 +52,7 @@ jobs:
run: |
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
pip install -r backend/requirements.txt
- name: Run tests with pytest
run: |
Expand Down
75 changes: 55 additions & 20 deletions backend/.github/workflows/backend-ci.yml
Original file line number Diff line number Diff line change
@@ -1,39 +1,74 @@
name: Backend CI
name: Backend CI Pipeline

on:
push:
paths:
- 'backend/**'
branches:
- develop
- main
pull_request:
paths:
- 'backend/**'
branches:
- develop
- main

jobs:
backend-ci:
runs-on: ubuntu-latest
lint:
runs-on: self-hosted

steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Checkout repository
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v4
uses: actions/setup-python@v2
with:
python-version: '3.11'

- name: Install dependencies
working-directory: ./backend
run: |
pip install -r requirements.txt
python -m venv venv
source venv/bin/activate
pip install -r backend/requirements.txt
- name: Run pylint on backend
working-directory: ./backend
- name: Run pylint on Python code
run: |
pylint main.py db.py init_db.py
source venv/bin/activate
pylint backend/main.py backend/db.py backend/init_db.py
- name: Run Pytest
working-directory: ./backend
test:
runs-on: self-hosted
needs: lint # Ensure this job runs after linting

steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.11'

- name: Install dependencies
run: |
python -m venv venv
source venv/bin/activate
pip install -r backend/requirements.txt
- name: Start the API server
run: |
nohup python backend/main.py & # Start the API server in the background
# Wait for the server to be ready
for i in {1..10}; do
if curl --silent --fail http://127.0.0.1:5000/; then
echo "Server is up and running."
break
fi
echo "Waiting for server to start... Attempt $i/10"
sleep 3
done
- name: Run tests with pytest
env:
BASE_URL: http://127.0.0.1:5000 # Assuming your API is running on this address
run: |
python main.py & # Run server
sleep 5 # Give it time to start
pytest tests/
source venv/bin/activate
pytest backend/tests --maxfail=1 --disable-warnings -q
35 changes: 9 additions & 26 deletions backend/db.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
"""Database utility functions for managing the pet adoption database."""

import sqlite3
import os
from utils import create_tables

def init_db():
"""Initialize the database, creating tables and inserting initial data."""
Expand All @@ -9,34 +12,14 @@ def init_db():
print("Database already initialized.")
return

# Connect to SQLite database (it will create the file if it doesn't exist)
conn = sqlite3.connect(db_path)
cursor = conn.cursor()

# Create tables
cursor.execute('''CREATE TABLE IF NOT EXISTS animals (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
species TEXT NOT NULL,
breed TEXT,
age INTEGER,
personality TEXT,
image_path TEXT,
adoption_status TEXT DEFAULT 'Available'
)''')
create_tables(conn)

cursor.execute('''CREATE TABLE IF NOT EXISTS adopters (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
password TEXT NOT NULL,
email TEXT UNIQUE,
join_date TEXT DEFAULT CURRENT_TIMESTAMP
)''')

# Insert initial data (e.g., a test animal)
cursor.execute('''INSERT INTO animals (name, species, breed, age, personality, image_path)
VALUES (?, ?, ?, ?, ?, ?)''',
('TestDog', 'Dog', 'Mixed', 2, 'Friendly', '/images/test.jpg'))
cursor = conn.cursor()
cursor.execute('''
INSERT INTO animals (name, species, breed, age, personality, image_path)
VALUES (?, ?, ?, ?, ?, ?)''',
('TestDog', 'Dog', 'Mixed', 2, 'Friendly', '/images/test.jpg'))

conn.commit()
conn.close()
Expand Down
69 changes: 29 additions & 40 deletions backend/init_db.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,42 @@
"""Script to initialize the database and populate it with initial data."""

import sqlite3
import random
import os
from utils import create_tables

def create_database_tables(conn):
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS animals (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
species TEXT NOT NULL,
breed TEXT,
age INTEGER,
personality TEXT,
image_path TEXT,
adoption_status TEXT DEFAULT 'Available'
)
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS adopters (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
password TEXT NOT NULL,
email TEXT UNIQUE,
join_date TEXT DEFAULT CURRENT_TIMESTAMP
)
''')
conn.commit()
# Constants
DB_PATH = os.path.join(os.path.dirname(__file__), 'animal_shelter.db')

def populate_initial_data(conn):
"""Populate the database with initial data for testing."""
cursor = conn.cursor()

# Sample animals data

animals = [
('Luna', 'Dog', 'Labrador Mix', 2, 'Playful and energetic', '/images/luna.jpg', 'Available'),
('Oliver', 'Cat', 'Tabby', 4, 'Independent but affectionate', '/images/oliver.jpg', 'Available'),
('Max', 'Dog', 'German Shepherd', 3, 'Loyal and intelligent', '/images/max.jpg', 'Available')
('Luna', 'Dog', 'Labrador Mix', 2,
'Playful and energetic', '/images/luna.jpg',
'Available'),
('Oliver', 'Cat', 'Tabby', 4,
'Independent but affectionate', '/images/oliver.jpg',
'Available'),
('Max', 'Dog', 'German Shepherd', 3,
'Loyal and intelligent', '/images/max.jpg',
'Available')
]

for animal in animals:
cursor.execute('''
INSERT INTO animals (name, species, breed, age, personality, image_path, adoption_status)
VALUES (?, ?, ?, ?, ?, ?, ?)
''', animal)

INSERT INTO animals (name, species, breed, age, personality, image_path, adoption_status)
VALUES (?, ?, ?, ?, ?, ?, ?)''', animal)

conn.commit()

if __name__ == "__main__":
print("Initializing pet adoption database...")
conn = sqlite3.connect('animal_shelter.db')
create_database_tables(conn)
def initialize_database(db_path):
"""Initialize the database by creating tables and populating initial data."""
conn = sqlite3.connect(db_path)
create_tables(conn)
populate_initial_data(conn)
print("Database setup complete!")
conn.close()
print("Database initialized successfully with initial data.")

if __name__ == "__main__":
initialize_database(DB_PATH)
Loading

0 comments on commit 2ec1f74

Please sign in to comment.