From b1928a2d7d7171d1dfad4242a556cba1bd9154e6 Mon Sep 17 00:00:00 2001 From: Prince D Rusweka Rwabongoya Date: Wed, 23 Apr 2025 17:40:12 -0400 Subject: [PATCH 01/33] Update backend-ci.yml --- .github/workflows/backend-ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/backend-ci.yml b/.github/workflows/backend-ci.yml index 5e8d02b..5c3ae3e 100644 --- a/.github/workflows/backend-ci.yml +++ b/.github/workflows/backend-ci.yml @@ -13,7 +13,7 @@ on: jobs: lint: - runs-on: ubuntu-latest + runs-on: self-hosted steps: - name: Checkout repository @@ -36,7 +36,7 @@ jobs: 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: From 651708cd636502a81e7dc385709e72e87cc3c727 Mon Sep 17 00:00:00 2001 From: Prince D Rusweka Rwabongoya Date: Wed, 23 Apr 2025 17:45:00 -0400 Subject: [PATCH 02/33] Update backend-ci.yml --- .github/workflows/backend-ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/backend-ci.yml b/.github/workflows/backend-ci.yml index 5c3ae3e..eec765c 100644 --- a/.github/workflows/backend-ci.yml +++ b/.github/workflows/backend-ci.yml @@ -28,7 +28,7 @@ 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: | @@ -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: | From 6d217bcffe02ade39b95692dd38cdd75f79707eb Mon Sep 17 00:00:00 2001 From: Prince D Rusweka Rwabongoya Date: Wed, 23 Apr 2025 17:47:33 -0400 Subject: [PATCH 03/33] Fix Flask version for CI compatibility --- backend/requirements.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/requirements.txt b/backend/requirements.txt index a9f380d..dc0d16a 100644 --- a/backend/requirements.txt +++ b/backend/requirements.txt @@ -1,4 +1,4 @@ -Flask==3.1.1 +Flask==3.1.0 Flask-Cors==5.0.2 flasgger==0.9.7.1 Werkzeug==3.1.3 @@ -9,4 +9,4 @@ click==8.1.8 blinker==1.9.0 PyYAML==6.0.2 mistune==3.1.2 -jsonschema==4.24.0 \ No newline at end of file +jsonschema==4.24.0 From f3e2db90664deb0d4b5a7c3032882b62a93b3ff1 Mon Sep 17 00:00:00 2001 From: Prince D Rusweka Rwabongoya Date: Wed, 23 Apr 2025 17:50:01 -0400 Subject: [PATCH 04/33] Fixing requirements.txt CI compatabilities --- backend/requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/requirements.txt b/backend/requirements.txt index dc0d16a..b2b2046 100644 --- a/backend/requirements.txt +++ b/backend/requirements.txt @@ -1,5 +1,5 @@ Flask==3.1.0 -Flask-Cors==5.0.2 +Flask-Cors==5.0.1 flasgger==0.9.7.1 Werkzeug==3.1.3 Jinja2==3.1.5 From 5915be0d4b857f5250eaab74ae0ce6959a672608 Mon Sep 17 00:00:00 2001 From: Prince D Rusweka Rwabongoya Date: Wed, 23 Apr 2025 17:51:23 -0400 Subject: [PATCH 05/33] Update requirements.txt --- backend/requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/requirements.txt b/backend/requirements.txt index b2b2046..2b74d94 100644 --- a/backend/requirements.txt +++ b/backend/requirements.txt @@ -9,4 +9,4 @@ click==8.1.8 blinker==1.9.0 PyYAML==6.0.2 mistune==3.1.2 -jsonschema==4.24.0 +jsonschema==4.23.0 From ef0303b9e780f1ef2c3a64bf8d1c720ecf28f71e Mon Sep 17 00:00:00 2001 From: Prince D Rusweka Rwabongoya Date: Wed, 23 Apr 2025 17:56:12 -0400 Subject: [PATCH 06/33] Update main.py --- backend/main.py | 117 ++++++------------------------------------------ 1 file changed, 13 insertions(+), 104 deletions(-) diff --git a/backend/main.py b/backend/main.py index f0ab6ce..13243e8 100644 --- a/backend/main.py +++ b/backend/main.py @@ -2,7 +2,6 @@ from flasgger import Swagger import sqlite3 import os -from backend.pets import add_pet, update_pet, delete_pet, search_pets, reset_pets # Initialize Flask app app = Flask(__name__) @@ -15,6 +14,7 @@ # Helper function to get a database connection def get_db(): + """Establish a connection to the SQLite database.""" conn = sqlite3.connect(DATABASE) conn.row_factory = sqlite3.Row # Allows row access by column name return conn @@ -55,40 +55,14 @@ def init_db(): INSERT INTO animals (name, species, breed, age, personality, image_path, adoption_status) VALUES (?, ?, ?, ?, ?, ?, ?) ''', pets) - + conn.commit() conn.close() # API Routes @app.route('/api/pets', methods=['GET']) def get_pets(): - """Retrieve all pets - --- - responses: - 200: - description: A list of pets - schema: - type: array - items: - type: object - properties: - id: - type: integer - name: - type: string - species: - type: string - breed: - type: string - age: - type: integer - personality: - type: string - image_path: - type: string - adoption_status: - type: string - """ + """Retrieve all pets""" try: conn = get_db() cursor = conn.cursor() @@ -100,53 +74,18 @@ def get_pets(): conn.close() return jsonify(pet_list), 200 - except Exception as e: + except sqlite3.Error as e: print(f"An error occurred: {e}") return jsonify({'error': 'Internal Server Error'}), 500 @app.route('/', methods=['GET']) def home(): + """Home route to welcome users to the API.""" return jsonify({'message': 'Welcome to the Pet API! Visit /apidocs for the Swagger documentation.'}) - - @app.route('/api/pets/', methods=['GET']) def get_pet(pet_id): - """Retrieve a single pet by ID - --- - parameters: - - name: pet_id - in: path - type: integer - required: true - description: The unique ID of the pet - responses: - 200: - description: A pet - schema: - type: object - properties: - id: - type: integer - name: - type: string - species: - type: string - breed: - type: string - age: - type: integer - personality: - type: string - image_path: - type: string - adoption_status: - type: string - 404: - description: Pet not found - 500: - description: Internal Server Error - """ + """Retrieve a single pet by ID""" try: conn = get_db() cursor = conn.cursor() @@ -161,43 +100,13 @@ def get_pet(pet_id): conn.close() return jsonify(pet_dict), 200 - except Exception as e: + except sqlite3.Error as e: print(f"An error occurred: {e}") return jsonify({'error': 'Internal Server Error'}), 500 @app.route('/api/pets', methods=['POST']) def add_pet(): - """Add a new pet - --- - parameters: - - name: pet - in: body - required: true - schema: - type: object - properties: - name: - type: string - species: - type: string - breed: - type: string - age: - type: integer - personality: - type: string - image_path: - type: string - adoption_status: - type: string - responses: - 201: - description: Pet created successfully - 400: - description: Invalid pet data - 500: - description: Internal Server Error - """ + """Add a new pet""" try: pet_data = request.get_json() @@ -209,26 +118,26 @@ def add_pet(): conn = get_db() cursor = conn.cursor() - + cursor.execute(''' INSERT INTO animals (name, species, breed, age, personality, image_path, adoption_status) VALUES (?, ?, ?, ?, ?, ?, ?) - ''', (pet_data['name'], pet_data['species'], pet_data['breed'], pet_data['age'], + ''', (pet_data['name'], pet_data['species'], pet_data['breed'], pet_data['age'], pet_data['personality'], pet_data.get('image_path', '/images/default.jpg'), 'Available')) - + conn.commit() conn.close() return jsonify({'message': 'Pet created successfully'}), 201 - except Exception as e: + except sqlite3.Error as e: print(f"An error occurred: {e}") return jsonify({'error': 'Internal Server Error'}), 500 # Initialize database on first run @app.before_first_request def before_first_request(): + """Initialize the database before the first request.""" init_db() if __name__ == "__main__": app.run(debug=True, use_reloader=False) - From 3f06c251be45a1e91956adcfb85f441422a71dc5 Mon Sep 17 00:00:00 2001 From: Prince D Rusweka Rwabongoya Date: Wed, 23 Apr 2025 17:56:44 -0400 Subject: [PATCH 07/33] Update init_db.py --- backend/init_db.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/backend/init_db.py b/backend/init_db.py index 8c52eea..3ff33ec 100644 --- a/backend/init_db.py +++ b/backend/init_db.py @@ -1,7 +1,7 @@ import sqlite3 -import random def create_database_tables(conn): + """Create tables for the database if they do not exist.""" cursor = conn.cursor() cursor.execute(''' CREATE TABLE IF NOT EXISTS animals ( @@ -27,21 +27,22 @@ def create_database_tables(conn): conn.commit() 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') ] - + for animal in animals: cursor.execute(''' INSERT INTO animals (name, species, breed, age, personality, image_path, adoption_status) VALUES (?, ?, ?, ?, ?, ?, ?) ''', animal) - + conn.commit() if __name__ == "__main__": From 7f0ae19ca320fdc3d5fddbe4e401557ee5d7e83a Mon Sep 17 00:00:00 2001 From: Prince D Rusweka Rwabongoya Date: Wed, 23 Apr 2025 17:57:28 -0400 Subject: [PATCH 08/33] Update db.py From c2c78ac482f24a664f0cd44858e28f97d75340f9 Mon Sep 17 00:00:00 2001 From: Prince D Rusweka Rwabongoya Date: Wed, 23 Apr 2025 18:01:47 -0400 Subject: [PATCH 09/33] Update main.py --- backend/main.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/backend/main.py b/backend/main.py index 13243e8..4a86eac 100644 --- a/backend/main.py +++ b/backend/main.py @@ -6,7 +6,6 @@ # Initialize Flask app app = Flask(__name__) -# Initialize Swagger (Flasgger) Swagger(app) # SQLite database file @@ -62,7 +61,7 @@ def init_db(): # API Routes @app.route('/api/pets', methods=['GET']) def get_pets(): - """Retrieve all pets""" + """Retrieve all pets.""" try: conn = get_db() cursor = conn.cursor() @@ -85,7 +84,7 @@ def home(): @app.route('/api/pets/', methods=['GET']) def get_pet(pet_id): - """Retrieve a single pet by ID""" + """Retrieve a single pet by ID.""" try: conn = get_db() cursor = conn.cursor() @@ -106,7 +105,7 @@ def get_pet(pet_id): @app.route('/api/pets', methods=['POST']) def add_pet(): - """Add a new pet""" + """Add a new pet.""" try: pet_data = request.get_json() From 650c2f6b835fac409b24e9873c93cb9820f5761d Mon Sep 17 00:00:00 2001 From: Prince D Rusweka Rwabongoya Date: Wed, 23 Apr 2025 18:02:21 -0400 Subject: [PATCH 10/33] Update init_db.py --- backend/init_db.py | 21 +++------------------ 1 file changed, 3 insertions(+), 18 deletions(-) diff --git a/backend/init_db.py b/backend/init_db.py index 3ff33ec..7ee3ac7 100644 --- a/backend/init_db.py +++ b/backend/init_db.py @@ -1,3 +1,5 @@ +"""Script to initialize the database and populate with initial data.""" + import sqlite3 def create_database_tables(conn): @@ -34,21 +36,4 @@ def populate_initial_data(conn): 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') - ] - - for animal in animals: - cursor.execute(''' - 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) - populate_initial_data(conn) - print("Database setup complete!") - conn.close() + From abe3e8fc61e250738b437c5efd83031bfa20be10 Mon Sep 17 00:00:00 2001 From: Prince D Rusweka Rwabongoya Date: Wed, 23 Apr 2025 18:03:05 -0400 Subject: [PATCH 11/33] Update db.py From c7b51af13adc022717240d0e792229bc485905e1 Mon Sep 17 00:00:00 2001 From: Prince D Rusweka Rwabongoya Date: Wed, 23 Apr 2025 18:07:35 -0400 Subject: [PATCH 12/33] Update init_db.py --- backend/init_db.py | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/backend/init_db.py b/backend/init_db.py index 7ee3ac7..84d5475 100644 --- a/backend/init_db.py +++ b/backend/init_db.py @@ -36,4 +36,31 @@ def populate_initial_data(conn): 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') + ] + + for animal in animals: + cursor.execute(''' + INSERT INTO animals (name, species, breed, age, personality, image_path, adoption_status) + VALUES (?, ?, ?, ?, ?, ?, ?) + ''', animal) + + conn.commit() + +def initialize_database(db_path): + """Initialize the database by creating tables and populating initial data.""" + conn = sqlite3.connect(db_path) + + # Create the database tables + create_database_tables(conn) + + # Populate the database with initial data + populate_initial_data(conn) + + conn.close() + print("Database initialized successfully with initial data.") + +if __name__ == "__main__": + # Path to the database + db_path = 'animal_shelter.db' + initialize_database(db_path) From 8385162dd3e2371af24316d4c7eb3b2b86f0b866 Mon Sep 17 00:00:00 2001 From: Prince D Rusweka Rwabongoya Date: Wed, 23 Apr 2025 18:10:04 -0400 Subject: [PATCH 13/33] Update main.py --- backend/main.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/backend/main.py b/backend/main.py index 4a86eac..e4c21af 100644 --- a/backend/main.py +++ b/backend/main.py @@ -1,8 +1,12 @@ -from flask import Flask, jsonify, request -from flasgger import Swagger +"""This is the main Flask application for the pet adoption API.""" + + import sqlite3 import os +from flask import Flask, jsonify, request +from flasgger import Swagger + # Initialize Flask app app = Flask(__name__) @@ -80,7 +84,8 @@ def get_pets(): @app.route('/', methods=['GET']) def home(): """Home route to welcome users to the API.""" - return jsonify({'message': 'Welcome to the Pet API! Visit /apidocs for the Swagger documentation.'}) + return jsonify({'message': + 'Welcome to the Pet API! Visit /apidocs for the Swagger documentation.'}) @app.route('/api/pets/', methods=['GET']) def get_pet(pet_id): @@ -121,8 +126,11 @@ def add_pet(): cursor.execute(''' INSERT INTO animals (name, species, breed, age, personality, image_path, adoption_status) VALUES (?, ?, ?, ?, ?, ?, ?) - ''', (pet_data['name'], pet_data['species'], pet_data['breed'], pet_data['age'], - pet_data['personality'], pet_data.get('image_path', '/images/default.jpg'), 'Available')) + ''', (pet_data['name'], pet_data['species'], + pet_data['breed'], pet_data['age'], + pet_data['personality'], + pet_data.get('image_path', '/images/default.jpg'), + 'Available')) conn.commit() conn.close() From 4ff028da27e0eec8fa93d9e88ec074c9824bf3d4 Mon Sep 17 00:00:00 2001 From: Prince D Rusweka Rwabongoya Date: Wed, 23 Apr 2025 18:10:26 -0400 Subject: [PATCH 14/33] Update db.py --- backend/db.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/backend/db.py b/backend/db.py index 93f618f..0d2cdaa 100644 --- a/backend/db.py +++ b/backend/db.py @@ -1,3 +1,5 @@ +"""Database utility functions for managing the pet adoption database.""" + import sqlite3 import os From 825bc468a7eb21ac9e0bd1f66ecfcea224b35b01 Mon Sep 17 00:00:00 2001 From: Prince D Rusweka Rwabongoya Date: Wed, 23 Apr 2025 18:17:47 -0400 Subject: [PATCH 15/33] Update db.py --- backend/db.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/backend/db.py b/backend/db.py index 0d2cdaa..ab21c6e 100644 --- a/backend/db.py +++ b/backend/db.py @@ -24,7 +24,8 @@ def init_db(): age INTEGER, personality TEXT, image_path TEXT, - adoption_status TEXT DEFAULT 'Available' + adoption_status TEXT + DEFAULT 'Available' )''') cursor.execute('''CREATE TABLE IF NOT EXISTS adopters ( From 0f3388db149efa22c58fb9cc5e2470e9a1c5afbe Mon Sep 17 00:00:00 2001 From: Prince D Rusweka Rwabongoya Date: Wed, 23 Apr 2025 18:20:33 -0400 Subject: [PATCH 16/33] Update init_db.py --- backend/init_db.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/backend/init_db.py b/backend/init_db.py index 84d5475..c70bbde 100644 --- a/backend/init_db.py +++ b/backend/init_db.py @@ -1,6 +1,11 @@ -"""Script to initialize the database and populate with initial data.""" +"""Script to initialize the database and populate it with initial data.""" import sqlite3 +import os + +# Constants +DB_PATH = os.path.join(os.path.dirname(__file__), 'animal_shelter.db') + def create_database_tables(conn): """Create tables for the database if they do not exist.""" @@ -28,6 +33,7 @@ def create_database_tables(conn): ''') conn.commit() + def populate_initial_data(conn): """Populate the database with initial data for testing.""" cursor = conn.cursor() @@ -47,6 +53,7 @@ def populate_initial_data(conn): conn.commit() + def initialize_database(db_path): """Initialize the database by creating tables and populating initial data.""" conn = sqlite3.connect(db_path) @@ -60,7 +67,6 @@ def initialize_database(db_path): conn.close() print("Database initialized successfully with initial data.") + if __name__ == "__main__": - # Path to the database - db_path = 'animal_shelter.db' - initialize_database(db_path) + initialize_database(DB_PATH) From d6277a1a1c724159d53ada37c72849aad4bcad9a Mon Sep 17 00:00:00 2001 From: Prince D Rusweka Rwabongoya Date: Wed, 23 Apr 2025 18:25:50 -0400 Subject: [PATCH 17/33] Update init_db.py --- backend/init_db.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/backend/init_db.py b/backend/init_db.py index c70bbde..98195e5 100644 --- a/backend/init_db.py +++ b/backend/init_db.py @@ -10,6 +10,8 @@ def create_database_tables(conn): """Create tables for the database if they do not exist.""" cursor = conn.cursor() + + # Animals table creation cursor.execute(''' CREATE TABLE IF NOT EXISTS animals ( id INTEGER PRIMARY KEY AUTOINCREMENT, @@ -22,6 +24,8 @@ def create_database_tables(conn): adoption_status TEXT DEFAULT 'Available' ) ''') + + # Adopters table creation cursor.execute(''' CREATE TABLE IF NOT EXISTS adopters ( id INTEGER PRIMARY KEY AUTOINCREMENT, @@ -31,6 +35,7 @@ def create_database_tables(conn): join_date TEXT DEFAULT CURRENT_TIMESTAMP ) ''') + conn.commit() From 767c8d9ad38c8a9f28947b112cbac6d78fe88f84 Mon Sep 17 00:00:00 2001 From: Prince D Rusweka Rwabongoya Date: Wed, 23 Apr 2025 18:26:15 -0400 Subject: [PATCH 18/33] Update db.py --- backend/db.py | 50 ++++++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/backend/db.py b/backend/db.py index ab21c6e..410935b 100644 --- a/backend/db.py +++ b/backend/db.py @@ -1,5 +1,3 @@ -"""Database utility functions for managing the pet adoption database.""" - import sqlite3 import os @@ -16,30 +14,34 @@ def init_db(): 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' - )''') - - 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 - )''') + 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 + ) + ''') # 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.execute(''' + INSERT INTO animals (name, species, breed, age, personality, image_path) + VALUES (?, ?, ?, ?, ?, ?) + ''', ('TestDog', 'Dog', 'Mixed', 2, 'Friendly', '/images/test.jpg')) conn.commit() conn.close() From 852c901954e931dbf08b9ee3990393cc561ee2b4 Mon Sep 17 00:00:00 2001 From: Prince D Rusweka Rwabongoya Date: Wed, 23 Apr 2025 18:27:51 -0400 Subject: [PATCH 19/33] Update db.py --- backend/db.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/backend/db.py b/backend/db.py index 410935b..3b88968 100644 --- a/backend/db.py +++ b/backend/db.py @@ -1,3 +1,7 @@ +""" +Database utility functions for managing the pet adoption database. +""" + import sqlite3 import os From bff7b580db17b42779232d9380f21a1efa904434 Mon Sep 17 00:00:00 2001 From: Prince D Rusweka Rwabongoya Date: Wed, 23 Apr 2025 18:29:10 -0400 Subject: [PATCH 20/33] Update init_db.py From 604d71e1f9edb58d4f8550b701447734fa0e525f Mon Sep 17 00:00:00 2001 From: Prince D Rusweka Rwabongoya Date: Wed, 23 Apr 2025 18:35:50 -0400 Subject: [PATCH 21/33] utils.py for refactoring --- backend/utils.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 backend/utils.py diff --git a/backend/utils.py b/backend/utils.py new file mode 100644 index 0000000..cfe1eaf --- /dev/null +++ b/backend/utils.py @@ -0,0 +1,31 @@ +# utils.py +import sqlite3 + +def create_tables(conn): + """Create tables for the database if they do not exist.""" + 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() From e007e4facb16f0defc56acccbf8eccef73fa756e Mon Sep 17 00:00:00 2001 From: Prince D Rusweka Rwabongoya Date: Wed, 23 Apr 2025 18:37:26 -0400 Subject: [PATCH 22/33] Update init_db.py --- backend/init_db.py | 38 ++------------------------------------ 1 file changed, 2 insertions(+), 36 deletions(-) diff --git a/backend/init_db.py b/backend/init_db.py index 98195e5..7b63e84 100644 --- a/backend/init_db.py +++ b/backend/init_db.py @@ -2,43 +2,11 @@ import sqlite3 import os +from utils import create_tables # Constants DB_PATH = os.path.join(os.path.dirname(__file__), 'animal_shelter.db') - -def create_database_tables(conn): - """Create tables for the database if they do not exist.""" - cursor = conn.cursor() - - # Animals table creation - 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' - ) - ''') - - # Adopters table creation - 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() - - def populate_initial_data(conn): """Populate the database with initial data for testing.""" cursor = conn.cursor() @@ -58,13 +26,12 @@ def populate_initial_data(conn): conn.commit() - def initialize_database(db_path): """Initialize the database by creating tables and populating initial data.""" conn = sqlite3.connect(db_path) # Create the database tables - create_database_tables(conn) + create_tables(conn) # Populate the database with initial data populate_initial_data(conn) @@ -72,6 +39,5 @@ def initialize_database(db_path): conn.close() print("Database initialized successfully with initial data.") - if __name__ == "__main__": initialize_database(DB_PATH) From 4cd69298c949e2fc44aab0587336c39d0721810e Mon Sep 17 00:00:00 2001 From: Prince D Rusweka Rwabongoya Date: Wed, 23 Apr 2025 18:37:57 -0400 Subject: [PATCH 23/33] Update main.py --- backend/main.py | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/backend/main.py b/backend/main.py index e4c21af..2f52d4e 100644 --- a/backend/main.py +++ b/backend/main.py @@ -1,11 +1,10 @@ """This is the main Flask application for the pet adoption API.""" - import sqlite3 import os - from flask import Flask, jsonify, request from flasgger import Swagger +from utils import create_tables # Initialize Flask app app = Flask(__name__) @@ -27,21 +26,7 @@ def init_db(): """Create tables and add initial data if they don't exist.""" if not os.path.exists(DATABASE): conn = get_db() - cursor = conn.cursor() - - # Create the animals table - 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) # Insert initial pet data for testing pets = [ @@ -53,7 +38,8 @@ def init_db(): ('Bella', 'Dog', 'Bulldog', 2, 'Gentle', '/images/bella.jpg', 'Available') ] - # Insert data into the animals table + conn = get_db() + cursor = conn.cursor() cursor.executemany(''' INSERT INTO animals (name, species, breed, age, personality, image_path, adoption_status) VALUES (?, ?, ?, ?, ?, ?, ?) From 1c1bdb26cd44eae1fecad2840a268c899df9316f Mon Sep 17 00:00:00 2001 From: Prince D Rusweka Rwabongoya Date: Wed, 23 Apr 2025 18:38:34 -0400 Subject: [PATCH 24/33] Update db.py --- backend/db.py | 39 +++++++-------------------------------- 1 file changed, 7 insertions(+), 32 deletions(-) diff --git a/backend/db.py b/backend/db.py index 3b88968..994231d 100644 --- a/backend/db.py +++ b/backend/db.py @@ -1,9 +1,8 @@ -""" -Database utility functions for managing the pet adoption database. -""" +"""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.""" @@ -15,37 +14,13 @@ def init_db(): # 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' - ) - ''') - - 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 - ) - ''') + create_tables(conn) # 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() From a00fa211c74eb2b4757a1ed84b7486de68df7346 Mon Sep 17 00:00:00 2001 From: Prince D Rusweka Rwabongoya Date: Wed, 23 Apr 2025 18:46:01 -0400 Subject: [PATCH 25/33] Update main.py --- backend/main.py | 47 ++++++++++------------------------------------- 1 file changed, 10 insertions(+), 37 deletions(-) diff --git a/backend/main.py b/backend/main.py index 2f52d4e..865ba36 100644 --- a/backend/main.py +++ b/backend/main.py @@ -6,29 +6,21 @@ from flasgger import Swagger from utils import create_tables -# Initialize Flask app app = Flask(__name__) - Swagger(app) -# SQLite database file DATABASE = 'animal_shelter.db' -# Helper function to get a database connection def get_db(): - """Establish a connection to the SQLite database.""" conn = sqlite3.connect(DATABASE) - conn.row_factory = sqlite3.Row # Allows row access by column name + conn.row_factory = sqlite3.Row return conn -# Database initialization function to create tables and initial data def init_db(): - """Create tables and add initial data if they don't exist.""" if not os.path.exists(DATABASE): conn = get_db() create_tables(conn) - # Insert initial pet data for testing pets = [ ('Buddy', 'Dog', 'Golden Retriever', 3, 'Friendly', '/images/buddy.jpg', 'Available'), ('Whiskers', 'Cat', 'Siamese', 2, 'Independent', '/images/whiskers.jpg', 'Available'), @@ -38,30 +30,23 @@ def init_db(): ('Bella', 'Dog', 'Bulldog', 2, 'Gentle', '/images/bella.jpg', 'Available') ] - conn = get_db() cursor = conn.cursor() cursor.executemany(''' INSERT INTO animals (name, species, breed, age, personality, image_path, adoption_status) - VALUES (?, ?, ?, ?, ?, ?, ?) - ''', pets) + VALUES (?, ?, ?, ?, ?, ?, ?)''', pets) conn.commit() conn.close() -# API Routes @app.route('/api/pets', methods=['GET']) def get_pets(): - """Retrieve all pets.""" try: conn = get_db() cursor = conn.cursor() cursor.execute("SELECT * FROM animals") pets = cursor.fetchall() - - # Convert rows to a list of dictionaries pet_list = [dict(pet) for pet in pets] conn.close() - return jsonify(pet_list), 200 except sqlite3.Error as e: print(f"An error occurred: {e}") @@ -69,13 +54,10 @@ def get_pets(): @app.route('/', methods=['GET']) def home(): - """Home route to welcome users to the API.""" - return jsonify({'message': - 'Welcome to the Pet API! Visit /apidocs for the Swagger documentation.'}) + return jsonify({'message': 'Welcome to the Pet API! Visit /apidocs for the Swagger documentation.'}) @app.route('/api/pets/', methods=['GET']) def get_pet(pet_id): - """Retrieve a single pet by ID.""" try: conn = get_db() cursor = conn.cursor() @@ -85,10 +67,8 @@ def get_pet(pet_id): if pet is None: return jsonify({'error': 'Pet not found'}), 404 - # Convert the SQLite row object to a dictionary pet_dict = {key: pet[key] for key in pet.keys()} conn.close() - return jsonify(pet_dict), 200 except sqlite3.Error as e: print(f"An error occurred: {e}") @@ -96,41 +76,34 @@ def get_pet(pet_id): @app.route('/api/pets', methods=['POST']) def add_pet(): - """Add a new pet.""" try: pet_data = request.get_json() - - # Validate required fields required_fields = ['name', 'species', 'breed', 'age', 'personality'] + for field in required_fields: if field not in pet_data: return jsonify({'error': f'Missing required field: {field}'}), 400 conn = get_db() cursor = conn.cursor() - cursor.execute(''' INSERT INTO animals (name, species, breed, age, personality, image_path, adoption_status) - VALUES (?, ?, ?, ?, ?, ?, ?) - ''', (pet_data['name'], pet_data['species'], - pet_data['breed'], pet_data['age'], - pet_data['personality'], - pet_data.get('image_path', '/images/default.jpg'), - 'Available')) + VALUES (?, ?, ?, ?, ?, ?, ?)''', + (pet_data['name'], pet_data['species'], pet_data['breed'], + pet_data['age'], pet_data['personality'], + pet_data.get('image_path', '/images/default.jpg'), 'Available')) conn.commit() conn.close() - return jsonify({'message': 'Pet created successfully'}), 201 except sqlite3.Error as e: print(f"An error occurred: {e}") return jsonify({'error': 'Internal Server Error'}), 500 -# Initialize database on first run -@app.before_first_request def before_first_request(): - """Initialize the database before the first request.""" init_db() +app.before_request_funcs.setdefault(None, []).append(before_first_request) + if __name__ == "__main__": app.run(debug=True, use_reloader=False) From fd5a5693e0e04e1808c7137372559e579a5d7d03 Mon Sep 17 00:00:00 2001 From: Prince D Rusweka Rwabongoya Date: Wed, 23 Apr 2025 18:46:28 -0400 Subject: [PATCH 26/33] Update db.py --- backend/db.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/backend/db.py b/backend/db.py index 994231d..7945515 100644 --- a/backend/db.py +++ b/backend/db.py @@ -12,15 +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) create_tables(conn) - # Insert initial data (e.g., a test animal) cursor = conn.cursor() - cursor.execute('''INSERT INTO animals (name, species, breed, age, personality, image_path) - VALUES (?, ?, ?, ?, ?, ?)''', - ('TestDog', 'Dog', 'Mixed', 2, 'Friendly', '/images/test.jpg')) + 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() From e90d151f4934ebcc60cf632f9108d550d4754dbb Mon Sep 17 00:00:00 2001 From: Prince D Rusweka Rwabongoya Date: Wed, 23 Apr 2025 18:46:49 -0400 Subject: [PATCH 27/33] Update init_db.py --- backend/init_db.py | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/backend/init_db.py b/backend/init_db.py index 7b63e84..b7b8776 100644 --- a/backend/init_db.py +++ b/backend/init_db.py @@ -11,31 +11,24 @@ 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') ] - + for animal in animals: cursor.execute(''' INSERT INTO animals (name, species, breed, age, personality, image_path, adoption_status) - VALUES (?, ?, ?, ?, ?, ?, ?) - ''', animal) - + VALUES (?, ?, ?, ?, ?, ?, ?)''', animal) + conn.commit() def initialize_database(db_path): """Initialize the database by creating tables and populating initial data.""" conn = sqlite3.connect(db_path) - - # Create the database tables create_tables(conn) - - # Populate the database with initial data populate_initial_data(conn) - conn.close() print("Database initialized successfully with initial data.") From bfa39c4e9238989cc354e7a69ea484adc80cd954 Mon Sep 17 00:00:00 2001 From: Prince D Rusweka Rwabongoya Date: Wed, 23 Apr 2025 18:50:55 -0400 Subject: [PATCH 28/33] Update main.py --- backend/main.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/backend/main.py b/backend/main.py index 865ba36..435fc78 100644 --- a/backend/main.py +++ b/backend/main.py @@ -12,11 +12,13 @@ DATABASE = 'animal_shelter.db' def get_db(): + """Establish a connection to the SQLite database.""" conn = sqlite3.connect(DATABASE) conn.row_factory = sqlite3.Row return conn def init_db(): + """Initialize the database and insert sample data if not already present.""" if not os.path.exists(DATABASE): conn = get_db() create_tables(conn) @@ -40,6 +42,7 @@ def init_db(): @app.route('/api/pets', methods=['GET']) def get_pets(): + """Retrieve and return all pets from the database.""" try: conn = get_db() cursor = conn.cursor() @@ -54,10 +57,14 @@ def get_pets(): @app.route('/', methods=['GET']) def home(): - return jsonify({'message': 'Welcome to the Pet API! Visit /apidocs for the Swagger documentation.'}) + """Return a welcome message for the API.""" + return jsonify({ + 'message': 'Welcome to the Pet API! Visit /apidocs for the Swagger documentation.' + }) @app.route('/api/pets/', methods=['GET']) def get_pet(pet_id): + """Retrieve a pet by its ID.""" try: conn = get_db() cursor = conn.cursor() @@ -76,6 +83,7 @@ def get_pet(pet_id): @app.route('/api/pets', methods=['POST']) def add_pet(): + """Add a new pet to the database.""" try: pet_data = request.get_json() required_fields = ['name', 'species', 'breed', 'age', 'personality'] @@ -101,6 +109,7 @@ def add_pet(): return jsonify({'error': 'Internal Server Error'}), 500 def before_first_request(): + """Run once before the first request to initialize the database.""" init_db() app.before_request_funcs.setdefault(None, []).append(before_first_request) From ef20f8e7c214c8456899126d51f605cc166a3ece Mon Sep 17 00:00:00 2001 From: Prince D Rusweka Rwabongoya Date: Wed, 23 Apr 2025 18:51:51 -0400 Subject: [PATCH 29/33] Update init_db.py --- backend/init_db.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/backend/init_db.py b/backend/init_db.py index b7b8776..8908b2a 100644 --- a/backend/init_db.py +++ b/backend/init_db.py @@ -12,9 +12,15 @@ def populate_initial_data(conn): cursor = conn.cursor() 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: From 03cf79005562effeb658a67c9021e07b322d4d6f Mon Sep 17 00:00:00 2001 From: Prince D Rusweka Rwabongoya Date: Wed, 23 Apr 2025 19:17:11 -0400 Subject: [PATCH 30/33] Update backend-ci.yml --- backend/.github/workflows/backend-ci.yml | 69 +++++++++++++++++------- 1 file changed, 49 insertions(+), 20 deletions(-) diff --git a/backend/.github/workflows/backend-ci.yml b/backend/.github/workflows/backend-ci.yml index 93a0191..607d9b2 100644 --- a/backend/.github/workflows/backend-ci.yml +++ b/backend/.github/workflows/backend-ci.yml @@ -1,39 +1,68 @@ -name: Backend CI +name: Backend CI Pipeline +# Trigger workflow on push or pull request to develop and main branches 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' # Adjust Python version if needed + + - name: Install dependencies + run: | + python -m venv venv + source venv/bin/activate + 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: 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 - 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: Start the API server run: | - pylint main.py db.py init_db.py + # Start the API server in the background + nohup python backend/main.py & # Replace with the command to start your API + sleep 5 # Wait for the server to be ready (adjust this if needed) - - name: Run Pytest - working-directory: ./backend + - 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 From 824bc9c10524378a390dc601f67f61804ec5dae0 Mon Sep 17 00:00:00 2001 From: Prince D Rusweka Rwabongoya Date: Wed, 23 Apr 2025 19:17:36 -0400 Subject: [PATCH 31/33] Update backend-ci.yml From 1d947119ae14958cf8a6eb5a662325eb73c8b8a1 Mon Sep 17 00:00:00 2001 From: Prince D Rusweka Rwabongoya Date: Wed, 23 Apr 2025 20:05:57 -0400 Subject: [PATCH 32/33] Update backend-ci.yml --- backend/.github/workflows/backend-ci.yml | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/backend/.github/workflows/backend-ci.yml b/backend/.github/workflows/backend-ci.yml index 607d9b2..e387e6b 100644 --- a/backend/.github/workflows/backend-ci.yml +++ b/backend/.github/workflows/backend-ci.yml @@ -1,6 +1,5 @@ name: Backend CI Pipeline -# Trigger workflow on push or pull request to develop and main branches on: push: branches: @@ -22,7 +21,7 @@ jobs: - name: Set up Python uses: actions/setup-python@v2 with: - python-version: '3.11' # Adjust Python version if needed + python-version: '3.11' - name: Install dependencies run: | @@ -33,11 +32,11 @@ jobs: - 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 + pylint backend/main.py backend/db.py backend/init_db.py test: runs-on: self-hosted - needs: lint # Ensure this job runs after linting + needs: lint steps: - name: Checkout repository @@ -54,15 +53,15 @@ jobs: source venv/bin/activate pip install -r backend/requirements.txt - - name: Start the API server + - name: Start API server in background run: | - # Start the API server in the background - nohup python backend/main.py & # Replace with the command to start your API - sleep 5 # Wait for the server to be ready (adjust this if needed) + source venv/bin/activate + nohup python backend/main.py > server.log 2>&1 & + sleep 5 - name: Run tests with pytest env: - BASE_URL: http://127.0.0.1:5000 # Assuming your API is running on this address + BASE_URL: http://127.0.0.1:5000 run: | source venv/bin/activate pytest backend/tests --maxfail=1 --disable-warnings -q From 8294fbabab995e3adb615aa724ff600da349a840 Mon Sep 17 00:00:00 2001 From: Prince D Rusweka Rwabongoya Date: Wed, 23 Apr 2025 20:32:01 -0400 Subject: [PATCH 33/33] Update backend-ci.yml --- backend/.github/workflows/backend-ci.yml | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/backend/.github/workflows/backend-ci.yml b/backend/.github/workflows/backend-ci.yml index e387e6b..4effa39 100644 --- a/backend/.github/workflows/backend-ci.yml +++ b/backend/.github/workflows/backend-ci.yml @@ -36,7 +36,7 @@ jobs: test: runs-on: self-hosted - needs: lint + needs: lint # Ensure this job runs after linting steps: - name: Checkout repository @@ -53,15 +53,22 @@ jobs: source venv/bin/activate pip install -r backend/requirements.txt - - name: Start API server in background + - name: Start the API server run: | - source venv/bin/activate - nohup python backend/main.py > server.log 2>&1 & - sleep 5 + 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 + BASE_URL: http://127.0.0.1:5000 # Assuming your API is running on this address run: | source venv/bin/activate pytest backend/tests --maxfail=1 --disable-warnings -q