11"""Tests for decorator functionality."""
22
3- import pytest
43import asyncio
5- from unittest .mock import Mock
64
7- from trusera_sdk import monitor , set_default_client , EventType
5+ import pytest
6+
7+ from trusera_sdk import EventType , monitor , set_default_client
8+
9+ # Check if pytest-asyncio is available
10+ try :
11+ import pytest_asyncio # noqa: F401
12+
13+ PYTEST_ASYNCIO_AVAILABLE = True
14+ except ImportError :
15+ PYTEST_ASYNCIO_AVAILABLE = False
16+
17+ # Skip marker for async tests
18+ skip_if_no_asyncio = pytest .mark .skipif (
19+ not PYTEST_ASYNCIO_AVAILABLE , reason = "pytest-asyncio not installed"
20+ )
821
922
1023def test_monitor_sync_function (trusera_client ):
@@ -28,9 +41,10 @@ def test_function(x: int, y: int) -> int:
2841 assert event .payload ["arguments" ]["y" ] == 3
2942 assert event .payload ["result" ] == 5
3043 assert event .metadata ["success" ] is True
31- assert event .metadata ["duration_ms" ] > 0
44+ assert event .metadata ["duration_ms" ] >= 0 # Can be 0 for very fast functions
3245
3346
47+ @skip_if_no_asyncio
3448@pytest .mark .asyncio
3549async def test_monitor_async_function (trusera_client ):
3650 """Test @monitor decorator on async function."""
@@ -118,7 +132,7 @@ def test_monitor_with_exception(trusera_client):
118132 def failing_function () -> None :
119133 raise ValueError ("Something went wrong" )
120134
121- with pytest .raises (ValueError ):
135+ with pytest .raises (ValueError , match = "Something went wrong" ):
122136 failing_function ()
123137
124138 event = trusera_client ._queue .get ()
@@ -127,6 +141,7 @@ def failing_function() -> None:
127141 assert event .payload ["error" ]["message" ] == "Something went wrong"
128142
129143
144+ @skip_if_no_asyncio
130145@pytest .mark .asyncio
131146async def test_monitor_async_with_exception (trusera_client ):
132147 """Test @monitor on async function with exception."""
@@ -174,6 +189,7 @@ def test_function() -> str:
174189
175190def test_monitor_preserves_function_metadata ():
176191 """Test that @monitor preserves function metadata."""
192+
177193 @monitor ()
178194 def documented_function (x : int ) -> int :
179195 """This is a docstring."""
0 commit comments