Skip to content
Myr

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

1 file+177 added−191 removed

modified:tests/test_testing.py+177 added-191 removed
14 unmodified lines
1516from flask._compat import text_type171819class TestTestTools(object):19def 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.url2721 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/'3330 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/'34def test_environ_defaults():35 app = flask.Flask(__name__)36 app.testing = True37 @app.route('/')38 def index():39 return flask.request.url4036 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/'4643 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/'47def test_redirect_keep_session():48 app = flask.Flask(__name__)49 app.secret_key = 'testing'5049 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'5753 @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>')6160 @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>'6564 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'7168 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'7574 # 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'7878 rv = c.get('/getsession')79 assert rv.data == b'foo'79def test_session_transactions():80 app = flask.Flask(__name__)81 app.testing = True82 app.secret_key = 'testing'8381 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'])8786 @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]9890 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]99def test_session_transactions_no_null_sessions():100 app = flask.Flask(__name__)101 app.testing = True102101 def test_session_transactions_no_null_sessions(self):102 app = flask.Flask(__name__)103 app.testing = True104105 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'113114 def test_session_transactions_keep_context(self):115 app = flask.Flask(__name__)116 app.testing = True117 app.secret_key = 'testing'118119 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()125126 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'111138 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!'112def test_session_transactions_keep_context():113 app = flask.Flask(__name__)114 app.testing = True115 app.secret_key = 'testing'116146 @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()123150 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 == 200155156 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 = 23161162 try:163 flask.g.value164 except (AttributeError, RuntimeError):124def 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'135169 def test_reuse_client(self):170 app = flask.Flask(__name__)171 c = app.test_client()136def 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!'143173 with c:174 assert c.get('/').status_code == 404144 @app.route('/other')145 def other():146 1 // 0147176 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 == 200153179 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 = 23159186 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')166192 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]167def test_reuse_client():168 app = flask.Flask(__name__)169 c = app.test_client()170201 def test_full_url_request(self):202 app = flask.Flask(__name__)203 app.testing = True171 with c:172 assert c.get('/').status_code == 404173205 @app.route('/action', methods=['POST'])206 def action():207 return 'x'174 with c:175 assert c.get('/').status_code == 404176209 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.args177def test_test_client_calls_teardown_handlers():178 app = flask.Flask(__name__)179 called = []180 @app.teardown_request181 def remember(error):182 called.append(error)183184 with app.test_client() as c:185 assert called == []186 c.get('/')187 assert called == []188 assert called == [None]189216class 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]198218 @pytest.fixture219 def app(self, request):220 app = flask.Flask(__name__)221 app.config['SERVER_NAME'] = 'example.com'199def test_full_url_request():200 app = flask.Flask(__name__)201 app.testing = True202223 ctx = app.test_request_context()224 ctx.push()203 @app.route('/action', methods=['POST'])204 def action():205 return 'x'206226 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.args212232 @pytest.fixture233 def client(self, app):234 return app.test_client()213def 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_id219236 def test_subdomain(self, app, client):237 @app.route('/', subdomain='<company_id>')238 def view(company_id):239 return company_id240220 with app.test_request_context():221 url = flask.url_for('view', company_id='xxx')242 response = client.get(url)222244 assert 200 == response.status_code245 assert b'xxx' == response.data223 with app.test_client() as c:224 response = c.get(url)225226 assert 200 == response.status_code227 assert b'xxx' == response.data228229def 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_id235236 with app.test_request_context():237 url = flask.url_for('view', company_id='xxx')254 response = client.get(url)238239 with app.test_client() as c:240 response = c.get(url)241256 assert 200 == response.status_code257 assert b'xxx' == response.data242 assert 200 == response.status_code243 assert b'xxx' == response.data