Skip to content

Commit

Permalink
* Added: reset an experiment by calling reset!.
Browse files Browse the repository at this point in the history
  • Loading branch information
assaf committed Nov 11, 2009
1 parent 37428fd commit d8c2c10
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
@@ -1,6 +1,7 @@
0.2.2
* Added: vanity binary, with single command for generating a report.
* Added: return alternative by value from experiment.alternative(val) method.
* Added: reset an experiment by calling reset!.

0.2.1 (2009-11-11)
* Added: z-score and confidence level for A/B test alternatives.
Expand Down
6 changes: 6 additions & 0 deletions lib/vanity/experiment/ab_test.rb
Expand Up @@ -262,6 +262,12 @@ def save #:nodoc:
super
end

def reset! #:nodoc:
redis.del key(:outcome)
alternatives.each(&:destroy)
super
end

def destroy #:nodoc:
redis.del key(:outcome)
alternatives.each(&:destroy)
Expand Down
10 changes: 9 additions & 1 deletion lib/vanity/experiment/base.rb
Expand Up @@ -117,7 +117,8 @@ def complete!

# Time stamp when experiment was completed.
def completed_at
Time.at(redis[key(:completed_at)].to_i)
time = redis[key(:completed_at)]
time && Time.at(time.to_i)
end

# Returns true if experiment active, false if completed.
Expand Down Expand Up @@ -145,6 +146,13 @@ def redis #:nodoc:
def save #:nodoc:
end

# Reset experiment.
def reset!
@created_at = Time.now
redis[key(:created_at)] = @created_at.to_i
redis.del key(:completed_at)
end

# Get rid of all experiment data.
def destroy
redis.del key(:created_at)
Expand Down
36 changes: 32 additions & 4 deletions test/ab_test_test.rb
Expand Up @@ -52,11 +52,19 @@ def test_requires_at_least_two_alternatives_per_experiment
alternatives "foo", "bar"
end
end

def test_returning_alternative_by_value
experiment :abcd do
alternatives :a, :b, :c, :d
end
assert_equal experiment(:abcd).alternatives[1], experiment(:abcd).alternative(:b)
assert_equal experiment(:abcd).alternatives[3], experiment(:abcd).alternative(:d)
end


# -- Running experiment --

def returns_the_same_alternative_consistently
def test_returns_the_same_alternative_consistently
experiment :foobar do
alternatives "foo", "bar"
identify { "6e98ec" }
Expand All @@ -68,7 +76,7 @@ def returns_the_same_alternative_consistently
end
end

def returns_different_alternatives_for_each_participant
def test_returns_different_alternatives_for_each_participant
experiment :foobar do
alternatives "foo", "bar"
identify { rand(1000).to_s }
Expand All @@ -78,7 +86,7 @@ def returns_different_alternatives_for_each_participant
assert_in_delta alts.select { |a| a == "foo" }.count, 500, 100 # this may fail, such is propability
end

def records_all_participants_in_each_alternative
def test_records_all_participants_in_each_alternative
ids = (Array.new(200) { |i| i.to_s } * 5).shuffle
experiment :foobar do
alternatives "foo", "bar"
Expand All @@ -90,7 +98,7 @@ def records_all_participants_in_each_alternative
assert_in_delta alts.first.participants, 100, 20
end

def records_each_converted_participant_only_once
def test_records_each_converted_participant_only_once
ids = (Array.new(100) { |i| i.to_s } * 5).shuffle
test = self
experiment :foobar do
Expand Down Expand Up @@ -123,6 +131,26 @@ def test_records_conversion_only_for_participants
assert_equal 100, alts.inject(0) { |t,a| t + a.converted }
end

def test_reset_experiment
experiment :simple do
identify { "me" }
complete_if { alternatives.map(&:converted).sum >= 1 }
outcome_is { alternative(true) }
end
experiment(:simple).choose
experiment(:simple).conversion!
refute experiment(:simple).active?
assert_equal true, experiment(:simple).outcome.value

experiment(:simple).reset!
assert experiment(:simple).active?
assert_nil experiment(:simple).outcome
assert_nil experiment(:simple).completed_at
assert_equal 0, experiment(:simple).alternatives.map(&:participants).sum
assert_equal 0, experiment(:simple).alternatives.map(&:conversions).sum
assert_equal 0, experiment(:simple).alternatives.map(&:converted).sum
end


# -- A/B helper methods --

Expand Down

0 comments on commit d8c2c10

Please sign in to comment.