Add in a fixture utilizing app factory
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Kyle Lawlor · committed by Kyle Lawlor · parent 8cf32bc · Browse files · 6 files·+72 added−24 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+72 added−24 removed
- tests:+30 added−19 removed
- other:+42 added−5 removed
Provenance
- Commit
54b6fc8- Tree
f753680- Author
Kyle Lawlor <klawlor419@gmail.com>- Committer
Kyle Lawlor <klawlor419@gmail.com>- Parent
8cf32bc
6 files changed+72 added−24 removed
modified:examples/flaskr/flaskr/_cliapp.py+12 added
1+# -*- coding: utf-8 -*-2+"""3+ Flaskr4+ ~~~~~~5+6+ A microblog example application written as Flask tutorial with7+ Flask and sqlite3.8+9+ :copyright: (c) 2015 by Armin Ronacher.10+ :license: BSD, see LICENSE for more details.11+"""12+13 from flaskr.factory import create_app14 15 app = create_app()modified:examples/flaskr/flaskr/factory.py+12 added
1+# -*- coding: utf-8 -*-2+"""3+ Flaskr4+ ~~~~~~5+6+ A microblog example application written as Flask tutorial with7+ Flask and sqlite3.8+9+ :copyright: (c) 2015 by Armin Ronacher.10+ :license: BSD, see LICENSE for more details.11+"""12+13 import os14 from flask import Flask, g15 from werkzeug.utils import find_modules, import_string16 from flaskr.blueprints.flaskr import init_db48 unmodified linesmodified:examples/flaskr/tests/test_flaskr.py+30 added-19 removed
14 unmodified lines15 from flaskr.factory import create_app16 from flaskr.blueprints.flaskr import init_db17 18 19-test_app = create_app()19+@pytest.fixture20+def app(request):21 22+ db_fd, temp_db_location = tempfile.mkstemp()23+ config = {24+ 'DATABASE': temp_db_location,25+ 'TESTING': True,26+ 'DB_FD': db_fd27+ }28 22-@pytest.fixture23-def client(request):24- db_fd, test_app.config['DATABASE'] = tempfile.mkstemp()25- test_app.config['TESTING'] = True26- client = test_app.test_client()27- with test_app.app_context():29+ app = create_app(config=config)30+31+ with app.app_context():32 init_db()33+ yield app34 35+36+@pytest.fixture37+def client(request, app):38+39+ client = app.test_client()40+41 def teardown():31- os.close(db_fd)32- os.unlink(test_app.config['DATABASE'])42+ os.close(app.config['DB_FD'])43+ os.unlink(app.config['DATABASE'])44 request.addfinalizer(teardown)45 46 return client47 14 unmodified lines62 rv = client.get('/')63 assert b'No entries here so far' in rv.data64 65 55-def test_login_logout(client):66+def test_login_logout(client, app):67 """Make sure login and logout works"""57- rv = login(client, test_app.config['USERNAME'],58- test_app.config['PASSWORD'])68+ rv = login(client, app.config['USERNAME'],69+ app.config['PASSWORD'])70 assert b'You were logged in' in rv.data71 rv = logout(client)72 assert b'You were logged out' in rv.data62- rv = login(client,test_app.config['USERNAME'] + 'x',63- test_app.config['PASSWORD'])73+ rv = login(client,app.config['USERNAME'] + 'x',74+ app.config['PASSWORD'])75 assert b'Invalid username' in rv.data65- rv = login(client, test_app.config['USERNAME'],66- test_app.config['PASSWORD'] + 'x')76+ rv = login(client, app.config['USERNAME'],77+ app.config['PASSWORD'] + 'x')78 assert b'Invalid password' in rv.data79 80 70-def test_messages(client):81+def test_messages(client, app):82 """Test that messages work"""72- login(client, test_app.config['USERNAME'],73- test_app.config['PASSWORD'])83+ login(client, app.config['USERNAME'],84+ app.config['PASSWORD'])85 rv = client.post('/add', data=dict(86 title='<Hello>',87 text='<strong>HTML</strong> allowed here'88 ), follow_redirects=True)3 unmodified linesmodified:examples/flaskr/README+5 added-3 removed
7 unmodified lines8 A sqlite powered thumble blog application9 10 ~ How do I use it?11 12- 1. edit the configuration in the flaskr.py file or12+ 1. edit the configuration in the factory.py file or13 export an FLASKR_SETTINGS environment variable14- pointing to a configuration file.14+ pointing to a configuration file or pass in a 15+ dictionary with config values using the create_app16+ function.17 18 2. install the app from the root of the project directory19 20 pip install --editable .21 22 3. Instruct flask to use the right application23 22- export FLASK_APP=flaskr24+ export FLASK_APP=flaskr._cliapp25 26 4. initialize the database with this command:27 28 flask initdb12 unmodified linesmodified:examples/flaskr/setup.cfg+2 added-2 removed
1-[tool:pytest]2-test=pytest1+[aliases]2+test=pytestmodified:examples/flaskr/setup.py+11 added
1+# -*- coding: utf-8 -*-2+"""3+ Flaskr Tests4+ ~~~~~~~~~~~~5+6+ Tests the Flaskr application.7+8+ :copyright: (c) 2015 by Armin Ronacher.9+ :license: BSD, see LICENSE for more details.10+"""11+12 from setuptools import setup, find_packages13 14 setup(15 name='flaskr',12 unmodified lines