def test_hashing_is_consistent(self): percentage = Percentage(value=0.67001) bucketer = ConsistentHashPercentageBucketer(percentage=percentage) for _ in range(10): self.assertTrue( bucketer.check(foo="bar", herp=99, derp=False) # score = 0.67 )
def test_sets_correct_data(self): percentage = Percentage(value=0.5) key_whitelist = ["foo"] data = { "type": ConsistentHashPercentageBucketer.get_type(), "key_whitelist": key_whitelist, "percentage": percentage.to_dict(), } bucketer = ConsistentHashPercentageBucketer.from_dict(data) self.assertEqual(data, bucketer.to_dict())
def test_returns_correct_data(self): percentage = Percentage(value=0.5) key_whitelist = ["foo"] bucketer = ConsistentHashPercentageBucketer( key_whitelist=key_whitelist, percentage=percentage) expected = { "type": ConsistentHashPercentageBucketer.get_type(), "key_whitelist": key_whitelist, "percentage": percentage.to_dict(), } self.assertEqual(expected, bucketer.to_dict())
def test_calls_rpc_client_with_correct_args(self): feature_name = self.txt() client_data = {self.txt(): self.txt()} created_date = self.date() meta = FeatureFlagStoreMeta( created_date, client_data, conditions=[Condition(foo__lte=99)], bucketer=ConsistentHashPercentageBucketer( key_whitelist=["baz"], percentage=LinearRampPercentage() ), ) self.store.set_meta(feature_name, meta) self.client.SetMeta.assert_called_once_with( feature_name, TFeatureFlagStoreMeta( created_date=meta.created_date, client_data=json.dumps(client_data), conditions=[ { "foo": [ TConditionCheck( variable="foo", value="99", operator=TConditionOperator(symbol="lte"), ) ] } ], bucketer=json.dumps(meta.bucketer.to_dict()), ), )
def setUp(self): self.meta = TFeatureFlagStoreMeta( created_date=now(), client_data='{"foo": 30}', conditions=[ { "foo": [ TConditionCheck( variable="foo", value="99", operator=TConditionOperator(symbol="lte"), ) ] } ], bucketer=json.dumps( ConsistentHashPercentageBucketer( key_whitelist=["baz"], percentage=LinearRampPercentage() ).to_dict() ), ) class FakeThriftClient: Create = MagicMock( return_value=TFeatureFlagStoreItem( feature_name=self.txt(), is_enabled=False, meta=self.meta ) ) Delete = MagicMock() Get = MagicMock( return_value=TFeatureFlagStoreItem( feature_name=self.txt(), is_enabled=False, meta=self.meta ) ) Set = MagicMock() SetMeta = MagicMock() List = MagicMock( return_value=[ TFeatureFlagStoreItem( feature_name=self.txt(), is_enabled=False, meta=self.meta ), TFeatureFlagStoreItem( feature_name=self.txt(), is_enabled=False, meta=self.meta ), TFeatureFlagStoreItem( feature_name=self.txt(), is_enabled=False, meta=self.meta ), ] ) self.client = FakeThriftClient() self.store = ThriftRPCFeatureFlagStore(self.client)
def test_converts_metadata_properly(self): feature_name = self.txt() self.store.create(feature_name, is_enabled=True) item = self.store.get(feature_name) expected = FeatureFlagStoreMeta( created_date=self.meta.created_date, client_data=json.loads(self.meta.client_data), conditions=[Condition(foo__lte=99)], bucketer=ConsistentHashPercentageBucketer( key_whitelist=["baz"], percentage=LinearRampPercentage( initial_time=json.loads(self.meta.bucketer)["percentage"][ "initial_time" ] ), ), ) self.assertEqual(expected.to_dict(), item.meta)
def test_is_correct_value(self): bucketer = ConsistentHashPercentageBucketer() self.assertEqual("ConsistentHashPercentageBucketer", bucketer.get_type())
def test_always_returns_false_when_percentage_is_zero_when_including_conditions( self): # noqa: E501 percentage = Percentage(value=0.0) bucketer = ConsistentHashPercentageBucketer(percentage=percentage) self.assertFalse(bucketer.check(foo="bar")) # score = 0.79
def test_returns_true_when_conditions_hash_to_value_equal_to_percentage( self): # noqa: E501 percentage = Percentage(value=0.79) bucketer = ConsistentHashPercentageBucketer(percentage=percentage) self.assertTrue(bucketer.check(foo="bar")) # score = 0.79
def test_returns_false_when_conditions_hash_to_value_greater_than_percentage( self): # noqa: E501 percentage = Percentage(value=0.5) bucketer = ConsistentHashPercentageBucketer(percentage=percentage) self.assertFalse(bucketer.check(foo="bar")) # score = 0.79
def test_always_returns_false_when_percentage_is_zero(self): percentage = Percentage(value=0.0) bucketer = ConsistentHashPercentageBucketer(percentage=percentage) self.assertFalse(bucketer.check()) # score = 0.32
def test_returns_false_when_randomized_is_greater_than_percentage(self): percentage = Percentage(value=0.2) bucketer = ConsistentHashPercentageBucketer(percentage=percentage) self.assertFalse(bucketer.check()) # score = 0.32
def test_returns_true_when_randomized_is_equal_to_percentage(self): percentage = Percentage(value=0.32) bucketer = ConsistentHashPercentageBucketer(percentage=percentage) self.assertTrue(bucketer.check()) # score = 0.32
def test_returns_percentage_value_as_float(self): percentage_raw_value = 0.5 percentage = Percentage(value=percentage_raw_value) bucketer = ConsistentHashPercentageBucketer(percentage=percentage) self.assertEqual(percentage_raw_value, bucketer.percentage)
def test_creates_consistent_hash_percentage_bucketer(self): bucketer = BucketerFactory.create( {"type": ConsistentHashPercentageBucketer.get_type()} ) self.assertIsInstance(bucketer, ConsistentHashPercentageBucketer)