示例#1
0
    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
        })
示例#2
0
    def test_read_defaults(self):
        handler = SettingsHandler()
        handler.widget_class = SimpleWidget

        defaults = {'a': 5, 'b': {1: 5}}
        with self.override_default_settings(SimpleWidget, defaults):
            handler.read_defaults()

        self.assertEqual(handler.defaults, defaults)
示例#3
0
    def test_read_defaults_ensures_no_schema_only(self):
        handler = SettingsHandler()
        handler.widget_class = SimpleWidget
        handler.provider = SettingProvider(SimpleWidget)

        def migrate_settings(settings, _):
            settings["setting"] = 5
            settings["schema_only_setting"] = True

        with patch.object(SimpleWidget, "migrate_settings", migrate_settings), \
             override_default_settings(SimpleWidget, {"value": 42}):
            handler.read_defaults()
            self.assertEqual(handler.defaults, {'value': 42, 'setting': 5})
示例#4
0
    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)
示例#5
0
    def test_initialize_with_no_provider(self, SettingProvider):
        """:type SettingProvider: unittest.mock.Mock"""
        handler = SettingsHandler()
        handler.provider = Mock(get_provider=Mock(return_value=None))
        handler.widget_class = SimpleWidget
        provider = Mock()
        SettingProvider.return_value = provider
        widget = SimpleWidget()

        # initializing an undeclared provider should display a warning
        with warnings.catch_warnings(record=True) as w:
            handler.initialize(widget)

            self.assertEqual(1, len(w))

        SettingProvider.assert_called_once_with(SimpleWidget)
        provider.initialize.assert_called_once_with(widget, None)
示例#6
0
    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)
示例#7
0
    def test_read_defaults_migrates_settings(self):
        handler = SettingsHandler()
        handler.widget_class = SimpleWidget

        migrate_settings = Mock()
        with patch.object(SimpleWidget, "migrate_settings", migrate_settings):
            # Old settings without version
            settings = {"value": 5}
            with override_default_settings(SimpleWidget, settings):
                handler.read_defaults()
            migrate_settings.assert_called_with(settings, 0)

            migrate_settings.reset()
            # Settings with version
            settings_with_version = dict(settings)
            settings_with_version[VERSION_KEY] = 1
            with override_default_settings(SimpleWidget, settings_with_version):
                handler.read_defaults()
            migrate_settings.assert_called_with(settings, 1)
示例#8
0
    def test_initialize_component(self):
        handler = SettingsHandler()
        handler.defaults = {'default': 42}
        provider = Mock()
        handler.widget_class = SimpleWidget
        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})