Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
butterflyhug committed Feb 4, 2013
1 parent 8a6b1e0 commit 7415b3f
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
env
*.pyc
*.egg
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

The `flask_filters` module helps you wrap [Flask][] views with coroutine filters. The bundled `hello_world.py` application demonstrates most of this module's functionality.

[Flask]: http://flask.pocoo.org/ (Flask is a web development microframework for Python)


## Installation

Expand All @@ -26,8 +24,7 @@ Otherwise, install Flask, then clone this git repository and install from `setup
* Python 2.5+ (primarily tested with Python 2.7, not compatible with Python 3)
* [Flask][]
* The bundled Hello World app uses Flask's jsonify method, which relies on [simplejson][] for Python 2.5. (`simplejson` was added to the standard library as the `json` module in Python 2.6.)

[simplejson]: http://pypi.python.org/pypi/simplejson (simplejson is a fast and extensible JSON encoder/decoder for Python)
* The bundled unit tests require Python 2.6+ and [nose][]


## Defining Filters
Expand Down Expand Up @@ -125,3 +122,13 @@ If you are using Flask's blueprints, you can provide a blueprint anywhere this m


## Development

To run the unit tests, you'll need the [nose][] package. Once that's installed, you can run the tests with:

nosetests


[Flask]: http://flask.pocoo.org/
[nose]: https://nose.readthedocs.org/
[simplejson]: http://pypi.python.org/pypi/simplejson
[virtualenv]: http://www.virtualenv.org/
113 changes: 113 additions & 0 deletions test_flask_filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
"""
Tests for flask_filters.
"""

from nose.tools import eq_, ok_

from flask import abort, Flask
from flask.ext import filters
from werkzeug.exceptions import NotFound


flask_app = Flask(__name__)


# YIELD A VALUE TO ABORT REQUEST

def abort_filter():
yield 'This is an error message.'
ok_(False) # we shouldn't get here

@filters.make_filter(abort_filter)
def aborted_view():
ok_(False) # we shouldn't get here

def test_abort_request():
with flask_app.test_request_context('/'):
response = aborted_view()
eq_(response, 'This is an error message.')
eq_([], filters.get_filters_before_run())


# YIELD A VALUE TO REPLACE RESPONSE

def response_filter():
yield
yield 'Hello, world! This is a replaced response.'

@filters.make_filter(response_filter)
def replaced_response_view():
return 'The filter should replace this message with "Hello, world!"'

def test_replaced_response():
with flask_app.test_request_context('/'):
response = replaced_response_view()
eq_(response, 'Hello, world! This is a replaced response.')
eq_([response_filter], filters.get_filters_before_run())
eq_([response_filter], filters.get_filters_after_run())


# DO NOT CHANGE THE RESPONSE IF YIELD SENDS NONE

def noop_filter():
yield None
yield

@filters.make_filter(noop_filter)
def hello_world_view():
return "Hello, world! It's a fabulous day."

def test_unmodified_response():
with flask_app.test_request_context('/'):
response = hello_world_view()
eq_(response, "Hello, world! It's a fabulous day.")
eq_([noop_filter], filters.get_filters_before_run())
eq_([noop_filter], filters.get_filters_after_run())


# APPLY MULTIPLE FILTERS IN THE CORRECT ORDER

@filters.apply_filters(noop_filter, abort_filter, response_filter)
def multiple_filter_view():
return 'This view has multiple filters.'

def test_multiple_filters():
with flask_app.test_request_context('/'):
response = multiple_filter_view()
eq_(response, 'This is an error message.')
eq_([noop_filter], filters.get_filters_before_run())


# APPLY FILTERS CORRECTLY USING VIEW DECORATOR

def another_noop_filter():
yield
yield None
ok_(False) # we shouldn't get here

_view = filters.make_view_decorator(flask_app, '/',
noop_filter, another_noop_filter)

@_view('', 'GET')
def reusable_decorator_view():
return 'This view is wrapped with a reusable view decorator.'

@_view('one_off', 'GET', filters=[response_filter])
def one_off_filter_view():
return 'This view adds a one-off filter to a reusable decorator.'

def test_reusable_decorator():
with flask_app.test_request_context('/'):
response = reusable_decorator_view()
eq_(response, 'This view is wrapped with a reusable view decorator.')
eq_([noop_filter, another_noop_filter],
filters.get_filters_before_run())
eq_([another_noop_filter, noop_filter],
filters.get_filters_after_run())
with flask_app.test_request_context('/one_off'):
response = one_off_filter_view()
eq_(response, 'Hello, world! This is a replaced response.')
eq_([response_filter, noop_filter, another_noop_filter],
filters.get_filters_before_run())
eq_([another_noop_filter, noop_filter, response_filter],
filters.get_filters_after_run())

0 comments on commit 7415b3f

Please sign in to comment.