def test_json_as_unicode(self): app = Flak(__name__) app.config["JSON_AS_ASCII"] = True with app.new_context() as cx: rv = cx.dumps(u"\N{SNOWMAN}") assert rv == '"\\u2603"' app.config["JSON_AS_ASCII"] = False with app.new_context() as cx: rv = cx.dumps(u"\N{SNOWMAN}") assert rv == u'"\u2603"'
def test_g_iteration_protocol(): app = Flak(__name__) with app.new_context() as cx: cx.globals.foo = 23 cx.globals.bar = 42 assert 'foo' in cx.globals assert 'foos' not in cx.globals assert sorted(cx.globals) == ['bar', 'foo']
def test_get_method_on_g(): app = Flak(__name__) with app.new_context() as cx: assert cx.globals.get('x') is None assert cx.globals.get('x', 11) == 11 cx.globals.x = 42 assert cx.globals.get('x') == 42 assert cx.globals.x == 42
def test_app_tearing_down(): cleanup_stuff = [] app = Flak(__name__) @app.teardown def cleanup(cx, exception): cleanup_stuff.append(exception) with app.new_context(): pass assert cleanup_stuff == [None]
def test_basic_url_generation(): app = Flak(__name__) app.config['SERVER_NAME'] = 'localhost' app.config['PREFERRED_URL_SCHEME'] = 'https' @app.route('/') def index(cx): pass with app.new_context() as cx: rv = cx.url_for('index') assert rv == 'https://localhost/'
def test_app_tearing_down_with_handled_exception(): cleanup_stuff = [] app = Flak(__name__) @app.teardown def cleanup(cx, exception): cleanup_stuff.append(exception) with app.new_context(): try: raise Exception('dummy') except Exception: pass assert cleanup_stuff == [None]
def test_app_context_globals_methods(): app = Flak(__name__) with app.new_context() as cx: # get assert cx.globals.get('foo') is None assert cx.globals.get('foo', 'bar') == 'bar' # __contains__ assert 'foo' not in cx.globals cx.globals.foo = 'bar' assert 'foo' in cx.globals # setdefault cx.globals.setdefault('bar', 'the cake is a lie') cx.globals.setdefault('bar', 'hello world') assert cx.globals.bar == 'the cake is a lie' # pop assert cx.globals.pop('bar') == 'the cake is a lie' with pytest.raises(KeyError): cx.globals.pop('bar') assert cx.globals.pop('bar', 'more cake') == 'more cake' # __iter__ assert list(cx.globals) == ['foo']
def test_new_context(): app = Flak(__name__) with app.new_context() as cx: assert cx.app is app
def test_url_generation_requires_server_name(): app = Flak(__name__) with app.new_context() as cx: with pytest.raises(RuntimeError): cx.url_for('index')