Skip to content
This repository has been archived by the owner on May 30, 2024. It is now read-only.

Commit

Permalink
add unit tests for basic bucketing logic and edge case
Browse files Browse the repository at this point in the history
  • Loading branch information
eli-darkly committed Dec 30, 2019
1 parent 24b85c3 commit 561ed34
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions test/evaluate_flag-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,66 @@ describe('evaluate', () => {
});
});

describe('rollout', () => {
it('selects bucket', done => {
const user = { key: 'userkey' };
const flagKey = 'flagkey';
const 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
const bucketValue = Math.floor(evaluate.bucketUser(user, flagKey, 'key', salt) * 100000);
expect(bucketValue).toBeGreaterThan(0);
expect(bucketValue).toBeLessThan(100000);

const badVariationA = 0, matchedVariation = 1, badVariationB = 2;
const rollout = {
variations: [
{ variation: badVariationA, weight: bucketValue }, // end of bucket range is not inclusive, so it will *not* match the target value
{ variation: matchedVariation, weight: 1 }, // size of this bucket is 1, so it only matches that specific value
{ variation: badVariationB, weight: 100000 - (bucketValue + 1) }
]
};
const flag = {
key: flagKey,
salt: salt,
on: true,
fallthrough: { rollout: rollout },
variations: [ null, null, null ]
};
evaluate.evaluate(flag, user, featureStore, eventFactory, (err, detail) => {
expect(err).toEqual(null);
expect(detail.variationIndex).toEqual(matchedVariation);
done();
});
});

it('uses last bucket if bucket value is equal to total weight', done => {
const user = { key: 'userkey' };
const flagKey = 'flagkey';
const salt = 'salt';

// We'll construct a list of variations that stops right at the target bucket value
const bucketValue = Math.floor(evaluate.bucketUser(user, flagKey, 'key', salt) * 100000);

const rollout = {
variations: [ { variation: 0, weight: bucketValue }]
};
const flag = {
key: flagKey,
salt: salt,
on: true,
fallthrough: { rollout: rollout },
variations: [ null, null, null ]
};
evaluate.evaluate(flag, user, featureStore, eventFactory, (err, detail) => {
expect(err).toEqual(null);
expect(detail.variationIndex).toEqual(0);
done();
});
});
});

describe('bucketUser', () => {
it('gets expected bucket values for specific keys', () => {
var user = { key: 'userKeyA' };
Expand Down

0 comments on commit 561ed34

Please sign in to comment.