From 9fef9f328842d14acfb3c6be0a7ec08cb7c74425 Mon Sep 17 00:00:00 2001 From: uniyalmani Date: Wed, 27 Mar 2024 12:44:46 +0530 Subject: [PATCH] adding test config and testcase --- blogexample/app.py | 15 ++++++++------ config/settings.py | 7 +++++++ config/test_config.py | 4 ++++ instance/test.db | Bin 0 -> 32768 bytes requirements.txt | 44 +++++++++++++++++++++++------------------- tests/__init__.py | 0 tests/test_routes.py | 24 +++++++++++++++++++++++ 7 files changed, 68 insertions(+), 26 deletions(-) create mode 100644 config/test_config.py create mode 100644 instance/test.db create mode 100644 tests/__init__.py create mode 100644 tests/test_routes.py 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 0000000000000000000000000000000000000000..a09ba988631fbcf75b161bc7f08806ccfcc0febe GIT binary patch literal 32768 zcmeI*v1$TA5CG8G#KbhGe!{hB(pU?r#YQZwMIu;8AovUZgg@XfS~?fJR<4tSH_I@4 z+$_7iYP(%67yIK+z5D$;Y>xFbCQ(X!*L6gkGC%G)ynhd}wo%&2gL;au^}K2tCK03E z|Co(t)wbF`SmhT10t5&UAV7cs0Rnvy7+*_X_C+f&zQ(kjcdWVO(y{fKZF$Vkj{BE$ z$3|@g2oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBly zK!5-N0t5&UAV7cs0RjXF5FkK+009C72oNAZfB*pk1PBlyK!5-N0t5&UAV7cs0RjXF w5FkK+0D*oAyyvl-H~yOXmf5`X_nP^VDF_fCK!5-N0t5&UAV7csfu0N80Dr0x761SM literal 0 HcmV?d00001 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