|
2 | 2 |
|
3 | 3 | describe LaunchDarkly::Evaluation do |
4 | 4 | subject { LaunchDarkly::Evaluation } |
| 5 | + let(:features) { LaunchDarkly::InMemoryFeatureStore.new } |
5 | 6 |
|
6 | 7 | include LaunchDarkly::Evaluation |
7 | 8 |
|
| 9 | + describe "evaluate" do |
| 10 | + it "returns off variation if flag is off" do |
| 11 | + flag = { |
| 12 | + key: 'feature', |
| 13 | + on: false, |
| 14 | + offVariation: 1, |
| 15 | + fallthrough: { variation: 0 }, |
| 16 | + variations: ['a', 'b', 'c'] |
| 17 | + } |
| 18 | + user = { key: 'x' } |
| 19 | + expect(evaluate(flag, user, features)).to eq({value: 'b', events: []}) |
| 20 | + end |
| 21 | + |
| 22 | + it "returns nil if flag is off and off variation is unspecified" do |
| 23 | + flag = { |
| 24 | + key: 'feature', |
| 25 | + on: false, |
| 26 | + fallthrough: { variation: 0 }, |
| 27 | + variations: ['a', 'b', 'c'] |
| 28 | + } |
| 29 | + user = { key: 'x' } |
| 30 | + expect(evaluate(flag, user, features)).to eq({value: nil, events: []}) |
| 31 | + end |
| 32 | + |
| 33 | + it "returns off variation if prerequisite is not found" do |
| 34 | + flag = { |
| 35 | + key: 'feature0', |
| 36 | + on: true, |
| 37 | + prerequisites: [{key: 'badfeature', variation: 1}], |
| 38 | + fallthrough: { variation: 0 }, |
| 39 | + offVariation: 1, |
| 40 | + variations: ['a', 'b', 'c'] |
| 41 | + } |
| 42 | + user = { key: 'x' } |
| 43 | + expect(evaluate(flag, user, features)).to eq({value: 'b', events: []}) |
| 44 | + end |
| 45 | + |
| 46 | + it "returns off variation and event if prerequisite is not met" do |
| 47 | + flag = { |
| 48 | + key: 'feature0', |
| 49 | + on: true, |
| 50 | + prerequisites: [{key: 'feature1', variation: 1}], |
| 51 | + fallthrough: { variation: 0 }, |
| 52 | + offVariation: 1, |
| 53 | + variations: ['a', 'b', 'c'], |
| 54 | + version: 1 |
| 55 | + } |
| 56 | + flag1 = { |
| 57 | + key: 'feature1', |
| 58 | + on: true, |
| 59 | + fallthrough: { variation: 0 }, |
| 60 | + variations: ['d', 'e'], |
| 61 | + version: 2 |
| 62 | + } |
| 63 | + features.upsert('feature1', flag1) |
| 64 | + user = { key: 'x' } |
| 65 | + events_should_be = [{kind: 'feature', key: 'feature1', value: 'd', version: 2, prereqOf: 'feature0'}] |
| 66 | + expect(evaluate(flag, user, features)).to eq({value: 'b', events: events_should_be}) |
| 67 | + end |
| 68 | + |
| 69 | + it "returns fallthrough variation and event if prerequisite is met and there are no rules" do |
| 70 | + flag = { |
| 71 | + key: 'feature0', |
| 72 | + on: true, |
| 73 | + prerequisites: [{key: 'feature1', variation: 1}], |
| 74 | + fallthrough: { variation: 0 }, |
| 75 | + offVariation: 1, |
| 76 | + variations: ['a', 'b', 'c'], |
| 77 | + version: 1 |
| 78 | + } |
| 79 | + flag1 = { |
| 80 | + key: 'feature1', |
| 81 | + on: true, |
| 82 | + fallthrough: { variation: 1 }, |
| 83 | + variations: ['d', 'e'], |
| 84 | + version: 2 |
| 85 | + } |
| 86 | + features.upsert('feature1', flag1) |
| 87 | + user = { key: 'x' } |
| 88 | + events_should_be = [{kind: 'feature', key: 'feature1', value: 'e', version: 2, prereqOf: 'feature0'}] |
| 89 | + expect(evaluate(flag, user, features)).to eq({value: 'a', events: events_should_be}) |
| 90 | + end |
| 91 | + |
| 92 | + it "matches user from targets" do |
| 93 | + flag = { |
| 94 | + key: 'feature0', |
| 95 | + on: true, |
| 96 | + targets: [ |
| 97 | + { values: [ 'whoever', 'userkey' ], variation: 2 } |
| 98 | + ], |
| 99 | + fallthrough: { variation: 0 }, |
| 100 | + offVariation: 1, |
| 101 | + variations: ['a', 'b', 'c'] |
| 102 | + } |
| 103 | + user = { key: 'userkey' } |
| 104 | + expect(evaluate(flag, user, features)).to eq({value: 'c', events: []}) |
| 105 | + end |
| 106 | + |
| 107 | + it "matches user from rules" do |
| 108 | + flag = { |
| 109 | + key: 'feature0', |
| 110 | + on: true, |
| 111 | + rules: [ |
| 112 | + { |
| 113 | + clauses: [ |
| 114 | + { |
| 115 | + attribute: 'key', |
| 116 | + op: 'in', |
| 117 | + values: [ 'userkey' ] |
| 118 | + } |
| 119 | + ], |
| 120 | + variation: 2 |
| 121 | + } |
| 122 | + ], |
| 123 | + fallthrough: { variation: 0 }, |
| 124 | + offVariation: 1, |
| 125 | + variations: ['a', 'b', 'c'] |
| 126 | + } |
| 127 | + user = { key: 'userkey' } |
| 128 | + expect(evaluate(flag, user, features)).to eq({value: 'c', events: []}) |
| 129 | + end |
| 130 | + end |
| 131 | + |
8 | 132 | describe "clause_match_user" do |
9 | 133 | it "can match built-in attribute" do |
10 | 134 | user = { key: 'x', name: 'Bob' } |
|
23 | 147 | clause = { attribute: 'legs', op: 'in', values: [4] } |
24 | 148 | expect(clause_match_user(clause, user)).to be false |
25 | 149 | end |
| 150 | + |
| 151 | + it "can be negated" do |
| 152 | + user = { key: 'x', name: 'Bob' } |
| 153 | + clause = { attribute: 'name', op: 'in', values: ['Bob'] } |
| 154 | + expect { |
| 155 | + clause[:negate] = true |
| 156 | + }.to change {clause_match_user(clause, user)}.from(true).to(false) |
| 157 | + end |
26 | 158 | end |
27 | 159 |
|
28 | 160 | describe "operators" do |
|
0 commit comments