Skip to content

Commit

Permalink
add test_decide_new_capture_activation
Browse files Browse the repository at this point in the history
  • Loading branch information
xvello committed Oct 26, 2023
1 parent 034efdb commit cb6dec8
Showing 1 changed file with 39 additions and 12 deletions.
51 changes: 39 additions & 12 deletions posthog/api/test/test_decide.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,35 @@
import base64
import json
import random
from unittest.mock import patch
import time
from django.conf import settings

from unittest.mock import patch

import pytest
from django.conf import settings
from django.core.cache import cache
from django.db import connection, connections
from django.test import TransactionTestCase, TestCase
from django.test.client import Client
from rest_framework.test import APIClient
from freezegun import freeze_time
import pytest
from rest_framework import status
from posthog.models.feature_flag.feature_flag import FeatureFlagHashKeyOverride
from posthog.models.group.group import Group

from rest_framework.test import APIClient

from posthog.api.test.test_feature_flag import QueryTimeoutWrapper
from posthog import redis
from posthog.api.decide import label_for_team_id_to_track
from posthog.api.test.test_feature_flag import QueryTimeoutWrapper
from posthog.database_healthcheck import postgres_healthcheck
from posthog.models import FeatureFlag, GroupTypeMapping, Person, PersonalAPIKey, Plugin, PluginConfig, PluginSourceFile
from posthog.models.cohort.cohort import Cohort
from posthog.models.feature_flag.feature_flag import FeatureFlagHashKeyOverride
from posthog.models.group.group import Group
from posthog.models.organization import Organization, OrganizationMembership
from posthog.models.person import PersonDistinctId
from posthog.models.personal_api_key import hash_key_value
from posthog.models.plugin import sync_team_inject_web_apps
from posthog.models.team.team import Team
from posthog.models.person import PersonDistinctId
from posthog.models.user import User
from posthog.models.utils import generate_random_token_personal
from posthog.test.base import BaseTest, QueryMatchingTest, snapshot_postgres_queries, snapshot_postgres_queries_context
from posthog.database_healthcheck import postgres_healthcheck
from posthog import redis


@patch("posthog.models.feature_flag.flag_matching.postgres_healthcheck.is_connected", return_value=True)
Expand Down Expand Up @@ -2320,6 +2318,35 @@ def test_decide_analytics_fire_for_survey_targeting_flags(self, *args):
# check that single increment made it to redis
self.assertEqual(client.hgetall(f"posthog:decide_requests:{self.team.pk}"), {b"165192618": b"1"})

@patch("posthog.models.feature_flag.flag_analytics.CACHE_BUCKET_SIZE", 10)
def test_decide_new_capture_activation(self, *args):
self.client.logout()
with self.settings(NEW_ANALYTICS_CAPTURE_TEAM_IDS={str(self.team.id)}, NEW_ANALYTICS_CAPTURE_SAMPLING_RATE=1.0):
response = self._post_decide(api_version=3)
self.assertEqual(response.status_code, 200)
self.assertTrue("analytics" in response.json())
self.assertEqual(response.json()["analytics"]["endpoint"], "/i/v0/e/")

with self.settings(
NEW_ANALYTICS_CAPTURE_TEAM_IDS={str(self.team.id)},
NEW_ANALYTICS_CAPTURE_SAMPLING_RATE=1.0,
NEW_ANALYTICS_CAPTURE_ENDPOINT="/custom",
):
response = self._post_decide(api_version=3)
self.assertEqual(response.status_code, 200)
self.assertTrue("analytics" in response.json())
self.assertEqual(response.json()["analytics"]["endpoint"], "/custom")

with self.settings(NEW_ANALYTICS_CAPTURE_TEAM_IDS={"0"}, NEW_ANALYTICS_CAPTURE_SAMPLING_RATE=1.0):
response = self._post_decide(api_version=3)
self.assertEqual(response.status_code, 200)
self.assertFalse("analytics" in response.json())

with self.settings(NEW_ANALYTICS_CAPTURE_TEAM_IDS={str(self.team.id)}, NEW_ANALYTICS_CAPTURE_SAMPLING_RATE=0):
response = self._post_decide(api_version=3)
self.assertEqual(response.status_code, 200)
self.assertFalse("analytics" in response.json())


class TestDatabaseCheckForDecide(BaseTest, QueryMatchingTest):
"""
Expand Down

0 comments on commit cb6dec8

Please sign in to comment.