: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
chore(query-scan): split pure stub tests from database coverage (#102174)
Paul D'Ambra50d12d570896
diff --git a/posthog/query_scan/test/test_stub.py b/posthog/query_scan/test/test_stub.pyindex 71165416e39d..b37fd6018ed6 100644--- a/posthog/query_scan/test/test_stub.py+++ b/posthog/query_scan/test/test_stub.py@@ -1,5 +1,3 @@-from posthog.test.base import BaseTest-from django.test import SimpleTestCasefrom parameterized import parameterized@@ -8,12 +6,9 @@ from posthog.hogql import astfrom posthog.hogql.context import HogQLContextfrom posthog.hogql.parser import parse_selectfrom posthog.hogql.printer import HogQLPrinter-from posthog.hogql.printer.utils import prepare_ast_for_printing, print_prepared_astfrom posthog.query_scan.stub import stub_in_subqueries-from products.warehouse_sources.backend.facade.models import DataWarehouseCredential, DataWarehouseTable-def print_hogql(node: ast.Expr) -> str:# HogQLPrinter serializes a parsed tree with no database, so the stub can be checked without one.@@ -147,50 +142,3 @@ class TestStubInSubqueries(SimpleTestCase):printed = [print_hogql(subquery) for subquery in result.subqueries]self.assertIn("'first'", printed[0])self.assertIn("'second'", printed[1])---class TestStubPrintsAsClickHouse(BaseTest):- def test_the_stubbed_tree_and_its_subqueries_print_in_the_clickhouse_dialect(self) -> None:- context = HogQLContext(team_id=self.team.pk, enable_select_queries=True)- prepared = prepare_ast_for_printing(- node=parse_select(- "SELECT count() FROM events "- "WHERE distinct_id IN (SELECT distinct_id FROM events WHERE event = 'x') "- "AND timestamp >= (SELECT min(timestamp) FROM events)"- ),- context=context,- dialect="clickhouse",- )-- assert prepared is not None- stub = stub_in_subqueries(prepared)- sql = print_prepared_ast(stub.stubbed, context, dialect="clickhouse")- subquery_sql = print_prepared_ast(stub_in_subqueries(stub.subqueries[0]).stubbed, context, dialect="clickhouse")-- assert "(SELECT" not in sql.replace("\n", " ")- assert len(stub.subqueries) == 2- assert "events" in subquery_sql and "IN (SELECT" not in subquery_sql-- def test_a_warehouse_table_prints_as_an_empty_table_without_its_credentials(self) -> None:- credential = DataWarehouseCredential.objects.create(team=self.team, access_key="key", access_secret="secret")- DataWarehouseTable.objects.create(- team=self.team,- name="customers",- format="Parquet",- url_pattern="http://localhost/customers/*.parquet",- credential=credential,- columns={"id": "String", "plan": "String"},- )- context = HogQLContext(team_id=self.team.pk, enable_select_queries=True)- prepared = prepare_ast_for_printing(- node=parse_select("SELECT count() FROM events AS e JOIN customers AS c ON e.distinct_id = c.id"),- context=context,- dialect="clickhouse",- )- assert prepared is not None-- sql = print_prepared_ast(stub_in_subqueries(prepared).stubbed, context, dialect="clickhouse")-- assert "null(" in sql and "s3(" not in sql- assert "`id` String, `plan` String" in context.values.values()- assert not any(key.endswith("_sensitive") for key in context.values)diff --git a/posthog/query_scan/test/test_stub_printing.py b/posthog/query_scan/test/test_stub_printing.pynew file mode 100644index 000000000000..d99be81aa754--- /dev/null+++ b/posthog/query_scan/test/test_stub_printing.py@@ -0,0 +1,56 @@+from posthog.test.base import BaseTest++from posthog.hogql.context import HogQLContext+from posthog.hogql.parser import parse_select+from posthog.hogql.printer.utils import prepare_ast_for_printing, print_prepared_ast++from posthog.query_scan.stub import stub_in_subqueries++from products.warehouse_sources.backend.facade.models import DataWarehouseCredential, DataWarehouseTable+++class TestStubPrintsAsClickHouse(BaseTest):+ def test_the_stubbed_tree_and_its_subqueries_print_in_the_clickhouse_dialect(self) -> None:+ context = HogQLContext(team_id=self.team.pk, enable_select_queries=True)+ prepared = prepare_ast_for_printing(+ node=parse_select(+ "SELECT count() FROM events "+ "WHERE distinct_id IN (SELECT distinct_id FROM events WHERE event = 'x') "+ "AND timestamp >= (SELECT min(timestamp) FROM events)"+ ),+ context=context,+ dialect="clickhouse",+ )++ assert prepared is not None+ stub = stub_in_subqueries(prepared)+ sql = print_prepared_ast(stub.stubbed, context, dialect="clickhouse")+ subquery_sql = print_prepared_ast(stub_in_subqueries(stub.subqueries[0]).stubbed, context, dialect="clickhouse")++ assert "(SELECT" not in sql.replace("\n", " ")+ assert len(stub.subqueries) == 2+ assert "events" in subquery_sql and "IN (SELECT" not in subquery_sql++ def test_a_warehouse_table_prints_as_an_empty_table_without_its_credentials(self) -> None:+ credential = DataWarehouseCredential.objects.create(team=self.team, access_key="key", access_secret="secret")+ DataWarehouseTable.objects.create(+ team=self.team,+ name="customers",+ format="Parquet",+ url_pattern="http://localhost/customers/*.parquet",+ credential=credential,+ columns={"id": "String", "plan": "String"},+ )+ context = HogQLContext(team_id=self.team.pk, enable_select_queries=True)+ prepared = prepare_ast_for_printing(+ node=parse_select("SELECT count() FROM events AS e JOIN customers AS c ON e.distinct_id = c.id"),+ context=context,+ dialect="clickhouse",+ )+ assert prepared is not None++ sql = print_prepared_ast(stub_in_subqueries(prepared).stubbed, context, dialect="clickhouse")++ assert "null(" in sql and "s3(" not in sql+ assert "`id` String, `plan` String" in context.values.values()+ assert not any(key.endswith("_sensitive") for key in context.values)