fixed tests env setup
Some checks failed
Build and Deploy Demo App / test (push) Failing after 10s
Build and Deploy Demo App / build (push) Has been skipped
Build and Deploy Demo App / scan (push) Has been skipped
Build and Deploy Demo App / deploy (push) Has been skipped

This commit is contained in:
2025-11-26 11:22:07 +03:30
parent a7940b2044
commit b3a9f19501
3 changed files with 18 additions and 11 deletions

View File

@@ -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

10
app.py
View File

@@ -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)

View File

@@ -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