Skip to content

Commit d9c3274

Browse files
authored
use TestData in our own tests (#174)
* use TestData in our own tests * fix test
1 parent bb3ecc5 commit d9c3274

File tree

4 files changed

+139
-180
lines changed

4 files changed

+139
-180
lines changed

spec/ldclient_evaluation_spec.rb

Lines changed: 78 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -21,53 +21,55 @@ module LaunchDarkly
2121
end
2222

2323
it "returns the value for an existing feature" do
24-
flag = FlagBuilder.new("flagkey").off_with_value("value").build
25-
store = InMemoryFeatureStore.new
26-
store.upsert(FEATURES, flag)
24+
td = Integrations::TestData.data_source
25+
td.update(td.flag("flagkey").variations("value").variation_for_all_users(0))
2726

28-
with_client(test_config(feature_store: store)) do |client|
27+
with_client(test_config(data_source: td)) do |client|
2928
expect(client.variation("flagkey", basic_user, "default")).to eq "value"
3029
end
3130
end
3231

3332
it "returns the default value if a feature evaluates to nil" do
34-
flag = FlagBuilder.new("flagkey").on(false).off_variation(nil).build
35-
store = InMemoryFeatureStore.new
36-
store.upsert(FEATURES, flag)
37-
38-
with_client(test_config(feature_store: store)) do |client|
33+
td = Integrations::TestData.data_source
34+
td.use_preconfigured_flag({ # TestData normally won't construct a flag with offVariation: nil
35+
key: "flagkey",
36+
on: false,
37+
offVariation: nil
38+
})
39+
40+
with_client(test_config(data_source: td)) do |client|
3941
expect(client.variation("flagkey", basic_user, "default")).to eq "default"
4042
end
4143
end
4244

4345
it "can evaluate a flag that references a segment" do
46+
td = Integrations::TestData.data_source
4447
segment = SegmentBuilder.new("segmentkey").included(basic_user[:key]).build
45-
flag = FlagBuilder.new("flagkey").on(true).variations(true, false).rule(
46-
RuleBuilder.new.variation(0).clause(Clauses.match_segment(segment))
47-
).build
48-
store = InMemoryFeatureStore.new
49-
store.upsert(SEGMENTS, segment)
50-
store.upsert(FEATURES, flag)
51-
52-
with_client(test_config(feature_store: store)) do |client|
48+
td.use_preconfigured_segment(segment)
49+
td.use_preconfigured_flag(
50+
FlagBuilder.new("flagkey").on(true).variations(true, false).rule(
51+
RuleBuilder.new.variation(0).clause(Clauses.match_segment(segment))
52+
).build)
53+
54+
with_client(test_config(data_source: td)) do |client|
5355
expect(client.variation("flagkey", basic_user, false)).to be true
5456
end
5557
end
5658

5759
it "can evaluate a flag that references a big segment" do
60+
td = Integrations::TestData.data_source
5861
segment = SegmentBuilder.new("segmentkey").unbounded(true).generation(1).build
59-
flag = FlagBuilder.new("flagkey").on(true).variations(true, false).rule(
60-
RuleBuilder.new.variation(0).clause(Clauses.match_segment(segment))
61-
).build
62-
store = InMemoryFeatureStore.new
63-
store.upsert(SEGMENTS, segment)
64-
store.upsert(FEATURES, flag)
62+
td.use_preconfigured_segment(segment)
63+
td.use_preconfigured_flag(
64+
FlagBuilder.new("flagkey").on(true).variations(true, false).rule(
65+
RuleBuilder.new.variation(0).clause(Clauses.match_segment(segment))
66+
).build)
6567

6668
segstore = MockBigSegmentStore.new
6769
segstore.setup_segment_for_user(basic_user[:key], segment, true)
6870
big_seg_config = BigSegmentsConfig.new(store: segstore)
6971

70-
with_client(test_config(feature_store: store, big_segments: big_seg_config)) do |client|
72+
with_client(test_config(data_source: td, big_segments: big_seg_config)) do |client|
7173
expect(client.variation("flagkey", basic_user, false)).to be true
7274
end
7375
end
@@ -94,45 +96,47 @@ module LaunchDarkly
9496
end
9597

9698
it "returns a value for an existing feature" do
97-
flag = FlagBuilder.new("key").off_with_value("value").build
98-
store = InMemoryFeatureStore.new
99-
store.upsert(FEATURES, flag)
100-
101-
with_client(test_config(feature_store: store)) do |client|
102-
result = client.variation_detail("key", basic_user, "default")
99+
td = Integrations::TestData.data_source
100+
td.update(td.flag("flagkey").variations("value").on(false).off_variation(0))
101+
102+
with_client(test_config(data_source: td)) do |client|
103+
result = client.variation_detail("flagkey", basic_user, "default")
103104
expected = EvaluationDetail.new("value", 0, EvaluationReason::off)
104105
expect(result).to eq expected
105106
end
106107
end
107108

108109
it "returns the default value if a feature evaluates to nil" do
109-
empty_feature = { key: "key", on: false, offVariation: nil }
110-
store = InMemoryFeatureStore.new
111-
store.upsert(FEATURES, empty_feature)
112-
113-
with_client(test_config(feature_store: store)) do |client|
114-
result = client.variation_detail("key", basic_user, "default")
110+
td = Integrations::TestData.data_source
111+
td.use_preconfigured_flag({ # TestData normally won't construct a flag with offVariation: nil
112+
key: "flagkey",
113+
on: false,
114+
offVariation: nil
115+
})
116+
117+
with_client(test_config(data_source: td)) do |client|
118+
result = client.variation_detail("flagkey", basic_user, "default")
115119
expected = EvaluationDetail.new("default", nil, EvaluationReason::off)
116120
expect(result).to eq expected
117121
expect(result.default_value?).to be true
118122
end
119123
end
120124

121125
it "includes big segment status in reason when evaluating a flag that references a big segment" do
126+
td = Integrations::TestData.data_source
122127
segment = SegmentBuilder.new("segmentkey").unbounded(true).generation(1).build
123-
flag = FlagBuilder.new("flagkey").on(true).variations(true, false).rule(
124-
RuleBuilder.new.variation(0).clause(Clauses.match_segment(segment))
125-
).build
126-
store = InMemoryFeatureStore.new
127-
store.upsert(SEGMENTS, segment)
128-
store.upsert(FEATURES, flag)
128+
td.use_preconfigured_segment(segment)
129+
td.use_preconfigured_flag(
130+
FlagBuilder.new("flagkey").on(true).variations(true, false).rule(
131+
RuleBuilder.new.variation(0).clause(Clauses.match_segment(segment))
132+
).build)
129133

130134
segstore = MockBigSegmentStore.new
131135
segstore.setup_segment_for_user(basic_user[:key], segment, true)
132136
segstore.setup_metadata(Time.now)
133137
big_seg_config = BigSegmentsConfig.new(store: segstore)
134138

135-
with_client(test_config(feature_store: store, big_segments: big_seg_config)) do |client|
139+
with_client(test_config(data_source: td, big_segments: big_seg_config)) do |client|
136140
result = client.variation_detail("flagkey", basic_user, false)
137141
expect(result.value).to be true
138142
expect(result.reason.big_segments_status).to eq(BigSegmentsStatus::HEALTHY)
@@ -143,42 +147,36 @@ module LaunchDarkly
143147
describe "all_flags" do
144148
let(:flag1) { { key: "key1", offVariation: 0, variations: [ 'value1' ] } }
145149
let(:flag2) { { key: "key2", offVariation: 0, variations: [ 'value2' ] } }
150+
let(:test_data) {
151+
td = Integrations::TestData.data_source
152+
td.use_preconfigured_flag(flag1)
153+
td.use_preconfigured_flag(flag2)
154+
td
155+
}
146156

147157
it "returns flag values" do
148-
store = InMemoryFeatureStore.new
149-
store.init({ FEATURES => { 'key1' => flag1, 'key2' => flag2 } })
150-
151-
with_client(test_config(feature_store: store)) do |client|
158+
with_client(test_config(data_source: test_data)) do |client|
152159
result = client.all_flags({ key: 'userkey' })
153160
expect(result).to eq({ 'key1' => 'value1', 'key2' => 'value2' })
154161
end
155162
end
156163

157164
it "returns empty map for nil user" do
158-
store = InMemoryFeatureStore.new
159-
store.init({ FEATURES => { 'key1' => flag1, 'key2' => flag2 } })
160-
161-
with_client(test_config(feature_store: store)) do |client|
165+
with_client(test_config(data_source: test_data)) do |client|
162166
result = client.all_flags(nil)
163167
expect(result).to eq({})
164168
end
165169
end
166170

167171
it "returns empty map for nil user key" do
168-
store = InMemoryFeatureStore.new
169-
store.init({ FEATURES => { 'key1' => flag1, 'key2' => flag2 } })
170-
171-
with_client(test_config(feature_store: store)) do |client|
172+
with_client(test_config(data_source: test_data)) do |client|
172173
result = client.all_flags({})
173174
expect(result).to eq({})
174175
end
175176
end
176177

177178
it "returns empty map if offline" do
178-
store = InMemoryFeatureStore.new
179-
store.init({ FEATURES => { 'key1' => flag1, 'key2' => flag2 } })
180-
181-
with_client(test_config(feature_store: store, offline: true)) do |offline_client|
179+
with_client(test_config(data_source: test_data, offline: true)) do |offline_client|
182180
result = offline_client.all_flags(nil)
183181
expect(result).to eq({})
184182
end
@@ -188,12 +186,16 @@ module LaunchDarkly
188186
context "all_flags_state" do
189187
let(:flag1) { { key: "key1", version: 100, offVariation: 0, variations: [ 'value1' ], trackEvents: false } }
190188
let(:flag2) { { key: "key2", version: 200, offVariation: 1, variations: [ 'x', 'value2' ], trackEvents: true, debugEventsUntilDate: 1000 } }
189+
let(:test_data) {
190+
td = Integrations::TestData.data_source
191+
td.use_preconfigured_flag(flag1)
192+
td.use_preconfigured_flag(flag2)
193+
td
194+
}
191195

192196
it "returns flags state" do
193-
store = InMemoryFeatureStore.new
194-
store.init({ FEATURES => { 'key1' => flag1, 'key2' => flag2 } })
195197

196-
with_client(test_config(feature_store: store)) do |client|
198+
with_client(test_config(data_source: test_data)) do |client|
197199
state = client.all_flags_state({ key: 'userkey' })
198200
expect(state.valid?).to be true
199201

@@ -222,17 +224,13 @@ module LaunchDarkly
222224
end
223225

224226
it "can be filtered for only client-side flags" do
225-
flag1 = { key: "server-side-1", offVariation: 0, variations: [ 'a' ], clientSide: false }
226-
flag2 = { key: "server-side-2", offVariation: 0, variations: [ 'b' ], clientSide: false }
227-
flag3 = { key: "client-side-1", offVariation: 0, variations: [ 'value1' ], clientSide: true }
228-
flag4 = { key: "client-side-2", offVariation: 0, variations: [ 'value2' ], clientSide: true }
227+
td = Integrations::TestData.data_source
228+
td.use_preconfigured_flag({ key: "server-side-1", offVariation: 0, variations: [ 'a' ], clientSide: false })
229+
td.use_preconfigured_flag({ key: "server-side-2", offVariation: 0, variations: [ 'b' ], clientSide: false })
230+
td.use_preconfigured_flag({ key: "client-side-1", offVariation: 0, variations: [ 'value1' ], clientSide: true })
231+
td.use_preconfigured_flag({ key: "client-side-2", offVariation: 0, variations: [ 'value2' ], clientSide: true })
229232

230-
store = InMemoryFeatureStore.new
231-
store.init({ FEATURES => {
232-
flag1[:key] => flag1, flag2[:key] => flag2, flag3[:key] => flag3, flag4[:key] => flag4
233-
}})
234-
235-
with_client(test_config(feature_store: store)) do |client|
233+
with_client(test_config(data_source: td)) do |client|
236234
state = client.all_flags_state({ key: 'userkey' }, client_side_only: true)
237235
expect(state.valid?).to be true
238236

@@ -243,14 +241,12 @@ module LaunchDarkly
243241

244242
it "can omit details for untracked flags" do
245243
future_time = (Time.now.to_f * 1000).to_i + 100000
246-
flag1 = { key: "key1", version: 100, offVariation: 0, variations: [ 'value1' ], trackEvents: false }
247-
flag2 = { key: "key2", version: 200, offVariation: 1, variations: [ 'x', 'value2' ], trackEvents: true }
248-
flag3 = { key: "key3", version: 300, offVariation: 1, variations: [ 'x', 'value3' ], debugEventsUntilDate: future_time }
249-
250-
store = InMemoryFeatureStore.new
251-
store.init({ FEATURES => { 'key1' => flag1, 'key2' => flag2, 'key3' => flag3 } })
244+
td = Integrations::TestData.data_source
245+
td.use_preconfigured_flag({ key: "key1", version: 100, offVariation: 0, variations: [ 'value1' ], trackEvents: false })
246+
td.use_preconfigured_flag({ key: "key2", version: 200, offVariation: 1, variations: [ 'x', 'value2' ], trackEvents: true })
247+
td.use_preconfigured_flag({ key: "key3", version: 300, offVariation: 1, variations: [ 'x', 'value3' ], debugEventsUntilDate: future_time })
252248

253-
with_client(test_config(feature_store: store)) do |client|
249+
with_client(test_config(data_source: td)) do |client|
254250
state = client.all_flags_state({ key: 'userkey' }, { details_only_for_tracked_flags: true })
255251
expect(state.valid?).to be true
256252

@@ -283,32 +279,23 @@ module LaunchDarkly
283279
end
284280

285281
it "returns empty state for nil user" do
286-
store = InMemoryFeatureStore.new
287-
store.init({ FEATURES => { 'key1' => flag1, 'key2' => flag2 } })
288-
289-
with_client(test_config(feature_store: store)) do |client|
282+
with_client(test_config(data_source: test_data)) do |client|
290283
state = client.all_flags_state(nil)
291284
expect(state.valid?).to be false
292285
expect(state.values_map).to eq({})
293286
end
294287
end
295288

296289
it "returns empty state for nil user key" do
297-
store = InMemoryFeatureStore.new
298-
store.init({ FEATURES => { 'key1' => flag1, 'key2' => flag2 } })
299-
300-
with_client(test_config(feature_store: store)) do |client|
290+
with_client(test_config(data_source: test_data)) do |client|
301291
state = client.all_flags_state({})
302292
expect(state.valid?).to be false
303293
expect(state.values_map).to eq({})
304294
end
305295
end
306296

307297
it "returns empty state if offline" do
308-
store = InMemoryFeatureStore.new
309-
store.init({ FEATURES => { 'key1' => flag1, 'key2' => flag2 } })
310-
311-
with_client(test_config(feature_store: store, offline: true)) do |offline_client|
298+
with_client(test_config(data_source: test_data, offline: true)) do |offline_client|
312299
state = offline_client.all_flags_state({ key: 'userkey' })
313300
expect(state.valid?).to be false
314301
expect(state.values_map).to eq({})

0 commit comments

Comments
 (0)