File: test_logger.py

package info (click to toggle)
python-proton-vpn-api-core 0.39.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 892 kB
  • sloc: python: 6,582; makefile: 8
file content (123 lines) | stat: -rw-r--r-- 3,488 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
"""
Copyright (c) 2023 Proton AG

This file is part of Proton VPN.

Proton VPN 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.

Proton VPN 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 ProtonVPN.  If not, see <https://www.gnu.org/licenses/>.
"""
import pytest
import tempfile
from proton.vpn import logging
import logging as _logging


@pytest.fixture(scope="module")
def test_logger():
    with tempfile.TemporaryDirectory() as tmpdir:
        logging.config("test-file", logdirpath=tmpdir)
        logger = logging.getLogger(__name__)
        logger.setLevel(_logging.DEBUG)
        yield logger


def log_debug(logger):
    logger.debug("test-message-debug", category="CAT", event="EV")


def log_info(logger):
    logger.info("test-message-info", category="CAT", event="EV")


def log_warning(logger):
    logger.warning("warning", category="CAT", event="EV")


def log_error(logger):
    logger.error("error", category="CAT", event="EV")


def log_critical(logger):
    logger.critical("critical", category="CAT", event="EV")


def log_exception(logger):
    try:
        raise Exception("test")
    except Exception:
        logger.exception("exception", category="CAT", event="EV")


def test_debug_with_custom_properties(caplog, test_logger):
    caplog.clear()
    log_debug(test_logger)
    for record in caplog.records:
        assert record.levelname == "DEBUG"
    assert len(caplog.records) == 1


def test_info_with_custom_properties(caplog, test_logger):
    caplog.clear()
    log_info(test_logger)
    for record in caplog.records:
        assert record.levelname == "INFO"
    assert len(caplog.records) == 1


def test_warning_with_custom_properties(caplog, test_logger):
    caplog.clear()
    log_warning(test_logger)
    for record in caplog.records:
        assert record.levelname == "WARNING"
    assert len(caplog.records) == 1


def test_error_with_custom_properties(caplog, test_logger):
    caplog.clear()
    log_error(test_logger)
    for record in caplog.records:
        assert record.levelname == "ERROR"
    assert len(caplog.records) == 1


def test_critical_with_custom_properties(caplog, test_logger):
    caplog.clear()
    log_critical(test_logger)
    for record in caplog.records:
        assert record.levelname == "CRITICAL"
    assert len(caplog.records) == 1


def test_exception_with_custom_properties(caplog, test_logger):
    caplog.clear()
    log_exception(test_logger)
    for record in caplog.records:
        assert record.levelname == "ERROR"
    assert len(caplog.records) == 1
    assert "exception" in caplog.text


def test_debug_with_only_message_logging_properties(caplog, test_logger):
    caplog.clear()
    test_logger.debug(msg="test-default-debug")
    for record in caplog.records:
        assert record.levelname == "DEBUG"
    assert len(caplog.records) == 1
    assert "test-default-debug" in caplog.text


def test_debug_with_no_logging_properties(caplog, test_logger):
    caplog.clear()
    test_logger.debug(msg="")
    assert len(caplog.records) == 1
    assert "" in caplog.text