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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
"""Unit tests for the base module."""
import logging
import importlib
# from os import environ
import pytest
from tiered_debug import set_level, set_stacklevel, lv1, lv2, lv3, lv4, lv5
import tiered_debug._base as base
ENVVAR = "TIERED_DEBUG_LEVEL"
# Fixture to reset global state before each test
@pytest.fixture(autouse=True)
def reset_state():
"""
Reset the global _level and _stacklevel to their default values before each test.
"""
base._level = 1
base._stacklevel = 2
# Tests for set_level
def test_set_level_valid():
"""Test that set_level sets _level correctly for valid inputs."""
set_level(1)
assert base._level == 1
set_level(3)
assert base._level == 3
set_level(5)
assert base._level == 5
def test_set_level_invalid():
"""Test that set_level raises ValueError for invalid inputs."""
with pytest.raises(ValueError, match="Debug level must be between 1 and 5"):
set_level(0)
with pytest.raises(ValueError, match="Debug level must be between 1 and 5"):
set_level(6)
# Tests for set_stacklevel
def test_set_stacklevel_valid():
"""Test that set_stacklevel sets _stacklevel correctly for valid inputs."""
set_stacklevel(1)
assert base._stacklevel == 1
set_stacklevel(2)
assert base._stacklevel == 2
set_stacklevel(3)
assert base._stacklevel == 3
def test_set_stacklevel_invalid():
"""Test that set_stacklevel raises ValueError for invalid inputs."""
with pytest.raises(ValueError, match="stacklevel must be between 1 and 3"):
set_stacklevel(0)
with pytest.raises(ValueError, match="stacklevel must be between 1 and 3"):
set_stacklevel(4)
# Tests for initialization based on environment variable
def test_initial_level_from_env(monkeypatch):
"""Test that _level is set from a valid environment variable."""
monkeypatch.setenv(ENVVAR, "4")
importlib.reload(base)
assert base._level == 4
def test_initial_level_invalid_env(monkeypatch):
"""Test that _level defaults to 1 with an invalid environment variable."""
monkeypatch.setenv(ENVVAR, "invalid")
importlib.reload(base)
assert base._level == 1
def test_initial_level_out_of_range_env(monkeypatch):
"""Test that _level defaults to 1 when environment variable is out of range."""
monkeypatch.setenv(ENVVAR, "6")
importlib.reload(base)
assert base._level == 1
monkeypatch.setenv(ENVVAR, "0")
importlib.reload(base)
assert base._level == 1
def test_initial_level_not_set(monkeypatch):
"""Test that _level defaults to 1 when environment variable is not set."""
monkeypatch.delenv(ENVVAR, raising=False)
importlib.reload(base)
assert base._level == 1
# Tests for logging functions using parametrize
@pytest.mark.parametrize(
"level, should_log",
[
(1, True),
(2, True),
(3, True),
(4, True),
(5, True),
],
)
def test_lv1_logs(caplog, level, should_log):
"""Test that lv1 logs messages when _level >= 1."""
set_level(level)
with caplog.at_level(logging.DEBUG):
lv1("Test message")
assert ("DEBUG1 Test message" in caplog.text) == should_log
@pytest.mark.parametrize(
"level, should_log",
[
(1, False),
(2, True),
(3, True),
(4, True),
(5, True),
],
)
def test_lv2_logs(caplog, level, should_log):
"""Test that lv2 logs messages when _level >= 2."""
set_level(level)
with caplog.at_level(logging.DEBUG):
lv2("Test message")
assert ("DEBUG2 Test message" in caplog.text) == should_log
@pytest.mark.parametrize(
"level, should_log",
[
(1, False),
(2, False),
(3, True),
(4, True),
(5, True),
],
)
def test_lv3_logs(caplog, level, should_log):
"""Test that lv3 logs messages when _level >= 3."""
set_level(level)
with caplog.at_level(logging.DEBUG):
lv3("Test message")
assert ("DEBUG3 Test message" in caplog.text) == should_log
@pytest.mark.parametrize(
"level, should_log",
[
(1, False),
(2, False),
(3, False),
(4, True),
(5, True),
],
)
def test_lv4_logs(caplog, level, should_log):
"""Test that lv4 logs messages when _level >= 4."""
set_level(level)
with caplog.at_level(logging.DEBUG):
lv4("Test message")
assert ("DEBUG4 Test message" in caplog.text) == should_log
@pytest.mark.parametrize(
"level, should_log",
[
(1, False),
(2, False),
(3, False),
(4, False),
(5, True),
],
)
def test_lv5_logs(caplog, level, should_log):
"""Test that lv5 logs messages when _level >= 5."""
set_level(level)
with caplog.at_level(logging.DEBUG):
lv5("Test message")
assert ("DEBUG5 Test message" in caplog.text) == should_log
|