Browse Source

Split instance tests off into own file

pull/1165/head
Markus Unterwaditzer 11 years ago
parent
commit
be42d5665e
  1. 399
      tests/test_config.py
  2. 132
      tests/test_instance_config.py

399
tests/test_config.py

@ -1,25 +1,16 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
""" """
tests.config tests.test_config
~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~
Configuration and instances. :copyright: (c) 2014 by the Flask Team, see AUTHORS for more details.
:copyright: (c) 2014 by Armin Ronacher.
:license: BSD, see LICENSE for more details. :license: BSD, see LICENSE for more details.
""" """
import pytest import pytest
import os import os
import sys
import flask import flask
import pkgutil
import unittest
import textwrap
from contextlib import contextmanager
from flask._compat import PY2
# config keys used for the TestConfig # config keys used for the TestConfig
@ -27,265 +18,167 @@ TEST_KEY = 'foo'
SECRET_KEY = 'devkey' SECRET_KEY = 'devkey'
class TestConfig(object): def common_object_test(app):
assert app.secret_key == 'devkey'
assert app.config['TEST_KEY'] == 'foo'
assert 'TestConfig' not in app.config
def common_object_test(self, app):
assert app.secret_key == 'devkey'
assert app.config['TEST_KEY'] == 'foo'
assert 'TestConfig' not in app.config
def test_config_from_file(self): def test_config_from_file():
app = flask.Flask(__name__) app = flask.Flask(__name__)
app.config.from_pyfile(__file__.rsplit('.', 1)[0] + '.py') app.config.from_pyfile(__file__.rsplit('.', 1)[0] + '.py')
self.common_object_test(app) common_object_test(app)
def test_config_from_object(self):
app = flask.Flask(__name__)
app.config.from_object(__name__)
self.common_object_test(app)
def test_config_from_json(self): def test_config_from_object():
app = flask.Flask(__name__) app = flask.Flask(__name__)
current_dir = os.path.dirname(os.path.abspath(__file__)) app.config.from_object(__name__)
app.config.from_json(os.path.join(current_dir, 'static', 'config.json')) common_object_test(app)
self.common_object_test(app)
def test_config_from_mapping(self):
app = flask.Flask(__name__)
app.config.from_mapping({
'SECRET_KEY': 'devkey',
'TEST_KEY': 'foo'
})
self.common_object_test(app)
app = flask.Flask(__name__) def test_config_from_json():
app.config.from_mapping([ app = flask.Flask(__name__)
('SECRET_KEY', 'devkey'), current_dir = os.path.dirname(os.path.abspath(__file__))
('TEST_KEY', 'foo') app.config.from_json(os.path.join(current_dir, 'static', 'config.json'))
]) common_object_test(app)
self.common_object_test(app)
app = flask.Flask(__name__)
def test_config_from_mapping():
app = flask.Flask(__name__)
app.config.from_mapping({
'SECRET_KEY': 'devkey',
'TEST_KEY': 'foo'
})
common_object_test(app)
app = flask.Flask(__name__)
app.config.from_mapping([
('SECRET_KEY', 'devkey'),
('TEST_KEY', 'foo')
])
common_object_test(app)
app = flask.Flask(__name__)
app.config.from_mapping(
SECRET_KEY='devkey',
TEST_KEY='foo'
)
common_object_test(app)
app = flask.Flask(__name__)
with pytest.raises(TypeError):
app.config.from_mapping( app.config.from_mapping(
SECRET_KEY='devkey', {}, {}
TEST_KEY='foo'
) )
self.common_object_test(app)
app = flask.Flask(__name__)
with pytest.raises(TypeError):
app.config.from_mapping(
{}, {}
)
def test_config_from_class(self):
class Base(object):
TEST_KEY = 'foo'
class Test(Base):
SECRET_KEY = 'devkey'
app = flask.Flask(__name__)
app.config.from_object(Test)
self.common_object_test(app)
def test_config_from_envvar(self): def test_config_from_class():
env = os.environ class Base(object):
try: TEST_KEY = 'foo'
os.environ = {}
app = flask.Flask(__name__) class Test(Base):
try: SECRET_KEY = 'devkey'
app.config.from_envvar('FOO_SETTINGS') app = flask.Flask(__name__)
except RuntimeError as e: app.config.from_object(Test)
assert "'FOO_SETTINGS' is not set" in str(e) common_object_test(app)
else:
assert 0, 'expected exception'
assert not app.config.from_envvar('FOO_SETTINGS', silent=True) def test_config_from_envvar():
env = os.environ
os.environ = {'FOO_SETTINGS': __file__.rsplit('.', 1)[0] + '.py'} try:
assert app.config.from_envvar('FOO_SETTINGS') os.environ = {}
self.common_object_test(app)
finally:
os.environ = env
def test_config_from_envvar_missing(self):
env = os.environ
try:
os.environ = {'FOO_SETTINGS': 'missing.cfg'}
try:
app = flask.Flask(__name__)
app.config.from_envvar('FOO_SETTINGS')
except IOError as e:
msg = str(e)
assert msg.startswith('[Errno 2] Unable to load configuration '
'file (No such file or directory):')
assert msg.endswith("missing.cfg'")
else:
assert False, 'expected IOError'
assert not app.config.from_envvar('FOO_SETTINGS', silent=True)
finally:
os.environ = env
def test_config_missing(self):
app = flask.Flask(__name__) app = flask.Flask(__name__)
try: try:
app.config.from_pyfile('missing.cfg') app.config.from_envvar('FOO_SETTINGS')
except IOError as e: except RuntimeError as e:
msg = str(e) assert "'FOO_SETTINGS' is not set" in str(e)
assert msg.startswith('[Errno 2] Unable to load configuration '
'file (No such file or directory):')
assert msg.endswith("missing.cfg'")
else: else:
assert 0, 'expected config' assert 0, 'expected exception'
assert not app.config.from_pyfile('missing.cfg', silent=True) assert not app.config.from_envvar('FOO_SETTINGS', silent=True)
def test_config_missing_json(self): os.environ = {'FOO_SETTINGS': __file__.rsplit('.', 1)[0] + '.py'}
app = flask.Flask(__name__) assert app.config.from_envvar('FOO_SETTINGS')
common_object_test(app)
finally:
os.environ = env
def test_config_from_envvar_missing():
env = os.environ
try:
os.environ = {'FOO_SETTINGS': 'missing.cfg'}
try: try:
app.config.from_json('missing.json') app = flask.Flask(__name__)
app.config.from_envvar('FOO_SETTINGS')
except IOError as e: except IOError as e:
msg = str(e) msg = str(e)
assert msg.startswith('[Errno 2] Unable to load configuration ' assert msg.startswith('[Errno 2] Unable to load configuration '
'file (No such file or directory):') 'file (No such file or directory):')
assert msg.endswith("missing.json'") assert msg.endswith("missing.cfg'")
else: else:
assert 0, 'expected config' assert False, 'expected IOError'
assert not app.config.from_json('missing.json', silent=True) assert not app.config.from_envvar('FOO_SETTINGS', silent=True)
finally:
def test_custom_config_class(self): os.environ = env
class Config(flask.Config):
pass
class Flask(flask.Flask): def test_config_missing():
config_class = Config app = flask.Flask(__name__)
app = Flask(__name__) try:
assert isinstance(app.config, Config) app.config.from_pyfile('missing.cfg')
app.config.from_object(__name__) except IOError as e:
self.common_object_test(app) msg = str(e)
assert msg.startswith('[Errno 2] Unable to load configuration '
def test_session_lifetime(self): 'file (No such file or directory):')
app = flask.Flask(__name__) assert msg.endswith("missing.cfg'")
app.config['PERMANENT_SESSION_LIFETIME'] = 42 else:
assert app.permanent_session_lifetime.seconds == 42 assert 0, 'expected config'
assert not app.config.from_pyfile('missing.cfg', silent=True)
def test_get_namespace(self):
app = flask.Flask(__name__)
app.config['FOO_OPTION_1'] = 'foo option 1' def test_config_missing_json():
app.config['FOO_OPTION_2'] = 'foo option 2' app = flask.Flask(__name__)
app.config['BAR_STUFF_1'] = 'bar stuff 1' try:
app.config['BAR_STUFF_2'] = 'bar stuff 2' app.config.from_json('missing.json')
foo_options = app.config.get_namespace('FOO_') except IOError as e:
assert 2 == len(foo_options) msg = str(e)
assert 'foo option 1' == foo_options['option_1'] assert msg.startswith('[Errno 2] Unable to load configuration '
assert 'foo option 2' == foo_options['option_2'] 'file (No such file or directory):')
bar_options = app.config.get_namespace('BAR_', lowercase=False) assert msg.endswith("missing.json'")
assert 2 == len(bar_options) else:
assert 'bar stuff 1' == bar_options['STUFF_1'] assert 0, 'expected config'
assert 'bar stuff 2' == bar_options['STUFF_2'] assert not app.config.from_json('missing.json', silent=True)
class TestInstance(object): def test_custom_config_class():
def test_explicit_instance_paths(self, modules_tmpdir): class Config(flask.Config):
with pytest.raises(ValueError) as excinfo: pass
flask.Flask(__name__, instance_path='instance')
assert 'must be absolute' in str(excinfo.value) class Flask(flask.Flask):
config_class = Config
app = flask.Flask(__name__, instance_path=str(modules_tmpdir)) app = Flask(__name__)
assert app.instance_path == str(modules_tmpdir) assert isinstance(app.config, Config)
app.config.from_object(__name__)
def test_main_module_paths(self, modules_tmpdir, purge_module): common_object_test(app)
app = modules_tmpdir.join('main_app.py')
app.write('import flask\n\napp = flask.Flask("__main__")')
purge_module('main_app') def test_session_lifetime():
app = flask.Flask(__name__)
from main_app import app app.config['PERMANENT_SESSION_LIFETIME'] = 42
here = os.path.abspath(os.getcwd()) assert app.permanent_session_lifetime.seconds == 42
assert app.instance_path == os.path.join(here, 'instance')
def test_uninstalled_module_paths(self, modules_tmpdir, purge_module): def test_get_namespace():
app = modules_tmpdir.join('config_module_app.py').write( app = flask.Flask(__name__)
'import os\n' app.config['FOO_OPTION_1'] = 'foo option 1'
'import flask\n' app.config['FOO_OPTION_2'] = 'foo option 2'
'here = os.path.abspath(os.path.dirname(__file__))\n' app.config['BAR_STUFF_1'] = 'bar stuff 1'
'app = flask.Flask(__name__)\n' app.config['BAR_STUFF_2'] = 'bar stuff 2'
) foo_options = app.config.get_namespace('FOO_')
purge_module('config_module_app') assert 2 == len(foo_options)
assert 'foo option 1' == foo_options['option_1']
from config_module_app import app assert 'foo option 2' == foo_options['option_2']
assert app.instance_path == str(modules_tmpdir.join('instance')) bar_options = app.config.get_namespace('BAR_', lowercase=False)
assert 2 == len(bar_options)
def test_uninstalled_package_paths(self, modules_tmpdir, purge_module): assert 'bar stuff 1' == bar_options['STUFF_1']
app = modules_tmpdir.mkdir('config_package_app') assert 'bar stuff 2' == bar_options['STUFF_2']
init = app.join('__init__.py')
init.write(
'import os\n'
'import flask\n'
'here = os.path.abspath(os.path.dirname(__file__))\n'
'app = flask.Flask(__name__)\n'
)
purge_module('config_package_app')
from config_package_app import app
assert app.instance_path == str(modules_tmpdir.join('instance'))
def test_installed_module_paths(self, modules_tmpdir, modules_tmpdir_prefix,
purge_module, site_packages, limit_loader):
site_packages.join('site_app.py').write(
'import flask\n'
'app = flask.Flask(__name__)\n'
)
purge_module('site_app')
from site_app import app
assert app.instance_path == \
modules_tmpdir.join('var').join('site_app-instance')
def test_installed_package_paths(self, limit_loader, modules_tmpdir,
modules_tmpdir_prefix, purge_module,
monkeypatch):
installed_path = modules_tmpdir.mkdir('path')
monkeypatch.syspath_prepend(installed_path)
app = installed_path.mkdir('installed_package')
init = app.join('__init__.py')
init.write('import flask\napp = flask.Flask(__name__)')
purge_module('installed_package')
from installed_package import app
assert app.instance_path == \
modules_tmpdir.join('var').join('installed_package-instance')
def test_prefix_package_paths(self, limit_loader, modules_tmpdir,
modules_tmpdir_prefix, purge_module,
site_packages):
app = site_packages.mkdir('site_package')
init = app.join('__init__.py')
init.write('import flask\napp = flask.Flask(__name__)')
purge_module('site_package')
import site_package
assert site_package.app.instance_path == \
modules_tmpdir.join('var').join('site_package-instance')
def test_egg_installed_paths(self, install_egg, modules_tmpdir,
modules_tmpdir_prefix):
modules_tmpdir.mkdir('site_egg').join('__init__.py').write(
'import flask\n\napp = flask.Flask(__name__)'
)
install_egg('site_egg')
try:
import site_egg
assert site_egg.app.instance_path == \
str(modules_tmpdir.join('var/').join('site_egg-instance'))
finally:
if 'site_egg' in sys.modules:
del sys.modules['site_egg']
if PY2:
def test_meta_path_loader_without_is_package(self):
class Loader(object):
def find_module(self, name):
return self
sys.meta_path.append(Loader())
try:
with pytest.raises(AttributeError):
flask.Flask(__name__)
finally:
sys.meta_path.pop()

132
tests/test_instance_config.py

@ -0,0 +1,132 @@
# -*- coding: utf-8 -*-
"""
tests.test_instance
~~~~~~~~~~~~~~~~~~~
:copyright: (c) 2014 by the Flask Team, see AUTHORS for more details.
:license: BSD, see LICENSE for more details.
"""
import os
import sys
import pytest
import flask
from flask._compat import PY2
def test_explicit_instance_paths(modules_tmpdir):
with pytest.raises(ValueError) as excinfo:
flask.Flask(__name__, instance_path='instance')
assert 'must be absolute' in str(excinfo.value)
app = flask.Flask(__name__, instance_path=str(modules_tmpdir))
assert app.instance_path == str(modules_tmpdir)
def test_main_module_paths(modules_tmpdir, purge_module):
app = modules_tmpdir.join('main_app.py')
app.write('import flask\n\napp = flask.Flask("__main__")')
purge_module('main_app')
from main_app import app
here = os.path.abspath(os.getcwd())
assert app.instance_path == os.path.join(here, 'instance')
def test_uninstalled_module_paths(modules_tmpdir, purge_module):
app = modules_tmpdir.join('config_module_app.py').write(
'import os\n'
'import flask\n'
'here = os.path.abspath(os.path.dirname(__file__))\n'
'app = flask.Flask(__name__)\n'
)
purge_module('config_module_app')
from config_module_app import app
assert app.instance_path == str(modules_tmpdir.join('instance'))
def test_uninstalled_package_paths(modules_tmpdir, purge_module):
app = modules_tmpdir.mkdir('config_package_app')
init = app.join('__init__.py')
init.write(
'import os\n'
'import flask\n'
'here = os.path.abspath(os.path.dirname(__file__))\n'
'app = flask.Flask(__name__)\n'
)
purge_module('config_package_app')
from config_package_app import app
assert app.instance_path == str(modules_tmpdir.join('instance'))
def test_installed_module_paths(modules_tmpdir, modules_tmpdir_prefix,
purge_module, site_packages, limit_loader):
site_packages.join('site_app.py').write(
'import flask\n'
'app = flask.Flask(__name__)\n'
)
purge_module('site_app')
from site_app import app
assert app.instance_path == \
modules_tmpdir.join('var').join('site_app-instance')
def test_installed_package_paths(limit_loader, modules_tmpdir,
modules_tmpdir_prefix, purge_module,
monkeypatch):
installed_path = modules_tmpdir.mkdir('path')
monkeypatch.syspath_prepend(installed_path)
app = installed_path.mkdir('installed_package')
init = app.join('__init__.py')
init.write('import flask\napp = flask.Flask(__name__)')
purge_module('installed_package')
from installed_package import app
assert app.instance_path == \
modules_tmpdir.join('var').join('installed_package-instance')
def test_prefix_package_paths(limit_loader, modules_tmpdir,
modules_tmpdir_prefix, purge_module,
site_packages):
app = site_packages.mkdir('site_package')
init = app.join('__init__.py')
init.write('import flask\napp = flask.Flask(__name__)')
purge_module('site_package')
import site_package
assert site_package.app.instance_path == \
modules_tmpdir.join('var').join('site_package-instance')
def test_egg_installed_paths(install_egg, modules_tmpdir,
modules_tmpdir_prefix):
modules_tmpdir.mkdir('site_egg').join('__init__.py').write(
'import flask\n\napp = flask.Flask(__name__)'
)
install_egg('site_egg')
try:
import site_egg
assert site_egg.app.instance_path == \
str(modules_tmpdir.join('var/').join('site_egg-instance'))
finally:
if 'site_egg' in sys.modules:
del sys.modules['site_egg']
@pytest.mark.skipif(not PY2, reason='This only works under Python 2.')
def test_meta_path_loader_without_is_package():
class Loader(object):
def find_module(self, name):
return self
sys.meta_path.append(Loader())
try:
with pytest.raises(AttributeError):
flask.Flask(__name__)
finally:
sys.meta_path.pop()
Loading…
Cancel
Save