File: test_macsypy.py

package info (click to toggle)
macsylib 1.0.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 30,120 kB
  • sloc: python: 10,279; xml: 92; sh: 22; makefile: 12
file content (87 lines) | stat: -rw-r--r-- 4,110 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
#########################################################################
# MacSyLib - Python library to detect macromolecular systems            #
#            in prokaryotes protein dataset using systems modelling     #
#            and similarity search.                                     #
#                                                                       #
# Authors: Sophie Abby, Bertrand Neron                                  #
# Copyright (c) 2014-2025  Institut Pasteur (Paris) and CNRS.           #
# See the COPYRIGHT file for details                                    #
#                                                                       #
# This file is part of MacSyLib package.                                #
#                                                                       #
# MacSyLib is free software: you can redistribute it and/or modify      #
# it under the terms of the GNU General Public License as published by  #
# the Free Software Foundation, either version 3 of the License, or     #
# (at your option) any later version.                                   #
#                                                                       #
# MacSyLib is distributed in the hope that it will be useful,           #
# but WITHOUT ANY WARRANTY; without even the implied warranty of        #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the          #
# GNU General Public License for more details .                         #
#                                                                       #
# You should have received a copy of the GNU General Public License     #
# along with MacSyLib (COPYING).                                        #
# If not, see <https://www.gnu.org/licenses/>.                          #
#########################################################################

import logging
import colorlog
import tempfile

import macsylib
from tests import MacsyTest


class Test(MacsyTest):

    def test_init_logger_default(self):
        handlers = macsylib.init_logger()
        self.assertEqual(len(handlers), 1)
        self.assertTrue(isinstance(handlers[0], logging.StreamHandler))
        logger = logging.getLogger('macsylib')
        self.assertEqual(logger.getEffectiveLevel(),
                         logging.WARNING)

    def test_init_logger_no_out(self):
        handlers = macsylib.init_logger(out=False)
        self.assertEqual(len(handlers), 1)
        self.assertTrue(isinstance(handlers[0], logging.NullHandler))

    def test_init_logger_logfile(self):
        with tempfile.NamedTemporaryFile() as log_file:
            handlers = macsylib.init_logger(log_file=log_file.name)
        try:
            self.assertEqual(len(handlers), 2)
            self.assertTrue(isinstance(handlers[1], logging.FileHandler))
        finally:
            for h in handlers:
                h.close()

    def test_logger_set_level_default(self):
        macsylib.init_logger()
        macsylib.logger_set_level()
        logger = logging.getLogger('macsylib')
        self.assertEqual(logger.getEffectiveLevel(),
                         logging.INFO)

    def test_logger_set_level_error(self):
        macsylib.init_logger()
        macsylib.logger_set_level(level='ERROR')
        logger = logging.getLogger('macsylib')
        self.assertEqual(logger.getEffectiveLevel(),
                         logging.ERROR)

    def test_logger_set_level_bad_level(self):
        macsylib.init_logger()
        with self.assertRaises(ValueError) as ctx:
            macsylib.logger_set_level(level=-1)
        self.assertEqual(str(ctx.exception),
                         'Level must be NOTSET, DEBUG, INFO, WARNING, ERROR, CRITICAL or a positive integer')

    def test_logger_set_level_handlers(self):
        macsylib.init_logger()
        macsylib.logger_set_level(level='DEBUG')
        logger = colorlog.getLogger('macsylib')
        self.assertEqual(logger.handlers[0].formatter.log_colors,
            {'DEBUG': 'cyan', 'INFO': 'green', 'WARNING': 'yellow', 'ERROR': 'red', 'CRITICAL': 'bold_red'}
                         )