Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

Commit

Permalink
Fixed unbound locals usage test case to support Flask 0.7+
Browse files Browse the repository at this point in the history
Accessing locals when not in a request context raises a RuntimeError
since Flask 0.7 and not an AttributeError like it used to.
  • Loading branch information
spantaleev committed Nov 26, 2011
1 parent 8a6afd5 commit 73cfe06
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
license = "BSD",
packages = ['flaskext'],
namespace_packages=['flaskext'],
install_requires = ['Flask', 'Sijax>=0.2.0'],
install_requires = ['Flask>=0.7.0', 'Sijax>=0.2.0'],
test_suite = 'tests',
zip_safe = False,
classifiers = [
Expand Down
23 changes: 13 additions & 10 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def _assert_response_json(context, string):
context.assertTrue('type' in item)

class SijaxFlaskTestCase(unittest.TestCase):

def test_route_always_adds_post_method(self):
class FlaskMock(object):
def __init__(self):
Expand Down Expand Up @@ -55,11 +55,14 @@ def test_sijax_helper_object_is_only_bound_to_g_in_a_request_context(self):
app.preprocess_request()
self.assertEqual(id(helper), id(flask.g.sijax))

# Make sure that access fails when outside of a request context
try:
flask.g.sijax
self.fail('Bound to g in a non-request context!')
except AttributeError:
except RuntimeError:
# RuntimeError('working outside of request context')
pass
else:
self.fail('Bound to g in a non-request context!')

def test_json_uri_config_is_used(self):
uri = '/some/json_uri.here'
Expand All @@ -84,7 +87,7 @@ def test_request_uri_changing_works(self):

with app.test_request_context():
app.preprocess_request()

js = helper.get_js()
self.assertTrue('Sijax.setRequestUri("/");' in js)

Expand Down Expand Up @@ -131,15 +134,15 @@ def test_registering_callbacks_in_a_request_context_with_no_preprocessing_fails(
except AttributeError:
# helper._sijax (and flask.g.sijax)
pass

def test_register_callback_works(self):
call_history = []

def callback(obj_response):
call_history.append('callback')
obj_response.alert('test')


app = flask.Flask(__name__)
helper = init_sijax(app)

Expand Down Expand Up @@ -180,7 +183,7 @@ def callback(obj_response, files, form_values):

helper.register_upload_callback('form_id', callback)
func_name = sijax.plugin.upload.func_name_by_form_id('form_id')

cls_sijax = helper._sijax.__class__

post = {cls_sijax.PARAM_REQUEST: func_name, cls_sijax.PARAM_ARGS: '["form_id"]', 'post_key': 'val'}
Expand All @@ -204,18 +207,18 @@ def test_sijax_helper_passes_correct_post_data(self):
app.preprocess_request()

self.assertEqual(id(helper._sijax.get_data()), id(flask.request.form))

def test_process_request_returns_a_string_or_a_flask_response_object(self):
# SijaxHelper.process_request should return a string for regular functions
# and a Flask.Response object for functions that use a generator (streaming functions)
from sijax.response import StreamingIframeResponse

app = flask.Flask(__name__)
helper = init_sijax(app)

with app.test_request_context():
app.preprocess_request()

cls_sijax = helper._sijax.__class__

post = {cls_sijax.PARAM_REQUEST: 'callback', cls_sijax.PARAM_ARGS: '[]'}
Expand Down

0 comments on commit 73cfe06

Please sign in to comment.