diff --git a/backend/.github/workflows/backend-ci.yml b/backend/.github/workflows/backend-ci.yml new file mode 100644 index 0000000..93a0191 --- /dev/null +++ b/backend/.github/workflows/backend-ci.yml @@ -0,0 +1,39 @@ +name: Backend CI + +on: + push: + paths: + - 'backend/**' + pull_request: + paths: + - 'backend/**' + +jobs: + backend-ci: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: '3.11' + + - name: Install dependencies + working-directory: ./backend + run: | + pip install -r requirements.txt + + - name: Run pylint on backend + working-directory: ./backend + run: | + pylint main.py db.py init_db.py + + - name: Run Pytest + working-directory: ./backend + run: | + python main.py & # Run server + sleep 5 # Give it time to start + pytest tests/ diff --git a/backend/main.py b/backend/main.py index ad707dd..91ce44f 100644 --- a/backend/main.py +++ b/backend/main.py @@ -229,4 +229,5 @@ def before_first_request(): init_db() if __name__ == "__main__": - app.run(debug=True) + app.run(debug=True, use_reloader=False) + diff --git a/backend/requirements.txt b/backend/requirements.txt index a9f380d..5de15b1 100644 --- a/backend/requirements.txt +++ b/backend/requirements.txt @@ -1,12 +1,5 @@ -Flask==3.1.1 -Flask-Cors==5.0.2 -flasgger==0.9.7.1 -Werkzeug==3.1.3 -Jinja2==3.1.5 -itsdangerous==2.2.0 -MarkupSafe==3.0.2 -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 +flask +flasgger +requests +pytest +pylint \ No newline at end of file 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 new file mode 100644 index 0000000..db01062 Binary files /dev/null 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 new file mode 100644 index 0000000..851f2d5 --- /dev/null +++ b/backend/tests/test_api.py @@ -0,0 +1,33 @@ +import requests +import sqlite3 +import os + +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") + assert response.status_code == 200 + assert isinstance(response.json(), list) + +def test_add_pet_boundary_age(): + new_pet = { + "name": "Oldy", + "species": "Turtle", + "breed": "Galapagos", + "age": 150, + "personality": "Wise elder", + "image_path": "/images/oldy.jpg" + } + response = requests.post(f"{BASE_URL}/api/pets", json=new_pet) + assert response.status_code == 201 + + # Check if it's actually in the DB + conn = sqlite3.connect(DB_PATH) + cursor = conn.cursor() + cursor.execute("SELECT * FROM animals WHERE name = ?", (new_pet['name'],)) + result = cursor.fetchone() + conn.close() + + assert result is not None + assert result[4] == new_pet["age"] # age