|
|
@ -41,6 +41,12 @@ class ConfigTestCase(FlaskTestCase): |
|
|
|
app.config.from_object(__name__) |
|
|
|
app.config.from_object(__name__) |
|
|
|
self.common_object_test(app) |
|
|
|
self.common_object_test(app) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_config_from_json(self): |
|
|
|
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
|
|
|
current_dir = os.path.dirname(os.path.abspath(__file__)) |
|
|
|
|
|
|
|
app.config.from_json(os.path.join(current_dir, 'static', 'config.json')) |
|
|
|
|
|
|
|
self.common_object_test(app) |
|
|
|
|
|
|
|
|
|
|
|
def test_config_from_class(self): |
|
|
|
def test_config_from_class(self): |
|
|
|
class Base(object): |
|
|
|
class Base(object): |
|
|
|
TEST_KEY = 'foo' |
|
|
|
TEST_KEY = 'foo' |
|
|
@ -100,11 +106,49 @@ class ConfigTestCase(FlaskTestCase): |
|
|
|
self.assert_true(0, 'expected config') |
|
|
|
self.assert_true(0, 'expected config') |
|
|
|
self.assert_false(app.config.from_pyfile('missing.cfg', silent=True)) |
|
|
|
self.assert_false(app.config.from_pyfile('missing.cfg', silent=True)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_config_missing_json(self): |
|
|
|
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
|
|
|
try: |
|
|
|
|
|
|
|
app.config.from_json('missing.json') |
|
|
|
|
|
|
|
except IOError as e: |
|
|
|
|
|
|
|
msg = str(e) |
|
|
|
|
|
|
|
self.assert_true(msg.startswith('[Errno 2] Unable to load configuration ' |
|
|
|
|
|
|
|
'file (No such file or directory):')) |
|
|
|
|
|
|
|
self.assert_true(msg.endswith("missing.json'")) |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
self.assert_true(0, 'expected config') |
|
|
|
|
|
|
|
self.assert_false(app.config.from_json('missing.json', silent=True)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_custom_config_class(self): |
|
|
|
|
|
|
|
class Config(flask.Config): |
|
|
|
|
|
|
|
pass |
|
|
|
|
|
|
|
class Flask(flask.Flask): |
|
|
|
|
|
|
|
config_class = Config |
|
|
|
|
|
|
|
app = Flask(__name__) |
|
|
|
|
|
|
|
self.assert_isinstance(app.config, Config) |
|
|
|
|
|
|
|
app.config.from_object(__name__) |
|
|
|
|
|
|
|
self.common_object_test(app) |
|
|
|
|
|
|
|
|
|
|
|
def test_session_lifetime(self): |
|
|
|
def test_session_lifetime(self): |
|
|
|
app = flask.Flask(__name__) |
|
|
|
app = flask.Flask(__name__) |
|
|
|
app.config['PERMANENT_SESSION_LIFETIME'] = 42 |
|
|
|
app.config['PERMANENT_SESSION_LIFETIME'] = 42 |
|
|
|
self.assert_equal(app.permanent_session_lifetime.seconds, 42) |
|
|
|
self.assert_equal(app.permanent_session_lifetime.seconds, 42) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_get_namespace(self): |
|
|
|
|
|
|
|
app = flask.Flask(__name__) |
|
|
|
|
|
|
|
app.config['FOO_OPTION_1'] = 'foo option 1' |
|
|
|
|
|
|
|
app.config['FOO_OPTION_2'] = 'foo option 2' |
|
|
|
|
|
|
|
app.config['BAR_STUFF_1'] = 'bar stuff 1' |
|
|
|
|
|
|
|
app.config['BAR_STUFF_2'] = 'bar stuff 2' |
|
|
|
|
|
|
|
foo_options = app.config.get_namespace('FOO_') |
|
|
|
|
|
|
|
self.assert_equal(2, len(foo_options)) |
|
|
|
|
|
|
|
self.assert_equal('foo option 1', foo_options['option_1']) |
|
|
|
|
|
|
|
self.assert_equal('foo option 2', foo_options['option_2']) |
|
|
|
|
|
|
|
bar_options = app.config.get_namespace('BAR_', lowercase=False) |
|
|
|
|
|
|
|
self.assert_equal(2, len(bar_options)) |
|
|
|
|
|
|
|
self.assert_equal('bar stuff 1', bar_options['STUFF_1']) |
|
|
|
|
|
|
|
self.assert_equal('bar stuff 2', bar_options['STUFF_2']) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class LimitedLoaderMockWrapper(object): |
|
|
|
class LimitedLoaderMockWrapper(object): |
|
|
|
def __init__(self, loader): |
|
|
|
def __init__(self, loader): |
|
|
|