File: test_log.py

package info (click to toggle)
django-dbbackup 4.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 512 kB
  • sloc: python: 3,767; makefile: 7
file content (130 lines) | stat: -rw-r--r-- 4,483 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import logging
from unittest.mock import patch

import django
from django.core import mail
from django.test import TestCase
from testfixtures import log_capture

from dbbackup import log


class LoggerDefaultTestCase(TestCase):
    @log_capture()
    def test_root(self, captures):
        logger = logging.getLogger()
        logger.debug("a noise")
        logger.info("a message")
        logger.warning("a warning")
        logger.error("an error")
        logger.critical("a critical error")
        captures.check(
            ("root", "DEBUG", "a noise"),
            ("root", "INFO", "a message"),
            ("root", "WARNING", "a warning"),
            ("root", "ERROR", "an error"),
            ("root", "CRITICAL", "a critical error"),
        )

    @log_capture()
    def test_django(self, captures):
        logger = logging.getLogger("django")
        logger.debug("a noise")
        logger.info("a message")
        logger.warning("a warning")
        logger.error("an error")
        logger.critical("a critical error")
        if django.VERSION < (1, 9):
            captures.check(
                ("django", "DEBUG", "a noise"),
                ("django", "INFO", "a message"),
                ("django", "WARNING", "a warning"),
                ("django", "ERROR", "an error"),
                ("django", "CRITICAL", "a critical error"),
            )
        else:
            captures.check(
                ("django", "INFO", "a message"),
                ("django", "WARNING", "a warning"),
                ("django", "ERROR", "an error"),
                ("django", "CRITICAL", "a critical error"),
            )

    @log_capture()
    def test_dbbackup(self, captures):
        logger = logging.getLogger("dbbackup")
        logger.debug("a noise")
        logger.info("a message")
        logger.warning("a warning")
        logger.error("an error")
        logger.critical("a critical error")
        captures.check(
            ("dbbackup", "INFO", "a message"),
            ("dbbackup", "WARNING", "a warning"),
            ("dbbackup", "ERROR", "an error"),
            ("dbbackup", "CRITICAL", "a critical error"),
        )

    @log_capture()
    def test_dbbackup_storage(self, captures):
        logger = logging.getLogger("dbbackup.storage")
        logger.debug("a noise")
        logger.info("a message")
        logger.warning("a warning")
        logger.error("an error")
        logger.critical("a critical error")
        captures.check(
            ("dbbackup.storage", "INFO", "a message"),
            ("dbbackup.storage", "WARNING", "a warning"),
            ("dbbackup.storage", "ERROR", "an error"),
            ("dbbackup.storage", "CRITICAL", "a critical error"),
        )

    @log_capture()
    def test_other_module(self, captures):
        logger = logging.getLogger("os.path")
        logger.debug("a noise")
        logger.info("a message")
        logger.warning("a warning")
        logger.error("an error")
        logger.critical("a critical error")
        captures.check(
            ("os.path", "DEBUG", "a noise"),
            ("os.path", "INFO", "a message"),
            ("os.path", "WARNING", "a warning"),
            ("os.path", "ERROR", "an error"),
            ("os.path", "CRITICAL", "a critical error"),
        )


class DbbackupAdminEmailHandlerTest(TestCase):
    def setUp(self):
        self.logger = logging.getLogger("dbbackup")

    @patch("dbbackup.settings.SEND_EMAIL", True)
    def test_send_mail(self):
        # Test mail error
        msg = "Super msg"
        self.logger.error(msg)
        self.assertEqual(mail.outbox[0].subject, "[dbbackup] ERROR: Super msg")
        # Test don't mail below
        self.logger.warning(msg)
        self.assertEqual(len(mail.outbox), 1)

    @patch("dbbackup.settings.SEND_EMAIL", False)
    def test_send_mail_is_false(self):
        msg = "Super msg"
        self.logger.error(msg)
        self.assertEqual(len(mail.outbox), 0)


class MailEnabledFilterTest(TestCase):
    @patch("dbbackup.settings.SEND_EMAIL", True)
    def test_filter_is_true(self):
        filter_ = log.MailEnabledFilter()
        self.assertTrue(filter_.filter("foo"))

    @patch("dbbackup.settings.SEND_EMAIL", False)
    def test_filter_is_false(self):
        filter_ = log.MailEnabledFilter()
        self.assertFalse(filter_.filter("foo"))