From 433bd9cf249b4eb9ecbe9258c0228d8f08264aa6 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Tue, 12 Nov 2024 05:19:21 -0800 Subject: [PATCH] Add some tests for flag matching --- .../feature_flag/test/test_flag_matching.py | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 posthog/models/feature_flag/test/test_flag_matching.py diff --git a/posthog/models/feature_flag/test/test_flag_matching.py b/posthog/models/feature_flag/test/test_flag_matching.py new file mode 100644 index 00000000000..de1be6a0666 --- /dev/null +++ b/posthog/models/feature_flag/test/test_flag_matching.py @@ -0,0 +1,31 @@ +from posthog.models.feature_flag.feature_flag import FeatureFlag +from posthog.models.feature_flag.flag_matching import FeatureFlagMatcher +from posthog.test.base import BaseTest + + +class TestFlagMatching(BaseTest): + def test_get_hash(self): + # Create test flag + flag = FeatureFlag(key="test-flag", rollout_percentage=50, team=self.team) + + # Test with different identifiers + matcher1 = FeatureFlagMatcher([flag], "distinct_id_1") + matcher2 = FeatureFlagMatcher([flag], "distinct_id_2") + + # Same identifier should get same hash + # distinct_id_1 + test-flag = 0.35140843114131903 + self.assertAlmostEqual(matcher1.get_hash(flag), 0.35140843114131903) + self.assertAlmostEqual(matcher1.get_hash(flag), 0.35140843114131903) + + # Different identifiers should get different hashes + # distinct_id_2 + test-flag = 0.34900843133051557 + self.assertAlmostEqual(matcher2.get_hash(flag), 0.34900843133051557) + + # Different salt should produce different hash + # distinct_id_1 + test-flag + salt = 0.05659409091269017 + self.assertAlmostEqual(matcher1.get_hash(flag, salt="salt"), 0.05659409091269017) + + # Different flag keys should produce different hashes + flag2 = FeatureFlag(key="different-flag", rollout_percentage=50, team=self.team) + # distinct_id_1 + different-flag = 0.5078604702829128 + self.assertAlmostEqual(matcher1.get_hash(flag2), 0.5078604702829128)