Skip to content

added tests and config for pytest #14

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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
benv/
*.pyc
.python-version
16 changes: 8 additions & 8 deletions blogexample/app.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/usr/bin/env python

from flask import Flask, render_template, session, request, jsonify, url_for
from flask_sqlalchemy import SQLAlchemy
import os

from flask import Flask, jsonify, render_template, request, session, url_for
from flask_sqlalchemy import SQLAlchemy
from werkzeug.debug import DebuggedApplication

# from flask_ckeditor import CKEditor#, CKEditorField
Expand All @@ -15,19 +16,18 @@
def create_app(main=True, debug=True):
"""Create an application."""
app = Flask(__name__)
app.config.from_object('config.settings')

app.config['SECRET_KEY'] = 'secret!'
config_type = os.getenv("CONFIG_TYPE", default="config.settings.DevelopmentConfig")
app.config.from_object(config_type)


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

return app
1 change: 0 additions & 1 deletion blogexample/blueprints/blog/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ def add_post():
if Post.create_blogpost(params):
flash('Post has been created successfully.', 'success')
return redirect(url_for('blog.show_posts'))

return render_template('add.html', form=form, blogpost=blogpost)


Expand Down
Empty file added blogexample/tests/__init__.py
Empty file.
66 changes: 66 additions & 0 deletions blogexample/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import os

import pytest
from flask import url_for

from blogexample.app import create_app, db
from blogexample.blueprints.blog.forms import AddPostForm
from blogexample.blueprints.blog.models import Post


@pytest.fixture(scope="function")
def client():
os.environ["CONFIG_TYPE"] = "config.settings.TestingConfig"
flask_app = create_app()

with flask_app.test_client() as testing_client:
with flask_app.app_context():

db.create_all()
yield testing_client # testing phase
db.drop_all()


def test_index(client):
response = client.get(url_for("blog.index"))
assert response.status_code == 200


def test_add_post(client):
form_data = {
"title": "Test Title",
"body": "Test Body",
"taglist": "two, new",
"visible": True,
}

response = client.post(
url_for("blog.add_post"), data=form_data, follow_redirects=True
)
assert response.status_code == 200

# test if the post exists in db
post = Post.query.filter_by(title="Test Title").first()
assert post is not None
assert post.body == "Test Body"


def test_show_posts(client):

test_post_1 = Post(
title="First Post", body="Body of Post 1", visible=True, url="first-post"
)
test_post_2 = Post(
title="Second Post", body="Body of Post 1", visible=True, url="second-post"
)

db.session.add(test_post_1)
db.session.add(test_post_2)

db.session.commit()

response = client.get(url_for("blog.show_posts"))
assert response.status_code == 200
assert b'First Post' in response.data


34 changes: 26 additions & 8 deletions config/settings.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
import os
from datetime import timedelta

DEBUG = True
BASEDIR = os.path.abspath(os.path.dirname(__file__))

SERVER_NAME = 'localhost:8000'
SECRET_KEY = 'insecurekeyfordev'

class Config(object):
FLASK_ENV = "development"
SERVER_NAME = "localhost:8000"
DEBUG = False
TESTING = False
REMEMBER_COOKIE_DURATION = timedelta(days=90)
SECRET_KEY = os.getenv("SECRET_KEY", default="BAD_SECRET_KEY")

# SQLAlchemy.
db_uri = 'postgresql://flaskblog:blogpassword@postgres:5432/flaskblog'
SQLALCHEMY_DATABASE_URI = db_uri
SQLALCHEMY_TRACK_MODIFICATIONS = False
SQLALCHEMY_DATABASE_URI = (
"postgresql://flaskblog:blogpassword@postgres:5432/flaskblog"
)
SQLALCHEMY_TRACK_MODIFICATIONS = False

REMEMBER_COOKIE_DURATION = timedelta(days=90)

class ProductionConfig(Config):
FLASK_ENV = "production"


class DevelopmentConfig(Config):
DEBUG = True


class TestingConfig(Config):
TESTING = True
SQLALCHEMY_DATABASE_URI = f"sqlite:///{os.path.join(BASEDIR, 'test.db')}"
WTF_CSRF_ENABLED = False
Binary file added config/test.db
Binary file not shown.
Binary file added image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[pytest]
addopts = -v
testpaths = ./blogexample/tests
filterwarnings =
ignore::DeprecationWarning
ignore::RuntimeWarning
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ WTForms-Alchemy==0.16.9
flask_ckeditor

pytz
pytest