• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zopefoundation / zLOG / 4096444155

pending completion
4096444155

push

github

Michael Howitz
Configuring for pure-python

26 of 26 branches covered (100.0%)

170 of 170 relevant lines covered (100.0%)

1.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/zLOG/tests/testzLog.py
1
##############################################################################
2
#
3
# Copyright (c) 2001, 2002 Zope Foundation and Contributors.
4
# All Rights Reserved.
5
#
6
# This software is subject to the provisions of the Zope Public License,
7
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
8
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
9
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
11
# FOR A PARTICULAR PURPOSE.
12
#
13
##############################################################################
14

15
import os
1✔
16
import sys
1✔
17
import tempfile
1✔
18
import time
1✔
19
import unittest
1✔
20
import zLOG
1✔
21
import logging
1✔
22

23
severity_string = {
1✔
24
    -300: 'TRACE',
25
    -200: 'DEBUG',
26
    -100: 'BLATHER',
27
    0: 'INFO',
28
    100: 'PROBLEM',
29
    200: 'ERROR',
30
    300: 'PANIC',
31
}
32

33

34
class EventLogTest(unittest.TestCase):
1✔
35
    """Test zLOG with the default implementation."""
36

37
    def setUp(self):
1✔
38
        self.f, self.path = tempfile.mkstemp()
1✔
39
        self._severity = 0
1✔
40
        self.loghandler = self.setLog()
1✔
41

42
    def tearDown(self):
1✔
43
        self.loghandler.close()
1✔
44
        os.close(self.f)
1✔
45
        os.remove(self.path)
1✔
46

47
    def setLog(self, severity=0):
1✔
48
        logger = logging.getLogger('basic')
1✔
49
        logger.setLevel(logging.DEBUG)
1✔
50
        formatter = logging.Formatter(
1✔
51
            fmt='------\n%(asctime)s %(name)s %(levelname)s %(message)s',
52
            datefmt='%Y-%m-%dT%H:%M:%S')
53
        handler = logging.FileHandler(self.path)
1✔
54
        handler.setFormatter(formatter)
1✔
55
        logger.addHandler(handler)
1✔
56

57
        self._severity = severity
1✔
58
        return handler
1✔
59

60
    def verifyEntry(self, f, time=None, subsys=None, severity=None,
1✔
61
                    summary=None, detail=None, error=None):
62
        # skip to the beginning of next entry
63
        line = f.readline().strip()
1✔
64
        line = f.readline().strip()
1✔
65
        _time, rest = line.split(" ", 1)
1✔
66
        if subsys is not None:  # pragma: no cover
67
            self.assertIn(subsys, rest, "subsystem mismatch")
68
        if severity is not None and severity >= self._severity:
1✔
69
            s = severity_string[severity]
1✔
70
            self.assertIn(s, rest, "severity mismatch")
1✔
71
        if summary is not None:
1✔
72
            self.assertIn(summary, rest, "summary mismatch")
1✔
73
        if detail is not None:
1✔
74
            line = f.readline()
1✔
75
            self.assertNotEqual(line.find(detail), -1, "missing detail")
1✔
76
        if error is not None:
1✔
77
            line = f.readline().strip()
1✔
78
            self.assertTrue(line.startswith('Traceback'),
1✔
79
                            "missing traceback")
80

81
    def getLogFile(self):
1✔
82
        return open(self.path, 'r')
1✔
83

84
    def test_basics(self):
1✔
85
        zLOG.LOG("basic", zLOG.INFO, "summary")
1✔
86
        with self.getLogFile() as f:
1✔
87
            self.verifyEntry(f, subsys="basic", summary="summary")
1✔
88

89
    def test_detail(self):
1✔
90
        zLOG.LOG("basic", zLOG.INFO, "xxx", "this is a detail")
1✔
91
        with self.getLogFile() as f:
1✔
92
            self.verifyEntry(f, subsys="basic", detail="detail")
1✔
93

94
    def test_error(self):
1✔
95
        try:
1✔
96
            1 / 0
1✔
97
        except ZeroDivisionError:
1✔
98
            err = sys.exc_info()
1✔
99

100
        zLOG.LOG("basic", zLOG.INFO, "summary")
1✔
101
        zLOG.LOG("basic", zLOG.ERROR, "raised exception", error=err)
1✔
102
        with self.getLogFile() as f:
1✔
103
            self.verifyEntry(f, subsys="basic", summary="summary")
1✔
104
            self.verifyEntry(f, subsys="basic", severity=zLOG.ERROR, error=err)
1✔
105

106
    def test_reraise_error(self):
1✔
107
        self.setLog()
1✔
108
        try:
1✔
109
            1 / 0
1✔
110
        except ZeroDivisionError:
1✔
111
            err = sys.exc_info()
1✔
112

113
        self.assertRaises(ZeroDivisionError, zLOG.LOG, "basic", zLOG.ERROR,
1✔
114
                          "raised exception", error=err, reraise=True)
115
        with self.getLogFile() as f:
1✔
116
            self.verifyEntry(f, subsys="basic", severity=zLOG.ERROR, error=err)
1✔
117

118
    def test_bbb(self):
1✔
119
        """Test existence of BBB methods that do nothing."""
120
        zLOG.initialize()
1✔
121
        zLOG.set_initializer(lambda: False)  # pragma: no cover
122
        zLOG.register_subsystem('foo')
1✔
123
        self.assertTrue('foo' in zLOG._subsystems)
1✔
124

125
    def test_severity_string(self):
1✔
126
        # severity in mapping
127
        self.assertEqual(zLOG.severity_string(100), 'PROBLEM(100)')
1✔
128
        # severity not in mapping
129
        self.assertEqual(zLOG.severity_string(99), '(99)')
1✔
130

131
    def test_log_time(self):
1✔
132
        self.assertTrue(zLOG.log_time().startswith(
1✔
133
            '%4.4d-%2.2d-%2.2dT' % time.localtime()[:3]))
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc