Skip to content

Commit

Permalink
tests : Added tests for custom logging level
Browse files Browse the repository at this point in the history
  • Loading branch information
Félix Rohrlich committed Oct 25, 2024
1 parent a1658ff commit 507fac9
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
6 changes: 3 additions & 3 deletions canaille/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def setup_logging(app):

security_level_name = "SECURITY"
if not hasattr(logging, security_level_name):
addLoggingLevel(security_level_name, logging.INFO + 5)
add_logging_level(security_level_name, logging.INFO + 5)

if conf is None:
log_level = "DEBUG" if app.debug else "INFO"
Expand Down Expand Up @@ -168,7 +168,7 @@ def create_app(
return app


def addLoggingLevel(levelName, levelNum, methodName=None):
def add_logging_level(levelName, levelNum, methodName=None):
"""Comprehensively adds a new logging level to the `logging` module and the
currently configured logging class.
Expand Down Expand Up @@ -198,7 +198,7 @@ def addLoggingLevel(levelName, levelNum, methodName=None):
raise AttributeError(f"{levelName} already defined in logging module")
if hasattr(logging, methodName):
raise AttributeError(f"{methodName} already defined in logging module")
if hasattr(logging.getLoggerClass(), methodName):
if hasattr(logging.getLoggerClass(), methodName): # pragma: no cover
raise AttributeError(f"{methodName} already defined in logger class")

# This method was inspired by the answers to Stack Overflow post
Expand Down
70 changes: 70 additions & 0 deletions tests/app/test_logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import logging

import pytest

from canaille import add_logging_level


@pytest.fixture(autouse=True)
def run_after_tests():
yield

safe_delattr(logging, "test")
safe_delattr(logging, "test1")
safe_delattr(logging, "test2")
safe_delattr(logging, "TEST")
safe_delattr(logging, "TEST1")
safe_delattr(logging, "TEST2")
safe_delattr(logging.getLoggerClass(), "test")
safe_delattr(logging.getLoggerClass(), "test1")
safe_delattr(logging.getLoggerClass(), "test2")


def test_add_logging_level_nominal(testclient, caplog):
add_logging_level("TEST", 10)

testclient.app.logger.test("Add logging level test")

assert (
"canaille",
logging.TEST,
"Add logging level test",
) in caplog.record_tuples


def test_add_logging_level_getlogger(testclient, caplog):
add_logging_level("TEST", 10)

logger = logging.getLogger("test_logger")
logger.test("Add logging level test")

assert (
"test_logger",
logging.TEST,
"Add logging level test",
) in caplog.record_tuples


def test_add_logging_level_name_already_defined():
add_logging_level("TEST", 0, "test1")

with pytest.raises(
AttributeError,
match=r"TEST already defined in logging module",
):
add_logging_level("TEST", 0, "test2")


def test_add_logging_method_name_already_defined():
add_logging_level("TEST1", 0, "test")

with pytest.raises(
AttributeError,
match=r"test already defined in logging module",
):
add_logging_level("TEST2", 0, "test")


def safe_delattr(self, attrname):
if hasattr(self, attrname):
delattr(self, attrname)

0 comments on commit 507fac9

Please sign in to comment.