def test_configure_all(self): """ It will add all handlers as store them in `_active_handlers`. """ self.config.file = manufacture.string() self.config.syslog = manufacture.string() self.config.windows_eventlog = manufacture.string() file_handler = object() syslog_handler = object() windows_eventlog_handler = object() self.logger._addFile = self.Mock(return_value=file_handler) self.logger._addSyslog = self.Mock(return_value=syslog_handler) self.logger._addWindowsEventLog = self.Mock( return_value=windows_eventlog_handler) self.logger.configure(configuration=self.config) self.logger._addFile.assert_called_once_with() self.assertEqual( self.logger._active_handlers['file'], file_handler) self.assertEqual( self.logger._active_handlers['syslog'], syslog_handler) if self.os_name == 'nt': self.assertEqual( self.logger._active_handlers['windows_eventlog'], windows_eventlog_handler, )
def test_updateWithNotify_valid(self): """ When notifications don't raise any error, the value is kept. """ section = self._getSection() initial_value = manufacture.string() section.file = initial_value value = manufacture.string() self._callback_called = False def callback(signal): self._callback_called = True # Check that during the notification, the new value # is already set. self.assertEqual(value, section.file) # Check signal values. self.assertEqual(section, signal.source) self.assertEqual(initial_value, signal.initial_value) self.assertEqual(value, signal.current_value) section.subscribe('file', callback) section._updateWithNotify( setter=section._proxy.setStringOrNone, name='file', value=value) self.assertEqual(value, section.file) self.assertTrue(self._callback_called)
def test_setJSON_valid(self): """ Any raw data can we set as JSON. """ content = ( '[section]\n' 'json: ""\n') config = self.makeFileConfiguration(content=content) config.setJSON('section', 'json', 'test') self.assertEqual(u'test', config.getJSON('section', 'json')) config.setJSON('section', 'json', 'multi\nline') self.assertEqual(u'multi\nline', config.getJSON('section', 'json')) config.setJSON('section', 'json', '') self.assertEqual(u'', config.getJSON('section', 'json')) value = manufacture.string() config.setJSON('section', 'json', value) self.assertEqual(value, config.getJSON('section', 'json')) config.setJSON('section', 'json', 2.45) self.assertEqual(2.45, config.getJSON('section', 'json')) config.setJSON('section', 'json', 2) self.assertEqual(2, config.getJSON('section', 'json')) config.setJSON('section', 'json', [1, 2]) self.assertEqual([1, 2], config.getJSON('section', 'json'))
def test_setProperty_ok_decorator(self): """ Value can be set even if is defined using @property decorator. """ new_value = manufacture.string() self.assertEqual( u'prop_wrapped', self.config.prop_section.prop_wrapped) self.config.setProperty('prop_section/prop_wrapped', new_value) self.assertEqual(new_value, self.config.prop_section.prop_wrapped)
def test_configure_subscribers(self): """ Configure will subscribe to changes on the configuration. """ self.logger.configure(configuration=self.config) self.logger._reconfigureHandler = self.Mock() self.config.file = manufacture.string() self.logger._reconfigureHandler.assert_called_once_with( name=u'file', setter=self.logger._addFile) self.logger._reconfigureHandler = self.Mock() self.config.syslog = manufacture.string() self.logger._reconfigureHandler.assert_called_once_with( name=u'syslog', setter=self.logger._addSyslog) self.logger._reconfigureHandler = self.Mock() self.config.windows_eventlog = manufacture.string() self.logger._reconfigureHandler.assert_called_once_with( name=u'windows_eventlog', setter=self.logger._addWindowsEventLog)
def test_updateWithNotify_failed_notification(self): """ When notifications fails, the value is restored and error re-raised. """ section = self._getSection() initial_value = manufacture.string() section.file = initial_value value = manufacture.string() callback = self.Mock(side_effect=[AssertionError('fail')]) section.subscribe('file', callback) with self.assertRaises(AssertionError) as context: section._updateWithNotify( setter=section._proxy.setStringOrNone, name='file', value=value, ) self.assertEqual(initial_value, section.file) self.assertEqual('fail', context.exception.message)
def test_setJSON_invalid(self): """ An error is raised when setting invalid JSON data. """ initial_value = manufacture.string() content = ( '[section]\n' 'json: "%s"\n') % (initial_value) config = self.makeFileConfiguration(content=content) # Check that value is valid, just to be extra safe. self.assertEqual(initial_value, config.getJSON('section', 'json')) with self.assertRaises(UtilsError) as context: config.setJSON('section', 'json', object()) self.assertExceptionID(u'1001', context.exception) self.assertContains( 'JSON data value', context.exception.message) self.assertEqual(initial_value, config.getJSON('section', 'json'))