class TestConfigNoAppBound: """The application is not bound to a specific app. """ def setup(self): self.env = Environment() def test_no_app_available(self): """Without an application bound, we can't do much.""" assert_raises(RuntimeError, getattr, self.env, 'url') assert_raises(RuntimeError, setattr, self.env, 'debug', True) assert_raises(RuntimeError, self.env.config.get, 'debug') def test_global_defaults(self): """We may set defaults even without an application, however.""" self.env.config.setdefault('FOO', 'BAR') with Flask(__name__).test_request_context(): assert self.env.config['FOO'] == 'BAR' def test_app_specific_defaults(self): """The defaults for url and directory are read from the app object. """ app = Flask(__name__, static_path='/foo') self.env.init_app(app) with app.test_request_context(): assert self.env.url.endswith('static') assert self.env.directory.endswith('foo') # Can be overridden self.env.directory = 'new_media_dir' assert self.env.directory == 'new_media_dir' def test_multiple_separate_apps(self): """Each app has it's own separate configuration. """ app1 = Flask(__name__) self.env.init_app(app1) # With no app yet available... assert_raises(RuntimeError, getattr, self.env, 'url') # ...set a default self.env.config.setdefault('FOO', 'BAR') # When an app is available, the default is used with app1.test_request_context(): assert self.env.config['FOO'] == 'BAR' # If the default is overridden for this application, it # is still valid for other apps. self.env.config['FOO'] = '42' assert self.env.config['FOO'] == '42' app2 = Flask(__name__) with app2.test_request_context(): assert self.env.config['FOO'] == 'BAR' def test_key_error(self): """KeyError is raised if a config value doesn't exist. """ with Flask(__name__).test_request_context(): assert_raises(KeyError, self.env.config.get, 'YADDAYADDA')
class TestConfigNoAppBound: """The application is not bound to a specific app. """ def setup(self): self.env = Environment() def test_no_app_available(self): """Without an application bound, we can't do much.""" assert_raises(RuntimeError, setattr, self.env, 'debug', True) assert_raises(RuntimeError, self.env.config.get, 'debug') def test_global_defaults(self): """We may set defaults even without an application, however.""" self.env.config.setdefault('FOO', 'BAR') with Flask(__name__).test_request_context(): assert self.env.config['FOO'] == 'BAR' def test_multiple_separate_apps(self): """Each app has it's own separate configuration. """ app1 = Flask(__name__) self.env.init_app(app1) # With no app yet available... assert_raises(RuntimeError, getattr, self.env, 'url') # ...set a default self.env.config.setdefault('FOO', 'BAR') # When an app is available, the default is used with app1.test_request_context(): assert self.env.config['FOO'] == 'BAR' # If the default is overridden for this application, it # is still valid for other apps. self.env.config['FOO'] = '42' assert self.env.config['FOO'] == '42' app2 = Flask(__name__) with app2.test_request_context(): assert self.env.config['FOO'] == 'BAR' def test_key_error(self): """KeyError is raised if a config value doesn't exist. """ with Flask(__name__).test_request_context(): assert_raises(KeyError, self.env.config.__getitem__, 'YADDAYADDA') # The get() helper, on the other hand, simply returns None assert self.env.config.get('YADDAYADDA') == None
class TestUrlAndDirectoryWithInitApp(object): """[Regression] Make sure the automatic "directory" and "url" values also work if the application is initialized via "init_app()". """ def setup(self): self.app = Flask(__name__, static_path='/initapp_static') self.env = Environment() self.env.init_app(self.app) def test(self): """Make sure the "url" and "directory" config values are read from the Flask app. """ with self.app.test_request_context(): assert not 'url' in self.env.config assert Bundle('foo').urls(self.env) == ['/initapp_static/foo'] assert not 'directory' in self.env.config root = self.app.root_path assert get_all_bundle_files(Bundle('foo'), self.env) == [root + '/static/foo']