Skip to content
Myr

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

6 files+72 added−24 removed

modified:examples/flaskr/flaskr/_cliapp.py+12 added
1# -*- coding: utf-8 -*-2"""3 Flaskr4 ~~~~~~56 A microblog example application written as Flask tutorial with7 Flask and sqlite3.89 :copyright: (c) 2015 by Armin Ronacher.10 :license: BSD, see LICENSE for more details.11"""1213from flaskr.factory import create_app1415app = create_app()
modified:examples/flaskr/flaskr/factory.py+12 added
1# -*- coding: utf-8 -*-2"""3 Flaskr4 ~~~~~~56 A microblog example application written as Flask tutorial with7 Flask and sqlite3.89 :copyright: (c) 2015 by Armin Ronacher.10 :license: BSD, see LICENSE for more details.11"""1213import os14from flask import Flask, g15from werkzeug.utils import find_modules, import_string16from flaskr.blueprints.flaskr import init_db
48 unmodified lines
modified:examples/flaskr/tests/test_flaskr.py+30 added-19 removed
14 unmodified lines
15from flaskr.factory import create_app16from flaskr.blueprints.flaskr import init_db171819test_app = create_app()19@pytest.fixture20def app(request):2122 db_fd, temp_db_location = tempfile.mkstemp()23 config = {24 'DATABASE': temp_db_location,25 'TESTING': True,26 'DB_FD': db_fd27 }2822@pytest.fixture23def 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)3031 with app.app_context():32 init_db()33 yield app343536@pytest.fixture37def client(request, app):3839 client = app.test_client()4041 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)4546 return client47
14 unmodified lines
62 rv = client.get('/')63 assert b'No entries here so far' in rv.data646555def test_login_logout(client):66def 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.data798070def test_messages(client):81def 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 lines
modified:examples/flaskr/README+5 added-3 removed
7 unmodified lines
8 A sqlite powered thumble blog application910 ~ How do I use it?1112 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.1718 2. install the app from the root of the project directory1920 pip install --editable .2122 3. Instruct flask to use the right application2322 export FLASK_APP=flaskr24 export FLASK_APP=flaskr._cliapp2526 4. initialize the database with this command:2728 flask initdb
12 unmodified lines
modified:examples/flaskr/setup.cfg+2 added-2 removed
1[tool:pytest]2test=pytest1[aliases]2test=pytest
modified:examples/flaskr/setup.py+11 added
1# -*- coding: utf-8 -*-2"""3 Flaskr Tests4 ~~~~~~~~~~~~56 Tests the Flaskr application.78 :copyright: (c) 2015 by Armin Ronacher.9 :license: BSD, see LICENSE for more details.10"""1112from setuptools import setup, find_packages1314setup(15 name='flaskr',
12 unmodified lines