forked from orbit-oss/flask
fix minitwit/flaskr test errors, improve docs about file open mode
app.open_resource needs to get called with the correct mode param (python3 will read bytes [not str] if the wrong mode is used), add mode param docs. rv.data is bytes, fix the data type we compare it with to be also bytes
This commit is contained in:
parent
85ba8c96e9
commit
8bb972e5ae
8 changed files with 42 additions and 40 deletions
|
|
@ -124,7 +124,7 @@ can do that for you::
|
|||
def init_db():
|
||||
with app.app_context():
|
||||
db = get_db()
|
||||
with app.open_resource('schema.sql') as f:
|
||||
with app.open_resource('schema.sql', mode='r') as f:
|
||||
db.cursor().executescript(f.read())
|
||||
db.commit()
|
||||
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ earlier. Just add that function below the `connect_db` function in
|
|||
|
||||
def init_db():
|
||||
with closing(connect_db()) as db:
|
||||
with app.open_resource('schema.sql') as f:
|
||||
with app.open_resource('schema.sql', mode='r') as f:
|
||||
db.cursor().executescript(f.read())
|
||||
db.commit()
|
||||
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ def init_db():
|
|||
"""Creates the database tables."""
|
||||
with app.app_context():
|
||||
db = get_db()
|
||||
with app.open_resource('schema.sql') as f:
|
||||
with app.open_resource('schema.sql', mode='r') as f:
|
||||
db.cursor().executescript(f.read())
|
||||
db.commit()
|
||||
|
||||
|
|
|
|||
|
|
@ -42,21 +42,21 @@ class FlaskrTestCase(unittest.TestCase):
|
|||
def test_empty_db(self):
|
||||
"""Start with a blank database."""
|
||||
rv = self.app.get('/')
|
||||
assert 'No entries here so far' in rv.data
|
||||
assert b'No entries here so far' in rv.data
|
||||
|
||||
def test_login_logout(self):
|
||||
"""Make sure login and logout works"""
|
||||
rv = self.login(flaskr.app.config['USERNAME'],
|
||||
flaskr.app.config['PASSWORD'])
|
||||
assert 'You were logged in' in rv.data
|
||||
assert b'You were logged in' in rv.data
|
||||
rv = self.logout()
|
||||
assert 'You were logged out' in rv.data
|
||||
assert b'You were logged out' in rv.data
|
||||
rv = self.login(flaskr.app.config['USERNAME'] + 'x',
|
||||
flaskr.app.config['PASSWORD'])
|
||||
assert 'Invalid username' in rv.data
|
||||
assert b'Invalid username' in rv.data
|
||||
rv = self.login(flaskr.app.config['USERNAME'],
|
||||
flaskr.app.config['PASSWORD'] + 'x')
|
||||
assert 'Invalid password' in rv.data
|
||||
assert b'Invalid password' in rv.data
|
||||
|
||||
def test_messages(self):
|
||||
"""Test that messages work"""
|
||||
|
|
@ -66,9 +66,9 @@ class FlaskrTestCase(unittest.TestCase):
|
|||
title='<Hello>',
|
||||
text='<strong>HTML</strong> allowed here'
|
||||
), follow_redirects=True)
|
||||
assert 'No entries here so far' not in rv.data
|
||||
assert '<Hello>' in rv.data
|
||||
assert '<strong>HTML</strong> allowed here' in rv.data
|
||||
assert b'No entries here so far' not in rv.data
|
||||
assert b'<Hello>' in rv.data
|
||||
assert b'<strong>HTML</strong> allowed here' in rv.data
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@ def init_db():
|
|||
"""Creates the database tables."""
|
||||
with app.app_context():
|
||||
db = get_db()
|
||||
with app.open_resource('schema.sql') as f:
|
||||
with app.open_resource('schema.sql', mode='r') as f:
|
||||
db.cursor().executescript(f.read())
|
||||
db.commit()
|
||||
|
||||
|
|
|
|||
|
|
@ -63,7 +63,7 @@ class MiniTwitTestCase(unittest.TestCase):
|
|||
rv = self.app.post('/add_message', data={'text': text},
|
||||
follow_redirects=True)
|
||||
if text:
|
||||
assert 'Your message was recorded' in rv.data
|
||||
assert b'Your message was recorded' in rv.data
|
||||
return rv
|
||||
|
||||
# testing functions
|
||||
|
|
@ -71,29 +71,29 @@ class MiniTwitTestCase(unittest.TestCase):
|
|||
def test_register(self):
|
||||
"""Make sure registering works"""
|
||||
rv = self.register('user1', 'default')
|
||||
assert 'You were successfully registered ' \
|
||||
'and can login now' in rv.data
|
||||
assert b'You were successfully registered ' \
|
||||
b'and can login now' in rv.data
|
||||
rv = self.register('user1', 'default')
|
||||
assert 'The username is already taken' in rv.data
|
||||
assert b'The username is already taken' in rv.data
|
||||
rv = self.register('', 'default')
|
||||
assert 'You have to enter a username' in rv.data
|
||||
assert b'You have to enter a username' in rv.data
|
||||
rv = self.register('meh', '')
|
||||
assert 'You have to enter a password' in rv.data
|
||||
assert b'You have to enter a password' in rv.data
|
||||
rv = self.register('meh', 'x', 'y')
|
||||
assert 'The two passwords do not match' in rv.data
|
||||
assert b'The two passwords do not match' in rv.data
|
||||
rv = self.register('meh', 'foo', email='broken')
|
||||
assert 'You have to enter a valid email address' in rv.data
|
||||
assert b'You have to enter a valid email address' in rv.data
|
||||
|
||||
def test_login_logout(self):
|
||||
"""Make sure logging in and logging out works"""
|
||||
rv = self.register_and_login('user1', 'default')
|
||||
assert 'You were logged in' in rv.data
|
||||
assert b'You were logged in' in rv.data
|
||||
rv = self.logout()
|
||||
assert 'You were logged out' in rv.data
|
||||
assert b'You were logged out' in rv.data
|
||||
rv = self.login('user1', 'wrongpassword')
|
||||
assert 'Invalid password' in rv.data
|
||||
assert b'Invalid password' in rv.data
|
||||
rv = self.login('user2', 'wrongpassword')
|
||||
assert 'Invalid username' in rv.data
|
||||
assert b'Invalid username' in rv.data
|
||||
|
||||
def test_message_recording(self):
|
||||
"""Check if adding messages works"""
|
||||
|
|
@ -101,8 +101,8 @@ class MiniTwitTestCase(unittest.TestCase):
|
|||
self.add_message('test message 1')
|
||||
self.add_message('<test message 2>')
|
||||
rv = self.app.get('/')
|
||||
assert 'test message 1' in rv.data
|
||||
assert '<test message 2>' in rv.data
|
||||
assert b'test message 1' in rv.data
|
||||
assert b'<test message 2>' in rv.data
|
||||
|
||||
def test_timelines(self):
|
||||
"""Make sure that timelines work"""
|
||||
|
|
@ -112,37 +112,37 @@ class MiniTwitTestCase(unittest.TestCase):
|
|||
self.register_and_login('bar', 'default')
|
||||
self.add_message('the message by bar')
|
||||
rv = self.app.get('/public')
|
||||
assert 'the message by foo' in rv.data
|
||||
assert 'the message by bar' in rv.data
|
||||
assert b'the message by foo' in rv.data
|
||||
assert b'the message by bar' in rv.data
|
||||
|
||||
# bar's timeline should just show bar's message
|
||||
rv = self.app.get('/')
|
||||
assert 'the message by foo' not in rv.data
|
||||
assert 'the message by bar' in rv.data
|
||||
assert b'the message by foo' not in rv.data
|
||||
assert b'the message by bar' in rv.data
|
||||
|
||||
# now let's follow foo
|
||||
rv = self.app.get('/foo/follow', follow_redirects=True)
|
||||
assert 'You are now following "foo"' in rv.data
|
||||
assert b'You are now following "foo"' in rv.data
|
||||
|
||||
# we should now see foo's message
|
||||
rv = self.app.get('/')
|
||||
assert 'the message by foo' in rv.data
|
||||
assert 'the message by bar' in rv.data
|
||||
assert b'the message by foo' in rv.data
|
||||
assert b'the message by bar' in rv.data
|
||||
|
||||
# but on the user's page we only want the user's message
|
||||
rv = self.app.get('/bar')
|
||||
assert 'the message by foo' not in rv.data
|
||||
assert 'the message by bar' in rv.data
|
||||
assert b'the message by foo' not in rv.data
|
||||
assert b'the message by bar' in rv.data
|
||||
rv = self.app.get('/foo')
|
||||
assert 'the message by foo' in rv.data
|
||||
assert 'the message by bar' not in rv.data
|
||||
assert b'the message by foo' in rv.data
|
||||
assert b'the message by bar' not in rv.data
|
||||
|
||||
# now unfollow and check if that worked
|
||||
rv = self.app.get('/foo/unfollow', follow_redirects=True)
|
||||
assert 'You are no longer following "foo"' in rv.data
|
||||
assert b'You are no longer following "foo"' in rv.data
|
||||
rv = self.app.get('/')
|
||||
assert 'the message by foo' not in rv.data
|
||||
assert 'the message by bar' in rv.data
|
||||
assert b'the message by foo' not in rv.data
|
||||
assert b'the message by bar' in rv.data
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
|
|||
|
|
@ -630,6 +630,7 @@ class Flask(_PackageBoundObject):
|
|||
|
||||
:param resource: the name of the resource. To access resources within
|
||||
subfolders use forward slashes as separator.
|
||||
:param mode: resource file opening mode, default is 'rb'.
|
||||
"""
|
||||
return open(os.path.join(self.instance_path, resource), mode)
|
||||
|
||||
|
|
|
|||
|
|
@ -838,6 +838,7 @@ class _PackageBoundObject(object):
|
|||
|
||||
:param resource: the name of the resource. To access resources within
|
||||
subfolders use forward slashes as separator.
|
||||
:param mode: resource file opening mode, default is 'rb'.
|
||||
"""
|
||||
if mode not in ('r', 'rb'):
|
||||
raise ValueError('Resources can only be opened for reading')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue