From 78da8a061f7fe452602541f51c41ddd8ab2fa369 Mon Sep 17 00:00:00 2001 From: gitea Date: Wed, 26 Nov 2025 11:30:52 +0330 Subject: [PATCH] revised testing critera --- models.py | 31 ++++++++++++++++++++++--------- test_app.py | 37 +++++++++++++++++++++++++------------ 2 files changed, 47 insertions(+), 21 deletions(-) diff --git a/models.py b/models.py index 78d217c..be7c749 100644 --- a/models.py +++ b/models.py @@ -2,17 +2,30 @@ from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy() + class Feature(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), nullable=False) description = db.Column(db.String(200), nullable=False) -def seed_db(): - if Feature.query.count() == 0: # Seed only if empty - features = [ - Feature(title="Responsive Design", description="Adapts seamlessly to mobile, tablet, and desktop devices."), - Feature(title="Modern UI", description="Uses Bootstrap 5 for clean, professional styling."), - Feature(title="Easy Deployment", description="Containerized with Docker for quick setup on any server.") - ] - db.session.bulk_save_objects(features) - db.session.commit() + @staticmethod + def seed_db(): + # Seed only if empty + if Feature.query.count() == 0: + features = [ + Feature( + title="Responsive Design", + description="Adapts seamlessly to mobile, tablet, and desktop devices.", + ), + Feature( + title="Modern UI", + description="Uses Bootstrap 5 for clean, professional styling.", + ), + Feature( + title="Easy Deployment", + description="Containerized with Docker for quick setup on any server.", + ), + ] + db.session.bulk_save_objects(features) + db.session.commit() + diff --git a/test_app.py b/test_app.py index b3078d3..1cb5e14 100644 --- a/test_app.py +++ b/test_app.py @@ -1,27 +1,40 @@ -import pytest import os +import pytest + +# Ensure tests always use the in-memory SQLite DB, even if CI sets DB_* secrets +for var in ("DB_USER", "DB_PASS", "DB_NAME"): + os.environ.pop(var, None) + from app import app from models import db, Feature + @pytest.fixture def client(): - app.config['TESTING'] = True - app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:' - app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False - db.init_app(app) # Re-init DB with test config + app.config["TESTING"] = True + # app was already initialised with SQLite because DB_USER is now unset + + # Fresh DB per test + with app.app_context(): + db.drop_all() + db.create_all() with app.test_client() as client: - with app.app_context(): - db.create_all() - yield client - db.drop_all() + yield client + + # Clean up after test + with app.app_context(): + db.drop_all() + def test_index(client): - response = client.get('/') + response = client.get("/") 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): # Add fixture here + +def test_seed_db(client): with app.app_context(): Feature.seed_db() assert Feature.query.count() == 3 +