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): avoid database setup in product backend tests (part 1) (#116231)

Paul D'Ambra1efe1f11fd9f
diff --git a/posthog/test/repo_invariants/database_free_test_classes_baseline.txt b/posthog/test/repo_invariants/database_free_test_classes_baseline.txt
index 542196e38782..242f6f53e577 100644
--- a/posthog/test/repo_invariants/database_free_test_classes_baseline.txt
+++ b/posthog/test/repo_invariants/database_free_test_classes_baseline.txt
@@ -21,32 +21,7 @@ posthog/temporal/proxy_service/test/test_cloudflare.py::TestCloudflareAPIErrorIs
posthog/test/activity_logging/test_feature_flag_activity_logging.py::TestChangesBetweenFeatureFlags
posthog/test/test_api_queries_budget.py::TestTokenBucket
posthog/test/test_check_product_migrations.py::TestCheckProductMigrations
-products/access_control/backend/tests/test_property_access_control.py::TestGetDefaultAccessLevel
-products/ai_observability/backend/api/test/test_personal_spend.py::TestPersonalSpendEuRedirect
-products/ai_observability/backend/models/test/test_taggers.py::TestLLMTaggerConfig
-products/alerts/backend/test/test_destinations.py::TestSerializeDeliveries
products/alerts/backend/tests/api/test_alert.py::TestAlertEventProperties
-products/analytics_platform/backend/lazy_computation/tests/test_lazy_computation_executor.py::TestComputationExecutor
-products/analytics_platform/backend/lazy_computation/tests/test_lazy_computation_executor.py::TestComputeQueryHash
-products/analytics_platform/backend/lazy_computation/tests/test_lazy_computation_executor.py::TestMaxWindowDaysCap
-products/analytics_platform/backend/lazy_computation/tests/test_lazy_computation_executor.py::TestParseTtlSchedule
-products/analytics_platform/backend/lazy_computation/tests/test_lazy_computation_executor.py::TestSplitRangesByTtl
-products/analytics_platform/backend/lazy_computation/tests/test_lazy_computation_executor.py::TestWrittenRows
-products/approvals/backend/tests/test_update_feature_flag_action.py::TestUpdateFeatureFlagActionDisplayData
-products/autoresearch/backend/dataset/test_labeling.py::TestBuildInferenceFeaturesSql
-products/autoresearch/backend/dataset/test_labeling.py::TestStripSqlComments
-products/autoresearch/backend/dataset/test_labeling.py::TestSubstituteAnchors
-products/business_knowledge/backend/tests/test_api.py::TestDeriveScopeGlobs
-products/business_knowledge/backend/tests/test_background_refresh.py::TestHostSerializedBatches
-products/business_knowledge/backend/tests/test_crawl_ingest.py::TestDiscoverSSRF
-products/business_knowledge/backend/tests/test_crawl_ingest.py::TestDiscoverSameOrigin
-products/business_knowledge/backend/tests/test_crawl_ingest.py::TestDiscoverSitemap
-products/business_knowledge/backend/tests/test_hybrid_search.py::TestRRFFusion
-products/business_knowledge/backend/tests/test_logic.py::TestChunker
-products/business_knowledge/backend/tests/test_url_ingest.py::TestFetchUrl
-products/business_knowledge/backend/tests/test_url_ingest.py::TestNormalizeUrl
-products/business_knowledge/backend/tests/test_url_ingest.py::TestParseHtml
-products/cdp/backend/templates/avo/test_template_avo.py::TestTemplateMigration
products/conversations/backend/api/tests/test_teams_token_storage.py::TestTrustedTeamsServiceUrl
products/data_modeling/backend/test/test_saved_query_dag_sync.py::TestGetDagId
products/posthog_ai/eval_harness/test/test_acp_log.py::TestSandboxedTraceCaptureParser
diff --git a/products/access_control/backend/tests/test_property_access_control.py b/products/access_control/backend/tests/test_property_access_control.py
index 35d1c2b3b6d7..5c5fdac68bca 100644
--- a/products/access_control/backend/tests/test_property_access_control.py
+++ b/products/access_control/backend/tests/test_property_access_control.py
@@ -1,6 +1,8 @@
from posthog.test.base import BaseTest
from unittest.mock import patch
+from django.test import SimpleTestCase
+
from parameterized import parameterized
from posthog.constants import AvailableFeature
@@ -26,7 +28,7 @@ def _enable_property_access_control(organization: Organization, *, role_based_ac
organization.save()
-class TestGetDefaultAccessLevel(BaseTest):
+class TestGetDefaultAccessLevel(SimpleTestCase):
def test_default_is_read_write(self):
assert get_default_access_level() == PropertyAccessLevel.READ_WRITE
diff --git a/products/ai_observability/backend/api/test/test_personal_spend.py b/products/ai_observability/backend/api/test/test_personal_spend.py
index b8b3ce96189e..8de549ff9ad8 100644
--- a/products/ai_observability/backend/api/test/test_personal_spend.py
+++ b/products/ai_observability/backend/api/test/test_personal_spend.py
@@ -18,7 +18,7 @@ from posthog.test.base import (
from unittest.mock import patch
from django.core.cache import cache
-from django.test import override_settings
+from django.test import SimpleTestCase, override_settings
from django.utils import timezone
import requests
@@ -50,7 +50,7 @@ def _by_product(rows: list[dict], product: str | None) -> dict | None:
return next((r for r in rows if r["product"] == product), None)
-class TestPersonalSpendEuRedirect(APIBaseTest):
+class TestPersonalSpendEuRedirect(SimpleTestCase):
"""Pure unit test of the EU redirect view — does not depend on URL conf for EU."""
def test_eu_redirect_view_preserves_query_string(self) -> None:
diff --git a/products/ai_observability/backend/models/test/test_taggers.py b/products/ai_observability/backend/models/test/test_taggers.py
index bf912fbebd6d..1b92f24b66bc 100644
--- a/products/ai_observability/backend/models/test/test_taggers.py
+++ b/products/ai_observability/backend/models/test/test_taggers.py
@@ -2,6 +2,7 @@ from posthog.test.base import BaseTest
from unittest.mock import patch
from django.core.exceptions import ValidationError
+from django.test import SimpleTestCase
from posthog.models.utils import UUIDTModel
from posthog.temporal.ai_observability.run_tagger import run_hog_tagger
@@ -9,7 +10,7 @@ from posthog.temporal.ai_observability.run_tagger import run_hog_tagger
from products.ai_observability.backend.models.taggers import LLMTaggerConfig, TagDefinition, Tagger
-class TestLLMTaggerConfig(BaseTest):
+class TestLLMTaggerConfig(SimpleTestCase):
def test_valid_config(self):
config = LLMTaggerConfig(
prompt="Which features were discussed?",
diff --git a/products/alerts/backend/test/test_destinations.py b/products/alerts/backend/test/test_destinations.py
index 069e3a9ca03f..900084c2a1a6 100644
--- a/products/alerts/backend/test/test_destinations.py
+++ b/products/alerts/backend/test/test_destinations.py
@@ -831,7 +831,7 @@ class TestRedactUrlsInName:
assert redact_urls_in_name(name) == expected
-class TestSerializeDeliveries(APIBaseTest):
+class TestSerializeDeliveries(SimpleTestCase):
def test_serialize_deliveries_roundtrips_dataclass_fields(self) -> None:
delivery = AlertDelivery(channel="email", target="[email protected]", at="2026-08-11T00:00:00+00:00")
result = serialize_deliveries([delivery])
diff --git a/products/analytics_platform/backend/lazy_computation/tests/test_lazy_computation_executor.py b/products/analytics_platform/backend/lazy_computation/tests/test_lazy_computation_executor.py
index 80e1a77e7bc6..267e42c76568 100644
--- a/products/analytics_platform/backend/lazy_computation/tests/test_lazy_computation_executor.py
+++ b/products/analytics_platform/backend/lazy_computation/tests/test_lazy_computation_executor.py
@@ -112,7 +112,7 @@ class TestComputationJob(BaseTest):
assert row_count == 1
-class TestComputeQueryHash(BaseTest):
+class TestComputeQueryHash(SimpleTestCase):
@parameterized.expand(
[
(
@@ -1507,7 +1507,7 @@ class TestEnsurePrecomputed(ClickhouseTestMixin, BaseTest):
)
-class TestParseTtlSchedule(BaseTest):
+class TestParseTtlSchedule(SimpleTestCase):
def test_int_returns_schedule_with_no_rules(self):
schedule = parse_ttl_schedule(3600)
assert schedule.rules == []
@@ -1665,7 +1665,7 @@ class TestParseTtlSchedule(BaseTest):
assert schedule.get_ttl(django_timezone.now() - timedelta(days=30), jittered=True) == 86400
-class TestSplitRangesByTtl(BaseTest):
+class TestSplitRangesByTtl(SimpleTestCase):
def test_single_range_uniform_ttl(self):
schedule = TtlSchedule.from_seconds(3600)
ranges = [(datetime(2024, 1, 1, tzinfo=UTC), datetime(2024, 1, 4, tzinfo=UTC))]
@@ -1708,7 +1708,7 @@ class TestSplitRangesByTtl(BaseTest):
assert result[1] == (datetime(2024, 1, 5, tzinfo=UTC), datetime(2024, 1, 7, tzinfo=UTC), 3600)
-class TestWrittenRows(BaseTest):
+class TestWrittenRows(SimpleTestCase):
"""`sync_execute` only swaps an INSERT's result for the `written_rows` counter when that
counter is nonzero, so the driver's passthrough result is how a zero-row insert shows up."""
@@ -1720,7 +1720,7 @@ class TestWrittenRows(BaseTest):
assert _written_rows(None) == 0
-class TestComputationExecutor(BaseTest):
+class TestComputationExecutor(SimpleTestCase):
def test_executor_with_custom_settings(self):
default_executor = LazyComputationExecutor()
assert default_executor.wait_timeout_seconds == DEFAULT_WAIT_TIMEOUT_SECONDS
@@ -3716,7 +3716,7 @@ class TestInsertsReportRowsWritten(BaseTest):
assert abs(expires_in - (60 * 60 if expected == 0 else 7 * 24 * 60 * 60)) < 100
-class TestMaxWindowDaysCap(BaseTest):
+class TestMaxWindowDaysCap(SimpleTestCase):
def test_parse_carries_max_window_days(self):
assert parse_ttl_schedule(3600, max_window_days=2).max_window_days == 2
assert parse_ttl_schedule({"default": 3600}, "UTC", max_window_days=1).max_window_days == 1
diff --git a/products/approvals/backend/tests/test_update_feature_flag_action.py b/products/approvals/backend/tests/test_update_feature_flag_action.py
index fe60786485cc..ba5e96a57a91 100644
--- a/products/approvals/backend/tests/test_update_feature_flag_action.py
+++ b/products/approvals/backend/tests/test_update_feature_flag_action.py
@@ -4,6 +4,7 @@ from typing import Any
from posthog.test.base import APIBaseTest
from unittest.mock import MagicMock, patch
+from django.test import SimpleTestCase
from django.utils import timezone
from parameterized import parameterized
@@ -576,7 +577,7 @@ class TestRelatedFieldsInIntent(APIBaseTest):
assert list(applied_flag.analytics_dashboards.all()) == [dashboard]
-class TestUpdateFeatureFlagActionDisplayData(APIBaseTest):
+class TestUpdateFeatureFlagActionDisplayData(SimpleTestCase):
@parameterized.expand(
[
(
diff --git a/products/autoresearch/backend/dataset/test_labeling.py b/products/autoresearch/backend/dataset/test_labeling.py
index 42089e1634f1..f9526464a6f1 100644
--- a/products/autoresearch/backend/dataset/test_labeling.py
+++ b/products/autoresearch/backend/dataset/test_labeling.py
@@ -1,14 +1,7 @@
from datetime import UTC, datetime, timedelta
from typing import Any
-from posthog.test.base import (
- APIBaseTest,
- BaseTest,
- ClickhouseTestMixin,
- _create_event,
- _create_person,
- flush_persons_and_events,
-)
+from posthog.test.base import APIBaseTest, ClickhouseTestMixin, _create_event, _create_person, flush_persons_and_events
from django.test import SimpleTestCase
from django.utils import timezone
@@ -45,7 +38,7 @@ from products.autoresearch.backend.dataset.labeling import (
from products.autoresearch.backend.query import run_hogql_rows
-class TestStripSqlComments(BaseTest):
+class TestStripSqlComments(SimpleTestCase):
@parameterized.expand(
[
("line_comment", "SELECT a -- the count\nFROM t", "SELECT a \nFROM t"),
@@ -74,7 +67,7 @@ class TestStripSqlComments(BaseTest):
self.assertEqual(strip_sql_comments(sql), sql)
-class TestSubstituteAnchors(BaseTest):
+class TestSubstituteAnchors(SimpleTestCase):
def test_placeholder_in_line_comment_does_not_corrupt_substitution(self) -> None:
# A multi-line anchors subquery substituted into a `--` comment would
# escape the comment and break the parse — stripping comments first avoids it.
@@ -102,7 +95,7 @@ class TestSubstituteAnchors(BaseTest):
self.assertEqual(result, "SELECT a.person_id AS distinct_id FROM (SELECT 1) a")
-class TestBuildInferenceFeaturesSql(BaseTest):
+class TestBuildInferenceFeaturesSql(SimpleTestCase):
def test_comment_in_feature_sql_is_stripped_before_substitution(self) -> None:
feature_sql = "SELECT a.person_id AS distinct_id -- {anchors}\nFROM {anchors} a"
sql, _values = build_inference_features_sql(
diff --git a/products/business_knowledge/backend/tests/test_api.py b/products/business_knowledge/backend/tests/test_api.py
index 23a84a5f59ad..e8acf55d524e 100644
--- a/products/business_knowledge/backend/tests/test_api.py
+++ b/products/business_knowledge/backend/tests/test_api.py
@@ -1,8 +1,9 @@
from uuid import UUID
-from posthog.test.base import APIBaseTest, BaseTest
+from posthog.test.base import APIBaseTest
from unittest.mock import patch
+from django.test import SimpleTestCase
from django.utils import timezone
from parameterized import parameterized
@@ -758,7 +759,7 @@ class TestKnowledgeDocumentSearchScopes(APIBaseTest):
assert response.status_code == status.HTTP_403_FORBIDDEN
-class TestDeriveScopeGlobs(BaseTest):
+class TestDeriveScopeGlobs(SimpleTestCase):
@parameterized.expand(
[
("subpath", "https://posthog.com/docs/support", ["/docs/support", "/docs/support/*"]),
diff --git a/products/business_knowledge/backend/tests/test_background_refresh.py b/products/business_knowledge/backend/tests/test_background_refresh.py
index f2a9c0acfeb5..c1b2b13828eb 100644
--- a/products/business_knowledge/backend/tests/test_background_refresh.py
+++ b/products/business_knowledge/backend/tests/test_background_refresh.py
@@ -77,7 +77,7 @@ class TestDueRefreshSelection(BaseTest):
assert len(logic.list_due_refresh_sources(limit=2)) == 2
-class TestHostSerializedBatches(BaseTest):
+class TestHostSerializedBatches(SimpleTestCase):
def test_same_host_never_shares_a_batch(self) -> None:
# 3 sources on host A, 1 on host B, generous concurrency.
due = [
diff --git a/products/business_knowledge/backend/tests/test_crawl_ingest.py b/products/business_knowledge/backend/tests/test_crawl_ingest.py
index 8fef81961e3a..54913ea95f26 100644
--- a/products/business_knowledge/backend/tests/test_crawl_ingest.py
+++ b/products/business_knowledge/backend/tests/test_crawl_ingest.py
@@ -22,7 +22,7 @@ Design notes:
from collections.abc import Mapping
-from posthog.test.base import APIBaseTest, BaseTest
+from posthog.test.base import APIBaseTest
from unittest.mock import patch
from django.test import SimpleTestCase
@@ -54,7 +54,7 @@ def _sitemap_index_xml(children: list[str]) -> str:
)
-class TestDiscoverSitemap(BaseTest):
+class TestDiscoverSitemap(SimpleTestCase):
def test_parses_flat_sitemap(self) -> None:
sitemap = _sitemap_xml(
[
@@ -255,7 +255,7 @@ def _no_robots(url: str, max_bytes: int = 0) -> str:
raise AssertionError(f"unexpected metadata fetch: {url}")
-class TestDiscoverSameOrigin(BaseTest):
+class TestDiscoverSameOrigin(SimpleTestCase):
def test_stays_on_origin_and_respects_depth(self) -> None:
pages = {
"https://ex.com/a": '<a href="/b">b</a><a href="https://other.com/x">external</a>',
@@ -879,7 +879,7 @@ class TestChunkIdIsolation(APIBaseTest):
assert a_chunks.isdisjoint(b_chunks), "chunk UUIDs must not collide across sources"
-class TestDiscoverSSRF(BaseTest):
+class TestDiscoverSSRF(SimpleTestCase):
"""
Regression: `discover._http_get_text` used to auto-follow redirects
(`allow_redirects=True`), so a sitemap or link page could 302 to
diff --git a/products/business_knowledge/backend/tests/test_hybrid_search.py b/products/business_knowledge/backend/tests/test_hybrid_search.py
index c5fff509def0..76e344fe0a60 100644
--- a/products/business_knowledge/backend/tests/test_hybrid_search.py
+++ b/products/business_knowledge/backend/tests/test_hybrid_search.py
@@ -4,6 +4,7 @@ from uuid import UUID
from posthog.test.base import BaseTest
from unittest.mock import MagicMock, patch
+from django.test import SimpleTestCase
from django.utils import timezone
from parameterized import parameterized
@@ -27,7 +28,7 @@ from products.business_knowledge.backend.models import (
)
-class TestRRFFusion(BaseTest):
+class TestRRFFusion(SimpleTestCase):
def test_fts_only_produces_ranked_output(self) -> None:
ids = [uuid.uuid4() for _ in range(3)]
fused = _rrf_fuse(ids, [])
diff --git a/products/business_knowledge/backend/tests/test_logic.py b/products/business_knowledge/backend/tests/test_logic.py
index e8d791728882..48e782945f4d 100644
--- a/products/business_knowledge/backend/tests/test_logic.py
+++ b/products/business_knowledge/backend/tests/test_logic.py
@@ -4,6 +4,7 @@ import datetime
from posthog.test.base import BaseTest
from unittest.mock import patch
+from django.test import SimpleTestCase
from django.utils import timezone
from parameterized import parameterized
@@ -35,7 +36,7 @@ from products.business_knowledge.backend.models import (
)
-class TestChunker(BaseTest):
+class TestChunker(SimpleTestCase):
@parameterized.expand(
[
("empty", "", 0),
diff --git a/products/business_knowledge/backend/tests/test_url_ingest.py b/products/business_knowledge/backend/tests/test_url_ingest.py
index b01681858b4b..947e525fe0e5 100644
--- a/products/business_knowledge/backend/tests/test_url_ingest.py
+++ b/products/business_knowledge/backend/tests/test_url_ingest.py
@@ -14,6 +14,8 @@ Design notes:
from posthog.test.base import APIBaseTest, BaseTest
from unittest.mock import MagicMock, patch
+from django.test import SimpleTestCase
+
from parameterized import parameterized
from rest_framework import status
@@ -61,7 +63,7 @@ def _mock_response(
return response
-class TestNormalizeUrl(BaseTest):
+class TestNormalizeUrl(SimpleTestCase):
def test_strips_userinfo(self) -> None:
assert url_fetch.strip_userinfo("https://user:[email protected]/x") == "https://example.com/x"
@@ -84,7 +86,7 @@ class TestNormalizeUrl(BaseTest):
url_fetch.normalize_url(raw)
-class TestFetchUrl(BaseTest):
+class TestFetchUrl(SimpleTestCase):
@patch(
"products.business_knowledge.backend.url_fetch.validate_url_and_pin_ips",
return_value=PinnedUrlVerdict(allowed=True, reason=None, pinned_ips=set()),
@@ -161,7 +163,7 @@ _BASIC_HTML = b"""<!doctype html>
</body></html>"""
-class TestParseHtml(BaseTest):
+class TestParseHtml(SimpleTestCase):
def test_blocks_are_blank_line_separated_and_chunk_on_paragraphs(self) -> None:
# Regression: txt extraction separated blocks with single newlines, so
# the chunker (which splits on blank lines) saw one mega-paragraph and
diff --git a/products/cdp/backend/templates/avo/test_template_avo.py b/products/cdp/backend/templates/avo/test_template_avo.py
index 06d4bfcba9e2..2526babcd1f6 100644
--- a/products/cdp/backend/templates/avo/test_template_avo.py
+++ b/products/cdp/backend/templates/avo/test_template_avo.py
@@ -1,4 +1,4 @@
-from posthog.test.base import BaseTest
+from django.test import SimpleTestCase
from products.cdp.backend.models.plugin import PluginConfig
from products.cdp.backend.templates.avo.template_avo import (
@@ -143,7 +143,7 @@ class TestTemplateAvo(BaseHogFunctionTemplateTest):
assert [item["propertyName"] for item in res] == expected_result
-class TestTemplateMigration(BaseTest):
+class TestTemplateMigration(SimpleTestCase):
def get_plugin_config(self, config: dict):
_config = {
"avoApiKey": "1234567890",