test/experiment_test.rb in verdict-0.12.0 vs test/experiment_test.rb in verdict-0.13.0

- old
+ new

@@ -412,10 +412,20 @@ e.assign(stub(id: '123')) assert e.started?, "The experiment should have started after the first assignment" end + def test_experiment_set_start_timestamp_handles_storage_that_does_not_implement_timestamps + e = Verdict::Experiment.new('starting_test') do + groups { group :all, 100 } + end + + e.storage.expects(:store_start_timestamp).raises(NotImplementedError) + + assert_nil e.send(:set_start_timestamp) + end + def test_no_storage e = Verdict::Experiment.new('starting_test') do groups { group :all, 100 } storage :none end @@ -455,9 +465,104 @@ end assert_raises(NotImplementedError) do experiment.assign("something") experiment.cleanup + end + end + + def test_is_scheduled + e = Verdict::Experiment.new(:json) do + groups do + group :a, :half + group :b, :rest + end + schedule_start_timestamp Time.new(2020, 1, 1) + schedule_end_timestamp Time.new(2020, 1, 3) + end + + # Within the interval + Timecop.freeze(Time.new(2020, 1, 2)) do + assert e.send(:is_scheduled?) + end + # Too early + Timecop.freeze(Time.new(2019, 12, 30)) do + assert !e.send(:is_scheduled?) + end + # Too late + Timecop.freeze(Time.new(2020, 1, 4)) do + assert !e.send(:is_scheduled?) + end + end + + def test_is_scheduled_no_end_timestamp + e = Verdict::Experiment.new(:json) do + groups do + group :a, :half + group :b, :rest + end + schedule_start_timestamp Time.new(2020, 1, 1) + end + + # Within the interval because there is no end date + Timecop.freeze(Time.new(2030, 1, 1)) do + assert e.send(:is_scheduled?) + end + # Too early + Timecop.freeze(Time.new(2019, 12, 30)) do + assert !e.send(:is_scheduled?) + end + end + + def test_is_scheduled_no_start_timestamp + e = Verdict::Experiment.new(:json) do + groups do + group :a, :half + group :b, :rest + end + schedule_end_timestamp Time.new(2020, 1, 3) + end + + # Within the interval because there is no start date + Timecop.freeze(Time.new(2019, 12, 30)) do + assert e.send(:is_scheduled?) + end + # Too late + Timecop.freeze(Time.new(2020, 1, 4)) do + assert !e.send(:is_scheduled?) + end + end + + def test_switch_respects_time_schedule + e = Verdict::Experiment.new('test') do + groups do + group :a, :half + group :b, :rest + end + schedule_start_timestamp Time.new(2020, 1, 1) + schedule_end_timestamp Time.new(2020, 1, 2) + end + + Timecop.freeze(Time.new(2020, 1, 3)) do + assert_nil e.switch(1) + end + end + + def test_switch_respects_time_schedule_even_after_assignment + e = Verdict::Experiment.new('test') do + groups do + group :a, :half + group :b, :rest + end + end + + assert_equal :a, e.switch(1) + + e.schedule_start_timestamp Time.new(2020, 1, 1) + e.schedule_end_timestamp Time.new(2020, 1, 2) + + Timecop.freeze(Time.new(2020, 1, 3)) do + assert_nil e.switch(1) end end private