Skip to content

Commit 9890848

Browse files
committed
fix tests
1 parent dcceb79 commit 9890848

File tree

2 files changed

+23
-22
lines changed

2 files changed

+23
-22
lines changed

posthog/scopes.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import contextvars
22
from contextlib import contextmanager
33
from typing import Any, Callable, Dict, TypeVar, cast
4-
from posthog import capture_exception
54

65
_context_stack: contextvars.ContextVar[list] = contextvars.ContextVar("posthog_context_stack", default=[{}])
76

@@ -26,13 +25,16 @@ def new_context():
2625
raise ValueError("Something went wrong")
2726
2827
"""
28+
import posthog
29+
2930
current_stack = _context_stack.get()
3031
new_stack = current_stack + [{}]
3132
token = _context_stack.set(new_stack)
33+
3234
try:
3335
yield
3436
except Exception as e:
35-
capture_exception(e)
37+
posthog.capture_exception(e)
3638
raise
3739
finally:
3840
_context_stack.reset(token)
@@ -95,5 +97,4 @@ def wrapper(*args, **kwargs):
9597
with new_context():
9698
return func(*args, **kwargs)
9799

98-
99100
return cast(F, wrapper)

posthog/test/test_scopes.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ def successful_function(x, y):
8484
def test_scoped_decorator_exception(self, mock_capture):
8585
test_exception = ValueError("Test exception")
8686

87+
def check_context_on_capture(exception, **kwargs):
88+
# Assert tags are available when capture_exception is called
89+
current_tags = get_tags()
90+
self.assertEqual(current_tags.get("important_context"), "value")
91+
92+
mock_capture.side_effect = check_context_on_capture
93+
8794
@scoped
8895
def failing_function():
8996
tag("important_context", "value")
@@ -93,15 +100,8 @@ def failing_function():
93100
with self.assertRaises(ValueError):
94101
failing_function()
95102

96-
# Exception should be captured with context
97-
mock_capture.assert_called_once()
98-
args, kwargs = mock_capture.call_args
99-
100-
# Check that the exception was passed
101-
self.assertEqual(args[0], test_exception)
102-
103-
# Check that the context was included in properties
104-
self.assertEqual(kwargs.get("properties", {}).get("important_context"), "value")
103+
# Verify capture_exception was called
104+
mock_capture.assert_called_once_with(test_exception)
105105

106106
# Context should be cleared after function execution
107107
self.assertEqual(get_tags(), {})
@@ -110,6 +110,13 @@ def failing_function():
110110
def test_new_context_exception_handling(self, mock_capture):
111111
test_exception = RuntimeError("Context exception")
112112

113+
def check_context_on_capture(exception, **kwargs):
114+
# Assert inner context tags are available when capture_exception is called
115+
current_tags = get_tags()
116+
self.assertEqual(current_tags.get("inner_context"), "inner_value")
117+
118+
mock_capture.side_effect = check_context_on_capture
119+
113120
# Set up outer context
114121
tag("outer_context", "outer_value")
115122

@@ -120,15 +127,8 @@ def test_new_context_exception_handling(self, mock_capture):
120127
except RuntimeError:
121128
pass # Expected exception
122129

123-
# Exception should be captured with inner context
124-
mock_capture.assert_called_once()
125-
args, kwargs = mock_capture.call_args
126-
127-
# Check that the exception was passed
128-
self.assertEqual(args[0], test_exception)
129-
130-
# Check that the inner context was included in properties
131-
self.assertEqual(kwargs.get("properties", {}).get("inner_context"), "inner_value")
130+
# Verify capture_exception was called
131+
mock_capture.assert_called_once_with(test_exception)
132132

133133
# Outer context should still be intact
134-
self.assertEqual(get_tags()["outer_context"], "outer_value")
134+
self.assertEqual(get_tags()["outer_context"], "outer_value")

0 commit comments

Comments
 (0)