Skip to content

Commit 16488d2

Browse files
committed
feat: add tests
1 parent d109be0 commit 16488d2

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
@@ -3543,3 +3543,71 @@ def test_warn_if_not_initialized_category():
35433543

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

0 commit comments

Comments
 (0)