Merge pull request #742 from ThomasWaldmann/sprint-branch
Before adoption: this commit came in with the history the project adopted, so no landing records it.
fix minitwit/flaskr test errors, improve docs about file open mode
authored by Daniel Neuhäuser · committed by Daniel Neuhäuser · parents 85ba8c9, 8bb972e · 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
Provenance
- Commit
ffbcad1- Tree
74462e6- Author
Daniel Neuhäuser <dasdasich@gmail.com>- Committer
Daniel Neuhäuser <dasdasich@gmail.com>- Parent 1
85ba8c9- Parent 2
8bb972e
8 files changed+42 added−40 removed
modified:docs/patterns/sqlite3.rst+1 added-1 removed
122 unmodified lines123 124 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()130 131 You can then create such a database from the python shell:3 unmodified linesmodified:docs/tutorial/dbinit.rst+1 added-1 removed
31 unmodified lines32 `flaskr.py`::33 34 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()39 40 The :func:`~contextlib.closing` helper function allows us to keep a26 unmodified linesmodified:examples/flaskr/flaskr_tests.py+8 added-8 removed
40 unmodified lines41 42 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.data46 47 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.data60 61 def test_messages(self):62 """Test that messages work"""63 self.login(flaskr.app.config['USERNAME'],1 unmodified line65 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 '<Hello>' 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'<Hello>' in rv.data71+ assert b'<strong>HTML</strong> allowed here' in rv.data72 73 74 if __name__ == '__main__':75 unittest.main()modified:examples/flaskr/flaskr.py+1 added-1 removed
29 unmodified lines30 def 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()37 38 66 unmodified linesmodified:examples/minitwit/minitwit_tests.py+28 added-28 removed
61 unmodified lines62 """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 rv68 69 # testing functions70 71 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.data86 87 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.data97 98 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 '<test message 2>' in rv.data104+ assert b'test message 1' in rv.data105+ assert b'<test message 2>' in rv.data106 107 def test_timelines(self):108 """Make sure that timelines work"""109 self.register_and_login('foo', 'default')1 unmodified line111 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.data117 118 # 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.data122 123 # now let's follow foo124 rv = self.app.get('/foo/follow', follow_redirects=True)125- assert 'You are now following "foo"' in rv.data125+ assert b'You are now following "foo"' in rv.data126 127 # 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.data131 132 # 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.data139 140 # now unfollow and check if that worked141 rv = self.app.get('/foo/unfollow', follow_redirects=True)142- assert 'You are no longer following "foo"' in rv.data142+ assert b'You are no longer following "foo"' 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.data146 147 148 if __name__ == '__main__':149 unittest.main()modified:examples/minitwit/minitwit.py+1 added-1 removed
51 unmodified lines52 def 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()59 60 195 unmodified linesmodified:flask/app.py+1 added
628 unmodified lines629 writing.630 631 :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)636 637 def create_jinja_environment(self):1178 unmodified linesmodified:flask/helpers.py+1 added
836 unmodified lines837 do_something_with(contents)838 839 :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)