11from datetime import datetime
2- from unittest .mock import MagicMock , patch
2+ from unittest .mock import patch
33
44from django .test import TestCase , override_settings
55
@@ -13,11 +13,9 @@ def setUp(self):
1313 self .mock_installation = Installation .get ()
1414 self .installation_id = self .mock_installation .id
1515
16- # Set up PostHog mock
17- self .posthog_patcher = patch ("config.telemetry.Posthog" )
18- self .mock_posthog_class = self .posthog_patcher .start ()
19- self .mock_posthog = MagicMock ()
20- self .mock_posthog_class .return_value = self .mock_posthog
16+ # Set up PostHog mock - now patching the module-level capture function
17+ self .posthog_patcher = patch ("config.telemetry.posthog.capture" )
18+ self .mock_posthog_capture = self .posthog_patcher .start ()
2119
2220 def tearDown (self ):
2321 self .posthog_patcher .stop ()
@@ -34,10 +32,10 @@ def test_record_event_success(self):
3432 result = record_event ("test_event" , {"test_prop" : "value" })
3533
3634 self .assertTrue (result )
37- self .mock_posthog . capture .assert_called_once ()
35+ self .mock_posthog_capture .assert_called_once ()
3836
3937 # Verify the capture call arguments
40- call_args = self .mock_posthog . capture .call_args [1 ]
38+ call_args = self .mock_posthog_capture .call_args [1 ]
4139 self .assertEqual (call_args ["distinct_id" ], str (self .installation_id ))
4240 self .assertEqual (call_args ["event" ], "opencontracts.test_event" )
4341 self .assertEqual (call_args ["properties" ]["package" ], "opencontracts" )
@@ -58,7 +56,7 @@ def test_record_event_telemetry_disabled(self):
5856 result = record_event ("test_event" )
5957
6058 self .assertFalse (result )
61- self .mock_posthog . capture .assert_not_called ()
59+ self .mock_posthog_capture .assert_not_called ()
6260
6361 def test_record_event_installation_inactive (self ):
6462 """Test when installation exists but is inactive"""
@@ -67,11 +65,11 @@ def test_record_event_installation_inactive(self):
6765 result = record_event ("test_event" )
6866
6967 self .assertFalse (result )
70- self .mock_posthog . capture .assert_not_called ()
68+ self .mock_posthog_capture .assert_not_called ()
7169
7270 def test_record_event_posthog_error (self ):
7371 """Test when PostHog client raises an error"""
74- self .mock_posthog . capture .side_effect = Exception ("PostHog Error" )
72+ self .mock_posthog_capture .side_effect = Exception ("PostHog Error" )
7573
7674 with override_settings (MODE = "DEV" , TELEMETRY_ENABLED = True ):
7775 result = record_event ("test_event" )
@@ -85,10 +83,10 @@ def test_record_event_without_properties(self):
8583 result = record_event ("test_event" )
8684
8785 self .assertTrue (result )
88- self .mock_posthog . capture .assert_called_once ()
86+ self .mock_posthog_capture .assert_called_once ()
8987
9088 # Verify only default properties are present
91- properties = self .mock_posthog . capture .call_args [1 ]["properties" ]
89+ properties = self .mock_posthog_capture .call_args [1 ]["properties" ]
9290 self .assertEqual (
9391 set (properties .keys ()), {"package" , "timestamp" , "installation_id" }
9492 )
0 commit comments