Tests pass now.
This commit is contained in:
parent
961db8ad72
commit
8fa5e32d9a
24 changed files with 421 additions and 530 deletions
|
|
@ -1,36 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Blueprint Example Tests
|
||||
~~~~~~~~~~~~~~
|
||||
|
||||
Tests the Blueprint example app
|
||||
"""
|
||||
import blueprintexample
|
||||
import unittest
|
||||
|
||||
|
||||
class BlueprintExampleTestCase(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.app = blueprintexample.app.test_client()
|
||||
|
||||
def test_urls(self):
|
||||
r = self.app.get('/')
|
||||
self.assertEquals(r.status_code, 200)
|
||||
|
||||
r = self.app.get('/hello')
|
||||
self.assertEquals(r.status_code, 200)
|
||||
|
||||
r = self.app.get('/world')
|
||||
self.assertEquals(r.status_code, 200)
|
||||
|
||||
#second blueprint instance
|
||||
r = self.app.get('/pages/hello')
|
||||
self.assertEquals(r.status_code, 200)
|
||||
|
||||
r = self.app.get('/pages/world')
|
||||
self.assertEquals(r.status_code, 200)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
33
examples/blueprintexample/test_blueprintexample.py
Normal file
33
examples/blueprintexample/test_blueprintexample.py
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Blueprint Example Tests
|
||||
~~~~~~~~~~~~~~
|
||||
|
||||
Tests the Blueprint example app
|
||||
"""
|
||||
import pytest
|
||||
|
||||
import blueprintexample
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client():
|
||||
return blueprintexample.app.test_client()
|
||||
|
||||
|
||||
def test_urls(client):
|
||||
r = client.get('/')
|
||||
assert r.status_code == 200
|
||||
|
||||
r = client.get('/hello')
|
||||
assert r.status_code == 200
|
||||
|
||||
r = client.get('/world')
|
||||
assert r.status_code == 200
|
||||
|
||||
# second blueprint instance
|
||||
r = client.get('/pages/hello')
|
||||
assert r.status_code == 200
|
||||
|
||||
r = client.get('/pages/world')
|
||||
assert r.status_code == 200
|
||||
|
|
@ -1,76 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Flaskr Tests
|
||||
~~~~~~~~~~~~
|
||||
|
||||
Tests the Flaskr application.
|
||||
|
||||
:copyright: (c) 2014 by Armin Ronacher.
|
||||
:license: BSD, see LICENSE for more details.
|
||||
"""
|
||||
import os
|
||||
import flaskr
|
||||
import unittest
|
||||
import tempfile
|
||||
|
||||
|
||||
class FlaskrTestCase(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
"""Before each test, set up a blank database"""
|
||||
self.db_fd, flaskr.app.config['DATABASE'] = tempfile.mkstemp()
|
||||
flaskr.app.config['TESTING'] = True
|
||||
self.app = flaskr.app.test_client()
|
||||
with flaskr.app.app_context():
|
||||
flaskr.init_db()
|
||||
|
||||
def tearDown(self):
|
||||
"""Get rid of the database again after each test."""
|
||||
os.close(self.db_fd)
|
||||
os.unlink(flaskr.app.config['DATABASE'])
|
||||
|
||||
def login(self, username, password):
|
||||
return self.app.post('/login', data=dict(
|
||||
username=username,
|
||||
password=password
|
||||
), follow_redirects=True)
|
||||
|
||||
def logout(self):
|
||||
return self.app.get('/logout', follow_redirects=True)
|
||||
|
||||
# testing functions
|
||||
|
||||
def test_empty_db(self):
|
||||
"""Start with a blank database."""
|
||||
rv = self.app.get('/')
|
||||
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 b'You were logged in' in rv.data
|
||||
rv = self.logout()
|
||||
assert b'You were logged out' in rv.data
|
||||
rv = self.login(flaskr.app.config['USERNAME'] + 'x',
|
||||
flaskr.app.config['PASSWORD'])
|
||||
assert b'Invalid username' in rv.data
|
||||
rv = self.login(flaskr.app.config['USERNAME'],
|
||||
flaskr.app.config['PASSWORD'] + 'x')
|
||||
assert b'Invalid password' in rv.data
|
||||
|
||||
def test_messages(self):
|
||||
"""Test that messages work"""
|
||||
self.login(flaskr.app.config['USERNAME'],
|
||||
flaskr.app.config['PASSWORD'])
|
||||
rv = self.app.post('/add', data=dict(
|
||||
title='<Hello>',
|
||||
text='<strong>HTML</strong> allowed here'
|
||||
), follow_redirects=True)
|
||||
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__':
|
||||
unittest.main()
|
||||
77
examples/flaskr/test_flaskr.py
Normal file
77
examples/flaskr/test_flaskr.py
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Flaskr Tests
|
||||
~~~~~~~~~~~~
|
||||
|
||||
Tests the Flaskr application.
|
||||
|
||||
:copyright: (c) 2014 by Armin Ronacher.
|
||||
:license: BSD, see LICENSE for more details.
|
||||
"""
|
||||
|
||||
import pytest
|
||||
|
||||
import os
|
||||
import flaskr
|
||||
import tempfile
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client(request):
|
||||
db_fd, flaskr.app.config['DATABASE'] = tempfile.mkstemp()
|
||||
flaskr.app.config['TESTING'] = True
|
||||
client = flaskr.app.test_client()
|
||||
with flaskr.app.app_context():
|
||||
flaskr.init_db()
|
||||
|
||||
def teardown():
|
||||
os.close(db_fd)
|
||||
os.unlink(flaskr.app.config['DATABASE'])
|
||||
request.addfinalizer(teardown)
|
||||
|
||||
return client
|
||||
|
||||
|
||||
def login(client, username, password):
|
||||
return client.post('/login', data=dict(
|
||||
username=username,
|
||||
password=password
|
||||
), follow_redirects=True)
|
||||
|
||||
|
||||
def logout(client):
|
||||
return client.get('/logout', follow_redirects=True)
|
||||
|
||||
|
||||
def test_empty_db(client):
|
||||
"""Start with a blank database."""
|
||||
rv = client.get('/')
|
||||
assert b'No entries here so far' in rv.data
|
||||
|
||||
|
||||
def test_login_logout(client):
|
||||
"""Make sure login and logout works"""
|
||||
rv = login(client, flaskr.app.config['USERNAME'],
|
||||
flaskr.app.config['PASSWORD'])
|
||||
assert b'You were logged in' in rv.data
|
||||
rv = logout(client)
|
||||
assert b'You were logged out' in rv.data
|
||||
rv = login(client, flaskr.app.config['USERNAME'] + 'x',
|
||||
flaskr.app.config['PASSWORD'])
|
||||
assert b'Invalid username' in rv.data
|
||||
rv = login(client, flaskr.app.config['USERNAME'],
|
||||
flaskr.app.config['PASSWORD'] + 'x')
|
||||
assert b'Invalid password' in rv.data
|
||||
|
||||
|
||||
def test_messages(client):
|
||||
"""Test that messages work"""
|
||||
login(client, flaskr.app.config['USERNAME'],
|
||||
flaskr.app.config['PASSWORD'])
|
||||
rv = client.post('/add', data=dict(
|
||||
title='<Hello>',
|
||||
text='<strong>HTML</strong> allowed here'
|
||||
), follow_redirects=True)
|
||||
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
|
||||
|
|
@ -1,150 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
MiniTwit Tests
|
||||
~~~~~~~~~~~~~~
|
||||
|
||||
Tests the MiniTwit application.
|
||||
|
||||
:copyright: (c) 2014 by Armin Ronacher.
|
||||
:license: BSD, see LICENSE for more details.
|
||||
"""
|
||||
import os
|
||||
import minitwit
|
||||
import unittest
|
||||
import tempfile
|
||||
|
||||
|
||||
class MiniTwitTestCase(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
"""Before each test, set up a blank database"""
|
||||
self.db_fd, minitwit.app.config['DATABASE'] = tempfile.mkstemp()
|
||||
self.app = minitwit.app.test_client()
|
||||
with minitwit.app.app_context():
|
||||
minitwit.init_db()
|
||||
|
||||
def tearDown(self):
|
||||
"""Get rid of the database again after each test."""
|
||||
os.close(self.db_fd)
|
||||
os.unlink(minitwit.app.config['DATABASE'])
|
||||
|
||||
# helper functions
|
||||
|
||||
def register(self, username, password, password2=None, email=None):
|
||||
"""Helper function to register a user"""
|
||||
if password2 is None:
|
||||
password2 = password
|
||||
if email is None:
|
||||
email = username + '@example.com'
|
||||
return self.app.post('/register', data={
|
||||
'username': username,
|
||||
'password': password,
|
||||
'password2': password2,
|
||||
'email': email,
|
||||
}, follow_redirects=True)
|
||||
|
||||
def login(self, username, password):
|
||||
"""Helper function to login"""
|
||||
return self.app.post('/login', data={
|
||||
'username': username,
|
||||
'password': password
|
||||
}, follow_redirects=True)
|
||||
|
||||
def register_and_login(self, username, password):
|
||||
"""Registers and logs in in one go"""
|
||||
self.register(username, password)
|
||||
return self.login(username, password)
|
||||
|
||||
def logout(self):
|
||||
"""Helper function to logout"""
|
||||
return self.app.get('/logout', follow_redirects=True)
|
||||
|
||||
def add_message(self, text):
|
||||
"""Records a message"""
|
||||
rv = self.app.post('/add_message', data={'text': text},
|
||||
follow_redirects=True)
|
||||
if text:
|
||||
assert b'Your message was recorded' in rv.data
|
||||
return rv
|
||||
|
||||
# testing functions
|
||||
|
||||
def test_register(self):
|
||||
"""Make sure registering works"""
|
||||
rv = self.register('user1', 'default')
|
||||
assert b'You were successfully registered ' \
|
||||
b'and can login now' in rv.data
|
||||
rv = self.register('user1', 'default')
|
||||
assert b'The username is already taken' in rv.data
|
||||
rv = self.register('', 'default')
|
||||
assert b'You have to enter a username' in rv.data
|
||||
rv = self.register('meh', '')
|
||||
assert b'You have to enter a password' in rv.data
|
||||
rv = self.register('meh', 'x', 'y')
|
||||
assert b'The two passwords do not match' in rv.data
|
||||
rv = self.register('meh', 'foo', email='broken')
|
||||
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 b'You were logged in' in rv.data
|
||||
rv = self.logout()
|
||||
assert b'You were logged out' in rv.data
|
||||
rv = self.login('user1', 'wrongpassword')
|
||||
assert b'Invalid password' in rv.data
|
||||
rv = self.login('user2', 'wrongpassword')
|
||||
assert b'Invalid username' in rv.data
|
||||
|
||||
def test_message_recording(self):
|
||||
"""Check if adding messages works"""
|
||||
self.register_and_login('foo', 'default')
|
||||
self.add_message('test message 1')
|
||||
self.add_message('<test message 2>')
|
||||
rv = self.app.get('/')
|
||||
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"""
|
||||
self.register_and_login('foo', 'default')
|
||||
self.add_message('the message by foo')
|
||||
self.logout()
|
||||
self.register_and_login('bar', 'default')
|
||||
self.add_message('the message by bar')
|
||||
rv = self.app.get('/public')
|
||||
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 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 b'You are now following "foo"' in rv.data
|
||||
|
||||
# we should now see foo's message
|
||||
rv = self.app.get('/')
|
||||
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 b'the message by foo' not in rv.data
|
||||
assert b'the message by bar' in rv.data
|
||||
rv = self.app.get('/foo')
|
||||
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 b'You are no longer following "foo"' in rv.data
|
||||
rv = self.app.get('/')
|
||||
assert b'the message by foo' not in rv.data
|
||||
assert b'the message by bar' in rv.data
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
151
examples/minitwit/test_minitwit.py
Normal file
151
examples/minitwit/test_minitwit.py
Normal file
|
|
@ -0,0 +1,151 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
MiniTwit Tests
|
||||
~~~~~~~~~~~~~~
|
||||
|
||||
Tests the MiniTwit application.
|
||||
|
||||
:copyright: (c) 2014 by Armin Ronacher.
|
||||
:license: BSD, see LICENSE for more details.
|
||||
"""
|
||||
import os
|
||||
import minitwit
|
||||
import tempfile
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client(request):
|
||||
db_fd, minitwit.app.config['DATABASE'] = tempfile.mkstemp()
|
||||
client = minitwit.app.test_client()
|
||||
with minitwit.app.app_context():
|
||||
minitwit.init_db()
|
||||
|
||||
def teardown():
|
||||
"""Get rid of the database again after each test."""
|
||||
os.close(db_fd)
|
||||
os.unlink(minitwit.app.config['DATABASE'])
|
||||
request.addfinalizer(teardown)
|
||||
return client
|
||||
|
||||
|
||||
def register(client, username, password, password2=None, email=None):
|
||||
"""Helper function to register a user"""
|
||||
if password2 is None:
|
||||
password2 = password
|
||||
if email is None:
|
||||
email = username + '@example.com'
|
||||
return client.post('/register', data={
|
||||
'username': username,
|
||||
'password': password,
|
||||
'password2': password2,
|
||||
'email': email,
|
||||
}, follow_redirects=True)
|
||||
|
||||
|
||||
def login(client, username, password):
|
||||
"""Helper function to login"""
|
||||
return client.post('/login', data={
|
||||
'username': username,
|
||||
'password': password
|
||||
}, follow_redirects=True)
|
||||
|
||||
|
||||
def register_and_login(client, username, password):
|
||||
"""Registers and logs in in one go"""
|
||||
register(client, username, password)
|
||||
return login(client, username, password)
|
||||
|
||||
|
||||
def logout(client):
|
||||
"""Helper function to logout"""
|
||||
return client.get('/logout', follow_redirects=True)
|
||||
|
||||
|
||||
def add_message(client, text):
|
||||
"""Records a message"""
|
||||
rv = client.post('/add_message', data={'text': text},
|
||||
follow_redirects=True)
|
||||
if text:
|
||||
assert b'Your message was recorded' in rv.data
|
||||
return rv
|
||||
|
||||
|
||||
def test_register(client):
|
||||
"""Make sure registering works"""
|
||||
rv = register(client, 'user1', 'default')
|
||||
assert b'You were successfully registered ' \
|
||||
b'and can login now' in rv.data
|
||||
rv = register(client, 'user1', 'default')
|
||||
assert b'The username is already taken' in rv.data
|
||||
rv = register(client, '', 'default')
|
||||
assert b'You have to enter a username' in rv.data
|
||||
rv = register(client, 'meh', '')
|
||||
assert b'You have to enter a password' in rv.data
|
||||
rv = register(client, 'meh', 'x', 'y')
|
||||
assert b'The two passwords do not match' in rv.data
|
||||
rv = register(client, 'meh', 'foo', email='broken')
|
||||
assert b'You have to enter a valid email address' in rv.data
|
||||
|
||||
|
||||
def test_login_logout(client):
|
||||
"""Make sure logging in and logging out works"""
|
||||
rv = register_and_login(client, 'user1', 'default')
|
||||
assert b'You were logged in' in rv.data
|
||||
rv = logout(client)
|
||||
assert b'You were logged out' in rv.data
|
||||
rv = login(client, 'user1', 'wrongpassword')
|
||||
assert b'Invalid password' in rv.data
|
||||
rv = login(client, 'user2', 'wrongpassword')
|
||||
assert b'Invalid username' in rv.data
|
||||
|
||||
|
||||
def test_message_recording(client):
|
||||
"""Check if adding messages works"""
|
||||
register_and_login(client, 'foo', 'default')
|
||||
add_message(client, 'test message 1')
|
||||
add_message(client, '<test message 2>')
|
||||
rv = client.get('/')
|
||||
assert b'test message 1' in rv.data
|
||||
assert b'<test message 2>' in rv.data
|
||||
|
||||
|
||||
def test_timelines(client):
|
||||
"""Make sure that timelines work"""
|
||||
register_and_login(client, 'foo', 'default')
|
||||
add_message(client, 'the message by foo')
|
||||
logout(client)
|
||||
register_and_login(client, 'bar', 'default')
|
||||
add_message(client, 'the message by bar')
|
||||
rv = client.get('/public')
|
||||
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 = client.get('/')
|
||||
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 = client.get('/foo/follow', follow_redirects=True)
|
||||
assert b'You are now following "foo"' in rv.data
|
||||
|
||||
# we should now see foo's message
|
||||
rv = client.get('/')
|
||||
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 = client.get('/bar')
|
||||
assert b'the message by foo' not in rv.data
|
||||
assert b'the message by bar' in rv.data
|
||||
rv = client.get('/foo')
|
||||
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 = client.get('/foo/unfollow', follow_redirects=True)
|
||||
assert b'You are no longer following "foo"' in rv.data
|
||||
rv = client.get('/')
|
||||
assert b'the message by foo' not in rv.data
|
||||
assert b'the message by bar' in rv.data
|
||||
Loading…
Add table
Add a link
Reference in a new issue