File: test_mixins.py

package info (click to toggle)
python-django-health-check 3.20.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 428 kB
  • sloc: python: 1,886; makefile: 6
file content (70 lines) | stat: -rw-r--r-- 2,283 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from unittest.mock import patch

import pytest

from health_check.backends import BaseHealthCheckBackend
from health_check.conf import HEALTH_CHECK
from health_check.mixins import CheckMixin
from health_check.plugins import plugin_dir


class FailPlugin(BaseHealthCheckBackend):
    def check_status(self):
        self.add_error("Oops")


class OkPlugin(BaseHealthCheckBackend):
    def check_status(self):
        pass


class Checker(CheckMixin):
    pass


class TestCheckMixin:
    @pytest.fixture(autouse=True)
    def setup(self):
        plugin_dir.reset()
        plugin_dir.register(FailPlugin)
        plugin_dir.register(OkPlugin)
        yield
        plugin_dir.reset()

    @pytest.mark.parametrize("disable_threading", [(True,), (False,)])
    def test_plugins(self, monkeypatch, disable_threading):
        monkeypatch.setitem(HEALTH_CHECK, "DISABLE_THREADING", disable_threading)

        assert len(Checker().plugins) == 2

    @pytest.mark.parametrize("disable_threading", [(True,), (False,)])
    def test_errors(self, monkeypatch, disable_threading):
        monkeypatch.setitem(HEALTH_CHECK, "DISABLE_THREADING", disable_threading)

        assert len(Checker().errors) == 1

    @pytest.mark.parametrize("disable_threading", [(True,), (False,)])
    def test_run_check(self, monkeypatch, disable_threading):
        monkeypatch.setitem(HEALTH_CHECK, "DISABLE_THREADING", disable_threading)

        assert len(Checker().run_check()) == 1

    def test_run_check_threading_enabled(self, monkeypatch):
        """Ensure threading used when not disabled."""
        # Ensure threading is enabled.
        monkeypatch.setitem(HEALTH_CHECK, "DISABLE_THREADING", False)

        # Ensure ThreadPoolExecutor is used
        with patch("health_check.mixins.ThreadPoolExecutor") as tpe:
            Checker().run_check()
            tpe.assert_called()

    def test_run_check_threading_disabled(self, monkeypatch):
        """Ensure threading not used when disabled."""
        # Ensure threading is disabled.
        monkeypatch.setitem(HEALTH_CHECK, "DISABLE_THREADING", True)

        # Ensure ThreadPoolExecutor is not used
        with patch("health_check.mixins.ThreadPoolExecutor") as tpe:
            Checker().run_check()
            tpe.assert_not_called()