Skip to content

Commit 00748fd

Browse files
committed
Update trial to init with goals instead
1 parent 482e700 commit 00748fd

File tree

4 files changed

+32
-32
lines changed

4 files changed

+32
-32
lines changed

lib/split/helper.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,14 @@ def finish_experiment(experiment, options = {:reset => true})
5151
return true
5252
else
5353
alternative_name = ab_user[experiment.key]
54-
trial = Trial.new(:user => ab_user, :experiment => experiment,
55-
:alternative => alternative_name)
56-
trial.complete!(options[:goals], self)
54+
trial = Trial.new(
55+
:user => ab_user,
56+
:experiment => experiment,
57+
:alternative => alternative_name,
58+
:goals => options[:goals],
59+
)
60+
61+
trial.complete!(self)
5762

5863
if should_reset
5964
reset!(experiment)

lib/split/trial.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
# frozen_string_literal: true
22
module Split
33
class Trial
4+
attr_accessor :goals
45
attr_accessor :experiment
56
attr_writer :metadata
67

78
def initialize(attrs = {})
89
self.experiment = attrs.delete(:experiment)
910
self.alternative = attrs.delete(:alternative)
1011
self.metadata = attrs.delete(:metadata)
12+
self.goals = attrs.delete(:goals) || []
1113

1214
@user = attrs.delete(:user)
1315
@options = attrs
@@ -33,7 +35,7 @@ def alternative=(alternative)
3335
end
3436
end
3537

36-
def complete!(goals=[], context = nil)
38+
def complete!(context = nil)
3739
if alternative
3840
if Array(goals).empty?
3941
alternative.increment_completion

spec/helper_spec.rb

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,15 +1138,9 @@ def should_finish_experiment(experiment_name, should_finish=true)
11381138
end
11391139

11401140
it "should increment the counter for the specified-goal completed alternative" do
1141-
expect(lambda {
1142-
expect(lambda {
1143-
ab_finished({"link_color" => ["purchase"]})
1144-
}).not_to change {
1145-
Split::Alternative.new(@alternative_name, @experiment_name).completed_count(@goal2)
1146-
}
1147-
}).to change {
1148-
Split::Alternative.new(@alternative_name, @experiment_name).completed_count(@goal1)
1149-
}.by(1)
1141+
expect{ ab_finished({"link_color" => ["purchase"]}) }
1142+
.to change{ Split::Alternative.new(@alternative_name, @experiment_name).completed_count(@goal2) }.by(0)
1143+
.and change{ Split::Alternative.new(@alternative_name, @experiment_name).completed_count(@goal1) }.by(1)
11501144
end
11511145
end
11521146
end

spec/trial_spec.rb

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -229,38 +229,37 @@ def expect_alternative(trial, alternative_name)
229229
end
230230

231231
describe "#complete!" do
232-
let(:trial) { Split::Trial.new(:user => user, :experiment => experiment) }
233232
context 'when there are no goals' do
233+
let(:trial) { Split::Trial.new(:user => user, :experiment => experiment) }
234234
it 'should complete the trial' do
235235
trial.choose!
236236
old_completed_count = trial.alternative.completed_count
237237
trial.complete!
238-
expect(trial.alternative.completed_count).to be(old_completed_count+1)
238+
expect(trial.alternative.completed_count).to eq(old_completed_count + 1)
239239
end
240240
end
241241

242-
context 'when there are many goals' do
243-
let(:goals) { ['first', 'second'] }
242+
context "when there are many goals" do
243+
let(:goals) { [ "goal1", "second" ] }
244244
let(:trial) { Split::Trial.new(:user => user, :experiment => experiment, :goals => goals) }
245-
shared_examples_for "goal completion" do
246-
it 'should not complete the trial' do
247-
trial.choose!
248-
old_completed_count = trial.alternative.completed_count
249-
trial.complete!(goal)
250-
expect(trial.alternative.completed_count).to_not be(old_completed_count+1)
251-
end
252-
end
253245

254-
describe 'Array of Goals' do
255-
let(:goal) { [goals.first] }
256-
it_behaves_like 'goal completion'
246+
it "increments the conmpleted count corresponding to the goals" do
247+
trial.choose!
248+
old_completed_counts = goals.map{ |goal| [goal, trial.alternative.completed_count(goal)] }.to_h
249+
trial.complete!
250+
goals.each { | goal | expect(trial.alternative.completed_count(goal)).to eq(old_completed_counts[goal] + 1) }
257251
end
252+
end
258253

259-
describe 'String of Goal' do
260-
let(:goal) { goals.first }
261-
it_behaves_like 'goal completion'
254+
context "when there is 1 goal of type string" do
255+
let(:goal) { "first" }
256+
let(:trial) { Split::Trial.new(:user => user, :experiment => experiment, :goals => goal) }
257+
it "increments the conmpleted count corresponding to the goal" do
258+
trial.choose!
259+
old_completed_count = trial.alternative.completed_count(goal)
260+
trial.complete!
261+
expect(trial.alternative.completed_count(goal)).to eq(old_completed_count + 1)
262262
end
263-
264263
end
265264
end
266265

0 commit comments

Comments
 (0)