예제 #1
0
    def test_extra_installed(self, _dict_plugin_iter_installed):
        plugin1 = plugins.BasePlugin("plugin1", None)
        plugin2 = plugins.BasePlugin("plugin2", None)

        plugins.OfficialPlugin.INSTALLED.append(plugin1)
        plugins.OfficialPlugin.INSTALLED.append(plugin2)
        with patch.object(
            plugins.EntrypointPlugin, "iter_installed", return_value=[plugin1],
        ):
            self.assertEqual(
                [plugin1, plugin2], list(plugins.iter_installed()),
            )
예제 #2
0
    def test_configure(self):
        config = {"ID": "id"}
        defaults = {}

        class plugin1:
            config = {
                "add": {
                    "PARAM1": "value1",
                    "PARAM2": "value2"
                },
                "set": {
                    "PARAM3": "value3"
                },
                "defaults": {
                    "PARAM4": "value4"
                },
            }

        with patch.object(
                plugins.Plugins,
                "iter_enabled",
                return_value=[plugins.BasePlugin("plugin1", plugin1)],
        ):
            tutor_config.load_plugins(config, defaults)

        self.assertEqual(
            {
                "ID": "id",
                "PARAM3": "value3",
                "PLUGIN1_PARAM1": "value1",
                "PLUGIN1_PARAM2": "value2",
            },
            config,
        )
        self.assertEqual({"PLUGIN1_PARAM4": "value4"}, defaults)
예제 #3
0
    def test_configure(self) -> None:
        class plugin1:
            config: Config = {
                "add": {
                    "PARAM1": "value1",
                    "PARAM2": "value2"
                },
                "set": {
                    "PARAM3": "value3"
                },
                "defaults": {
                    "PARAM4": "value4"
                },
            }

        with patch.object(
                plugins.Plugins,
                "iter_enabled",
                return_value=[plugins.BasePlugin("plugin1", plugin1)],
        ):
            base = tutor_config.get_base({})
            defaults = tutor_config.get_defaults({})

        self.assertEqual(base["PARAM3"], "value3")
        self.assertEqual(base["PLUGIN1_PARAM1"], "value1")
        self.assertEqual(base["PLUGIN1_PARAM2"], "value2")
        self.assertEqual(defaults["PLUGIN1_PARAM4"], "value4")
예제 #4
0
 def test_plugin_without_patches(self):
     with patch.object(
             plugins.Plugins,
             "iter_enabled",
             return_value=[plugins.BasePlugin("plugin1", None)],
     ):
         patches = list(plugins.iter_patches({}, "patch1"))
     self.assertEqual([], patches)
예제 #5
0
    def test_patches(self):
        class plugin1:
            patches = {"patch1": "Hello {{ ID }}"}

        with patch.object(
                plugins.Plugins,
                "iter_enabled",
                return_value=[plugins.BasePlugin("plugin1", plugin1)],
        ):
            patches = list(plugins.iter_patches({}, "patch1"))
        self.assertEqual([("plugin1", "Hello {{ ID }}")], patches)
예제 #6
0
    def test_hooks(self):
        class plugin1:
            hooks = {"init": ["myclient"]}

        with patch.object(
                plugins.Plugins,
                "iter_enabled",
                return_value=[plugins.BasePlugin("plugin1", plugin1)],
        ):
            self.assertEqual([("plugin1", ["myclient"])],
                             list(plugins.iter_hooks({}, "init")))
예제 #7
0
 def test_plugins_are_updated_on_config_change(self):
     config = {"PLUGINS": []}
     plugins1 = plugins.Plugins(config)
     self.assertEqual(0, len(list(plugins1.iter_enabled())))
     config["PLUGINS"].append("plugin1")
     with patch.object(
             plugins.Plugins,
             "iter_installed",
             return_value=[plugins.BasePlugin("plugin1", None)],
     ):
         plugins2 = plugins.Plugins(config)
         self.assertEqual(1, len(list(plugins2.iter_enabled())))
예제 #8
0
    def test_configure_set_random_string(self):
        config = {}

        class plugin1:
            config = {"set": {"PARAM1": "{{ 128|random_string }}"}}

        with patch.object(
                plugins.Plugins,
                "iter_enabled",
                return_value=[plugins.BasePlugin("plugin1", plugin1)],
        ):
            tutor_config.load_plugins(config, {})
        self.assertEqual(128, len(config["PARAM1"]))
예제 #9
0
    def test_configure_default_value_with_previous_definition(self) -> None:
        config: Config = {"PARAM1": "value"}

        class plugin1:
            config: Config = {"defaults": {"PARAM2": "{{ PARAM1 }}"}}

        with patch.object(
                plugins.Plugins,
                "iter_enabled",
                return_value=[plugins.BasePlugin("plugin1", plugin1)],
        ):
            tutor_config.update_with_defaults(config)
        self.assertEqual("{{ PARAM1 }}", config["PLUGIN1_PARAM2"])
예제 #10
0
    def test_configure_set_random_string(self) -> None:
        class plugin1:
            config: Config = {"set": {"PARAM1": "{{ 128|random_string }}"}}

        with patch.object(
                plugins.Plugins,
                "iter_enabled",
                return_value=[plugins.BasePlugin("plugin1", plugin1)],
        ):
            config = tutor_config.get_base({})
        tutor_config.render_full(config)

        self.assertEqual(128, len(get_typed(config, "PARAM1", str)))
예제 #11
0
    def test_configure_set_does_not_override(self):
        config = {"ID": "oldid"}

        class plugin1:
            config = {"set": {"ID": "newid"}}

        with patch.object(
                plugins.Plugins,
                "iter_enabled",
                return_value=[plugins.BasePlugin("plugin1", plugin1)],
        ):
            tutor_config.load_plugins(config, {})

        self.assertEqual({"ID": "oldid"}, config)
예제 #12
0
    def test_configure_default_value_with_previous_definition(self):
        config = {}
        defaults = {"PARAM1": "value"}

        class plugin1:
            config = {"defaults": {"PARAM2": "{{ PARAM1 }}"}}

        with unittest.mock.patch.object(
                plugins.Plugins,
                "iter_enabled",
                return_value=[plugins.BasePlugin("plugin1", plugin1)],
        ):
            tutor_config.load_plugins(config, defaults)
        self.assertEqual("{{ PARAM1 }}", defaults["PLUGIN1_PARAM2"])
예제 #13
0
    def test_configure_add_twice(self):
        config = {}

        class plugin1:
            config = {"add": {"PARAM1": "{{ 10|random_string }}"}}

        with patch.object(
                plugins.Plugins,
                "iter_enabled",
                return_value=[plugins.BasePlugin("plugin1", plugin1)],
        ):
            tutor_config.load_plugins(config, {})
        value1 = config["PLUGIN1_PARAM1"]
        with patch.object(
                plugins.Plugins,
                "iter_enabled",
                return_value=[plugins.BasePlugin("plugin1", plugin1)],
        ):
            tutor_config.load_plugins(config, {})
        value2 = config["PLUGIN1_PARAM1"]

        self.assertEqual(10, len(value1))
        self.assertEqual(10, len(value2))
        self.assertEqual(value1, value2)
예제 #14
0
    def test_configure_set_does_not_override(self) -> None:
        config: Config = {"ID1": "oldid"}

        class plugin1:
            config: Config = {"set": {"ID1": "newid", "ID2": "id2"}}

        with patch.object(
                plugins.Plugins,
                "iter_enabled",
                return_value=[plugins.BasePlugin("plugin1", plugin1)],
        ):
            tutor_config.update_with_base(config)

        self.assertEqual("oldid", config["ID1"])
        self.assertEqual("id2", config["ID2"])
예제 #15
0
    def test_config_load_from_plugins(self) -> None:
        config: Config = {}

        class plugin1:
            config: Config = {"add": {"PARAM1": "{{ 10|random_string }}"}}

        with patch.object(
                plugins.Plugins,
                "iter_enabled",
                return_value=[plugins.BasePlugin("plugin1", plugin1)],
        ):
            tutor_config.update_with_base(config)
            tutor_config.update_with_defaults(config)
        tutor_config.render_full(config)
        value1 = get_typed(config, "PLUGIN1_PARAM1", str)

        self.assertEqual(10, len(value1))