Skip to content
Myr

fix minitwit/flaskr test errors, improve docs about file open mode

Before adoption: this commit came in with the history the project adopted, so no landing records it.

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

authored by Thomas Waldmann · committed by Thomas Waldmann · parent 85ba8c9 · Browse files · 8 files+42 added−40 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

8 files+42 added−40 removed

  • other:+42 added−40 removed

8 files+42 added−40 removed

modified:docs/patterns/sqlite3.rst+1 added-1 removed
122 unmodified lines
123124 def init_db():125 with app.app_context():126 db = get_db()127 with app.open_resource('schema.sql') as f:127 with app.open_resource('schema.sql', mode='r') as f:128 db.cursor().executescript(f.read())129 db.commit()130131You can then create such a database from the python shell:
3 unmodified lines
modified:docs/tutorial/dbinit.rst+1 added-1 removed
31 unmodified lines
32`flaskr.py`::3334 def init_db():35 with closing(connect_db()) as db:36 with app.open_resource('schema.sql') as f:36 with app.open_resource('schema.sql', mode='r') as f:37 db.cursor().executescript(f.read())38 db.commit()3940The :func:`~contextlib.closing` helper function allows us to keep a
26 unmodified lines
modified:examples/flaskr/flaskr_tests.py+8 added-8 removed
40 unmodified lines
4142 def test_empty_db(self):43 """Start with a blank database."""44 rv = self.app.get('/')45 assert 'No entries here so far' in rv.data45 assert b'No entries here so far' in rv.data4647 def test_login_logout(self):48 """Make sure login and logout works"""49 rv = self.login(flaskr.app.config['USERNAME'],50 flaskr.app.config['PASSWORD'])51 assert 'You were logged in' in rv.data51 assert b'You were logged in' in rv.data52 rv = self.logout()53 assert 'You were logged out' in rv.data53 assert b'You were logged out' in rv.data54 rv = self.login(flaskr.app.config['USERNAME'] + 'x',55 flaskr.app.config['PASSWORD'])56 assert 'Invalid username' in rv.data56 assert b'Invalid username' in rv.data57 rv = self.login(flaskr.app.config['USERNAME'],58 flaskr.app.config['PASSWORD'] + 'x')59 assert 'Invalid password' in rv.data59 assert b'Invalid password' in rv.data6061 def test_messages(self):62 """Test that messages work"""63 self.login(flaskr.app.config['USERNAME'],
1 unmodified line
65 rv = self.app.post('/add', data=dict(66 title='<Hello>',67 text='<strong>HTML</strong> allowed here'68 ), follow_redirects=True)69 assert 'No entries here so far' not in rv.data70 assert '&lt;Hello&gt;' in rv.data71 assert '<strong>HTML</strong> allowed here' in rv.data69 assert b'No entries here so far' not in rv.data70 assert b'&lt;Hello&gt;' in rv.data71 assert b'<strong>HTML</strong> allowed here' in rv.data727374if __name__ == '__main__':75 unittest.main()
modified:examples/flaskr/flaskr.py+1 added-1 removed
29 unmodified lines
30def init_db():31 """Creates the database tables."""32 with app.app_context():33 db = get_db()34 with app.open_resource('schema.sql') as f:34 with app.open_resource('schema.sql', mode='r') as f:35 db.cursor().executescript(f.read())36 db.commit()3738
66 unmodified lines
modified:examples/minitwit/minitwit_tests.py+28 added-28 removed
61 unmodified lines
62 """Records a message"""63 rv = self.app.post('/add_message', data={'text': text},64 follow_redirects=True)65 if text:66 assert 'Your message was recorded' in rv.data66 assert b'Your message was recorded' in rv.data67 return rv6869 # testing functions7071 def test_register(self):72 """Make sure registering works"""73 rv = self.register('user1', 'default')74 assert 'You were successfully registered ' \75 'and can login now' in rv.data74 assert b'You were successfully registered ' \75 b'and can login now' in rv.data76 rv = self.register('user1', 'default')77 assert 'The username is already taken' in rv.data77 assert b'The username is already taken' in rv.data78 rv = self.register('', 'default')79 assert 'You have to enter a username' in rv.data79 assert b'You have to enter a username' in rv.data80 rv = self.register('meh', '')81 assert 'You have to enter a password' in rv.data81 assert b'You have to enter a password' in rv.data82 rv = self.register('meh', 'x', 'y')83 assert 'The two passwords do not match' in rv.data83 assert b'The two passwords do not match' in rv.data84 rv = self.register('meh', 'foo', email='broken')85 assert 'You have to enter a valid email address' in rv.data85 assert b'You have to enter a valid email address' in rv.data8687 def test_login_logout(self):88 """Make sure logging in and logging out works"""89 rv = self.register_and_login('user1', 'default')90 assert 'You were logged in' in rv.data90 assert b'You were logged in' in rv.data91 rv = self.logout()92 assert 'You were logged out' in rv.data92 assert b'You were logged out' in rv.data93 rv = self.login('user1', 'wrongpassword')94 assert 'Invalid password' in rv.data94 assert b'Invalid password' in rv.data95 rv = self.login('user2', 'wrongpassword')96 assert 'Invalid username' in rv.data96 assert b'Invalid username' in rv.data9798 def test_message_recording(self):99 """Check if adding messages works"""100 self.register_and_login('foo', 'default')101 self.add_message('test message 1')102 self.add_message('<test message 2>')103 rv = self.app.get('/')104 assert 'test message 1' in rv.data105 assert '&lt;test message 2&gt;' in rv.data104 assert b'test message 1' in rv.data105 assert b'&lt;test message 2&gt;' in rv.data106107 def test_timelines(self):108 """Make sure that timelines work"""109 self.register_and_login('foo', 'default')
1 unmodified line
111 self.logout()112 self.register_and_login('bar', 'default')113 self.add_message('the message by bar')114 rv = self.app.get('/public')115 assert 'the message by foo' in rv.data116 assert 'the message by bar' in rv.data115 assert b'the message by foo' in rv.data116 assert b'the message by bar' in rv.data117118 # bar's timeline should just show bar's message119 rv = self.app.get('/')120 assert 'the message by foo' not in rv.data121 assert 'the message by bar' in rv.data120 assert b'the message by foo' not in rv.data121 assert b'the message by bar' in rv.data122123 # now let's follow foo124 rv = self.app.get('/foo/follow', follow_redirects=True)125 assert 'You are now following &#34;foo&#34;' in rv.data125 assert b'You are now following &#34;foo&#34;' in rv.data126127 # we should now see foo's message128 rv = self.app.get('/')129 assert 'the message by foo' in rv.data130 assert 'the message by bar' in rv.data129 assert b'the message by foo' in rv.data130 assert b'the message by bar' in rv.data131132 # but on the user's page we only want the user's message133 rv = self.app.get('/bar')134 assert 'the message by foo' not in rv.data135 assert 'the message by bar' in rv.data134 assert b'the message by foo' not in rv.data135 assert b'the message by bar' in rv.data136 rv = self.app.get('/foo')137 assert 'the message by foo' in rv.data138 assert 'the message by bar' not in rv.data137 assert b'the message by foo' in rv.data138 assert b'the message by bar' not in rv.data139140 # now unfollow and check if that worked141 rv = self.app.get('/foo/unfollow', follow_redirects=True)142 assert 'You are no longer following &#34;foo&#34;' in rv.data142 assert b'You are no longer following &#34;foo&#34;' in rv.data143 rv = self.app.get('/')144 assert 'the message by foo' not in rv.data145 assert 'the message by bar' in rv.data144 assert b'the message by foo' not in rv.data145 assert b'the message by bar' in rv.data146147148if __name__ == '__main__':149 unittest.main()
modified:examples/minitwit/minitwit.py+1 added-1 removed
51 unmodified lines
52def init_db():53 """Creates the database tables."""54 with app.app_context():55 db = get_db()56 with app.open_resource('schema.sql') as f:56 with app.open_resource('schema.sql', mode='r') as f:57 db.cursor().executescript(f.read())58 db.commit()5960
195 unmodified lines
modified:flask/app.py+1 added
628 unmodified lines
629 writing.630631 :param resource: the name of the resource. To access resources within632 subfolders use forward slashes as separator.633 :param mode: resource file opening mode, default is 'rb'.634 """635 return open(os.path.join(self.instance_path, resource), mode)636637 def create_jinja_environment(self):
1178 unmodified lines
modified:flask/helpers.py+1 added
836 unmodified lines
837 do_something_with(contents)838839 :param resource: the name of the resource. To access resources within840 subfolders use forward slashes as separator.841 :param mode: resource file opening mode, default is 'rb'.842 """843 if mode not in ('r', 'rb'):844 raise ValueError('Resources can only be opened for reading')845 return open(os.path.join(self.root_path, resource), mode)