Skip to content

Commit

Permalink
spec
Browse files Browse the repository at this point in the history
  • Loading branch information
omkarmoghe committed May 21, 2024
1 parent 7547031 commit a9f8877
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/lab_coat/experiment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def select_observation(result)
end

# Runs the control and candidate and publishes the result. Always returns the result of `control`.
# It's not recommended to override this method.
# @param context [Hash] Any data needed at runtime.
# @return [Object] An `Observation` value.
def run!(**context) # rubocop:disable Metrics/MethodLength
Expand Down
25 changes: 24 additions & 1 deletion test/test_experiment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class TestExperiment < Minitest::Test
attr_reader :raised_observations, :publish_io

def enabled?
context[:num].even?
context[:num]&.even?
end

def control
Expand Down Expand Up @@ -42,6 +42,10 @@ def publishable_value(observation)
def publish!(result)
@publish_io = StringIO.new(JSON.generate(result.to_h))
end

def select_observation(result)
context[:rollout] ? result.candidate : result.control
end
end

def setup
Expand Down Expand Up @@ -99,6 +103,25 @@ def test_publish!
assert_match(/"experiment":"test-experiment"/, @experiment.publish_io.read)
end

def test_select_observation # rubocop:disable Metrics/MethodLength
# Returns control even if rollout is true if not enabled.
assert_equal(
{ result: "abc", status: :ok },
@experiment.run!(rollout: true)
)

# Returns control if rollout is false, even if enabled.
assert_equal(
{ result: "abc", status: :ok },
@experiment.run!(rollout: false, num: 2)
)

# Returns candidate when rollout is true and is enabled.
assert_nil(
@experiment.run!(rollout: true, num: 2)
)
end

def test_run!
assert_equal(
{ result: "abc", status: :ok },
Expand Down

0 comments on commit a9f8877

Please sign in to comment.