Ejemplo n.º 1
0
def test_bucket_by_user_key():
    user = { u'key': u'userKeyA' }
    bucket = _bucket_user(user, 'hashKey', 'saltyA', 'key')
    assert bucket == pytest.approx(0.42157587)

    user = { u'key': u'userKeyB' }
    bucket = _bucket_user(user, 'hashKey', 'saltyA', 'key')
    assert bucket == pytest.approx(0.6708485)

    user = { u'key': u'userKeyC' }
    bucket = _bucket_user(user, 'hashKey', 'saltyA', 'key')
    assert bucket == pytest.approx(0.10343106)
Ejemplo n.º 2
0
def test_bucket_by_int_attr():
    user = {
        u'key': u'userKey',
        u'custom': {
            u'intAttr': 33333,
            u'stringAttr': u'33333'
        }
    }
    bucket = _bucket_user(user, 'hashKey', 'saltyA', 'intAttr')
    assert bucket == pytest.approx(0.54771423)
    bucket2 = _bucket_user(user, 'hashKey', 'saltyA', 'stringAttr')
    assert bucket2 == bucket
Ejemplo n.º 3
0
def test_bucket_by_user_key():
    user = { u'key': u'userKeyA' }
    bucket = _bucket_user(user, 'hashKey', 'saltyA', 'key')
    assert bucket == pytest.approx(0.42157587)

    user = { u'key': u'userKeyB' }
    bucket = _bucket_user(user, 'hashKey', 'saltyA', 'key')
    assert bucket == pytest.approx(0.6708485)

    user = { u'key': u'userKeyC' }
    bucket = _bucket_user(user, 'hashKey', 'saltyA', 'key')
    assert bucket == pytest.approx(0.10343106)
Ejemplo n.º 4
0
def test_bucket_by_int_attr():
    feature = { u'key': u'hashKey', u'salt': u'saltyA' }
    user = {
        u'key': u'userKey',
        u'custom': {
            u'intAttr': 33333,
            u'stringAttr': u'33333'
        }
    }
    bucket = _bucket_user(user, 'hashKey', 'saltyA', 'intAttr')
    assert bucket == pytest.approx(0.54771423)
    bucket2 = _bucket_user(user, 'hashKey', 'saltyA', 'stringAttr')
    assert bucket2 == bucket
Ejemplo n.º 5
0
def test_bucket_by_float_attr_not_allowed():
    user = {
        u'key': u'userKey',
        u'custom': {
            u'floatAttr': 33.5
        }
    }
    bucket = _bucket_user(user, 'hashKey', 'saltyA', 'floatAttr')
    assert bucket == 0.0
Ejemplo n.º 6
0
def test_bucket_by_float_attr_not_allowed():
    feature = { u'key': u'hashKey', u'salt': u'saltyA' }
    user = {
        u'key': u'userKey',
        u'custom': {
            u'floatAttr': 33.5
        }
    }
    bucket = _bucket_user(user, 'hashKey', 'saltyA', 'floatAttr')
    assert bucket == 0.0
Ejemplo n.º 7
0
def test_last_bucket_is_used_if_bucket_value_equals_total_weight():
    user = { 'key': 'userkey' }
    flag = { 'key': 'flagkey', 'salt': 'salt' }

    # We'll construct a list of variations that stops right at the target bucket value
    bucket_value = math.trunc(_bucket_user(user, flag['key'], flag['salt'], 'key') * 100000)
    
    rule = {
        'rollout': {
            'variations': [
                { 'variation': 0, 'weight': bucket_value }
            ]
        }
    }
    result_variation = _variation_index_for_user(flag, rule, user)
    assert result_variation == 0
Ejemplo n.º 8
0
def test_variation_index_is_returned_for_bucket():
    user = { 'key': 'userkey' }
    flag = { 'key': 'flagkey', 'salt': 'salt' }

    # First verify that with our test inputs, the bucket value will be greater than zero and less than 100000,
    # so we can construct a rollout whose second bucket just barely contains that value
    bucket_value = math.trunc(_bucket_user(user, flag['key'], flag['salt'], 'key') * 100000)
    assert bucket_value > 0 and bucket_value < 100000
    
    bad_variation_a = 0
    matched_variation = 1
    bad_variation_b = 2
    rule = {
        'rollout': {
            'variations': [
                { 'variation': bad_variation_a, 'weight': bucket_value }, # end of bucket range is not inclusive, so it will *not* match the target value
                { 'variation': matched_variation, 'weight': 1 }, # size of this bucket is 1, so it only matches that specific value
                { 'variation': bad_variation_b, 'weight': 100000 - (bucket_value + 1) }
            ]
        }
    }
    result_variation = _variation_index_for_user(flag, rule, user)
    assert result_variation == matched_variation