Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

chore(tests): speed up task test suites (#70785)

Paul D'Ambrae54d036511de
diff --git a/posthog/tasks/test/test_ai_observability_usage_report.py b/posthog/tasks/test/test_ai_observability_usage_report.py
index e320f58d17a6..e5446416c6b2 100644
--- a/posthog/tasks/test/test_ai_observability_usage_report.py
+++ b/posthog/tasks/test/test_ai_observability_usage_report.py
@@ -40,11 +40,6 @@ class TestAIObservabilityUsageReport(APIBaseTest, ClickhouseTestMixin, Clickhous
# Stop merges to prevent row collapsing
sync_execute("SYSTEM STOP MERGES")
- # Clear existing data
- sync_execute("TRUNCATE TABLE events")
- sync_execute("TRUNCATE TABLE person")
- sync_execute("TRUNCATE TABLE person_distinct_id")
-
def tearDown(self) -> None:
sync_execute("SYSTEM START MERGES")
super().tearDown()
diff --git a/posthog/tasks/test/test_email.py b/posthog/tasks/test/test_email.py
index fd803185ca54..fb7b301d9938 100644
--- a/posthog/tasks/test/test_email.py
+++ b/posthog/tasks/test/test_email.py
@@ -391,7 +391,10 @@ class TestEmail(APIBaseTest, ClickhouseTestMixin):
assert "Set your password" in mocked_email_messages[0].html_body
assert "via" not in mocked_email_messages[0].html_body
- def test_send_wizard_pr_ready_email_uses_customer_io_context(self, MockEmailMessage: MagicMock) -> None:
+ @patch("posthog.tasks.email.ph_scoped_capture")
+ def test_send_wizard_pr_ready_email_uses_customer_io_context(
+ self, _mock_ph_scoped_capture: MagicMock, MockEmailMessage: MagicMock
+ ) -> None:
mocked_email_messages = mock_email_messages(MockEmailMessage)
org, user = create_org_team_and_user("2022-01-02 00:00:00", "[email protected]")
team = user.team
@@ -1683,9 +1686,10 @@ class TestEmail(APIBaseTest, ClickhouseTestMixin):
assert len(mocked_email_messages) == 0
+ @patch("posthog.tasks.email.get_client")
@patch("posthog.tasks.email.check_and_cache_login_device")
def test_login_from_new_device_notification(
- self, mock_check_device: MagicMock, MockEmailMessage: MagicMock
+ self, mock_check_device: MagicMock, _mock_get_client: MagicMock, MockEmailMessage: MagicMock
) -> None:
mocked_email_messages = mock_email_messages(MockEmailMessage)
mock_check_device.return_value = True # Simulate new device
@@ -1708,9 +1712,10 @@ class TestEmail(APIBaseTest, ClickhouseTestMixin):
assert "Canada" in html_body
assert "Google OAuth" in html_body
+ @patch("posthog.tasks.email.get_client")
@patch("posthog.tasks.email.check_and_cache_login_device")
def test_login_from_new_device_notification_email_password(
- self, mock_check_device: MagicMock, MockEmailMessage: MagicMock
+ self, mock_check_device: MagicMock, _mock_get_client: MagicMock, MockEmailMessage: MagicMock
) -> None:
mocked_email_messages = mock_email_messages(MockEmailMessage)
mock_check_device.return_value = True # Simulate new device
diff --git a/posthog/tasks/test/test_usage_report.py b/posthog/tasks/test/test_usage_report.py
index 47c6e1ce6c7f..90eb569acb67 100644
--- a/posthog/tasks/test/test_usage_report.py
+++ b/posthog/tasks/test/test_usage_report.py
@@ -207,11 +207,6 @@ class TestUsageReport(APIBaseTest, ClickhouseTestMixin, ClickhouseDestroyTablesM
# make sure we don't collapse duplicate rows
sync_execute("SYSTEM STOP MERGES")
- # Clear existing data
- sync_execute("TRUNCATE TABLE events")
- sync_execute("TRUNCATE TABLE person")
- sync_execute("TRUNCATE TABLE person_distinct_id")
-
materialize("events", "$exception_values")
self.expected_properties: dict = {}
diff --git a/posthog/tasks/test/utils_email_tests.py b/posthog/tasks/test/utils_email_tests.py
index b4fdcfa09a46..d87cbf5b164c 100644
--- a/posthog/tasks/test/utils_email_tests.py
+++ b/posthog/tasks/test/utils_email_tests.py
@@ -4,7 +4,10 @@ from typing import Any
from unittest.mock import MagicMock
+from django.utils import timezone
+
from posthog.email import EmailMessage
+from posthog.models.messaging import MessagingRecord
from posthog.utils import get_absolute_path
@@ -43,9 +46,11 @@ def mock_email_messages(MockEmailMessage: MagicMock, path: str = "tasks/test/__e
def _email_message_side_effect(**kwargs: Any) -> EmailMessage:
email_message = EmailMessage(**kwargs)
- _original_send = email_message.send
- def _send_side_effect(send_async: bool = True) -> Any:
+ def _send_side_effect(send_async: bool = True) -> None:
+ if not email_message.to:
+ raise ValueError("No recipients provided! Use EmailMessage.add_recipient() first!")
+
# Already appended before send() runs, so subtract 1 to get this message's index.
index_in_test = len(mocked_email_messages) - 1
base = test_name or email_message.campaign_key
@@ -58,7 +63,16 @@ def mock_email_messages(MockEmailMessage: MagicMock, path: str = "tasks/test/__e
print(f"Email rendered to {output_file}") # noqa: T201
- return _original_send()
+ for recipient in email_message.to:
+ sent_at = timezone.now()
+ record, created = MessagingRecord.objects.get_or_create(
+ raw_email=recipient["raw_email"],
+ campaign_key=email_message.campaign_key,
+ defaults={"sent_at": sent_at},
+ )
+ if not created and record.sent_at is None:
+ record.sent_at = sent_at
+ record.save(update_fields=["sent_at"])
email_message.send = MagicMock() # type: ignore
email_message.send.side_effect = _send_side_effect