diff --git a/blogexample/app.py b/blogexample/app.py index 7c19bbb..bd8f350 100644 --- a/blogexample/app.py +++ b/blogexample/app.py @@ -12,22 +12,25 @@ # ckeditor = CKEditor() -def create_app(main=True, debug=True): +# app.py + +def create_app(config_object=None): """Create an application.""" app = Flask(__name__) - app.config.from_object('config.settings') - app.config['SECRET_KEY'] = 'secret!' + if config_object is None: + app.config.from_object('config.settings') + else: + app.config.from_object(config_object) - from blogexample.blueprints.blog import blog app.register_blueprint(blog) - + db.init_app(app) - # ckeditor.init_app(app) if app.debug: app.wsgi_app = DebuggedApplication(app.wsgi_app, evalex=True) return app + diff --git a/config/settings.py b/config/settings.py index 1cf2d55..f81385f 100755 --- a/config/settings.py +++ b/config/settings.py @@ -12,3 +12,10 @@ SQLALCHEMY_TRACK_MODIFICATIONS = False REMEMBER_COOKIE_DURATION = timedelta(days=90) + + +class TestConfig: + TESTING = True + DEBUG = False + SQLALCHEMY_DATABASE_URI = 'sqlite:///:memory:' + SECRET_KEY = 'test-secret' diff --git a/config/test_config.py b/config/test_config.py new file mode 100644 index 0000000..4f1cffb --- /dev/null +++ b/config/test_config.py @@ -0,0 +1,4 @@ +from config.settings import TestConfig + +class TestConfigOverride(TestConfig): + SQLALCHEMY_DATABASE_URI = 'sqlite:///test.db' \ No newline at end of file diff --git a/instance/test.db b/instance/test.db new file mode 100644 index 0000000..a09ba98 Binary files /dev/null and b/instance/test.db differ diff --git a/requirements.txt b/requirements.txt index d5f92bc..7e25e4a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,22 +1,26 @@ -Flask==1.0.2 -werkzeug==0.15.3 - -# Application server for both development and production. +blinker==1.7.0 +click==8.1.7 +exceptiongroup==1.2.0 +Flask==3.0.2 +Flask-SQLAlchemy==3.1.1 +Flask-WTF==1.2.1 +greenlet==3.0.3 gunicorn==19.7.0 - -# Data and workers. -psycopg2>=2.6.1 -Flask-SQLAlchemy==2.3.2 -SQLAlchemy==1.3.1 -alembic==1.0.8 - -# Forms. +infinity==1.5 +iniconfig==2.0.0 +itsdangerous==2.1.2 +Jinja2==3.1.3 +MarkupSafe==2.1.5 +packaging==24.0 +pluggy==1.4.0 +pytest==8.1.1 +pytest-flask==1.3.0 +python-editor==1.0.4 +pytz==2024.1 +six==1.16.0 +SQLAlchemy==2.0.29 +tomli==2.0.1 +typing_extensions==4.10.0 +validators==0.24.0 +Werkzeug==3.0.1 WTForms==2.2.1 -Flask-WTF==0.14.2 -WTForms-Components==0.10.4 -WTForms-Alchemy==0.16.9 - -#rich-text -flask_ckeditor - -pytz diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_routes.py b/tests/test_routes.py new file mode 100644 index 0000000..bb9054a --- /dev/null +++ b/tests/test_routes.py @@ -0,0 +1,24 @@ +import pytest +from blogexample.app import create_app, db +from config.test_config import TestConfigOverride +from flask import url_for + + +@pytest.fixture +def app(): + app = create_app(TestConfigOverride) + with app.app_context(): + # Ensure app context is pushed + + assert app.testing # Verify that app is in testing mode + assert app.config['SQLALCHEMY_DATABASE_URI'] == 'sqlite:///test.db' # Verify database URI + assert app.secret_key == 'test-secret' # Verify secret key + db.create_all() + yield app + db.drop_all() + + + +def test_index_route(client, app): + response = client.get(url_for('blog.index')) + assert response.status_code == 200