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(query-cache): split database-free storage tests (#99658)

Paul D'Ambra9c917b4d7230
diff --git a/posthog/query_cache/test/test_storage.py b/posthog/query_cache/test/test_storage.py
index a1d9cd7de990..0f11e04af9af 100644
--- a/posthog/query_cache/test/test_storage.py
+++ b/posthog/query_cache/test/test_storage.py
@@ -5,7 +5,7 @@ from datetime import UTC, datetime, timedelta
from posthog.test.base import BaseTest
from unittest.mock import MagicMock, patch
-from django.test import SimpleTestCase, override_settings
+from django.test import override_settings
import zstd
from parameterized import parameterized
@@ -15,18 +15,9 @@ from posthog.query_cache import (
get_stale_insights,
storage as qc_storage,
)
-from posthog.query_cache.serialization import QUERY_CACHE_SPLIT_MAGIC, encode_split_cached_response
+from posthog.query_cache.serialization import encode_split_cached_response
from posthog.query_cache.size_tracker import TeamCacheSizeTracker
-from posthog.query_cache.storage import (
- S3_POINTER_MAGIC,
- ZSTD_FRAME_MAGIC,
- S3BlobPointer,
- decode_pointer,
- encode_pointer,
- entry_redis_key,
- is_s3_pointer,
- s3_write_mode,
-)
+from posthog.query_cache.storage import S3_POINTER_MAGIC, ZSTD_FRAME_MAGIC, entry_redis_key
from posthog.storage.object_storage import ObjectStorageError
@@ -56,84 +47,6 @@ class FakeObjectStorage:
self.objects.pop((bucket, key), None)
-class TestS3PointerCodec(SimpleTestCase):
- def test_pointer_round_trips(self):
- pointer = S3BlobPointer(bucket="cache-bucket", key="query_cache/1/some_key")
- assert decode_pointer(encode_pointer(pointer)) == pointer
-
- @parameterized.expand(
- [
- ("legacy_json_blob", b'{"results": []}'),
- ("split_format_blob", QUERY_CACHE_SPLIT_MAGIC + b"\x00rest"),
- ("empty", b""),
- ]
- )
- def test_blob_formats_are_not_pointers(self, _name, data):
- assert not is_s3_pointer(data)
- assert decode_pointer(data) is None
-
- @parameterized.expand(
- [
- ("not_json", S3_POINTER_MAGIC + b"notjson"),
- ("missing_keys", S3_POINTER_MAGIC + b'{"v": 1}'),
- ("unknown_version", S3_POINTER_MAGIC + b'{"v": 2, "b": "bucket", "k": "key"}'),
- ("non_string_key", S3_POINTER_MAGIC + b'{"v": 1, "b": "bucket", "k": [1, 2]}'),
- ]
- )
- def test_corrupt_pointer_decodes_to_none(self, _name, data):
- assert is_s3_pointer(data)
- assert decode_pointer(data) is None
-
-
-@override_settings(OBJECT_STORAGE_ENABLED=True)
-class TestS3WriteMode(SimpleTestCase):
- def test_disabled_object_storage_fails_closed(self):
- # UnavailableStorage swallows writes silently, so routing while storage is off would
- # store pointers to blobs that were never written.
- with (
- override_settings(OBJECT_STORAGE_ENABLED=False),
- patch("posthog.query_cache.storage._organization_id_for_team") as org_mock,
- ):
- assert s3_write_mode(team_id=1) == "off"
- org_mock.assert_not_called()
-
- @parameterized.expand(
- [
- ("on", "on"),
- ("shadow", "shadow"),
- (True, "off"),
- (False, "off"),
- (None, "off"),
- ("unknown-variant", "off"),
- ]
- )
- def test_only_known_variants_activate(self, variant, expected):
- with (
- patch("posthog.query_cache.storage._organization_id_for_team", return_value="0189-org-uuid"),
- patch("posthog.query_cache.storage.get_feature_flag_or_none", return_value=variant),
- ):
- assert s3_write_mode(team_id=1) == expected
-
- def test_unresolvable_organization_fails_closed_without_flag_evaluation(self):
- with (
- patch("posthog.query_cache.storage._organization_id_for_team", return_value=None),
- patch("posthog.query_cache.storage.get_feature_flag_or_none") as flag_mock,
- ):
- assert s3_write_mode(team_id=1) == "off"
- flag_mock.assert_not_called()
-
- def test_flag_evaluation_supplies_group_properties(self):
- # Without group_properties, an id-filtered rollout evaluates inconclusive under
- # only_evaluate_locally and silently reads as off.
- with (
- patch("posthog.query_cache.storage._organization_id_for_team", return_value="0189-org-uuid"),
- patch("posthog.query_cache.storage.get_feature_flag_or_none", return_value="on") as flag_mock,
- ):
- assert s3_write_mode(team_id=1) == "on"
- assert flag_mock.call_args.kwargs["groups"] == {"organization": "0189-org-uuid"}
- assert flag_mock.call_args.kwargs["group_properties"] == {"organization": {"id": "0189-org-uuid"}}
-
-
def _redis_raw(cache_key: str) -> bytes | None:
# Reach the client through the module so the fakeredis monkeypatch in conftest applies.
return qc_storage.query_cache_raw_client().get(entry_redis_key(cache_key))
diff --git a/posthog/query_cache/test/test_storage_unit.py b/posthog/query_cache/test/test_storage_unit.py
new file mode 100644
index 000000000000..086d21d5a35b
--- /dev/null
+++ b/posthog/query_cache/test/test_storage_unit.py
@@ -0,0 +1,93 @@
+from unittest.mock import patch
+
+from django.test import SimpleTestCase, override_settings
+
+from parameterized import parameterized
+
+from posthog.query_cache.serialization import QUERY_CACHE_SPLIT_MAGIC
+from posthog.query_cache.storage import (
+ S3_POINTER_MAGIC,
+ S3BlobPointer,
+ decode_pointer,
+ encode_pointer,
+ is_s3_pointer,
+ s3_write_mode,
+)
+
+
+class TestS3PointerCodec(SimpleTestCase):
+ def test_pointer_round_trips(self):
+ pointer = S3BlobPointer(bucket="cache-bucket", key="query_cache/1/some_key")
+ assert decode_pointer(encode_pointer(pointer)) == pointer
+
+ @parameterized.expand(
+ [
+ ("legacy_json_blob", b'{"results": []}'),
+ ("split_format_blob", QUERY_CACHE_SPLIT_MAGIC + b"\x00rest"),
+ ("empty", b""),
+ ]
+ )
+ def test_blob_formats_are_not_pointers(self, _name, data):
+ assert not is_s3_pointer(data)
+ assert decode_pointer(data) is None
+
+ @parameterized.expand(
+ [
+ ("not_json", S3_POINTER_MAGIC + b"notjson"),
+ ("missing_keys", S3_POINTER_MAGIC + b'{"v": 1}'),
+ ("unknown_version", S3_POINTER_MAGIC + b'{"v": 2, "b": "bucket", "k": "key"}'),
+ ("non_string_key", S3_POINTER_MAGIC + b'{"v": 1, "b": "bucket", "k": [1, 2]}'),
+ ]
+ )
+ def test_corrupt_pointer_decodes_to_none(self, _name, data):
+ assert is_s3_pointer(data)
+ assert decode_pointer(data) is None
+
+
+@override_settings(OBJECT_STORAGE_ENABLED=True)
+class TestS3WriteMode(SimpleTestCase):
+ def test_disabled_object_storage_fails_closed(self):
+ # UnavailableStorage swallows writes silently, so routing while storage is off would
+ # store pointers to blobs that were never written.
+ with (
+ override_settings(OBJECT_STORAGE_ENABLED=False),
+ patch("posthog.query_cache.storage._organization_id_for_team") as org_mock,
+ ):
+ assert s3_write_mode(team_id=1) == "off"
+ org_mock.assert_not_called()
+
+ @parameterized.expand(
+ [
+ ("on", "on"),
+ ("shadow", "shadow"),
+ (True, "off"),
+ (False, "off"),
+ (None, "off"),
+ ("unknown-variant", "off"),
+ ]
+ )
+ def test_only_known_variants_activate(self, variant, expected):
+ with (
+ patch("posthog.query_cache.storage._organization_id_for_team", return_value="0189-org-uuid"),
+ patch("posthog.query_cache.storage.get_feature_flag_or_none", return_value=variant),
+ ):
+ assert s3_write_mode(team_id=1) == expected
+
+ def test_unresolvable_organization_fails_closed_without_flag_evaluation(self):
+ with (
+ patch("posthog.query_cache.storage._organization_id_for_team", return_value=None),
+ patch("posthog.query_cache.storage.get_feature_flag_or_none") as flag_mock,
+ ):
+ assert s3_write_mode(team_id=1) == "off"
+ flag_mock.assert_not_called()
+
+ def test_flag_evaluation_supplies_group_properties(self):
+ # Without group_properties, an id-filtered rollout evaluates inconclusive under
+ # only_evaluate_locally and silently reads as off.
+ with (
+ patch("posthog.query_cache.storage._organization_id_for_team", return_value="0189-org-uuid"),
+ patch("posthog.query_cache.storage.get_feature_flag_or_none", return_value="on") as flag_mock,
+ ):
+ assert s3_write_mode(team_id=1) == "on"
+ assert flag_mock.call_args.kwargs["groups"] == {"organization": "0189-org-uuid"}
+ assert flag_mock.call_args.kwargs["group_properties"] == {"organization": {"id": "0189-org-uuid"}}