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
Provenance
- Commit
f2dc38c- Tree
be1cde7- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
03168a5
4 files changed+153 added−4 removed
added:examples/minitwit/minitwit_tests.py+145 added
1+# -*- coding: utf-8 -*-2+"""3+ MiniTwit Tests4+ ~~~~~~~~~~~~~~5+6+ Tests the MiniTwit application.7+8+ :copyright: (c) 2010 by Armin Ronacher.9+ :license: BSD, see LICENSE for more details.10+"""11+import minitwit12+import unittest13+import tempfile14+from contextlib import closing15+16+17+class MiniTwitTestCase(unittest.TestCase):18+19+ 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()25+26+ # helper functions27+28+ 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)40+41+ 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)47+48+ 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)52+53+ def logout(self):54+ """Helper function to logout"""55+ return self.app.get('/logout', follow_redirects=True)56+57+ 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 rv64+65+ # testing functions66+67+ 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.data82+83+ 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.data93+94+ 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 '<test message 2>' in rv.data102+103+ 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.data113+114+ # 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.data118+119+ # now let's follow foo120+ rv = self.app.get('/foo/follow', follow_redirects=True)121+ assert 'You are now following "foo"' in rv.data122+123+ # 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.data127+128+ # 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.data135+136+ # now unfollow and check if that worked137+ rv = self.app.get('/foo/unfollow', follow_redirects=True)138+ assert 'You are no longer following "foo"' in rv.data139+ rv = self.app.get('/')140+ assert 'the message by foo' not in rv.data141+ assert 'the message by bar' in rv.data142+143+144+if __name__ == '__main__':145+ unittest.main()modified:examples/minitwit/minitwit.py+1 added-1 removed
214 unmodified lines215 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 linesmodified:examples/minitwit/README+5 added
17 unmodified lines18 19 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?24+25+ You betcha. Run the `minitwit_tests.py` file to26+ see the tests pass.modified:flask.py+2 added-3 removed
263 unmodified lines264 options.setdefault('use_reloader', self.debug)265 options.setdefault('use_debugger', self.debug)266 return run_simple(host, port, self, **options)267 268- @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)272 273 def open_resource(self, resource):320 unmodified lines