File: test_logger.py

package info (click to toggle)
python-ws4py 0.6.0%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 676 kB
  • sloc: python: 4,552; makefile: 137; javascript: 96
file content (68 lines) | stat: -rw-r--r-- 2,250 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
# -*- coding: utf-8 -*-
import logging
import logging.handlers as handlers
import os, os.path
import unittest
import sys

from ws4py import configure_logger

def clean_logger():
    logger = logging.getLogger('ws4py')
    for handler in list(logger.handlers):
        try:
            handler.close()
        except KeyError:
            pass
        logger.removeHandler(handler)

class WSTestLogger(unittest.TestCase):
    LOG_FILE = './my.log'

    def tearDown(self):
        clean_logger()
        if os.path.exists(self.LOG_FILE):
            os.remove(self.LOG_FILE)

    def test_named_logger(self):
        logger = configure_logger(stdout=False, filepath=self.LOG_FILE)

        logger = logging.getLogger('ws4py')
        self.assertEqual(logger.getEffectiveLevel(), logging.INFO)

    def test_level(self):
        logger = configure_logger(stdout=True, filepath=self.LOG_FILE,
                                  level=logging.DEBUG)

        self.assertEqual(logger.getEffectiveLevel(), logging.DEBUG)
        for handler in logger.handlers:
            self.assertEqual(handler.level, logging.DEBUG)

    def test_file_logger(self):
        filepath = os.path.abspath(self.LOG_FILE)
        logger = configure_logger(stdout=False, filepath=filepath)
        for handler in logger.handlers:
            if isinstance(handler, handlers.RotatingFileHandler):
                self.assertEqual(handler.baseFilename, filepath)
                self.assertEqual(handler.stream.name, filepath)
                break
        else:
            self.fail("File logger not configured")

    def test_stdout_logger(self):
        logger = configure_logger()
        for handler in logger.handlers:
            if isinstance(handler, logging.StreamHandler) and not\
              isinstance(handler, handlers.RotatingFileHandler):
                self.assertTrue(handler.stream is sys.stdout)
                break
        else:
            self.fail("Stream logger not configured")

if __name__ == '__main__':
    suite = unittest.TestSuite()
    loader = unittest.TestLoader()
    for testcase in [WSTestLogger]:
        tests = loader.loadTestsFromTestCase(testcase)
        suite.addTests(tests)
    unittest.TextTestRunner(verbosity=2).run(suite)