def test_app_missing_instance(): """ Explicitly passed application directories are created. """ directory_name = 'i_do_not_exist' make_app(_setup_empty, directory_name) assert os.path.exists(directory_name), "make_app did not create the "\ "instance path" os.rmdir(directory_name)
def test_add_config(): def _setup_config(app): app.add_config_var('foo', str, 'blah') app = make_app(_setup_config, 'test_output') assert app.conf['foo'] == 'blah'
def test_app_no_instance(): """ Test creating an application with no instance directory """ app = make_app(_setup_empty) assert app.instance_dir == os.getcwd()
def setup(self): def _setup_json(app): app.add_url('/', '', view=_Service()) app = make_app(_setup_json, 'test_output') self.client = Client(app)
def test_datetime(): """Might not work on your system""" app = make_app(i18n.setup_i18n, 'test_output') dt = datetime(2008, 1, 2) assert i18n.format_datetime(dt) == 'Jan 2, 2008 12:00:00 AM' assert i18n.format_datetime(dt, 'short') == '1/2/08 12:00 AM' assert i18n.format_datetime(dt, 'long') == 'January 2, 2008 12:00:00 AM +0000'
def setup(self): if os.path.exists(path.join('test_output', 'gh.sqlite')): os.unlink(path.join('test_output', 'gh.sqlite')) if metadata.bind: metadata.drop_all() # The SQLA tables notes = Table('notes', metadata, Column('id', types.Integer, primary_key=True), Column('title', types.Text), Column('note', types.Text), Column('importance', types.Text), useexisting=True, ) # The ORM mapped class class Note(object): """ Represents a note """ self.Note = Note # Make a mapper which gives you the objects manager mapper(Note, notes) def setup_app(app): app.add_setup(setup_sqlalchdb, 'sqlite://') self._setup_app = setup_app refresh_engine() self.app = make_app(setup_app, 'test_output') self.c = Client(self.app)
def test_check_login(): from glashammer.bundles.auth import setup_auth, login from glashammer.bundles.sessions import get_session called = [] def check(u, p): called.append((u, p)) return u == p def view(req): login('a') return Response() def setup_app(app): app.add_setup(setup_auth) app.connect_event('password-check', check) app.add_url('/', 'a/b', view=view) app = make_app(setup_app, 'test_output') c = Client(app) c.open() session = get_session() token_key = get_app().conf['auth/token_key'] assert token_key in session assert session[token_key] == 'a'
def test_add_rule(): """ Adding a rule, and that it's in the map """ def _add_rule_setup(app): app.add_url('/', endpoint='foo/blah') app = make_app(_add_rule_setup, 'test_output') assert _has_rule(app.map, '/', 'foo/blah')
def test_add_template_searchpath(self): """ Add a template search path """ def _add_template_searchpath(app): app.add_template_searchpath('test_output/templates') app = make_app(_add_template_searchpath, 'test_output') assert app.template_env.get_template('hello.html').render() == 'hello'
def test_add_view(): """ Adding a rule, endpoint and view, and that it's called. """ def _add_view_setup(app): app.add_url('/', endpoint='foo/blah', view=_aview) app = make_app(_add_view_setup, 'test_output') assert _has_rule(app.map, '/', 'foo/blah') assert app.get_view(None, 'foo/blah', {}) == 1
def test_add_rule(): def _add_rule_setup(app): from werkzeug.routing import Rule r = Rule('/', endpoint='ep') app.add_url_rule(r, view=_aview) app = make_app(_add_rule_setup, 'test_output') assert _has_rule(app.map, '/', 'ep') assert app.get_view(None, 'ep', {}) == 1
def test_endpoint_lookup(): """ Adding a rule, and that it's in the map """ def _add_rule_setup(app): app.add_url('/', endpoint='foo/blah') app = make_app(_add_rule_setup, 'test_output') assert url_for('foo/blah') == '/'
def test_add_template_global(self): """ Add a template global and ensure it is available for rendering """ def _add_template_global(app): app.add_template_global('hello', 'byebye') app.add_template_searchpath('test_output/templates') app = make_app(_add_template_global, 'test_output') assert app.template_env.get_template('variables.html').render() == 'byebye'
def setup(self): def view(req): return Response(self.p.generate(next_link=True, prev_link=True)) def setup_app(app): app.add_url('/', 'app/page', view=view) app = make_app(setup_app, 'test_output') self.c = Client(app) self.p = Pagination('app/page', 3, 10, 500)
def setup(self): def _a_view(req): assert req is local.request return Response('hello') def _setup_view(app): app.add_url('/', '', view=_a_view) self.app = make_app(_setup_view, 'test_output') self.client = Client(self.app)
def test_render_template(self): """ Test a template render """ def _add_bits(app): app.add_template_searchpath('test_output/templates') app = make_app(_add_bits, 'test_output') s = render_template('variables.html', hello='byebye') assert s == 'byebye'
def test_htmlhelpers_setup(): def _setup_helpers(app): from glashammer.bundles.htmlhelpers import setup_app app.add_setup(setup_app) app = make_app(_setup_helpers, 'test_output') assert 'h' in app.template_env.globals # now test you can actually use the thing h = app.template_env.globals['h'] assert h.meta() == '<meta />'
def test_session_setup(): def _setup_sessions(app): from glashammer.bundles.sessions import setup_app app.add_setup(setup_app) app.add_url('/', '', view=_sessioned_view) app = make_app(_setup_sessions, 'test_output') c = Client(app) c.open()
def test_openid(): def setup_openid(app): from glashammer.bundles.contrib.auth.openidauth import setup_openid app.add_setup(setup_openid) app = make_app(setup_openid, 'test_output') c = Client(app) iter, status, headers = c.post('/openid/login', data={'openid':'http://unpythonic.blogspot.com'}) assert '302' in status
def test_event_map(): app = make_app(lambda app: None, 'test_output') from glashammer.utils.system import build_eventmap em = build_eventmap(app) assert 'app-setup' in em assert 'request-start' in em assert 'request-end' in em assert 'response-start' in em assert 'response-end' in em
def test_add_template_tests(self): """ Add template tests and ensure they are available for rendering """ def _template_test(n): return True def _add_template_tests(app): app.add_template_test('asdf', _template_test) app.add_template_searchpath('test_output/templates') app = make_app(_add_template_tests, 'test_output') assert app.template_env.tests['asdf'] == _template_test
def test_event_log_handler(): logs = [] def log(level, record): logs.append((level, record)) def setup_app(app): app.connect_event('log', log) app = make_app(setup_app, 'test_output') c = Client(app) c.open() assert len(logs) > 5
def _test_finalized(): def _bview(req): app = get_app() assert_raises((RuntimeError,), app.add_config_var, 'a', str, 'a') return Response('hello') def _setup_app(app): app.add_url('/', 'hoo', _bview) app = make_app(_setup_app, 'test_output') c = Client(app) c.open()
def setup(self): """Creates a test environment.""" if os.path.exists(path.join('test_output', 'gh.sqlite')): os.unlink(path.join('test_output', 'gh.sqlite')); with open(path.join('test_output', 'config.ini'), 'w') as file: file.write(SQLA_TRACK_CONFIG) refresh_engine() self.app = make_app(self.setup_app, 'test_output') data_init() # Bind the model to the new engine Author.metadata.bind = get_engine()
def _test_redirect_helper(getpath): app = make_app(_setup_redirect_app, 'test_output') c = Client(app) appiter, status, headers = c.open('/foo') s = list(appiter)[0] location = dict(headers).get('Location') assert location path = urlparse.urlparse(location)[2] assert path == '/home'
def test_add_prefixed_controller(): """ Add a controller with target_prefix attribute and check the views. """ c = _PrefixedController() def _add_controller(app): app.add_views_controller('foo', c) app = make_app(_add_controller, 'test_output') for method, expected in [ ('index', c.view_index), ('edit', c.view_edit), ('inaccessible', None), ]: found = app.view_finder.find('foo/' + method) assert found == expected, "%s: %s != %s" % (method, found, expected)
def test_local_processor(): def _a_view(req): return Response('%s' % (local.foo == 1)) def _local_proc(req): local.foo = 1 def _setup_local(app): app.connect_event('request-start', _local_proc) app.add_url('/', '', view=_a_view) app = make_app(_setup_local, 'test_output') c = Client(app) appiter, status, headers = c.open() assert list(appiter)[0] == 'True'
def test_render_response(self): """ Test a full response. """ def _simple_view(req): return render_response('variables.html', hello='byebye') def _add_bits(app): app.add_template_searchpath('test_output/templates') app.add_url('/', 'foo/blah', _simple_view) app = make_app(_add_bits, 'test_output') c = Client(app) i, status, headers = c.open() assert list(i) == ['byebye'] assert status == '200 OK'
def test_resp_processor(): def _a_view(req): return Response('hello') def _process_response(resp): resp.data = 'byebye' def _setup_proc(app): app.connect_event('response-start', _process_response) app.add_url('/', '', view=_a_view) app = make_app(_setup_proc, 'test_output') c = Client(app) appiter, status, headers = c.open() assert list(appiter)[0] == 'byebye'
def test_req_processor(): def _a_view(req): return Response('%s' % (req.foo == 1)) def _process_request(req): req.foo = 1 def _setup_proc(app): app.connect_event('request-end', _process_request) app.add_url('/', '', view=_a_view) app = make_app(_setup_proc, 'test_output') c = Client(app) appiter, status, headers = c.open() assert list(appiter)[0] == 'True'