Skip to content

Commit

Permalink
split the tests into separate files; added real changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
JackStouffer committed Sep 28, 2013
1 parent f1c03e9 commit 41aa19f
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 102 deletions.
1 change: 0 additions & 1 deletion CHANGELOG

This file was deleted.

17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#Changelog

##1.1
* switched to py.test for tests
* form tests
* url tests
* testing database submitting on model tests
* added documentation on how to deploy your application

##1.0
* MVC with blueprints, SQLAlchemy models, and templates
* A makefile
* nose tests
* Flask-Assets css and js management
* Flask-Cache for caching jinja templates
* A Flask-Script management script
* Flask-WTF form management
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,16 @@ The models in this application are SQLAlchemy models in the models.py file, an e

If you are still confused about how this project is structured, I encourage you to read the blog posts listed at the top of the README file.

Need help implementing some common features like user login or ajax? For a full blown tutorial covering almost every flask topic that you can think of, I recommend [The Flask Mega-Tutorial](http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world).

Lets talk about the tests. All of the tests are in the test_appname.py file, and the tests are run with py.test. Nothing is to fancy in the first test class, but in the next one we see some special initialization with the database, this is due to flask not actually running and Flask-SQLAlechmy not being initialized properly. Also, we see here the use of app.test\_client(), which means we can use functions to send GET and POST requests from our tests. This is how we test our forms and if the urls are returning correctly.

Sending post and get requests is all well and good, but if you want to get really advanced, check out [webtest](http://webtest.pythonpaste.org/en/latest/).

##Production
First off, it is very, very important that if you ever open source a flask application based upon this, to not include the settings.py file in your repo. Obviously, your database password is in it, but your secret key as well, which is used to cryptographically sign all of flask's session data.

When going into production there are several things that you should do. First, look at your options for deploying on an actual server [here](http://flask.pocoo.org/docs/deploying/). Using the flask development server is NOT recommended for production for several good reasons. Deploying to the server manually is tedious, so you might want to look into deploying with [fabric](http://flask.pocoo.org/docs/patterns/fabric/) or [distributee](http://flask.pocoo.org/docs/patterns/distribute/#distribute-deployment). This isn't php, so logging errors doesn't come out of the box, [here](http://flask.pocoo.org/docs/errorhandling/) is a great resource on the subject. Also, there are several awesome plug-ins available for flask, they can be found on the flask website [here](http://flask.pocoo.org/extensions/), or just searching "flask" on github.
When going into production there are several things that you should do. First, look at your options for deploying on an actual server [here](http://flask.pocoo.org/docs/deploying/). Using the flask development server is NOT recommended for production for several good reasons. Deploying to the server manually is tedious, so you might want to look into deploying with [fabric](http://flask.pocoo.org/docs/patterns/fabric/) or [distribute](http://flask.pocoo.org/docs/patterns/distribute/#distribute-deployment). This isn't php, so logging errors doesn't come out of the box, [here](http://flask.pocoo.org/docs/errorhandling/) is a great resource on the subject. Also, there are several awesome plug-ins available for flask that add in functionality that you might need for your application, they can be found on the flask website [here](http://flask.pocoo.org/extensions/), or just searching "flask" on github.

##Licenses
The original bootstrapy project and the added code from this project are licensed under the BSD license.
100 changes: 0 additions & 100 deletions tests/test_appname.py

This file was deleted.

19 changes: 19 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#! ../env/bin/python
# -*- coding: utf-8 -*-
from appname import create_app


class TestConfig:
def test_dev_config(self):
app = create_app('appname.settings.DevConfig', env='dev')

assert app.config['DEBUG'] is True
assert app.config['SQLALCHEMY_DATABASE_URI'] == 'sqlite:///../database.db'
assert app.config['SQLALCHEMY_ECHO'] is True
assert app.config['CACHE_TYPE'] == 'null'

def test_prod_config(self):
app = create_app('appname.settings.ProdConfig', env='prod')

assert app.config['SQLALCHEMY_DATABASE_URI'] == 'sqlite:///../database.db'
assert app.config['CACHE_TYPE'] == 'simple'
43 changes: 43 additions & 0 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#! ../env/bin/python
# -*- coding: utf-8 -*-
from appname import create_app
from appname.models import db


class TestForm:
def setup(self):
app = create_app('appname.settings.DevConfig', env='dev')
self.app = app.test_client()
db.app = app
db.create_all()

def teardown(self):
db.session.remove()
db.drop_all()

def test_user_form_empty(self):
rv = self.app.post('/wtform', data=dict(
user_name="",
message=""
), follow_redirects=True)

assert rv.status_code == 200
assert 'There was a problem submitting the form!' in rv.data

def test_user_form_name(self):
rv = self.app.post('/wtform', data=dict(
user_name="admin",
message=""
), follow_redirects=True)

assert rv.status_code == 200
assert 'The form was successfully submitted' in rv.data

def test_user_form_both(self):
rv = self.app.post('/wtform', data=dict(
user_name="admin",
message="test message"
), follow_redirects=True)

assert rv.status_code == 200
assert 'The form was successfully submitted' in rv.data
25 changes: 25 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#! ../env/bin/python
# -*- coding: utf-8 -*-
from appname import create_app
from appname.models import db, User


class TestModels:
def setup(self):
app = create_app('appname.settings.DevConfig', env='dev')
self.app = app.test_client()
db.app = app
db.create_all()

def teardown(self):
db.session.remove()
db.drop_all()

def test_user(self):
admin = User('admin', 'supersafepassword')

assert admin.username == 'admin'
assert admin.password == 'supersafepassword'

db.session.add(admin)
db.session.commit()
24 changes: 24 additions & 0 deletions tests/test_urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#! ../env/bin/python
# -*- coding: utf-8 -*-
from appname import create_app
from appname.models import db


class TestURLs:
def setup(self):
app = create_app('appname.settings.DevConfig', env='dev')
self.app = app.test_client()
db.app = app
db.create_all()

def teardown(self):
db.session.remove()
db.drop_all()

def test_home(self):
rv = self.app.get('/')
assert rv.status_code == 200

def test_form(self):
rv = self.app.get('/wtform')
assert rv.status_code == 200

0 comments on commit 41aa19f

Please sign in to comment.