Skip to content

Commit edb6214

Browse files
authored
Merge pull request #757 from splitrb/increase-test-coverage
Add specs for certain paths in preparation for a future refactor
2 parents 9dad9ae + beb13e4 commit edb6214

2 files changed

Lines changed: 116 additions & 1 deletion

File tree

spec/experiment_catalog_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,27 @@
5151
expect(subject.find("non_existent_experiment")).to be_nil
5252
end
5353
end
54+
55+
describe ".all" do
56+
it "returns every registered experiment" do
57+
subject.find_or_create("link_color", "blue", "red")
58+
subject.find_or_create("button_size", "small", "big")
59+
60+
expect(subject.all.map(&:name)).to match_array(%w[link_color button_size])
61+
end
62+
63+
it "is empty when nothing is registered" do
64+
expect(subject.all).to eq([])
65+
end
66+
end
67+
68+
describe ".all_active_first" do
69+
it "lists experiments without a winner first, each group sorted by name" do
70+
subject.find_or_create("b_active", "1", "2")
71+
subject.find_or_create("a_active", "1", "2")
72+
subject.find_or_create("c_finished", "1", "2").winner = "1"
73+
74+
expect(subject.all_active_first.map(&:name)).to eq(%w[a_active b_active c_finished])
75+
end
76+
end
5477
end

spec/helper_spec.rb

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,19 @@
127127
expect(alternative).to eq("blue")
128128
end
129129

130+
it "should allow the alternative to be forced by a cookie" do
131+
@request = build_request(cookies: { "split_override" => '{ "link_color": "red" }' })
132+
133+
expect(ab_test("link_color", "blue", "red")).to eq("red")
134+
end
135+
136+
it "should ignore a malformed split_override cookie and assign normally" do
137+
@request = build_request(cookies: { "split_override" => "{ this is not valid json" })
138+
139+
expect { ab_test("link_color", "blue", "red") }.not_to raise_error
140+
expect(["blue", "red"]).to include(ab_user["link_color"])
141+
end
142+
130143
it "should not store the split when a param forced alternative" do
131144
@params = { "ab_test" => { "link_color" => "blue" } }
132145
expect(ab_user).not_to receive(:[]=)
@@ -611,6 +624,51 @@ def should_finish_experiment(experiment_name, should_finish = true)
611624
expect(alternative.extra_info).to eql({})
612625
end
613626
end
627+
628+
context "for an experiment that the user is not participating in" do
629+
before do
630+
Split::ExperimentCatalog.find_or_create("link_color", "blue", "red")
631+
end
632+
633+
it "does not record extra data for any alternative" do
634+
ab_record_extra_info("link_color", "some_data", 10)
635+
636+
expect(Split::Alternative.new("blue", "link_color").extra_info).to eql({})
637+
expect(Split::Alternative.new("red", "link_color").extra_info).to eql({})
638+
end
639+
end
640+
641+
context "when the visitor is excluded" do
642+
before do
643+
Split::ExperimentCatalog.find_or_create("link_color", "blue", "red")
644+
@request = build_request(user_agent: "Googlebot/2.1 (+http://www.google.com/bot.html)")
645+
end
646+
647+
it "does not record extra data" do
648+
ab_record_extra_info("link_color", "some_data", 10)
649+
650+
expect(Split::Alternative.new("blue", "link_color").extra_info).to eql({})
651+
expect(Split::Alternative.new("red", "link_color").extra_info).to eql({})
652+
end
653+
end
654+
655+
context "when redis is not available" do
656+
before do
657+
expect(Split).to receive(:redis).and_raise(Errno::ECONNREFUSED)
658+
end
659+
660+
it "raises the error when db_failover is off" do
661+
Split.configuration.db_failover = false
662+
expect { ab_record_extra_info("link_color", "some_data", 10) }.to raise_error(Errno::ECONNREFUSED)
663+
end
664+
665+
it "calls db_failover_on_db_error when db_failover is on" do
666+
Split.configuration.db_failover = true
667+
expect(Split.configuration.db_failover_on_db_error).to receive(:call).with(instance_of(Errno::ECONNREFUSED))
668+
669+
expect { ab_record_extra_info("link_color", "some_data", 10) }.not_to raise_error
670+
end
671+
end
614672
end
615673

616674
describe "conversions" do
@@ -687,6 +745,40 @@ def should_finish_experiment(experiment_name, should_finish = true)
687745
end
688746
end
689747

748+
describe "ab_active_experiments" do
749+
it "returns the experiments the user is actively participating in" do
750+
Split.configure do |config|
751+
config.allow_multiple_experiments = true
752+
end
753+
alternative = ab_test("def", "4", "5", "6")
754+
another_alternative = ab_test("ghi", "7", "8", "9")
755+
756+
experiments = ab_active_experiments
757+
expect(experiments.count).to eq 2
758+
expect(experiments["def"]).to eq alternative
759+
expect(experiments["ghi"]).to eq another_alternative
760+
end
761+
762+
context "when redis is not available" do
763+
before do
764+
ab_test("link_color", "blue", "red")
765+
allow(Split).to receive(:redis).and_raise(Errno::ECONNREFUSED)
766+
end
767+
768+
it "raises the error when db_failover is off" do
769+
Split.configuration.db_failover = false
770+
expect { ab_active_experiments }.to raise_error(Errno::ECONNREFUSED)
771+
end
772+
773+
it "calls db_failover_on_db_error when db_failover is on" do
774+
Split.configuration.db_failover = true
775+
expect(Split.configuration.db_failover_on_db_error).to receive(:call).with(Errno::ECONNREFUSED)
776+
777+
expect { ab_active_experiments }.not_to raise_error
778+
end
779+
end
780+
end
781+
690782
describe "when user is a robot" do
691783
before(:each) do
692784
@request = build_request(user_agent: "Googlebot/2.1 (+http://www.google.com/bot.html)")
@@ -915,7 +1007,7 @@ def should_finish_experiment(experiment_name, should_finish = true)
9151007

9161008
context "when redis is not available" do
9171009
before(:each) do
918-
expect(Split).to receive(:redis).at_most(5).times.and_raise(Errno::ECONNREFUSED.new)
1010+
allow(Split).to receive(:redis).and_raise(Errno::ECONNREFUSED)
9191011
end
9201012

9211013
context "and db_failover config option is turned off" do

0 commit comments

Comments
 (0)