예제 #1
0
    def test_initialize(self):
        handler = ContextHandler()
        handler.provider = Mock()
        handler.widget_class = SimpleWidget

        # Context settings from data
        widget = SimpleWidget()
        context_settings = [DummyContext()]
        handler.initialize(widget, {'context_settings': context_settings})
        self.assertTrue(hasattr(widget, 'context_settings'))
        self.assertEqual(widget.context_settings, context_settings)

        # Default (global) context settings
        widget = SimpleWidget()
        handler.initialize(widget)
        self.assertTrue(hasattr(widget, 'context_settings'))
        self.assertEqual(widget.context_settings, handler.global_contexts)
예제 #2
0
    def test_read_defaults(self):
        contexts = [DummyContext() for _ in range(3)]

        handler = ContextHandler()
        handler.widget_class = SimpleWidget

        # Old settings without version
        migrate_context = Mock()
        with patch.object(SimpleWidget, "migrate_context", migrate_context):
            handler.read_defaults_file(create_defaults_file(contexts))
        self.assertSequenceEqual(handler.global_contexts, 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.reset_mock()
        with patch.object(SimpleWidget, "migrate_context", migrate_context):
            handler.read_defaults_file(create_defaults_file(contexts))
        self.assertSequenceEqual(handler.global_contexts, contexts)
        migrate_context.assert_has_calls([call(c, c.values[VERSION_KEY]) for c in contexts])
    def test_read_defaults_ensures_no_schema_only(self):
        handler = ContextHandler()
        handler.widget_class = SimpleWidget
        handler.provider = SettingProvider(SimpleWidget)

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

        def migrate_context(context, _):
            context.values["context_setting"] = 5
            context.values["schema_only_context_setting"] = True

        with patch.object(SimpleWidget, "migrate_settings", migrate_settings), \
             patch.object(SimpleWidget, "migrate_context", migrate_context), \
             override_default_settings(SimpleWidget, {"value": 42},
                                       [DummyContext()], handler=ContextHandler):
            handler.read_defaults()
            self.assertEqual(handler.defaults, {'value': 42, 'setting': 5})
            self.assertEqual(handler.global_contexts[0].values, {'context_setting': 5})