Skip to content
Myr

test_regression.py

This file

Path

tests/test_regression.py

103 lines · 2.4 KiB · Python

Last changed by

Merge pull request #1389 from untitaker/always-memory-testMarkus Unterwaditzerbefore adoptionecb8f81

History of this file

Take it

Merge pull request #1165 from untitaker/new_testsuiteMarkus Unterwaditzerbefore adoptionbb94a61

lines 1–7why, one step back
1# -*- coding: utf-8 -*-
2"""3 tests.regression4 ~~~~~~~~~~~~~~~~~~~~~~~~~~56 Tests regressions.7

Merge pull request #1303 from Parkayun/masterMarkus Unterwaditzerbefore adoption5b98266

line 8why, one step back
8 :copyright: (c) 2015 by Armin Ronacher.

Merge pull request #1165 from untitaker/new_testsuiteMarkus Unterwaditzerbefore adoptionbb94a61

lines 9–103why, one step back
9 :license: BSD, see LICENSE for more details.
10"""1112import pytest1314import os15import gc16import sys17import flask18import threading19from werkzeug.exceptions import NotFound202122_gc_lock = threading.Lock()232425class assert_no_leak(object):2627 def __enter__(self):28 gc.disable()29 _gc_lock.acquire()30 loc = flask._request_ctx_stack._local3132 # Force Python to track this dictionary at all times.33 # This is necessary since Python only starts tracking34 # dicts if they contain mutable objects. It's a horrible,35 # horrible hack but makes this kinda testable.36 loc.__storage__['FOOO'] = [1, 2, 3]3738 gc.collect()39 self.old_objects = len(gc.get_objects())4041 def __exit__(self, exc_type, exc_value, tb):42 if not hasattr(sys, 'getrefcount'):43 gc.collect()44 new_objects = len(gc.get_objects())45 if new_objects > self.old_objects:46 pytest.fail('Example code leaked')47 _gc_lock.release()48 gc.enable()495051def test_memory_consumption():52 app = flask.Flask(__name__)5354 @app.route('/')55 def index():56 return flask.render_template('simple_template.html', whiskey=42)5758 def fire():59 with app.test_client() as c:60 rv = c.get('/')61 assert rv.status_code == 20062 assert rv.data == b'<h1>42</h1>'6364 # Trigger caches65 fire()6667 # This test only works on CPython 2.7.68 if sys.version_info >= (2, 7) and \69 not hasattr(sys, 'pypy_translation_info'):70 with assert_no_leak():71 for x in range(10):72 fire()737475def test_safe_join_toplevel_pardir():76 from flask.helpers import safe_join77 with pytest.raises(NotFound):78 safe_join('/foo', '..')798081def test_aborting():82 class Foo(Exception):83 whatever = 4284 app = flask.Flask(__name__)85 app.testing = True8687 @app.errorhandler(Foo)88 def handle_foo(e):89 return str(e.whatever)9091 @app.route('/')92 def index():93 raise flask.abort(flask.redirect(flask.url_for('test')))9495 @app.route('/test')96 def test():97 raise Foo()9899 with app.test_client() as c:100 rv = c.get('/')101 assert rv.headers['Location'] == 'http://localhost/test'102 rv = c.get('/test')103 assert rv.data == b'42'