Skip to content

Commit 0a17875

Browse files
committed
feat: add tests
1 parent 309508b commit 0a17875

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

tests/test_logfire.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3545,3 +3545,71 @@ def test_warn_if_not_initialized_category():
35453545

35463546
assert warnings_list[0].category == LogfireNotConfiguredWarning
35473547
assert issubclass(LogfireNotConfiguredWarning, UserWarning)
3548+
3549+
3550+
def test_warn_if_span_inside_generator():
3551+
"""Test that warning is issued when a span is created inside a generator."""
3552+
3553+
def generator():
3554+
with logfire.span('span inside generator'):
3555+
yield
3556+
3557+
with pytest.warns(RuntimeWarning) as warnings_list:
3558+
next(generator())
3559+
3560+
assert warnings_list[0].category is RuntimeWarning
3561+
assert (
3562+
str(warnings_list[0].message)
3563+
== 'Span is inside a generator function. See https://logfire.pydantic.dev/docs/reference/advanced/generators/#move-the-span-outside-the-generator.'
3564+
)
3565+
3566+
3567+
def test_no_warn_if_span_inside_generator():
3568+
"""Test that warning is not issued when a span is created inside a generator with the
3569+
_warn_if_inside_generator option disabled."""
3570+
3571+
def generator():
3572+
with logfire.span('span inside generator', _warn_if_inside_generator=False):
3573+
yield
3574+
3575+
with warnings.catch_warnings(record=True) as warnings_list:
3576+
warnings.simplefilter('always')
3577+
next(generator())
3578+
3579+
assert len(warnings_list) == 0
3580+
3581+
3582+
@pytest.mark.anyio
3583+
async def test_warn_if_span_inside_async_generator():
3584+
"""Test that warning is issued when a span is created inside an async generator."""
3585+
3586+
async def async_generator():
3587+
with logfire.span('span inside async generator'):
3588+
yield
3589+
3590+
with pytest.warns(RuntimeWarning) as warnings_list:
3591+
# we can replace this with global anext() when 3.9 is deprecated
3592+
await async_generator().__anext__()
3593+
3594+
assert warnings_list[0].category is RuntimeWarning
3595+
assert (
3596+
str(warnings_list[0].message)
3597+
== 'Span is inside a generator function. See https://logfire.pydantic.dev/docs/reference/advanced/generators/#move-the-span-outside-the-generator.'
3598+
)
3599+
3600+
3601+
@pytest.mark.anyio
3602+
async def test_no_warn_if_span_inside_async_generator():
3603+
"""Test that warning is not issued when a span is created inside an async generator with the
3604+
_warn_if_inside_generator option disabled."""
3605+
3606+
async def async_generator():
3607+
with logfire.span('span inside async generator', _warn_if_inside_generator=False):
3608+
yield
3609+
3610+
with warnings.catch_warnings(record=True) as warnings_list:
3611+
warnings.simplefilter('always')
3612+
# we can replace this with global anext() when 3.9 is deprecated
3613+
await async_generator().__anext__()
3614+
3615+
assert len(warnings_list) == 0

0 commit comments

Comments
 (0)