Skip to content

Commit 119e520

Browse files
authored
Merge pull request #39 from launchdarkly/eb/flag-tests
more unit tests for flag evals
2 parents ff5986c + c308d9d commit 119e520

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed

spec/evaluation_spec.rb

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,133 @@
22

33
describe LaunchDarkly::Evaluation do
44
subject { LaunchDarkly::Evaluation }
5+
let(:features) { LaunchDarkly::InMemoryFeatureStore.new }
56

67
include LaunchDarkly::Evaluation
78

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+
8132
describe "clause_match_user" do
9133
it "can match built-in attribute" do
10134
user = { key: 'x', name: 'Bob' }
@@ -23,6 +147,14 @@
23147
clause = { attribute: 'legs', op: 'in', values: [4] }
24148
expect(clause_match_user(clause, user)).to be false
25149
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
26158
end
27159

28160
describe "operators" do

0 commit comments

Comments
 (0)