|
| 1 | +""" |
| 2 | +Test that demonstrates the exception capture bug and fix. |
| 3 | +
|
| 4 | +This test uses a real PostHog client with a test consumer to verify that |
| 5 | +exceptions are actually captured to PostHog, not just that 500 responses are returned. |
| 6 | +
|
| 7 | +Bug: Without process_exception(), view exceptions are NOT captured to PostHog. |
| 8 | +Fix: PR #350 adds process_exception() which Django calls to capture exceptions. |
| 9 | +""" |
| 10 | +import os |
| 11 | +import django |
| 12 | + |
| 13 | +# Setup Django before importing anything else |
| 14 | +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "testdjango.settings") |
| 15 | +django.setup() |
| 16 | + |
| 17 | +import pytest |
| 18 | +from httpx import AsyncClient, ASGITransport |
| 19 | +from django.core.asgi import get_asgi_application |
| 20 | +from posthog import Client |
| 21 | + |
| 22 | + |
| 23 | +@pytest.mark.asyncio |
| 24 | +async def test_async_exception_is_captured(): |
| 25 | + """ |
| 26 | + Test that async view exceptions are captured to PostHog. |
| 27 | +
|
| 28 | + With process_exception() (PR #350), exceptions are captured. |
| 29 | + Without it, exceptions are NOT captured even though 500 is returned. |
| 30 | + """ |
| 31 | + from unittest.mock import patch |
| 32 | + |
| 33 | + # Track captured exceptions |
| 34 | + captured = [] |
| 35 | + |
| 36 | + def mock_capture(exception, **kwargs): |
| 37 | + """Mock capture_exception to record calls.""" |
| 38 | + captured.append({ |
| 39 | + 'exception': exception, |
| 40 | + 'type': type(exception).__name__, |
| 41 | + 'message': str(exception) |
| 42 | + }) |
| 43 | + |
| 44 | + with patch('posthog.capture_exception', side_effect=mock_capture): |
| 45 | + app = get_asgi_application() |
| 46 | + async with AsyncClient(transport=ASGITransport(app=app), base_url="http://testserver") as ac: |
| 47 | + response = await ac.get("/test/async-exception") |
| 48 | + |
| 49 | + # Django returns 500 |
| 50 | + assert response.status_code == 500 |
| 51 | + |
| 52 | + # CRITICAL: Verify PostHog captured the exception |
| 53 | + assert len(captured) > 0, f"Exception was NOT captured to PostHog!" |
| 54 | + |
| 55 | + # Verify it's the right exception |
| 56 | + exception_data = captured[0] |
| 57 | + assert exception_data['type'] == 'ValueError' |
| 58 | + assert 'Test exception from Django 5 async view' in exception_data['message'] |
| 59 | + |
| 60 | + print(f"✓ Async exception captured: {len(captured)} exception event(s)") |
| 61 | + print(f" Exception type: {exception_data['type']}") |
| 62 | + print(f" Exception message: {exception_data['message']}") |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.asyncio |
| 66 | +async def test_sync_exception_is_captured(): |
| 67 | + """ |
| 68 | + Test that sync view exceptions are captured to PostHog. |
| 69 | +
|
| 70 | + With process_exception() (PR #350), exceptions are captured. |
| 71 | + Without it, exceptions are NOT captured even though 500 is returned. |
| 72 | + """ |
| 73 | + from unittest.mock import patch |
| 74 | + |
| 75 | + # Track captured exceptions |
| 76 | + captured = [] |
| 77 | + |
| 78 | + def mock_capture(exception, **kwargs): |
| 79 | + """Mock capture_exception to record calls.""" |
| 80 | + captured.append({ |
| 81 | + 'exception': exception, |
| 82 | + 'type': type(exception).__name__, |
| 83 | + 'message': str(exception) |
| 84 | + }) |
| 85 | + |
| 86 | + with patch('posthog.capture_exception', side_effect=mock_capture): |
| 87 | + app = get_asgi_application() |
| 88 | + async with AsyncClient(transport=ASGITransport(app=app), base_url="http://testserver") as ac: |
| 89 | + response = await ac.get("/test/sync-exception") |
| 90 | + |
| 91 | + # Django returns 500 |
| 92 | + assert response.status_code == 500 |
| 93 | + |
| 94 | + # CRITICAL: Verify PostHog captured the exception |
| 95 | + assert len(captured) > 0, f"Exception was NOT captured to PostHog!" |
| 96 | + |
| 97 | + # Verify it's the right exception |
| 98 | + exception_data = captured[0] |
| 99 | + assert exception_data['type'] == 'ValueError' |
| 100 | + assert 'Test exception from Django 5 sync view' in exception_data['message'] |
| 101 | + |
| 102 | + print(f"✓ Sync exception captured: {len(captured)} exception event(s)") |
| 103 | + print(f" Exception type: {exception_data['type']}") |
| 104 | + print(f" Exception message: {exception_data['message']}") |
| 105 | + |
| 106 | + |
| 107 | +if __name__ == "__main__": |
| 108 | + """Run tests directly.""" |
| 109 | + import asyncio |
| 110 | + |
| 111 | + async def run_tests(): |
| 112 | + print("\nTesting exception capture with process_exception() fix...\n") |
| 113 | + |
| 114 | + try: |
| 115 | + await test_async_exception_is_captured() |
| 116 | + except AssertionError as e: |
| 117 | + print(f"✗ Async exception capture failed: {e}") |
| 118 | + except Exception as e: |
| 119 | + print(f"✗ Async test error: {e}") |
| 120 | + |
| 121 | + try: |
| 122 | + await test_sync_exception_is_captured() |
| 123 | + except AssertionError as e: |
| 124 | + print(f"✗ Sync exception capture failed: {e}") |
| 125 | + except Exception as e: |
| 126 | + print(f"✗ Sync test error: {e}") |
| 127 | + |
| 128 | + print("\nDone!\n") |
| 129 | + |
| 130 | + asyncio.run(run_tests()) |
0 commit comments