forked from mmcdermott/MEDS_transforms
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconftest.py
More file actions
50 lines (41 loc) · 1.37 KB
/
conftest.py
File metadata and controls
50 lines (41 loc) · 1.37 KB
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
"""Test set-up and fixtures code."""
import json
import tempfile
from contextlib import contextmanager
from datetime import datetime
from functools import partial
from typing import Any
from unittest.mock import MagicMock, patch
import polars as pl
import pytest
from omegaconf import DictConfig
@contextmanager
def print_warnings(caplog: pytest.LogCaptureFixture):
"""Captures all logged warnings within this context block and prints them upon exit.
This is useful in doctests, where you want to show printed outputs for documentation and testing purposes.
"""
n_current_records = len(caplog.records)
with caplog.at_level("WARNING"):
yield
# Print all captured warnings upon exit
for record in caplog.records[n_current_records:]:
print(f"Warning: {record.getMessage()}")
@pytest.fixture(autouse=True)
def __MEDS_transforms_setup_doctest_namespace(
doctest_namespace: dict[str, Any],
caplog: pytest.LogCaptureFixture,
simple_static_MEDS,
) -> None:
doctest_namespace.update(
{
"DictConfig": DictConfig,
"MagicMock": MagicMock,
"patch": patch,
"simple_static_MEDS": simple_static_MEDS,
"print_warnings": partial(print_warnings, caplog),
"json": json,
"pl": pl,
"datetime": datetime,
"tempfile": tempfile,
}
)