Skip to content

Commit

Permalink
Add unit test ensuring "lazy" args are called exactly once
Browse files Browse the repository at this point in the history
  • Loading branch information
Delgan committed Nov 26, 2024
1 parent ad18146 commit fddcd45
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions tests/test_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,22 @@ def laziness():
assert writer.read() == "10 => 1: 1\n17 => 4: 1\n20 => 7: 2\n"


def test_lazy_function_executed_only_once(writer):
counter = 0

def laziness():
nonlocal counter
counter += 1
return counter

logger.add(writer, level=10, format="{level.name} => {message}")

logger.opt(lazy=True).info("1: {lazy} {lazy}", lazy=laziness)
logger.opt(lazy=True).info("2: {0} {0}", laziness)

assert writer.read() == "INFO => 1: 1 1\nINFO => 2: 2 2\n"


def test_logging_within_lazy_function(writer):
logger.add(writer, level=20, format="{message}")

Expand Down

0 comments on commit fddcd45

Please sign in to comment.