コード例 #1
0
 def test_close_context(self):
     handler = ContextHandler()
     handler.bind(SimpleWidget)
     widget = SimpleWidget()
     widget.storeSpecificSettings = Mock()
     handler.initialize(widget)
     widget.schema_only_setting = 0xD06F00D
     widget.current_context = handler.new_context()
     handler.close_context(widget)
     self.assertEqual(widget.schema_only_setting, 0xD06F00D)
コード例 #2
0
 def test_about_pack_settings_signal(self):
     handler = ContextHandler()
     handler.bind(SimpleWidget)
     widget = SimpleWidget()
     handler.initialize(widget)
     fn = Mock()
     widget.settingsAboutToBePacked.connect(fn)
     handler.pack_data(widget)
     self.assertEqual(1, fn.call_count)
     handler.update_defaults(widget)
     self.assertEqual(2, fn.call_count)
コード例 #3
0
    def test_pack_settings_stores_version(self):
        handler = ContextHandler()
        handler.bind(SimpleWidget)

        widget = SimpleWidget()
        handler.initialize(widget)
        widget.context_setting = [DummyContext() for _ in range(3)]

        settings = handler.pack_data(widget)
        self.assertIn("context_settings", settings)
        for c in settings["context_settings"]:
            self.assertIn(VERSION_KEY, c.values)
コード例 #4
0
    def test_fast_save(self):
        handler = ContextHandler()
        handler.bind(SimpleWidget)

        widget = SimpleWidget()
        handler.initialize(widget)

        context = widget.current_context = handler.new_context()
        handler.fast_save(widget, 'context_setting', 55)
        self.assertEqual(context.values['context_setting'], 55)
        self.assertEqual(handler.known_settings['context_setting'].default,
                         SimpleWidget.context_setting.default)
コード例 #5
0
    def test_write_defaults_stores_version(self):
        handler = ContextHandler()
        handler.bind(SimpleWidget)
        widget = SimpleWidget()
        widget.current_context = None
        widget.context_settings = [DummyContext() for _ in range(3)]
        handler.update_defaults(widget)

        f = BytesIO()
        f.close = lambda: None
        with patch("builtins.open", Mock(return_value=f)):
            handler.write_defaults()
            f.seek(0)
            pickle.load(f)  # settings
            contexts = pickle.load(f)
            for c in contexts:
                self.assertEqual(c.values.get("__version__", 0xBAD), 1)
コード例 #6
0
    def test_migrates_settings_removes_incompatible(self):
        handler = ContextHandler()
        handler.bind(SimpleWidget)

        widget = SimpleWidget()

        contexts = [Context(foo=i) for i in (13, 13, 0, 1, 13, 2, 13)]

        def migrate_context(context, _):
            if context.foo == 13:
                raise IncompatibleContext()

        with patch.object(SimpleWidget, "migrate_context", migrate_context):
            handler.initialize(widget, dict(context_settings=contexts))
            contexts = widget.context_settings
            self.assertEqual(len(contexts), 3)
            self.assertTrue(
                all(context.foo == i for i, context in enumerate(contexts)))
コード例 #7
0
    def test_initialize_migrates_contexts(self):
        handler = ContextHandler()
        handler.bind(SimpleWidget)

        widget = SimpleWidget()

        # Old settings without version
        contexts = [DummyContext() for _ in range(3)]
        migrate_context = Mock()
        with patch.object(SimpleWidget, "migrate_context", migrate_context):
            handler.initialize(widget, dict(context_settings=contexts))
        migrate_context.assert_has_calls([call(c, 0) for c in contexts])

        # Settings with version
        contexts = [DummyContext(version=i) for i in range(1, 4)]
        migrate_context = Mock()
        with patch.object(SimpleWidget, "migrate_context", migrate_context):
            handler.initialize(widget, dict(context_settings=deepcopy(contexts)))
        migrate_context.assert_has_calls([call(c, c.values[VERSION_KEY]) for c in contexts])
コード例 #8
0
    def test_schema_only_settings(self):
        handler = ContextHandler()
        with override_default_settings(SimpleWidget, handler=ContextHandler):
            handler.bind(SimpleWidget)

        # fast_save should not update defaults
        widget = SimpleWidget()
        handler.initialize(widget)
        context = widget.current_context = handler.new_context()
        widget.context_settings.append(context)
        handler.fast_save(widget, 'schema_only_context_setting', 5)
        self.assertEqual(
            handler.known_settings['schema_only_context_setting'].default, None)
        handler.fast_save(widget, 'component.schema_only_context_setting', 5)
        self.assertEqual(
            handler.known_settings['component.schema_only_context_setting'].default, "only")

        # update_defaults should not update defaults
        widget.schema_only_context_setting = 5
        handler.update_defaults(widget)
        self.assertEqual(
            handler.known_settings['schema_only_context_setting'].default, None)
        widget.component.schema_only_setting = 5
        self.assertEqual(
            handler.known_settings['component.schema_only_context_setting'].default, "only")

        # close_context should pack setting
        widget.schema_only_context_setting = 5
        widget.component.context_setting = 5
        widget.component.schema_only_context_setting = 5
        handler.close_context(widget)
        global_values = handler.global_contexts[0].values
        self.assertTrue('context_setting' in global_values)
        self.assertFalse('schema_only_context_setting' in global_values)
        self.assertTrue('context_setting' in global_values["component"])
        self.assertFalse('schema_only_context_setting' in global_values["component"])