forked from splitrb/split
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrial_spec.rb
More file actions
326 lines (263 loc) · 10.3 KB
/
trial_spec.rb
File metadata and controls
326 lines (263 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# frozen_string_literal: true
require 'spec_helper'
require 'split/trial'
describe Split::Trial do
let(:user) { mock_user }
let(:alternatives) { ['basket', 'cart'] }
let(:experiment) do
Split::Experiment.new('basket_text', :alternatives => alternatives).save
end
it "should be initializeable" do
experiment = double('experiment')
alternative = double('alternative', :kind_of? => Split::Alternative)
trial = Split::Trial.new(:experiment => experiment, :alternative => alternative)
expect(trial.experiment).to eq(experiment)
expect(trial.alternative).to eq(alternative)
end
describe "alternative" do
it "should use the alternative if specified" do
alternative = double('alternative', :kind_of? => Split::Alternative)
trial = Split::Trial.new(:experiment => double('experiment'),
:alternative => alternative, :user => user)
expect(trial).not_to receive(:choose)
expect(trial.alternative).to eq(alternative)
end
it "should load the alternative when the alternative name is set" do
experiment = Split::Experiment.new('basket_text', :alternatives => ['basket', 'cart'])
experiment.save
trial = Split::Trial.new(:experiment => experiment, :alternative => 'basket')
expect(trial.alternative.name).to eq('basket')
end
end
describe "metadata" do
let(:metadata) { Hash[alternatives.map { |k| [k, "Metadata for #{k}"] }] }
let(:experiment) do
Split::Experiment.new('basket_text', :alternatives => alternatives, :metadata => metadata).save
end
it 'has metadata on each trial' do
trial = Split::Trial.new(:experiment => experiment, :user => user, :metadata => metadata['cart'],
:override => 'cart')
expect(trial.metadata).to eq(metadata['cart'])
end
it 'has metadata on each trial from the experiment' do
trial = Split::Trial.new(:experiment => experiment, :user => user)
trial.choose!
expect(trial.metadata).to eq(metadata[trial.alternative.name])
expect(trial.metadata).to match(/#{trial.alternative.name}/)
end
end
describe "#choose!" do
let(:context) { double(on_trial_callback: 'test callback') }
let(:trial) do
Split::Trial.new(:user => user, :experiment => experiment)
end
shared_examples_for 'a trial with callbacks' do
it 'does not run if on_trial callback is not respondable' do
Split.configuration.on_trial = :foo
allow(context).to receive(:respond_to?).with(:foo, true).and_return false
expect(context).to_not receive(:foo)
trial.choose! context
end
it 'runs on_trial callback' do
Split.configuration.on_trial = :on_trial_callback
expect(context).to receive(:on_trial_callback)
trial.choose! context
end
it 'does not run nil on_trial callback' do
Split.configuration.on_trial = nil
expect(context).not_to receive(:on_trial_callback)
trial.choose! context
end
end
def expect_alternative(trial, alternative_name)
3.times do
trial.choose! context
expect(alternative_name).to include(trial.alternative.name)
end
end
context "when override is present" do
let(:override) { 'cart' }
let(:trial) do
Split::Trial.new(:user => user, :experiment => experiment, :override => override)
end
it_behaves_like 'a trial with callbacks'
it "picks the override" do
expect(experiment).to_not receive(:next_alternative)
expect_alternative(trial, override)
end
context "when alternative doesn't exist" do
let(:override) { nil }
it 'falls back on next_alternative' do
expect(experiment).to receive(:next_alternative).and_call_original
expect_alternative(trial, alternatives)
end
end
end
context "when disabled option is true" do
let(:trial) do
Split::Trial.new(:user => user, :experiment => experiment, :disabled => true)
end
it "picks the control", :aggregate_failures do
Split.configuration.on_trial = :on_trial_callback
expect(experiment).to_not receive(:next_alternative)
expect(context).not_to receive(:on_trial_callback)
expect_alternative(trial, 'basket')
Split.configuration.on_trial = nil
end
end
context "when Split is globally disabled" do
it "picks the control and does not run on_trial callbacks", :aggregate_failures do
Split.configuration.enabled = false
Split.configuration.on_trial = :on_trial_callback
expect(experiment).to_not receive(:next_alternative)
expect(context).not_to receive(:on_trial_callback)
expect_alternative(trial, 'basket')
Split.configuration.enabled = true
Split.configuration.on_trial = nil
end
end
context "when experiment has winner" do
let(:trial) do
Split::Trial.new(:user => user, :experiment => experiment)
end
it_behaves_like 'a trial with callbacks'
it "picks the winner" do
experiment.winner = 'cart'
expect(experiment).to_not receive(:next_alternative)
expect_alternative(trial, 'cart')
end
end
context "when exclude is true" do
let(:trial) do
Split::Trial.new(:user => user, :experiment => experiment, :exclude => true)
end
it_behaves_like 'a trial with callbacks'
it "picks the control" do
expect(experiment).to_not receive(:next_alternative)
expect_alternative(trial, 'basket')
end
end
context "when user is already participating" do
it_behaves_like 'a trial with callbacks'
it "picks the same alternative" do
user[experiment.key] = 'basket'
expect(experiment).to_not receive(:next_alternative)
expect_alternative(trial, 'basket')
end
context "when alternative is not found" do
it "falls back on next_alternative" do
user[experiment.key] = 'notfound'
expect(experiment).to receive(:next_alternative).and_call_original
expect_alternative(trial, alternatives)
end
end
end
context "when user is a new participant" do
it "picks a new alternative and runs on_trial_choose callback", :aggregate_failures do
Split.configuration.on_trial_choose = :on_trial_choose_callback
expect(experiment).to receive(:next_alternative).and_call_original
expect(context).to receive(:on_trial_choose_callback)
trial.choose! context
expect(trial.alternative.name).to_not be_empty
Split.configuration.on_trial_choose = nil
end
it "assigns user to an alternative" do
trial.choose! context
expect(alternatives).to include(user[experiment.name])
end
context "when cohorting is disabled" do
before(:each) { allow(experiment).to receive(:cohorting_disabled?).and_return(true) }
it "picks the control and does not run on_trial callbacks" do
Split.configuration.on_trial = :on_trial_callback
expect(experiment).to_not receive(:next_alternative)
expect(context).not_to receive(:on_trial_callback)
expect_alternative(trial, 'basket')
Split.configuration.enabled = true
Split.configuration.on_trial = nil
end
it "user is not assigned an alternative" do
trial.choose! context
expect(user[experiment]).to eq(nil)
end
end
end
end
describe "#complete!" do
let(:trial) { Split::Trial.new(:user => user, :experiment => experiment) }
context 'when there are no goals' do
it 'should complete the trial' do
trial.choose!
old_completed_count = trial.alternative.completed_count
trial.complete!
expect(trial.alternative.completed_count).to be(old_completed_count+1)
end
end
context 'when there are many goals' do
let(:goals) { ['first', 'second'] }
let(:trial) { Split::Trial.new(:user => user, :experiment => experiment, :goals => goals) }
shared_examples_for "goal completion" do
it 'should not complete the trial' do
trial.choose!
old_completed_count = trial.alternative.completed_count
trial.complete!(goal)
expect(trial.alternative.completed_count).to_not be(old_completed_count+1)
end
end
describe 'Array of Goals' do
let(:goal) { [goals.first] }
it_behaves_like 'goal completion'
end
describe 'String of Goal' do
let(:goal) { goals.first }
it_behaves_like 'goal completion'
end
end
end
describe "alternative recording" do
before(:each) { Split.configuration.store_override = false }
context "when override is present" do
it "stores when store_override is true" do
trial = Split::Trial.new(:user => user, :experiment => experiment, :override => 'basket')
Split.configuration.store_override = true
expect(user).to receive("[]=")
trial.choose!
expect(trial.alternative.participant_count).to eq(1)
end
it "does not store when store_override is false" do
trial = Split::Trial.new(:user => user, :experiment => experiment, :override => 'basket')
expect(user).to_not receive("[]=")
trial.choose!
end
end
context "when disabled is present" do
it "stores when store_override is true" do
trial = Split::Trial.new(:user => user, :experiment => experiment, :disabled => true)
Split.configuration.store_override = true
expect(user).to receive("[]=")
trial.choose!
end
it "does not store when store_override is false" do
trial = Split::Trial.new(:user => user, :experiment => experiment, :disabled => true)
expect(user).to_not receive("[]=")
trial.choose!
end
end
context "when exclude is present" do
it "does not store" do
trial = Split::Trial.new(:user => user, :experiment => experiment, :exclude => true)
expect(user).to_not receive("[]=")
trial.choose!
end
end
context 'when experiment has winner' do
let(:trial) do
experiment.winner = 'cart'
Split::Trial.new(:user => user, :experiment => experiment)
end
it 'does not store' do
expect(user).to_not receive("[]=")
trial.choose!
end
end
end
end