Browse Source

Fixed flask tests

pull/1040/head
Armin Ronacher 11 years ago
parent
commit
d2d8e66130
  1. 11
      examples/flaskr/flaskr.py
  2. 1
      examples/flaskr/flaskr_tests.py
  3. 12
      examples/minitwit/minitwit.py
  4. 1
      examples/minitwit/minitwit_tests.py
  5. 4
      flask/cli.py

11
examples/flaskr/flaskr.py

@ -37,13 +37,18 @@ def connect_db():
return rv return rv
@app.cli.command() def init_db():
def initdb(): """Initializes the database."""
"""Creates the database tables."""
db = get_db() db = get_db()
with app.open_resource('schema.sql', mode='r') as f: with app.open_resource('schema.sql', mode='r') as f:
db.cursor().executescript(f.read()) db.cursor().executescript(f.read())
db.commit() db.commit()
@app.cli.command('initdb')
def initdb_command():
"""Creates the database tables."""
init_db()
print('Initialized the database.') print('Initialized the database.')

1
examples/flaskr/flaskr_tests.py

@ -21,6 +21,7 @@ class FlaskrTestCase(unittest.TestCase):
self.db_fd, flaskr.app.config['DATABASE'] = tempfile.mkstemp() self.db_fd, flaskr.app.config['DATABASE'] = tempfile.mkstemp()
flaskr.app.config['TESTING'] = True flaskr.app.config['TESTING'] = True
self.app = flaskr.app.test_client() self.app = flaskr.app.test_client()
with flaskr.app.app_context():
flaskr.init_db() flaskr.init_db()
def tearDown(self): def tearDown(self):

12
examples/minitwit/minitwit.py

@ -49,15 +49,21 @@ def close_database(exception):
top.sqlite_db.close() top.sqlite_db.close()
@app.cli.command() def init_db():
def initdb(): """Initializes the database."""
"""Creates the database tables."""
db = get_db() db = get_db()
with app.open_resource('schema.sql', mode='r') as f: with app.open_resource('schema.sql', mode='r') as f:
db.cursor().executescript(f.read()) db.cursor().executescript(f.read())
db.commit() db.commit()
@app.cli.command('initdb')
def initdb_command():
"""Creates the database tables."""
init_db()
print('Initialized the database.')
def query_db(query, args=(), one=False): def query_db(query, args=(), one=False):
"""Queries the database and returns a list of dictionaries.""" """Queries the database and returns a list of dictionaries."""
cur = get_db().execute(query, args) cur = get_db().execute(query, args)

1
examples/minitwit/minitwit_tests.py

@ -20,6 +20,7 @@ class MiniTwitTestCase(unittest.TestCase):
"""Before each test, set up a blank database""" """Before each test, set up a blank database"""
self.db_fd, minitwit.app.config['DATABASE'] = tempfile.mkstemp() self.db_fd, minitwit.app.config['DATABASE'] = tempfile.mkstemp()
self.app = minitwit.app.test_client() self.app = minitwit.app.test_client()
with minitwit.app.app_context():
minitwit.init_db() minitwit.init_db()
def tearDown(self): def tearDown(self):

4
flask/cli.py

@ -199,9 +199,9 @@ class FlaskGroup(click.Group):
value = prepare_exec_for_file(value) value = prepare_exec_for_file(value)
elif '.' not in sys.path: elif '.' not in sys.path:
sys.path.insert(0, '.') sys.path.insert(0, '.')
ctx.obj.app_import_path = value ctx.ensure_object(ScriptInfo).app_import_path = value
def set_debug(ctx, value): def set_debug(ctx, value):
ctx.obj.debug = value ctx.ensure_object(ScriptInfo).debug = value
click.Group.__init__(self, help=help, params=[ click.Group.__init__(self, help=help, params=[
click.Option(['-a', '--app'], click.Option(['-a', '--app'],

Loading…
Cancel
Save