Skip to content
Myr

Added tests for minitwit. Testing with Flask is awesome

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

authored by Armin Ronacher · committed by Armin Ronacher · parent 03168a5 · Browse files · 4 files+153 added−4 removed
This commit

State

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

Diff stats

4 files+153 added−4 removed

  • other:+153 added−4 removed

4 files+153 added−4 removed

added:examples/minitwit/minitwit_tests.py+145 added
1# -*- coding: utf-8 -*-2"""3 MiniTwit Tests4 ~~~~~~~~~~~~~~56 Tests the MiniTwit application.78 :copyright: (c) 2010 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.10"""11import minitwit12import unittest13import tempfile14from contextlib import closing151617class MiniTwitTestCase(unittest.TestCase):1819 def setUp(self):20 """Before each test, set up a blank database"""21 self.db = tempfile.NamedTemporaryFile()22 self.app = minitwit.app.test_client()23 minitwit.DATABASE = self.db.name24 minitwit.init_db()2526 # helper functions2728 def register(self, username, password, password2=None, email=None):29 """Helper function to register a user"""30 if password2 is None:31 password2 = password32 if email is None:33 email = username + '@example.com'34 return self.app.post('/register', data={35 'username': username,36 'password': password,37 'password2': password2,38 'email': email,39 }, follow_redirects=True)4041 def login(self, username, password):42 """Helper function to login"""43 return self.app.post('/login', data={44 'username': username,45 'password': password46 }, follow_redirects=True)4748 def register_and_login(self, username, password):49 """Registers and logs in in one go"""50 self.register(username, password)51 return self.login(username, password)5253 def logout(self):54 """Helper function to logout"""55 return self.app.get('/logout', follow_redirects=True)5657 def add_message(self, text):58 """Records a message"""59 rv = self.app.post('/add_message', data={'text': text},60 follow_redirects=True)61 if text:62 assert 'Your message was recorded' in rv.data63 return rv6465 # testing functions6667 def test_register(self):68 """Make sure registering works"""69 rv = self.register('user1', 'default')70 assert 'You were successfully registered ' \71 'and can login now' in rv.data72 rv = self.register('user1', 'default')73 assert 'The username is already taken' in rv.data74 rv = self.register('', 'default')75 assert 'You have to enter a username' in rv.data76 rv = self.register('meh', '')77 assert 'You have to enter a password' in rv.data78 rv = self.register('meh', 'x', 'y')79 assert 'The two passwords do not match' in rv.data80 rv = self.register('meh', 'foo', email='broken')81 assert 'You have to enter a valid email address' in rv.data8283 def test_login_logout(self):84 """Make sure logging in and logging out works"""85 rv = self.register_and_login('user1', 'default')86 assert 'You were logged in' in rv.data87 rv = self.logout()88 assert 'You were logged out' in rv.data89 rv = self.login('user1', 'wrongpassword')90 assert 'Invalid password' in rv.data91 rv = self.login('user2', 'wrongpassword')92 assert 'Invalid username' in rv.data9394 def test_message_recording(self):95 """Check if adding messages works"""96 self.register_and_login('foo', 'default')97 self.add_message('test message 1')98 self.add_message('<test message 2>')99 rv = self.app.get('/')100 assert 'test message 1' in rv.data101 assert '&lt;test message 2&gt;' in rv.data102103 def test_timelines(self):104 """Make sure that timelines work"""105 self.register_and_login('foo', 'default')106 self.add_message('the message by foo')107 self.logout()108 self.register_and_login('bar', 'default')109 self.add_message('the message by bar')110 rv = self.app.get('/public')111 assert 'the message by foo' in rv.data112 assert 'the message by bar' in rv.data113114 # bar's timeline should just show bar's message115 rv = self.app.get('/')116 assert 'the message by foo' not in rv.data117 assert 'the message by bar' in rv.data118119 # now let's follow foo120 rv = self.app.get('/foo/follow', follow_redirects=True)121 assert 'You are now following &#34;foo&#34;' in rv.data122123 # we should now see foo's message124 rv = self.app.get('/')125 assert 'the message by foo' in rv.data126 assert 'the message by bar' in rv.data127128 # but on the user's page we only want the user's message129 rv = self.app.get('/bar')130 assert 'the message by foo' not in rv.data131 assert 'the message by bar' in rv.data132 rv = self.app.get('/foo')133 assert 'the message by foo' in rv.data134 assert 'the message by bar' not in rv.data135136 # now unfollow and check if that worked137 rv = self.app.get('/foo/unfollow', follow_redirects=True)138 assert 'You are no longer following &#34;foo&#34;' in rv.data139 rv = self.app.get('/')140 assert 'the message by foo' not in rv.data141 assert 'the message by bar' in rv.data142143144if __name__ == '__main__':145 unittest.main()
modified:examples/minitwit/minitwit.py+1 added-1 removed
214 unmodified lines
215 error = 'You have to enter a valid email address'216 elif not request.form['password']:217 error = 'You have to enter a password'218 elif request.form['password'] != request.form['password2']:219 error = 'The two passwords to not match'219 error = 'The two passwords do not match'220 elif get_user_id(request.form['username']) is not None:221 error = 'The username is already taken'222 else:223 g.db.execute('''insert into user (
27 unmodified lines
modified:examples/minitwit/README+5 added
17 unmodified lines
1819 3. now you can run the minitwit.py file with your20 python interpreter and the application will21 greet you on http://localhost:5000/22 23 ~ Is it tested?2425 You betcha. Run the `minitwit_tests.py` file to26 see the tests pass.
modified:flask.py+2 added-3 removed
263 unmodified lines
264 options.setdefault('use_reloader', self.debug)265 options.setdefault('use_debugger', self.debug)266 return run_simple(host, port, self, **options)267268 @cached_property269 def test(self):270 """A test client for this application"""268 def test_client(self):269 """Creates a test client for this application"""270 from werkzeug import Client271 return Client(self, self.response_class, use_cookies=True)272273 def open_resource(self, resource):
320 unmodified lines