Skip to content

Commit

Permalink
Fixed flask tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed May 2, 2014
1 parent e059bf3 commit d2d8e66
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 10 deletions.
11 changes: 8 additions & 3 deletions examples/flaskr/flaskr.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,18 @@ def connect_db():
return rv


@app.cli.command()
def initdb():
"""Creates the database tables."""
def init_db():
"""Initializes the database."""
db = get_db()
with app.open_resource('schema.sql', mode='r') as f:
db.cursor().executescript(f.read())
db.commit()


@app.cli.command('initdb')
def initdb_command():
"""Creates the database tables."""
init_db()
print('Initialized the database.')


Expand Down
3 changes: 2 additions & 1 deletion examples/flaskr/flaskr_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ def setUp(self):
self.db_fd, flaskr.app.config['DATABASE'] = tempfile.mkstemp()
flaskr.app.config['TESTING'] = True
self.app = flaskr.app.test_client()
flaskr.init_db()
with flaskr.app.app_context():
flaskr.init_db()

def tearDown(self):
"""Get rid of the database again after each test."""
Expand Down
12 changes: 9 additions & 3 deletions examples/minitwit/minitwit.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,21 @@ def close_database(exception):
top.sqlite_db.close()


@app.cli.command()
def initdb():
"""Creates the database tables."""
def init_db():
"""Initializes the database."""
db = get_db()
with app.open_resource('schema.sql', mode='r') as f:
db.cursor().executescript(f.read())
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):
"""Queries the database and returns a list of dictionaries."""
cur = get_db().execute(query, args)
Expand Down
3 changes: 2 additions & 1 deletion examples/minitwit/minitwit_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ def setUp(self):
"""Before each test, set up a blank database"""
self.db_fd, minitwit.app.config['DATABASE'] = tempfile.mkstemp()
self.app = minitwit.app.test_client()
minitwit.init_db()
with minitwit.app.app_context():
minitwit.init_db()

def tearDown(self):
"""Get rid of the database again after each test."""
Expand Down
4 changes: 2 additions & 2 deletions flask/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,9 @@ def set_app_id(ctx, value):
value = prepare_exec_for_file(value)
elif '.' not in sys.path:
sys.path.insert(0, '.')
ctx.obj.app_import_path = value
ctx.ensure_object(ScriptInfo).app_import_path = value
def set_debug(ctx, value):
ctx.obj.debug = value
ctx.ensure_object(ScriptInfo).debug = value

click.Group.__init__(self, help=help, params=[
click.Option(['-a', '--app'],
Expand Down

0 comments on commit d2d8e66

Please sign in to comment.