Kill classes in test_testing
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Markus Unterwaditzer · committed by Markus Unterwaditzer · parent 77d8875 · Browse files · 1 file·+177 added−191 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+177 added−191 removed
- tests:+177 added−191 removed
Provenance
- Commit
f8a778d- Tree
e98bcfc- Author
Markus Unterwaditzer <markus@unterwaditzer.net>- Committer
Markus Unterwaditzer <markus@unterwaditzer.net>- Parent
77d8875
1 file changed+177 added−191 removed
modified:tests/test_testing.py+177 added-191 removed
14 unmodified lines15 16 from flask._compat import text_type17 18 19-class TestTestTools(object):19+def test_environ_defaults_from_config():20+ app = flask.Flask(__name__)21+ app.testing = True22+ app.config['SERVER_NAME'] = 'example.com:1234'23+ app.config['APPLICATION_ROOT'] = '/foo'24+ @app.route('/')25+ def index():26+ return flask.request.url27 21- def test_environ_defaults_from_config(self):22- app = flask.Flask(__name__)23- app.testing = True24- app.config['SERVER_NAME'] = 'example.com:1234'25- app.config['APPLICATION_ROOT'] = '/foo'26- @app.route('/')27- def index():28- return flask.request.url28+ ctx = app.test_request_context()29+ assert ctx.request.url == 'http://example.com:1234/foo/'30+ with app.test_client() as c:31+ rv = c.get('/')32+ assert rv.data == b'http://example.com:1234/foo/'33 30- ctx = app.test_request_context()31- assert ctx.request.url == 'http://example.com:1234/foo/'32- with app.test_client() as c:33- rv = c.get('/')34- assert rv.data == b'http://example.com:1234/foo/'34+def test_environ_defaults():35+ app = flask.Flask(__name__)36+ app.testing = True37+ @app.route('/')38+ def index():39+ return flask.request.url40 36- def test_environ_defaults(self):37- app = flask.Flask(__name__)38- app.testing = True39- @app.route('/')40- def index():41- return flask.request.url41+ ctx = app.test_request_context()42+ assert ctx.request.url == 'http://localhost/'43+ with app.test_client() as c:44+ rv = c.get('/')45+ assert rv.data == b'http://localhost/'46 43- ctx = app.test_request_context()44- assert ctx.request.url == 'http://localhost/'45- with app.test_client() as c:46- rv = c.get('/')47- assert rv.data == b'http://localhost/'47+def test_redirect_keep_session():48+ app = flask.Flask(__name__)49+ app.secret_key = 'testing'50 49- def test_redirect_keep_session(self):50- app = flask.Flask(__name__)51- app.secret_key = 'testing'51+ @app.route('/', methods=['GET', 'POST'])52+ def index():53+ if flask.request.method == 'POST':54+ return flask.redirect('/getsession')55+ flask.session['data'] = 'foo'56+ return 'index'57 53- @app.route('/', methods=['GET', 'POST'])54- def index():55- if flask.request.method == 'POST':56- return flask.redirect('/getsession')57- flask.session['data'] = 'foo'58- return 'index'58+ @app.route('/getsession')59+ def get_session():60+ return flask.session.get('data', '<missing>')61 60- @app.route('/getsession')61- def get_session():62- return flask.session.get('data', '<missing>')62+ with app.test_client() as c:63+ rv = c.get('/getsession')64+ assert rv.data == b'<missing>'65 64- with app.test_client() as c:65- rv = c.get('/getsession')66- assert rv.data == b'<missing>'66+ rv = c.get('/')67+ assert rv.data == b'index'68+ assert flask.session.get('data') == 'foo'69+ rv = c.post('/', data={}, follow_redirects=True)70+ assert rv.data == b'foo'71 68- rv = c.get('/')69- assert rv.data == b'index'72+ # This support requires a new Werkzeug version73+ if not hasattr(c, 'redirect_client'):74 assert flask.session.get('data') == 'foo'71- rv = c.post('/', data={}, follow_redirects=True)72- assert rv.data == b'foo'75 74- # This support requires a new Werkzeug version75- if not hasattr(c, 'redirect_client'):76- assert flask.session.get('data') == 'foo'76+ rv = c.get('/getsession')77+ assert rv.data == b'foo'78 78- rv = c.get('/getsession')79- assert rv.data == b'foo'79+def test_session_transactions():80+ app = flask.Flask(__name__)81+ app.testing = True82+ app.secret_key = 'testing'83 81- def test_session_transactions(self):82- app = flask.Flask(__name__)83- app.testing = True84- app.secret_key = 'testing'84+ @app.route('/')85+ def index():86+ return text_type(flask.session['foo'])87 86- @app.route('/')87- def index():88- return text_type(flask.session['foo'])88+ with app.test_client() as c:89+ with c.session_transaction() as sess:90+ assert len(sess) == 091+ sess['foo'] = [42]92+ assert len(sess) == 193+ rv = c.get('/')94+ assert rv.data == b'[42]'95+ with c.session_transaction() as sess:96+ assert len(sess) == 197+ assert sess['foo'] == [42]98 90- with app.test_client() as c:91- with c.session_transaction() as sess:92- assert len(sess) == 093- sess['foo'] = [42]94- assert len(sess) == 195- rv = c.get('/')96- assert rv.data == b'[42]'97- with c.session_transaction() as sess:98- assert len(sess) == 199- assert sess['foo'] == [42]99+def test_session_transactions_no_null_sessions():100+ app = flask.Flask(__name__)101+ app.testing = True102 101- def test_session_transactions_no_null_sessions(self):102- app = flask.Flask(__name__)103- app.testing = True104-105- with app.test_client() as c:103+ with app.test_client() as c:106- try:107- with c.session_transaction() as sess:108- pass109- except RuntimeError as e:110- assert 'Session backend did not open a session' in str(e)111- else:112- assert False, 'Expected runtime error'113-114- def test_session_transactions_keep_context(self):115- app = flask.Flask(__name__)116- app.testing = True117- app.secret_key = 'testing'118-119- with app.test_client() as c:120- rv = c.get('/')121- req = flask.request._get_current_object()122- assert req is not None123- with c.session_transaction():124- assert req is flask.request._get_current_object()125-126- def test_session_transaction_needs_cookies(self):127- app = flask.Flask(__name__)128- app.testing = True129- c = app.test_client(use_cookies=False)104 try:131- with c.session_transaction() as s:105+ with c.session_transaction() as sess:106 pass107 except RuntimeError as e:134- assert 'cookies' in str(e)108+ assert 'Session backend did not open a session' in str(e)109 else:110 assert False, 'Expected runtime error'111 138- def test_test_client_context_binding(self):139- app = flask.Flask(__name__)140- app.config['LOGGER_HANDLER_POLICY'] = 'never'141- @app.route('/')142- def index():143- flask.g.value = 42144- return 'Hello World!'112+def test_session_transactions_keep_context():113+ app = flask.Flask(__name__)114+ app.testing = True115+ app.secret_key = 'testing'116 146- @app.route('/other')147- def other():148- 1 // 0117+ with app.test_client() as c:118+ rv = c.get('/')119+ req = flask.request._get_current_object()120+ assert req is not None121+ with c.session_transaction():122+ assert req is flask.request._get_current_object()123 150- with app.test_client() as c:151- resp = c.get('/')152- assert flask.g.value == 42153- assert resp.data == b'Hello World!'154- assert resp.status_code == 200155-156- resp = c.get('/other')157- assert not hasattr(flask.g, 'value')158- assert b'Internal Server Error' in resp.data159- assert resp.status_code == 500160- flask.g.value = 23161-162- try:163- flask.g.value164- except (AttributeError, RuntimeError):124+def test_session_transaction_needs_cookies():125+ app = flask.Flask(__name__)126+ app.testing = True127+ c = app.test_client(use_cookies=False)128+ try:129+ with c.session_transaction() as s:130 pass166- else:167- raise AssertionError('some kind of exception expected')131+ except RuntimeError as e:132+ assert 'cookies' in str(e)133+ else:134+ assert False, 'Expected runtime error'135 169- def test_reuse_client(self):170- app = flask.Flask(__name__)171- c = app.test_client()136+def test_test_client_context_binding():137+ app = flask.Flask(__name__)138+ app.config['LOGGER_HANDLER_POLICY'] = 'never'139+ @app.route('/')140+ def index():141+ flask.g.value = 42142+ return 'Hello World!'143 173- with c:174- assert c.get('/').status_code == 404144+ @app.route('/other')145+ def other():146+ 1 // 0147 176- with c:177- assert c.get('/').status_code == 404148+ with app.test_client() as c:149+ resp = c.get('/')150+ assert flask.g.value == 42151+ assert resp.data == b'Hello World!'152+ assert resp.status_code == 200153 179- def test_test_client_calls_teardown_handlers(self):180- app = flask.Flask(__name__)181- called = []182- @app.teardown_request183- def remember(error):184- called.append(error)154+ resp = c.get('/other')155+ assert not hasattr(flask.g, 'value')156+ assert b'Internal Server Error' in resp.data157+ assert resp.status_code == 500158+ flask.g.value = 23159 186- with app.test_client() as c:187- assert called == []188- c.get('/')189- assert called == []190- assert called == [None]160+ try:161+ flask.g.value162+ except (AttributeError, RuntimeError):163+ pass164+ else:165+ raise AssertionError('some kind of exception expected')166 192- del called[:]193- with app.test_client() as c:194- assert called == []195- c.get('/')196- assert called == []197- c.get('/')198- assert called == [None]199- assert called == [None, None]167+def test_reuse_client():168+ app = flask.Flask(__name__)169+ c = app.test_client()170 201- def test_full_url_request(self):202- app = flask.Flask(__name__)203- app.testing = True171+ with c:172+ assert c.get('/').status_code == 404173 205- @app.route('/action', methods=['POST'])206- def action():207- return 'x'174+ with c:175+ assert c.get('/').status_code == 404176 209- with app.test_client() as c:210- rv = c.post('http://domain.com/action?vodka=42', data={'gin': 43})211- assert rv.status_code == 200212- assert 'gin' in flask.request.form213- assert 'vodka' in flask.request.args177+def test_test_client_calls_teardown_handlers():178+ app = flask.Flask(__name__)179+ called = []180+ @app.teardown_request181+ def remember(error):182+ called.append(error)183 184+ with app.test_client() as c:185+ assert called == []186+ c.get('/')187+ assert called == []188+ assert called == [None]189 216-class TestSubdomain(object):190+ del called[:]191+ with app.test_client() as c:192+ assert called == []193+ c.get('/')194+ assert called == []195+ c.get('/')196+ assert called == [None]197+ assert called == [None, None]198 218- @pytest.fixture219- def app(self, request):220- app = flask.Flask(__name__)221- app.config['SERVER_NAME'] = 'example.com'199+def test_full_url_request():200+ app = flask.Flask(__name__)201+ app.testing = True202 223- ctx = app.test_request_context()224- ctx.push()203+ @app.route('/action', methods=['POST'])204+ def action():205+ return 'x'206 226- def teardown():227- if ctx is not None:228- ctx.pop()229- request.addfinalizer(teardown)230- return app207+ with app.test_client() as c:208+ rv = c.post('http://domain.com/action?vodka=42', data={'gin': 43})209+ assert rv.status_code == 200210+ assert 'gin' in flask.request.form211+ assert 'vodka' in flask.request.args212 232- @pytest.fixture233- def client(self, app):234- return app.test_client()213+def test_subdomain():214+ app = flask.Flask(__name__)215+ app.config['SERVER_NAME'] = 'example.com'216+ @app.route('/', subdomain='<company_id>')217+ def view(company_id):218+ return company_id219 236- def test_subdomain(self, app, client):237- @app.route('/', subdomain='<company_id>')238- def view(company_id):239- return company_id240-220+ with app.test_request_context():221 url = flask.url_for('view', company_id='xxx')242- response = client.get(url)222 244- assert 200 == response.status_code245- assert b'xxx' == response.data223+ with app.test_client() as c:224+ response = c.get(url)225 226+ assert 200 == response.status_code227+ assert b'xxx' == response.data228 229+def test_nosubdomain():230+ app = flask.Flask(__name__)248- def test_nosubdomain(self, app, client):249- @app.route('/<company_id>')250- def view(company_id):251- return company_id231+ app.config['SERVER_NAME'] = 'example.com'232+ @app.route('/<company_id>')233+ def view(company_id):234+ return company_id235 236+ with app.test_request_context():237 url = flask.url_for('view', company_id='xxx')254- response = client.get(url)238 239+ with app.test_client() as c:240+ response = c.get(url)241+256- assert 200 == response.status_code257- assert b'xxx' == response.data242+ assert 200 == response.status_code243+ assert b'xxx' == response.data