class ConfigLoaderTestCase(unittest.TestCase): def setUp(self): self._flask = Mock() self._config_loader = ConfigLoader(self._flask) def tearDown(self): SIMPLE_TEST_CONFIG.reset() PROVIDER_TEST_CONFIG.reset() def test_config_loader_returns_list_of_apps(self): apps, services = self._config_loader.load_from_config(StringIO("")) self.assertEquals(0, len(apps)) def test_config_loader_loads_apps(self): apps, services = self._config_loader.load_from_config(SIMPLE_TEST_CONFIG) self.assertEquals('test', apps[0].instance_name) def test_config_loader_passes_config_dict_to_app(self): apps, services = self._config_loader.load_from_config(SIMPLE_TEST_CONFIG) self.assertEquals('bar', apps[0].config['foo']) def test_config_loader_creates_providers_for_app(self): apps, services = self._config_loader.load_from_config(PROVIDER_TEST_CONFIG) self.assertIsInstance(apps[0].providers[0], TestProvider) def test_config_loader_puts_config_into_provider(self): apps, services = self._config_loader.load_from_config(PROVIDER_TEST_CONFIG) self.assertEquals('baz', apps[0].providers[0].config['bar']) def test_config_loader_raises_config_exception_on_no_such_app(self): self.assertRaises(ConfigError, self._config_loader.load_from_config, BAD_APP_CONFIG) def test_config_loader_raises_config_exception_on_no_such_provider(self): self.assertRaises(ConfigError, self._config_loader.load_from_config, BAD_PROVIDER_CONFIG) def test_module_is_compulsory_field(self): self.assertRaises(ConfigError, self._config_loader.load_from_config, MISSING_APP_CONFIG) def test_provider_is_compulsory_field(self): self.assertRaises(ConfigError, self._config_loader.load_from_config, MISSING_PROVIDER_CONFIG) def test_services_are_passed_to_app(self): apps, services = self._config_loader.load_from_config(SERVICES_CONFIG) self.assertIsInstance(apps[0].services['test'], TestService) self.assertIsInstance(services['test'], TestService) def test_global_config_is_extracted(self): self._config_loader.load_from_config(GLOBAL_CONFIG) self._flask.config.update.assert_called_once_with({'DEBUG': True}) def test_default_cli_service_is_created(self): apps, services = self._config_loader.load_from_config(StringIO("")) self.assertIsInstance(services['cli'], Manager)
def configure_flask_app(): flask_app = Flask('molly') flask_app.url_map.converters['float'] = NegativeFloatConverter with open(os.environ.get('MOLLY_CONFIG', 'conf/default.conf')) as fd: config_loader = ConfigLoader(flask_app) with flask_app.app_context(): apps, services = config_loader.load_from_config(fd) for service in services.values(): for dependent_service_name, dependent_service in services.iteritems(): if hasattr(service, 'init_{0}'.format(dependent_service_name)): getattr(service, 'init_{0}'.format(dependent_service_name))(dependent_service) for app in apps: flask_app.register_blueprint(app.blueprint, url_prefix='/' + app.instance_name) flask_app.register_blueprint(Homepage(apps).blueprint) return flask_app, services['cli']