Skip to content
test_checks.py 43.7 KiB
Newer Older
                            side_effect=is_dir_side_effect):
                self.assertCheckMessages(
                    ["relate_override_templates_dirs.W001",
                     "relate_override_templates_dirs.W001"])
class CheckRelateCustomPageTypesRemovedDeadline(CheckRelateSettingsBase):
    msg_id_prefix = "relate_custom_page_types_removed_deadline"
    VALID_CONF = datetime(2017, 12, 31, 0, 0)
    INVALID_CONF = "2017-12-31 00:00"

    @override_settings(RELATE_CUSTOM_PAGE_TYPES_REMOVED_DEADLINE=None)
    def test_valid_conf_none(self):
        self.assertCheckMessages([])

    @override_settings(RELATE_CUSTOM_PAGE_TYPES_REMOVED_DEADLINE=VALID_CONF)
    def test_valid_conf(self):
        self.assertCheckMessages([])

    @override_settings(RELATE_CUSTOM_PAGE_TYPES_REMOVED_DEADLINE=INVALID_CONF)
    def test_invalid_conf(self):
        self.assertCheckMessages(
            ["relate_custom_page_types_removed_deadline.E001"])


class CheckRelateDisableCodehiliteMarkdownExtensions(CheckRelateSettingsBase):
Dong Zhuang's avatar
Dong Zhuang committed
    msg_id_prefix = "relate_disable_codehilite_markdown_extension"
    VALID_CONF = None
    VALID_CONF_NO_WARNING = True

    WARNING_CONF_NOT_BOOL1 = "some string"
    WARNING_CONF_NOT_BOOL2 = ["markdown.extensions.codehilite"]
    WARNING_CONF_FALSE = False

    @override_settings(RELATE_DISABLE_CODEHILITE_MARKDOWN_EXTENSION=VALID_CONF)
    def test_valid_conf(self):
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertCheckMessages([])

    @override_settings(
        RELATE_DISABLE_CODEHILITE_MARKDOWN_EXTENSION=VALID_CONF_NO_WARNING)
    def test_valid_conf_no_warning(self):
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertCheckMessages([])

    @override_settings(
        RELATE_DISABLE_CODEHILITE_MARKDOWN_EXTENSION=WARNING_CONF_NOT_BOOL1)
    def test_warning_conf_not_bool1(self):
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertCheckMessages(
            ["relate_disable_codehilite_markdown_extension.W001"])

    @override_settings(
        RELATE_DISABLE_CODEHILITE_MARKDOWN_EXTENSION=WARNING_CONF_NOT_BOOL2)
    def test_warning_conf_not_bool2(self):
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertCheckMessages(
            ["relate_disable_codehilite_markdown_extension.W001"])

    @override_settings(
        RELATE_DISABLE_CODEHILITE_MARKDOWN_EXTENSION=WARNING_CONF_FALSE)
    def test_warning_conf_false(self):
Dong Zhuang's avatar
Dong Zhuang committed
        self.assertCheckMessages(
            ["relate_disable_codehilite_markdown_extension.W002"])


class RelateStartupChecksExtraCheckTest(CheckRelateSettingsBase):
    msg_id_prefix = "my_custom_check_msg"

    INSTANCE_WRONG1 = "tests.resource.my_check_func"
    INSTANCE_WRONG2 = {"path": "tests.resource.my_check_func"}

    @override_settings()
    def test_not_configured(self):
        del settings.RELATE_STARTUP_CHECKS_EXTRA
        register_startup_checks_extra()

    @override_settings(RELATE_STARTUP_CHECKS_EXTRA=None)
    def test_none(self):
        register_startup_checks_extra()

    @override_settings(RELATE_STARTUP_CHECKS_EXTRA=INSTANCE_WRONG1)
    def test_instance_error1(self):
        with self.assertRaises(ImproperlyConfigured) as cm:
            register_startup_checks_extra()

        expected_error_msg = (
            "RELATE_STARTUP_CHECKS_EXTRA must be an instance of list or tuple")
        self.assertIn(expected_error_msg, str(cm.exception))

    @override_settings(RELATE_STARTUP_CHECKS_EXTRA=INSTANCE_WRONG2)
    def test_instance_error2(self):
        with self.assertRaises(ImproperlyConfigured) as cm:
            register_startup_checks_extra()

        expected_error_msg = (
            "RELATE_STARTUP_CHECKS_EXTRA must be an instance of list or tuple")
        self.assertIn(expected_error_msg, str(cm.exception))

    @override_settings(RELATE_STARTUP_CHECKS_EXTRA=[])
    def test_empty_list(self):
        register_startup_checks_extra()

    @override_settings(RELATE_STARTUP_CHECKS_EXTRA=[
        "unknown_package.unknown_module.func"])
    def test_not_importable_check_func(self):
        with self.assertRaises(ImproperlyConfigured) as cm:
            register_startup_checks_extra()
        expected_error_msg = ("No module named 'unknown_package'")
        self.assertIn(expected_error_msg, str(cm.exception))

    @override_settings(RELATE_STARTUP_CHECKS_EXTRA=[
        "tests.resource.my_custom_check_func1",
        "tests.resource.my_custom_check_func2"])
    def test_do_check(self):
        from tests.utils import mock
        with mock.patch("relate.checks.register") as mock_register:
            register_startup_checks_extra()
            self.assertEqual(mock_register.call_count, 2)
            stringified_call_args = ". ".join(
                [repr(call) for call in mock_register.call_args_list])
            self.assertIn(
                "function my_custom_check_func1", stringified_call_args)
            self.assertIn(
                "function my_custom_check_func2", stringified_call_args)