def test_settings_initialize_events(self): from ptah.settings import init_settings self.init_ptah() sm = self.config.registry events = [] def h1(ev): events.append(ev) def h2(ev): events.append(ev) sm.registerHandler(h1, (ptah.events.SettingsInitializing,)) sm.registerHandler(h2, (ptah.events.SettingsInitialized,)) init_settings(self.config, {}) self.assertTrue(isinstance(events[0], ptah.events.SettingsInitializing)) self.assertTrue(isinstance(events[1], ptah.events.SettingsInitialized)) self.assertTrue(events[0].config is self.config) self.assertTrue(events[1].config is self.config)
def test_settings_initialize_load_settings_include(self): from ptah.settings import init_settings path = os.path.join(self.dir, 'settings.cfg') f = open(path, 'wb') f.write(bytes_('[DEFAULT]\ngroup.node1 = value\n\n','ascii')) f.close() node1 = ptah.form.TextField( 'node1', default = 'default1') node2 = ptah.form.IntegerField( 'node2', default = 10) ptah.register_settings('group', node1, node2) self.init_ptah() init_settings(self.config, {'include': path}) group = ptah.get_settings('group', self.request.registry) self.assertEqual(group['node1'], 'value') self.assertEqual(group['node2'], 10)
def test_settings_initialize_only_once(self): from ptah.settings import init_settings self.init_ptah() init_settings(self.config, {}) self.assertRaises(RuntimeError, init_settings, self.config, {})
def test_settings_initialize_events(self): from ptah.settings import init_settings self.init_ptah() sm = self.config.registry events = [] def h1(ev): events.append(ev) def h2(ev): events.append(ev) sm.registerHandler(h1, (ptah.events.SettingsInitializing, )) sm.registerHandler(h2, (ptah.events.SettingsInitialized, )) init_settings(self.config, {}) self.assertTrue(isinstance(events[0], ptah.events.SettingsInitializing)) self.assertTrue(isinstance(events[1], ptah.events.SettingsInitialized)) self.assertTrue(events[0].config is self.config) self.assertTrue(events[1].config is self.config)
def test_settings_initialize_only_once(self): from ptah.settings import init_settings self.init_ptah() init_settings(self.config, {}) self.assertRaises( RuntimeError, init_settings, self.config, {})
def test_settings_initialize_load_preparer(self): from ptah.settings import init_settings node1 = ptah.form.TextField('node', default='default1', preparer=lambda s: s.lower()) ptah.register_settings('group', node1) self.init_ptah() init_settings(self.config, {'group.node': 'Test'}) group = ptah.get_settings('group', self.request.registry) self.assertEqual(group['node'], 'test')
def test_settings_initialize_load_preparer(self): from ptah.settings import init_settings node1 = ptah.form.TextField( 'node', default = 'default1', preparer = lambda s: s.lower()) ptah.register_settings('group', node1) self.init_ptah() init_settings(self.config, {'group.node': 'Test'}) group = ptah.get_settings('group', self.request.registry) self.assertEqual(group['node'], 'test')
def test_settings_initialize_load_partly_defaults(self): from ptah.settings import init_settings node1 = ptah.form.TextField('node1', default='default1') node2 = ptah.form.TextField('node2', default=10) ptah.register_settings('group', node1, node2) self.init_ptah() init_settings(self.config, {'group.node1': 'setting from ini'}) group = ptah.get_settings('group', self.request.registry) self.assertEqual(group['node1'], 'setting from ini') self.assertEqual(group['node2'], 10)
def ptah_initialize(cfg): """ Initialize ptah package.""" from pyramid.exceptions import ConfigurationExecutionError cfg.begin() try: auth_service.set_userid(SUPERUSER_URI) # initialize settings init_settings(cfg, cfg.registry.settings) except config.StopException: config.shutdown() raise finally: cfg.end()
def test_settings_initialize_load_partly_defaults(self): from ptah.settings import init_settings node1 = ptah.form.TextField( 'node1', default = 'default1') node2 = ptah.form.TextField( 'node2', default = 10) ptah.register_settings('group', node1, node2) self.init_ptah() init_settings(self.config, {'group.node1': 'setting from ini'}) group = ptah.get_settings('group', self.request.registry) self.assertEqual(group['node1'], 'setting from ini') self.assertEqual(group['node2'], 10)
def test_settings_initialize_load_settings_include(self): from ptah.settings import init_settings path = os.path.join(self.dir, 'settings.cfg') f = open(path, 'wb') f.write(bytes_('[DEFAULT]\ngroup.node1 = value\n\n', 'ascii')) f.close() node1 = ptah.form.TextField('node1', default='default1') node2 = ptah.form.IntegerField('node2', default=10) ptah.register_settings('group', node1, node2) self.init_ptah() init_settings(self.config, {'include': path}) group = ptah.get_settings('group', self.request.registry) self.assertEqual(group['node1'], 'value') self.assertEqual(group['node2'], 10)
def test_settings_initialize_events_exceptions(self): from ptah.settings import init_settings self.init_ptah() sm = self.config.registry err_tp = TypeError() def h1(ev): raise err_tp sm.registerHandler(h1, (ptah.events.SettingsInitializing,)) err = None try: init_settings(self.config, {}) except Exception as exc: err = exc self.assertIsInstance(err, ptah.config.StopException) self.assertIs(err.exc, err_tp)
def test_settings_initialize_events_exceptions(self): from ptah.settings import init_settings self.init_ptah() sm = self.config.registry err_tp = TypeError() def h1(ev): raise err_tp sm.registerHandler(h1, (ptah.events.SettingsInitializing, )) err = None try: init_settings(self.config, {}) except Exception as exc: err = exc self.assertIsInstance(err, ptah.config.StopException) self.assertIs(err.exc, err_tp)