33
44import asyncio
55from unittest import TestCase
6- from unittest .mock import MagicMock , patch , PropertyMock
6+ from unittest .mock import MagicMock , PropertyMock , patch
77
88from amazon .opentelemetry .distro .code_correlation import (
9- CODE_FUNCTION_NAME ,
109 CODE_FILE_PATH ,
10+ CODE_FUNCTION_NAME ,
1111 CODE_LINE_NUMBER ,
1212 _add_code_attributes_to_span ,
1313 add_code_attributes_to_span ,
@@ -35,6 +35,7 @@ def setUp(self):
3535
3636 def test_add_code_attributes_to_recording_span (self ):
3737 """Test adding code attributes to a recording span."""
38+
3839 def test_function ():
3940 pass
4041
@@ -68,7 +69,7 @@ def test_add_code_attributes_function_without_code(self):
6869 # Create a mock function without __code__ attribute
6970 mock_func = MagicMock ()
7071 mock_func .__name__ = "mock_function"
71- delattr (mock_func , ' __code__' )
72+ delattr (mock_func , " __code__" )
7273
7374 _add_code_attributes_to_span (self .mock_span , mock_func )
7475
@@ -87,7 +88,7 @@ def test_add_code_attributes_function_without_name(self):
8788 """Test handling of functions without __name__ attribute."""
8889 # Create an object without __name__ attribute
8990 mock_func = MagicMock ()
90- delattr (mock_func , ' __name__' )
91+ delattr (mock_func , " __name__" )
9192 mock_func .__code__ = MagicMock ()
9293 mock_func .__code__ .co_filename = "/test/file.py"
9394 mock_func .__code__ .co_firstlineno = 10
@@ -115,7 +116,7 @@ def test_add_code_attributes_exception_handling(self):
115116 self .mock_span .set_attribute .assert_any_call (CODE_FUNCTION_NAME , "test_func" )
116117 self .mock_span .set_attribute .assert_any_call (CODE_FILE_PATH , "/test/file.py" )
117118
118- @patch (' amazon.opentelemetry.distro.code_correlation.getattr' )
119+ @patch (" amazon.opentelemetry.distro.code_correlation.getattr" )
119120 def test_add_code_attributes_getattr_exception (self , mock_getattr ):
120121 """Test exception handling when getattr fails."""
121122 mock_getattr .side_effect = Exception ("Test exception" )
@@ -204,7 +205,7 @@ def setUp(self):
204205 self .mock_span = MagicMock (spec = Span )
205206 self .mock_span .is_recording .return_value = True
206207
207- @patch (' amazon.opentelemetry.distro.code_correlation.trace.get_current_span' )
208+ @patch (" amazon.opentelemetry.distro.code_correlation.trace.get_current_span" )
208209 def test_decorator_sync_function (self , mock_get_current_span ):
209210 """Test decorator with synchronous function."""
210211 mock_get_current_span .return_value = self .mock_span
@@ -222,7 +223,7 @@ def test_sync_function(arg1, arg2=None):
222223 # Verify span attributes were set
223224 self .mock_span .set_attribute .assert_any_call (CODE_FUNCTION_NAME , "test_sync_function" )
224225
225- @patch (' amazon.opentelemetry.distro.code_correlation.trace.get_current_span' )
226+ @patch (" amazon.opentelemetry.distro.code_correlation.trace.get_current_span" )
226227 def test_decorator_async_function (self , mock_get_current_span ):
227228 """Test decorator with asynchronous function."""
228229 mock_get_current_span .return_value = self .mock_span
@@ -245,7 +246,7 @@ async def test_async_function(arg1, arg2=None):
245246 # Verify span attributes were set
246247 self .mock_span .set_attribute .assert_any_call (CODE_FUNCTION_NAME , "test_async_function" )
247248
248- @patch (' amazon.opentelemetry.distro.code_correlation.trace.get_current_span' )
249+ @patch (" amazon.opentelemetry.distro.code_correlation.trace.get_current_span" )
249250 def test_decorator_no_current_span (self , mock_get_current_span ):
250251 """Test decorator when there's no current span."""
251252 mock_get_current_span .return_value = None
@@ -263,7 +264,7 @@ def test_function():
263264 # Verify no span attributes were set
264265 self .mock_span .set_attribute .assert_not_called ()
265266
266- @patch (' amazon.opentelemetry.distro.code_correlation.trace.get_current_span' )
267+ @patch (" amazon.opentelemetry.distro.code_correlation.trace.get_current_span" )
267268 def test_decorator_exception_handling (self , mock_get_current_span ):
268269 """Test decorator handles exceptions gracefully."""
269270 mock_get_current_span .side_effect = Exception ("Test exception" )
@@ -280,6 +281,7 @@ def test_function():
280281
281282 def test_decorator_preserves_function_metadata (self ):
282283 """Test that decorator preserves original function metadata."""
284+
283285 @add_code_attributes_to_span
284286 def test_function ():
285287 """Test function docstring."""
@@ -291,6 +293,7 @@ def test_function():
291293
292294 def test_async_function_detection (self ):
293295 """Test that async functions are properly detected."""
296+
294297 # Create a regular function
295298 def sync_func ():
296299 pass
@@ -309,7 +312,7 @@ async def async_func():
309312 # Check that async function returns a coroutine function
310313 self .assertTrue (asyncio .iscoroutinefunction (decorated_async ))
311314
312- @patch (' amazon.opentelemetry.distro.code_correlation.trace.get_current_span' )
315+ @patch (" amazon.opentelemetry.distro.code_correlation.trace.get_current_span" )
313316 def test_decorator_with_function_that_raises_exception (self , mock_get_current_span ):
314317 """Test decorator with function that raises exception."""
315318 mock_get_current_span .return_value = self .mock_span
@@ -325,7 +328,7 @@ def test_function():
325328 # Verify span attributes were still set before exception
326329 self .mock_span .set_attribute .assert_any_call (CODE_FUNCTION_NAME , "test_function" )
327330
328- @patch (' amazon.opentelemetry.distro.code_correlation.trace.get_current_span' )
331+ @patch (" amazon.opentelemetry.distro.code_correlation.trace.get_current_span" )
329332 def test_decorator_with_async_function_that_raises_exception (self , mock_get_current_span ):
330333 """Test decorator with async function that raises exception."""
331334 mock_get_current_span .return_value = self .mock_span
@@ -346,8 +349,8 @@ async def test_async_function():
346349 # Verify span attributes were still set before exception
347350 self .mock_span .set_attribute .assert_any_call (CODE_FUNCTION_NAME , "test_async_function" )
348351
349- @patch (' amazon.opentelemetry.distro.code_correlation._add_code_attributes_to_span' )
350- @patch (' amazon.opentelemetry.distro.code_correlation.trace.get_current_span' )
352+ @patch (" amazon.opentelemetry.distro.code_correlation._add_code_attributes_to_span" )
353+ @patch (" amazon.opentelemetry.distro.code_correlation.trace.get_current_span" )
351354 def test_decorator_internal_exception_handling_sync (self , mock_get_current_span , mock_add_attributes ):
352355 """Test that decorator handles internal exceptions gracefully in sync function."""
353356 mock_get_current_span .return_value = self .mock_span
@@ -364,8 +367,8 @@ def test_function():
364367 # Verify the function still works correctly despite internal exception
365368 self .assertEqual (result , "test result" )
366369
367- @patch (' amazon.opentelemetry.distro.code_correlation._add_code_attributes_to_span' )
368- @patch (' amazon.opentelemetry.distro.code_correlation.trace.get_current_span' )
370+ @patch (" amazon.opentelemetry.distro.code_correlation._add_code_attributes_to_span" )
371+ @patch (" amazon.opentelemetry.distro.code_correlation.trace.get_current_span" )
369372 def test_decorator_internal_exception_handling_async (self , mock_get_current_span , mock_add_attributes ):
370373 """Test that decorator handles internal exceptions gracefully in async function."""
371374 mock_get_current_span .return_value = self .mock_span
0 commit comments