def test_initialize_widget(self): handler = SettingsHandler() handler.defaults = {'default': 42, 'setting': 1} handler.provider = provider = Mock() handler.widget_class = SimpleWidget provider.get_provider.return_value = provider widget = SimpleWidget() def reset_provider(): provider.get_provider.return_value = None provider.reset_mock() provider.get_provider.return_value = provider # No data handler.initialize(widget) provider.initialize.assert_called_once_with(widget, {'default': 42, 'setting': 1}) # Dictionary data reset_provider() handler.initialize(widget, {'setting': 5}) provider.initialize.assert_called_once_with(widget, {'default': 42, 'setting': 5}) # Pickled data reset_provider() handler.initialize(widget, pickle.dumps({'setting': 5})) provider.initialize.assert_called_once_with(widget, {'default': 42, 'setting': 5})
def test_initialize_widget(self): handler = SettingsHandler() handler.defaults = {'default': 42, 'setting': 1} handler.provider = provider = Mock() handler.widget_class = SimpleWidget provider.get_provider.return_value = provider widget = SimpleWidget() def reset_provider(): provider.get_provider.return_value = None provider.reset_mock() provider.get_provider.return_value = provider # No data handler.initialize(widget) provider.initialize.assert_called_once_with(widget, { 'default': 42, 'setting': 1 }) # Dictionary data reset_provider() handler.initialize(widget, {'setting': 5}) provider.initialize.assert_called_once_with(widget, { 'default': 42, 'setting': 5 }) # Pickled data reset_provider() handler.initialize(widget, pickle.dumps({'setting': 5})) provider.initialize.assert_called_once_with(widget, { 'default': 42, 'setting': 5 })
def test_initialize_copies_mutables(self): handler = SettingsHandler() handler.bind(SimpleWidget) handler.defaults = dict(list_setting=[]) widget = SimpleWidget() handler.initialize(widget) widget2 = SimpleWidget() handler.initialize(widget2) self.assertNotEqual(id(widget.list_setting), id(widget2.list_setting))
def override_default_settings(self, widget, defaults=None): if defaults is None: defaults = {} h = SettingsHandler() h.widget_class = widget h.defaults = defaults filename = h._get_settings_filename() h.write_defaults() yield if os.path.isfile(filename): os.remove(filename)
def test_write_defaults(self): f, settings_file = mkstemp(suffix='.ini') handler = SettingsHandler() handler.defaults = {'a': 5, 'b': {1: 5}} handler._get_settings_filename = lambda: settings_file handler.write_defaults() with open(settings_file, 'rb') as f: default_settings = pickle.load(f) self.assertEqual(handler.defaults, default_settings) os.remove(settings_file)
def override_default_settings(self, widget, defaults=None): if defaults is None: defaults = {} h = SettingsHandler() h.widget_class = widget h.defaults = defaults filename = h._get_settings_filename() os.makedirs(os.path.dirname(filename), exist_ok=True) with open(filename, "wb") as f: pickle.dump(defaults, f) yield if os.path.isfile(filename): os.remove(filename)
def test_write_defaults(self): fd, settings_file = mkstemp(suffix='.ini') handler = SettingsHandler() handler.widget_class = SimpleWidget handler.defaults = {'a': 5, 'b': {1: 5}} handler._get_settings_filename = lambda: settings_file handler.write_defaults() with open(settings_file, 'rb') as f: default_settings = pickle.load(f) os.close(fd) self.assertEqual(default_settings.pop(VERSION_KEY, -0xBAD), handler.widget_class.settings_version,) self.assertEqual(default_settings, handler.defaults) os.remove(settings_file)
def test_write_defaults(self): fd, settings_file = mkstemp(suffix='.ini') handler = SettingsHandler() handler.widget_class = SimpleWidget handler.defaults = {'a': 5, 'b': {1: 5}} handler._get_settings_filename = lambda: settings_file handler.write_defaults() with open(settings_file, 'rb') as f: default_settings = pickle.load(f) os.close(fd) self.assertEqual( default_settings.pop(VERSION_KEY, -0xBAD), handler.widget_class.settings_version, ) self.assertEqual(default_settings, handler.defaults) os.remove(settings_file)
def test_initialize_component(self): handler = SettingsHandler() handler.defaults = {'default': 42} provider = Mock() handler.provider = Mock(get_provider=Mock(return_value=provider)) widget = SimpleWidget() # No data handler.initialize(widget) provider.initialize.assert_called_once_with(widget, None) # Dictionary data provider.reset_mock() handler.initialize(widget, {'setting': 5}) provider.initialize.assert_called_once_with(widget, {'setting': 5}) # Pickled data provider.reset_mock() handler.initialize(widget, pickle.dumps({'setting': 5})) provider.initialize.assert_called_once_with(widget, {'setting': 5})