fixed tests env setup
This commit is contained in:
@@ -18,7 +18,11 @@ jobs:
|
|||||||
- name: Install deps
|
- name: Install deps
|
||||||
run: pip install -r requirements.txt
|
run: pip install -r requirements.txt
|
||||||
- name: Run tests
|
- name: Run tests
|
||||||
run: pytest tests.py
|
env:
|
||||||
|
DB_USER: '' # Empty to force SQLite fallback
|
||||||
|
DB_PASS: ''
|
||||||
|
DB_NAME: ''
|
||||||
|
run: pytest
|
||||||
|
|
||||||
build:
|
build:
|
||||||
needs: test
|
needs: test
|
||||||
|
|||||||
10
app.py
10
app.py
@@ -4,10 +4,10 @@ from flask_bootstrap import Bootstrap5
|
|||||||
from models import db, Feature
|
from models import db, Feature
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
db_user = os.getenv('DB_USER', 'postgres')
|
db_user = os.getenv('DB_USER')
|
||||||
db_pass = os.getenv('DB_PASS', 'password') # Fallback; overridden by env
|
db_pass = os.getenv('DB_PASS')
|
||||||
db_name = os.getenv('DB_NAME', 'demo_db')
|
db_name = os.getenv('DB_NAME')
|
||||||
app.config['SQLALCHEMY_DATABASE_URI'] = f'postgresql://{db_user}:{db_pass}@db:5432/{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
|
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
||||||
|
|
||||||
bootstrap = Bootstrap5(app)
|
bootstrap = Bootstrap5(app)
|
||||||
@@ -16,6 +16,6 @@ db.init_app(app)
|
|||||||
@app.route('/')
|
@app.route('/')
|
||||||
def index():
|
def index():
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
Feature.seed_db() # Seed on first load if empty
|
Feature.seed_db()
|
||||||
features = Feature.query.all()
|
features = Feature.query.all()
|
||||||
return render_template('index.html', features=features)
|
return render_template('index.html', features=features)
|
||||||
|
|||||||
13
tests.py
13
tests.py
@@ -1,11 +1,15 @@
|
|||||||
import pytest
|
import pytest
|
||||||
from app import app, db
|
import os
|
||||||
from models import Feature
|
from app import app
|
||||||
|
from models import db, Feature
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def client():
|
def client():
|
||||||
app.config['TESTING'] = True
|
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.test_client() as client:
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
db.create_all()
|
db.create_all()
|
||||||
@@ -17,8 +21,7 @@ def test_index(client):
|
|||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
assert b'Professional Demo Site' in response.data
|
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():
|
with app.app_context():
|
||||||
db.create_all()
|
|
||||||
Feature.seed_db()
|
Feature.seed_db()
|
||||||
assert Feature.query.count() == 3
|
assert Feature.query.count() == 3
|
||||||
|
|||||||
Reference in New Issue
Block a user