Skip to content

Commit 67e6280

Browse files
committed
add string matching to assert_log_level
1 parent e6a1efc commit 67e6280

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
### Added
99
- `ModeData.dispersion` and `ModeSolverData.dispersion` are calculated together with the group index.
10+
- String matching feature `contains_str` to `assert_log_level` testing utility.
1011

1112
## [2.5.0] - 2023-12-13
1213

tests/test_package/test_log.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from tidy3d.exceptions import Tidy3dError
1010
from tidy3d.log import DEFAULT_LEVEL, _get_level_int, set_logging_level
1111

12+
from ..utils import assert_log_level, log_capture
13+
1214

1315
def test_log():
1416
td.log.debug("debug test")
@@ -269,3 +271,18 @@ def test_log_suppression():
269271
assert td.log._counts is None
270272

271273
td.config.log_suppression = True
274+
275+
276+
def test_assert_log_level(log_capture):
277+
"""Test features of the assert_log_level"""
278+
279+
# log was captured
280+
td.log.warning("ABC")
281+
assert_log_level(log_capture, "WARNING", contains_str="ABC")
282+
log_capture.clear()
283+
284+
# string was not matched
285+
td.log.warning("ABC")
286+
with pytest.raises(Exception):
287+
assert_log_level(log_capture, "WARNING", contains_str="DEF")
288+
log_capture.clear()

tests/utils.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from pathlib import Path
2-
from typing import Dict, Tuple
2+
from typing import Dict, Tuple, List
33
import pydantic.v1 as pd
44
import trimesh
55

@@ -660,11 +660,25 @@ def log_capture(monkeypatch):
660660
return log_capture.records
661661

662662

663-
def assert_log_level(records, log_level_expected: str):
664-
"""ensure something got logged if log_level is not None.
665-
note: I put this here rather than utils.py because if we import from utils.py,
666-
it will validate the sims there and those get included in log.
663+
def assert_log_level(
664+
records: List[Tuple[int, str]], log_level_expected: str, contains_str: str = None
665+
) -> None:
666+
"""Testing tool: Raises error if a log was not recorded as expected.
667+
668+
Parameters
669+
----------
670+
records : List[Tuple[int, str]]
671+
List of (log_level: int, message: str) holding all of the captured logs.
672+
log_level_expected: str
673+
String version of expected log level (all uppercase).
674+
contains_str : str = None
675+
If specified, errors if not found in the log message.
676+
677+
Returns
678+
-------
679+
None
667680
"""
681+
668682
import sys
669683

670684
sys.stderr.write(str(records) + "\n")
@@ -685,9 +699,15 @@ def assert_log_level(records, log_level_expected: str):
685699
# both expected and got log, check the log levels match
686700
if records and log_level_expected:
687701
for log in records:
688-
log_level = log[0]
702+
log_level, log_message = log
689703
if log_level == log_level_expected_int:
690704
# log level was triggered, exit
705+
706+
if contains_str:
707+
assert (
708+
contains_str in log_message
709+
), f"log message '{log_message}' didnt contain '{contains_str}'."
710+
691711
return
692712
raise Exception
693713

0 commit comments

Comments
 (0)