Skip to content

adding test config and testcase #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions blogexample/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


7 changes: 7 additions & 0 deletions config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
4 changes: 4 additions & 0 deletions config/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from config.settings import TestConfig

class TestConfigOverride(TestConfig):
SQLALCHEMY_DATABASE_URI = 'sqlite:///test.db'
Binary file added instance/test.db
Binary file not shown.
44 changes: 24 additions & 20 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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
Empty file added tests/__init__.py
Empty file.
24 changes: 24 additions & 0 deletions tests/test_routes.py
Original file line number Diff line number Diff line change
@@ -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