diff --git a/.gitea/workflows/cicd.yaml b/.gitea/workflows/cicd.yaml index 65e06ee..b057b44 100644 --- a/.gitea/workflows/cicd.yaml +++ b/.gitea/workflows/cicd.yaml @@ -18,7 +18,11 @@ jobs: - name: Install deps run: pip install -r requirements.txt - name: Run tests - run: pytest tests.py + env: + DB_USER: '' # Empty to force SQLite fallback + DB_PASS: '' + DB_NAME: '' + run: pytest build: needs: test diff --git a/app.py b/app.py index 8d095d4..56df6a5 100644 --- a/app.py +++ b/app.py @@ -4,10 +4,10 @@ from flask_bootstrap import Bootstrap5 from models import db, Feature app = Flask(__name__) -db_user = os.getenv('DB_USER', 'postgres') -db_pass = os.getenv('DB_PASS', 'password') # Fallback; overridden by env -db_name = os.getenv('DB_NAME', 'demo_db') -app.config['SQLALCHEMY_DATABASE_URI'] = f'postgresql://{db_user}:{db_pass}@db:5432/{db_name}' +db_user = os.getenv('DB_USER') +db_pass = os.getenv('DB_PASS') +db_name = os.getenv('DB_NAME') +app.config['SQLALCHEMY_DATABASE_URI'] = f'postgresql://{db_user}:{db_pass}@db:5432/{db_name}' if db_user else 'sqlite:///:memory:' # Fallback only if no env (for tests) app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False bootstrap = Bootstrap5(app) @@ -16,6 +16,6 @@ db.init_app(app) @app.route('/') def index(): with app.app_context(): - Feature.seed_db() # Seed on first load if empty + Feature.seed_db() features = Feature.query.all() return render_template('index.html', features=features) diff --git a/tests.py b/tests.py index 42475ea..b3078d3 100644 --- a/tests.py +++ b/tests.py @@ -1,11 +1,15 @@ import pytest -from app import app, db -from models import Feature +import os +from app import app +from models import db, Feature @pytest.fixture def client(): app.config['TESTING'] = True - app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:' # In-memory DB for tests + app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:' + app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False + db.init_app(app) # Re-init DB with test config + with app.test_client() as client: with app.app_context(): db.create_all() @@ -17,8 +21,7 @@ def test_index(client): assert response.status_code == 200 assert b'Professional Demo Site' in response.data -def test_seed_db(client): +def test_seed_db(client): # Add fixture here with app.app_context(): - db.create_all() Feature.seed_db() assert Feature.query.count() == 3