Search is not available for this dataset
id
stringlengths 1
8
| text
stringlengths 72
9.81M
| addition_count
int64 0
10k
| commit_subject
stringlengths 0
3.7k
| deletion_count
int64 0
8.43k
| file_extension
stringlengths 0
32
| lang
stringlengths 1
94
| license
stringclasses 10
values | repo_name
stringlengths 9
59
|
---|---|---|---|---|---|---|---|---|
10072550 | <NME> dual_adapter_spec.rb
<BEF> # frozen_string_literal: true
require "spec_helper"
describe Split::Persistence::DualAdapter do
let(:context){ "some context" }
let(:just_adapter){ Class.new }
let(:selected_adapter_instance){ double }
let(:selected_adapter){
c = Class.new
expect(c).to receive(:new){ selected_adapter_instance }
c
}
let(:not_selected_adapter){
c = Class.new
expect(c).not_to receive(:new)
c
}
shared_examples_for "forwarding calls" do
it "#[]=" do
expect(selected_adapter_instance).to receive(:[]=).with('my_key', 'my_value')
expect_any_instance_of(not_selected_adapter).not_to receive(:[]=)
subject["my_key"] = "my_value"
end
it "#[]" do
expect(selected_adapter_instance).to receive(:[]).with('my_key'){'my_value'}
expect_any_instance_of(not_selected_adapter).not_to receive(:[])
expect(subject["my_key"]).to eq('my_value')
end
it "#delete" do
expect(selected_adapter_instance).to receive(:delete).with('my_key'){'my_value'}
expect_any_instance_of(not_selected_adapter).not_to receive(:delete)
expect(subject.delete("my_key")).to eq('my_value')
end
it "#keys" do
expect(selected_adapter_instance).to receive(:keys){'my_value'}
expect_any_instance_of(not_selected_adapter).not_to receive(:keys)
expect(subject.keys).to eq('my_value')
end
end
context "when logged in" do
subject {
described_class.with_config(
logged_in: lambda { |context| true },
logged_in_adapter: selected_adapter,
logged_out_adapter: not_selected_adapter
).new(context)
}
it_should_behave_like "forwarding calls"
end
context "when not logged in" do
subject {
described_class.with_config(
logged_in: lambda { |context| false },
logged_in_adapter: not_selected_adapter,
logged_out_adapter: selected_adapter
).new(context)
}
it_should_behave_like "forwarding calls"
end
describe "when errors in config" do
before{
described_class.config.clear
}
let(:some_proc){ ->{} }
it "when no logged in adapter" do
expect{
described_class.with_config(
logged_in: some_proc,
logged_out_adapter: just_adapter
).new(context)
}.to raise_error(StandardError, /:logged_in_adapter/)
end
it "when no logged out adapter" do
expect{
described_class.with_config(
logged_in: some_proc,
logged_in_adapter: just_adapter
).new(context)
}.to raise_error(StandardError, /:logged_out_adapter/)
end
it "when no logged in detector" do
expect{
described_class.with_config(
logged_in_adapter: just_adapter,
logged_out_adapter: just_adapter
).new(context)
}.to raise_error(StandardError, /:logged_in$/)
end
end
end
<MSG> Merge pull request #588 from santib/improve-dual-adapter
Improve DualAdapter
<DFF> @@ -1,102 +1,194 @@
# frozen_string_literal: true
-require "spec_helper"
+
+require 'spec_helper'
describe Split::Persistence::DualAdapter do
+ let(:context) { 'some context' }
- let(:context){ "some context" }
-
- let(:just_adapter){ Class.new }
- let(:selected_adapter_instance){ double }
- let(:selected_adapter){
- c = Class.new
- expect(c).to receive(:new){ selected_adapter_instance }
- c
- }
- let(:not_selected_adapter){
- c = Class.new
- expect(c).not_to receive(:new)
- c
- }
-
- shared_examples_for "forwarding calls" do
- it "#[]=" do
- expect(selected_adapter_instance).to receive(:[]=).with('my_key', 'my_value')
- expect_any_instance_of(not_selected_adapter).not_to receive(:[]=)
- subject["my_key"] = "my_value"
- end
+ let(:logged_in_adapter_instance) { double }
+ let(:logged_in_adapter) do
+ Class.new.tap { |c| allow(c).to receive(:new) { logged_in_adapter_instance } }
+ end
+ let(:logged_out_adapter_instance) { double }
+ let(:logged_out_adapter) do
+ Class.new.tap { |c| allow(c).to receive(:new) { logged_out_adapter_instance } }
+ end
- it "#[]" do
- expect(selected_adapter_instance).to receive(:[]).with('my_key'){'my_value'}
- expect_any_instance_of(not_selected_adapter).not_to receive(:[])
- expect(subject["my_key"]).to eq('my_value')
- end
+ context 'when fallback_to_logged_out_adapter is false' do
+ context 'when logged in' do
+ subject do
+ described_class.with_config(
+ logged_in: lambda { |context| true },
+ logged_in_adapter: logged_in_adapter,
+ logged_out_adapter: logged_out_adapter,
+ fallback_to_logged_out_adapter: false
+ ).new(context)
+ end
+
+ it '#[]=' do
+ expect(logged_in_adapter_instance).to receive(:[]=).with('my_key', 'my_value')
+ expect_any_instance_of(logged_out_adapter).not_to receive(:[]=)
+ subject['my_key'] = 'my_value'
+ end
+
+ it '#[]' do
+ expect(logged_in_adapter_instance).to receive(:[]).with('my_key') { 'my_value' }
+ expect_any_instance_of(logged_out_adapter).not_to receive(:[])
+ expect(subject['my_key']).to eq('my_value')
+ end
- it "#delete" do
- expect(selected_adapter_instance).to receive(:delete).with('my_key'){'my_value'}
- expect_any_instance_of(not_selected_adapter).not_to receive(:delete)
- expect(subject.delete("my_key")).to eq('my_value')
+ it '#delete' do
+ expect(logged_in_adapter_instance).to receive(:delete).with('my_key') { 'my_value' }
+ expect_any_instance_of(logged_out_adapter).not_to receive(:delete)
+ expect(subject.delete('my_key')).to eq('my_value')
+ end
+
+ it '#keys' do
+ expect(logged_in_adapter_instance).to receive(:keys) { ['my_value'] }
+ expect_any_instance_of(logged_out_adapter).not_to receive(:keys)
+ expect(subject.keys).to eq(['my_value'])
+ end
end
- it "#keys" do
- expect(selected_adapter_instance).to receive(:keys){'my_value'}
- expect_any_instance_of(not_selected_adapter).not_to receive(:keys)
- expect(subject.keys).to eq('my_value')
+ context 'when logged out' do
+ subject do
+ described_class.with_config(
+ logged_in: lambda { |context| false },
+ logged_in_adapter: logged_in_adapter,
+ logged_out_adapter: logged_out_adapter,
+ fallback_to_logged_out_adapter: false
+ ).new(context)
+ end
+
+ it '#[]=' do
+ expect_any_instance_of(logged_in_adapter).not_to receive(:[]=)
+ expect(logged_out_adapter_instance).to receive(:[]=).with('my_key', 'my_value')
+ subject['my_key'] = 'my_value'
+ end
+
+ it '#[]' do
+ expect_any_instance_of(logged_in_adapter).not_to receive(:[])
+ expect(logged_out_adapter_instance).to receive(:[]).with('my_key') { 'my_value' }
+ expect(subject['my_key']).to eq('my_value')
+ end
+
+ it '#delete' do
+ expect_any_instance_of(logged_in_adapter).not_to receive(:delete)
+ expect(logged_out_adapter_instance).to receive(:delete).with('my_key') { 'my_value' }
+ expect(subject.delete('my_key')).to eq('my_value')
+ end
+
+ it '#keys' do
+ expect_any_instance_of(logged_in_adapter).not_to receive(:keys)
+ expect(logged_out_adapter_instance).to receive(:keys) { ['my_value', 'my_value2'] }
+ expect(subject.keys).to eq(['my_value', 'my_value2'])
+ end
end
end
- context "when logged in" do
- subject {
- described_class.with_config(
- logged_in: lambda { |context| true },
- logged_in_adapter: selected_adapter,
- logged_out_adapter: not_selected_adapter
+ context 'when fallback_to_logged_out_adapter is true' do
+ context 'when logged in' do
+ subject do
+ described_class.with_config(
+ logged_in: lambda { |context| true },
+ logged_in_adapter: logged_in_adapter,
+ logged_out_adapter: logged_out_adapter,
+ fallback_to_logged_out_adapter: true
).new(context)
- }
+ end
- it_should_behave_like "forwarding calls"
- end
+ it '#[]=' do
+ expect(logged_in_adapter_instance).to receive(:[]=).with('my_key', 'my_value')
+ expect(logged_out_adapter_instance).to receive(:[]=).with('my_key', 'my_value')
+ expect(logged_out_adapter_instance).to receive(:[]).with('my_key') { nil }
+ subject['my_key'] = 'my_value'
+ end
+
+ it '#[]' do
+ expect(logged_in_adapter_instance).to receive(:[]).with('my_key') { 'my_value' }
+ expect_any_instance_of(logged_out_adapter).not_to receive(:[])
+ expect(subject['my_key']).to eq('my_value')
+ end
+
+ it '#delete' do
+ expect(logged_in_adapter_instance).to receive(:delete).with('my_key') { 'my_value' }
+ expect(logged_out_adapter_instance).to receive(:delete).with('my_key') { 'my_value' }
+ expect(subject.delete('my_key')).to eq('my_value')
+ end
+
+ it '#keys' do
+ expect(logged_in_adapter_instance).to receive(:keys) { ['my_value'] }
+ expect(logged_out_adapter_instance).to receive(:keys) { ['my_value', 'my_value2'] }
+ expect(subject.keys).to eq(['my_value', 'my_value2'])
+ end
+ end
- context "when not logged in" do
- subject {
- described_class.with_config(
- logged_in: lambda { |context| false },
- logged_in_adapter: not_selected_adapter,
- logged_out_adapter: selected_adapter
+ context 'when logged out' do
+ subject do
+ described_class.with_config(
+ logged_in: lambda { |context| false },
+ logged_in_adapter: logged_in_adapter,
+ logged_out_adapter: logged_out_adapter,
+ fallback_to_logged_out_adapter: true
).new(context)
- }
+ end
+
+ it '#[]=' do
+ expect_any_instance_of(logged_in_adapter).not_to receive(:[]=)
+ expect(logged_out_adapter_instance).to receive(:[]=).with('my_key', 'my_value')
+ expect(logged_out_adapter_instance).to receive(:[]).with('my_key') { nil }
+ subject['my_key'] = 'my_value'
+ end
+
+ it '#[]' do
+ expect_any_instance_of(logged_in_adapter).not_to receive(:[])
+ expect(logged_out_adapter_instance).to receive(:[]).with('my_key') { 'my_value' }
+ expect(subject['my_key']).to eq('my_value')
+ end
+
+ it '#delete' do
+ expect(logged_in_adapter_instance).to receive(:delete).with('my_key') { 'my_value' }
+ expect(logged_out_adapter_instance).to receive(:delete).with('my_key') { 'my_value' }
+ expect(subject.delete('my_key')).to eq('my_value')
+ end
- it_should_behave_like "forwarding calls"
+ it '#keys' do
+ expect(logged_in_adapter_instance).to receive(:keys) { ['my_value'] }
+ expect(logged_out_adapter_instance).to receive(:keys) { ['my_value', 'my_value2'] }
+ expect(subject.keys).to eq(['my_value', 'my_value2'])
+ end
+ end
end
- describe "when errors in config" do
- before{
- described_class.config.clear
- }
- let(:some_proc){ ->{} }
- it "when no logged in adapter" do
+ describe 'when errors in config' do
+ before { described_class.config.clear }
+ let(:some_proc) { ->{} }
+
+ it 'when no logged in adapter' do
expect{
described_class.with_config(
logged_in: some_proc,
- logged_out_adapter: just_adapter
- ).new(context)
+ logged_out_adapter: logged_out_adapter
+ ).new(context)
}.to raise_error(StandardError, /:logged_in_adapter/)
end
- it "when no logged out adapter" do
+
+ it 'when no logged out adapter' do
expect{
described_class.with_config(
logged_in: some_proc,
- logged_in_adapter: just_adapter
- ).new(context)
+ logged_in_adapter: logged_in_adapter
+ ).new(context)
}.to raise_error(StandardError, /:logged_out_adapter/)
end
- it "when no logged in detector" do
+
+ it 'when no logged in detector' do
expect{
described_class.with_config(
- logged_in_adapter: just_adapter,
- logged_out_adapter: just_adapter
- ).new(context)
+ logged_in_adapter: logged_in_adapter,
+ logged_out_adapter: logged_out_adapter
+ ).new(context)
}.to raise_error(StandardError, /:logged_in$/)
end
end
-
end
| 160 | Merge pull request #588 from santib/improve-dual-adapter | 68 | .rb | rb | mit | splitrb/split |
10072551 | <NME> dual_adapter_spec.rb
<BEF> # frozen_string_literal: true
require "spec_helper"
describe Split::Persistence::DualAdapter do
let(:context){ "some context" }
let(:just_adapter){ Class.new }
let(:selected_adapter_instance){ double }
let(:selected_adapter){
c = Class.new
expect(c).to receive(:new){ selected_adapter_instance }
c
}
let(:not_selected_adapter){
c = Class.new
expect(c).not_to receive(:new)
c
}
shared_examples_for "forwarding calls" do
it "#[]=" do
expect(selected_adapter_instance).to receive(:[]=).with('my_key', 'my_value')
expect_any_instance_of(not_selected_adapter).not_to receive(:[]=)
subject["my_key"] = "my_value"
end
it "#[]" do
expect(selected_adapter_instance).to receive(:[]).with('my_key'){'my_value'}
expect_any_instance_of(not_selected_adapter).not_to receive(:[])
expect(subject["my_key"]).to eq('my_value')
end
it "#delete" do
expect(selected_adapter_instance).to receive(:delete).with('my_key'){'my_value'}
expect_any_instance_of(not_selected_adapter).not_to receive(:delete)
expect(subject.delete("my_key")).to eq('my_value')
end
it "#keys" do
expect(selected_adapter_instance).to receive(:keys){'my_value'}
expect_any_instance_of(not_selected_adapter).not_to receive(:keys)
expect(subject.keys).to eq('my_value')
end
end
context "when logged in" do
subject {
described_class.with_config(
logged_in: lambda { |context| true },
logged_in_adapter: selected_adapter,
logged_out_adapter: not_selected_adapter
).new(context)
}
it_should_behave_like "forwarding calls"
end
context "when not logged in" do
subject {
described_class.with_config(
logged_in: lambda { |context| false },
logged_in_adapter: not_selected_adapter,
logged_out_adapter: selected_adapter
).new(context)
}
it_should_behave_like "forwarding calls"
end
describe "when errors in config" do
before{
described_class.config.clear
}
let(:some_proc){ ->{} }
it "when no logged in adapter" do
expect{
described_class.with_config(
logged_in: some_proc,
logged_out_adapter: just_adapter
).new(context)
}.to raise_error(StandardError, /:logged_in_adapter/)
end
it "when no logged out adapter" do
expect{
described_class.with_config(
logged_in: some_proc,
logged_in_adapter: just_adapter
).new(context)
}.to raise_error(StandardError, /:logged_out_adapter/)
end
it "when no logged in detector" do
expect{
described_class.with_config(
logged_in_adapter: just_adapter,
logged_out_adapter: just_adapter
).new(context)
}.to raise_error(StandardError, /:logged_in$/)
end
end
end
<MSG> Merge pull request #588 from santib/improve-dual-adapter
Improve DualAdapter
<DFF> @@ -1,102 +1,194 @@
# frozen_string_literal: true
-require "spec_helper"
+
+require 'spec_helper'
describe Split::Persistence::DualAdapter do
+ let(:context) { 'some context' }
- let(:context){ "some context" }
-
- let(:just_adapter){ Class.new }
- let(:selected_adapter_instance){ double }
- let(:selected_adapter){
- c = Class.new
- expect(c).to receive(:new){ selected_adapter_instance }
- c
- }
- let(:not_selected_adapter){
- c = Class.new
- expect(c).not_to receive(:new)
- c
- }
-
- shared_examples_for "forwarding calls" do
- it "#[]=" do
- expect(selected_adapter_instance).to receive(:[]=).with('my_key', 'my_value')
- expect_any_instance_of(not_selected_adapter).not_to receive(:[]=)
- subject["my_key"] = "my_value"
- end
+ let(:logged_in_adapter_instance) { double }
+ let(:logged_in_adapter) do
+ Class.new.tap { |c| allow(c).to receive(:new) { logged_in_adapter_instance } }
+ end
+ let(:logged_out_adapter_instance) { double }
+ let(:logged_out_adapter) do
+ Class.new.tap { |c| allow(c).to receive(:new) { logged_out_adapter_instance } }
+ end
- it "#[]" do
- expect(selected_adapter_instance).to receive(:[]).with('my_key'){'my_value'}
- expect_any_instance_of(not_selected_adapter).not_to receive(:[])
- expect(subject["my_key"]).to eq('my_value')
- end
+ context 'when fallback_to_logged_out_adapter is false' do
+ context 'when logged in' do
+ subject do
+ described_class.with_config(
+ logged_in: lambda { |context| true },
+ logged_in_adapter: logged_in_adapter,
+ logged_out_adapter: logged_out_adapter,
+ fallback_to_logged_out_adapter: false
+ ).new(context)
+ end
+
+ it '#[]=' do
+ expect(logged_in_adapter_instance).to receive(:[]=).with('my_key', 'my_value')
+ expect_any_instance_of(logged_out_adapter).not_to receive(:[]=)
+ subject['my_key'] = 'my_value'
+ end
+
+ it '#[]' do
+ expect(logged_in_adapter_instance).to receive(:[]).with('my_key') { 'my_value' }
+ expect_any_instance_of(logged_out_adapter).not_to receive(:[])
+ expect(subject['my_key']).to eq('my_value')
+ end
- it "#delete" do
- expect(selected_adapter_instance).to receive(:delete).with('my_key'){'my_value'}
- expect_any_instance_of(not_selected_adapter).not_to receive(:delete)
- expect(subject.delete("my_key")).to eq('my_value')
+ it '#delete' do
+ expect(logged_in_adapter_instance).to receive(:delete).with('my_key') { 'my_value' }
+ expect_any_instance_of(logged_out_adapter).not_to receive(:delete)
+ expect(subject.delete('my_key')).to eq('my_value')
+ end
+
+ it '#keys' do
+ expect(logged_in_adapter_instance).to receive(:keys) { ['my_value'] }
+ expect_any_instance_of(logged_out_adapter).not_to receive(:keys)
+ expect(subject.keys).to eq(['my_value'])
+ end
end
- it "#keys" do
- expect(selected_adapter_instance).to receive(:keys){'my_value'}
- expect_any_instance_of(not_selected_adapter).not_to receive(:keys)
- expect(subject.keys).to eq('my_value')
+ context 'when logged out' do
+ subject do
+ described_class.with_config(
+ logged_in: lambda { |context| false },
+ logged_in_adapter: logged_in_adapter,
+ logged_out_adapter: logged_out_adapter,
+ fallback_to_logged_out_adapter: false
+ ).new(context)
+ end
+
+ it '#[]=' do
+ expect_any_instance_of(logged_in_adapter).not_to receive(:[]=)
+ expect(logged_out_adapter_instance).to receive(:[]=).with('my_key', 'my_value')
+ subject['my_key'] = 'my_value'
+ end
+
+ it '#[]' do
+ expect_any_instance_of(logged_in_adapter).not_to receive(:[])
+ expect(logged_out_adapter_instance).to receive(:[]).with('my_key') { 'my_value' }
+ expect(subject['my_key']).to eq('my_value')
+ end
+
+ it '#delete' do
+ expect_any_instance_of(logged_in_adapter).not_to receive(:delete)
+ expect(logged_out_adapter_instance).to receive(:delete).with('my_key') { 'my_value' }
+ expect(subject.delete('my_key')).to eq('my_value')
+ end
+
+ it '#keys' do
+ expect_any_instance_of(logged_in_adapter).not_to receive(:keys)
+ expect(logged_out_adapter_instance).to receive(:keys) { ['my_value', 'my_value2'] }
+ expect(subject.keys).to eq(['my_value', 'my_value2'])
+ end
end
end
- context "when logged in" do
- subject {
- described_class.with_config(
- logged_in: lambda { |context| true },
- logged_in_adapter: selected_adapter,
- logged_out_adapter: not_selected_adapter
+ context 'when fallback_to_logged_out_adapter is true' do
+ context 'when logged in' do
+ subject do
+ described_class.with_config(
+ logged_in: lambda { |context| true },
+ logged_in_adapter: logged_in_adapter,
+ logged_out_adapter: logged_out_adapter,
+ fallback_to_logged_out_adapter: true
).new(context)
- }
+ end
- it_should_behave_like "forwarding calls"
- end
+ it '#[]=' do
+ expect(logged_in_adapter_instance).to receive(:[]=).with('my_key', 'my_value')
+ expect(logged_out_adapter_instance).to receive(:[]=).with('my_key', 'my_value')
+ expect(logged_out_adapter_instance).to receive(:[]).with('my_key') { nil }
+ subject['my_key'] = 'my_value'
+ end
+
+ it '#[]' do
+ expect(logged_in_adapter_instance).to receive(:[]).with('my_key') { 'my_value' }
+ expect_any_instance_of(logged_out_adapter).not_to receive(:[])
+ expect(subject['my_key']).to eq('my_value')
+ end
+
+ it '#delete' do
+ expect(logged_in_adapter_instance).to receive(:delete).with('my_key') { 'my_value' }
+ expect(logged_out_adapter_instance).to receive(:delete).with('my_key') { 'my_value' }
+ expect(subject.delete('my_key')).to eq('my_value')
+ end
+
+ it '#keys' do
+ expect(logged_in_adapter_instance).to receive(:keys) { ['my_value'] }
+ expect(logged_out_adapter_instance).to receive(:keys) { ['my_value', 'my_value2'] }
+ expect(subject.keys).to eq(['my_value', 'my_value2'])
+ end
+ end
- context "when not logged in" do
- subject {
- described_class.with_config(
- logged_in: lambda { |context| false },
- logged_in_adapter: not_selected_adapter,
- logged_out_adapter: selected_adapter
+ context 'when logged out' do
+ subject do
+ described_class.with_config(
+ logged_in: lambda { |context| false },
+ logged_in_adapter: logged_in_adapter,
+ logged_out_adapter: logged_out_adapter,
+ fallback_to_logged_out_adapter: true
).new(context)
- }
+ end
+
+ it '#[]=' do
+ expect_any_instance_of(logged_in_adapter).not_to receive(:[]=)
+ expect(logged_out_adapter_instance).to receive(:[]=).with('my_key', 'my_value')
+ expect(logged_out_adapter_instance).to receive(:[]).with('my_key') { nil }
+ subject['my_key'] = 'my_value'
+ end
+
+ it '#[]' do
+ expect_any_instance_of(logged_in_adapter).not_to receive(:[])
+ expect(logged_out_adapter_instance).to receive(:[]).with('my_key') { 'my_value' }
+ expect(subject['my_key']).to eq('my_value')
+ end
+
+ it '#delete' do
+ expect(logged_in_adapter_instance).to receive(:delete).with('my_key') { 'my_value' }
+ expect(logged_out_adapter_instance).to receive(:delete).with('my_key') { 'my_value' }
+ expect(subject.delete('my_key')).to eq('my_value')
+ end
- it_should_behave_like "forwarding calls"
+ it '#keys' do
+ expect(logged_in_adapter_instance).to receive(:keys) { ['my_value'] }
+ expect(logged_out_adapter_instance).to receive(:keys) { ['my_value', 'my_value2'] }
+ expect(subject.keys).to eq(['my_value', 'my_value2'])
+ end
+ end
end
- describe "when errors in config" do
- before{
- described_class.config.clear
- }
- let(:some_proc){ ->{} }
- it "when no logged in adapter" do
+ describe 'when errors in config' do
+ before { described_class.config.clear }
+ let(:some_proc) { ->{} }
+
+ it 'when no logged in adapter' do
expect{
described_class.with_config(
logged_in: some_proc,
- logged_out_adapter: just_adapter
- ).new(context)
+ logged_out_adapter: logged_out_adapter
+ ).new(context)
}.to raise_error(StandardError, /:logged_in_adapter/)
end
- it "when no logged out adapter" do
+
+ it 'when no logged out adapter' do
expect{
described_class.with_config(
logged_in: some_proc,
- logged_in_adapter: just_adapter
- ).new(context)
+ logged_in_adapter: logged_in_adapter
+ ).new(context)
}.to raise_error(StandardError, /:logged_out_adapter/)
end
- it "when no logged in detector" do
+
+ it 'when no logged in detector' do
expect{
described_class.with_config(
- logged_in_adapter: just_adapter,
- logged_out_adapter: just_adapter
- ).new(context)
+ logged_in_adapter: logged_in_adapter,
+ logged_out_adapter: logged_out_adapter
+ ).new(context)
}.to raise_error(StandardError, /:logged_in$/)
end
end
-
end
| 160 | Merge pull request #588 from santib/improve-dual-adapter | 68 | .rb | rb | mit | splitrb/split |
10072552 | <NME> dual_adapter_spec.rb
<BEF> # frozen_string_literal: true
require "spec_helper"
describe Split::Persistence::DualAdapter do
let(:context){ "some context" }
let(:just_adapter){ Class.new }
let(:selected_adapter_instance){ double }
let(:selected_adapter){
c = Class.new
expect(c).to receive(:new){ selected_adapter_instance }
c
}
let(:not_selected_adapter){
c = Class.new
expect(c).not_to receive(:new)
c
}
shared_examples_for "forwarding calls" do
it "#[]=" do
expect(selected_adapter_instance).to receive(:[]=).with('my_key', 'my_value')
expect_any_instance_of(not_selected_adapter).not_to receive(:[]=)
subject["my_key"] = "my_value"
end
it "#[]" do
expect(selected_adapter_instance).to receive(:[]).with('my_key'){'my_value'}
expect_any_instance_of(not_selected_adapter).not_to receive(:[])
expect(subject["my_key"]).to eq('my_value')
end
it "#delete" do
expect(selected_adapter_instance).to receive(:delete).with('my_key'){'my_value'}
expect_any_instance_of(not_selected_adapter).not_to receive(:delete)
expect(subject.delete("my_key")).to eq('my_value')
end
it "#keys" do
expect(selected_adapter_instance).to receive(:keys){'my_value'}
expect_any_instance_of(not_selected_adapter).not_to receive(:keys)
expect(subject.keys).to eq('my_value')
end
end
context "when logged in" do
subject {
described_class.with_config(
logged_in: lambda { |context| true },
logged_in_adapter: selected_adapter,
logged_out_adapter: not_selected_adapter
).new(context)
}
it_should_behave_like "forwarding calls"
end
context "when not logged in" do
subject {
described_class.with_config(
logged_in: lambda { |context| false },
logged_in_adapter: not_selected_adapter,
logged_out_adapter: selected_adapter
).new(context)
}
it_should_behave_like "forwarding calls"
end
describe "when errors in config" do
before{
described_class.config.clear
}
let(:some_proc){ ->{} }
it "when no logged in adapter" do
expect{
described_class.with_config(
logged_in: some_proc,
logged_out_adapter: just_adapter
).new(context)
}.to raise_error(StandardError, /:logged_in_adapter/)
end
it "when no logged out adapter" do
expect{
described_class.with_config(
logged_in: some_proc,
logged_in_adapter: just_adapter
).new(context)
}.to raise_error(StandardError, /:logged_out_adapter/)
end
it "when no logged in detector" do
expect{
described_class.with_config(
logged_in_adapter: just_adapter,
logged_out_adapter: just_adapter
).new(context)
}.to raise_error(StandardError, /:logged_in$/)
end
end
end
<MSG> Merge pull request #588 from santib/improve-dual-adapter
Improve DualAdapter
<DFF> @@ -1,102 +1,194 @@
# frozen_string_literal: true
-require "spec_helper"
+
+require 'spec_helper'
describe Split::Persistence::DualAdapter do
+ let(:context) { 'some context' }
- let(:context){ "some context" }
-
- let(:just_adapter){ Class.new }
- let(:selected_adapter_instance){ double }
- let(:selected_adapter){
- c = Class.new
- expect(c).to receive(:new){ selected_adapter_instance }
- c
- }
- let(:not_selected_adapter){
- c = Class.new
- expect(c).not_to receive(:new)
- c
- }
-
- shared_examples_for "forwarding calls" do
- it "#[]=" do
- expect(selected_adapter_instance).to receive(:[]=).with('my_key', 'my_value')
- expect_any_instance_of(not_selected_adapter).not_to receive(:[]=)
- subject["my_key"] = "my_value"
- end
+ let(:logged_in_adapter_instance) { double }
+ let(:logged_in_adapter) do
+ Class.new.tap { |c| allow(c).to receive(:new) { logged_in_adapter_instance } }
+ end
+ let(:logged_out_adapter_instance) { double }
+ let(:logged_out_adapter) do
+ Class.new.tap { |c| allow(c).to receive(:new) { logged_out_adapter_instance } }
+ end
- it "#[]" do
- expect(selected_adapter_instance).to receive(:[]).with('my_key'){'my_value'}
- expect_any_instance_of(not_selected_adapter).not_to receive(:[])
- expect(subject["my_key"]).to eq('my_value')
- end
+ context 'when fallback_to_logged_out_adapter is false' do
+ context 'when logged in' do
+ subject do
+ described_class.with_config(
+ logged_in: lambda { |context| true },
+ logged_in_adapter: logged_in_adapter,
+ logged_out_adapter: logged_out_adapter,
+ fallback_to_logged_out_adapter: false
+ ).new(context)
+ end
+
+ it '#[]=' do
+ expect(logged_in_adapter_instance).to receive(:[]=).with('my_key', 'my_value')
+ expect_any_instance_of(logged_out_adapter).not_to receive(:[]=)
+ subject['my_key'] = 'my_value'
+ end
+
+ it '#[]' do
+ expect(logged_in_adapter_instance).to receive(:[]).with('my_key') { 'my_value' }
+ expect_any_instance_of(logged_out_adapter).not_to receive(:[])
+ expect(subject['my_key']).to eq('my_value')
+ end
- it "#delete" do
- expect(selected_adapter_instance).to receive(:delete).with('my_key'){'my_value'}
- expect_any_instance_of(not_selected_adapter).not_to receive(:delete)
- expect(subject.delete("my_key")).to eq('my_value')
+ it '#delete' do
+ expect(logged_in_adapter_instance).to receive(:delete).with('my_key') { 'my_value' }
+ expect_any_instance_of(logged_out_adapter).not_to receive(:delete)
+ expect(subject.delete('my_key')).to eq('my_value')
+ end
+
+ it '#keys' do
+ expect(logged_in_adapter_instance).to receive(:keys) { ['my_value'] }
+ expect_any_instance_of(logged_out_adapter).not_to receive(:keys)
+ expect(subject.keys).to eq(['my_value'])
+ end
end
- it "#keys" do
- expect(selected_adapter_instance).to receive(:keys){'my_value'}
- expect_any_instance_of(not_selected_adapter).not_to receive(:keys)
- expect(subject.keys).to eq('my_value')
+ context 'when logged out' do
+ subject do
+ described_class.with_config(
+ logged_in: lambda { |context| false },
+ logged_in_adapter: logged_in_adapter,
+ logged_out_adapter: logged_out_adapter,
+ fallback_to_logged_out_adapter: false
+ ).new(context)
+ end
+
+ it '#[]=' do
+ expect_any_instance_of(logged_in_adapter).not_to receive(:[]=)
+ expect(logged_out_adapter_instance).to receive(:[]=).with('my_key', 'my_value')
+ subject['my_key'] = 'my_value'
+ end
+
+ it '#[]' do
+ expect_any_instance_of(logged_in_adapter).not_to receive(:[])
+ expect(logged_out_adapter_instance).to receive(:[]).with('my_key') { 'my_value' }
+ expect(subject['my_key']).to eq('my_value')
+ end
+
+ it '#delete' do
+ expect_any_instance_of(logged_in_adapter).not_to receive(:delete)
+ expect(logged_out_adapter_instance).to receive(:delete).with('my_key') { 'my_value' }
+ expect(subject.delete('my_key')).to eq('my_value')
+ end
+
+ it '#keys' do
+ expect_any_instance_of(logged_in_adapter).not_to receive(:keys)
+ expect(logged_out_adapter_instance).to receive(:keys) { ['my_value', 'my_value2'] }
+ expect(subject.keys).to eq(['my_value', 'my_value2'])
+ end
end
end
- context "when logged in" do
- subject {
- described_class.with_config(
- logged_in: lambda { |context| true },
- logged_in_adapter: selected_adapter,
- logged_out_adapter: not_selected_adapter
+ context 'when fallback_to_logged_out_adapter is true' do
+ context 'when logged in' do
+ subject do
+ described_class.with_config(
+ logged_in: lambda { |context| true },
+ logged_in_adapter: logged_in_adapter,
+ logged_out_adapter: logged_out_adapter,
+ fallback_to_logged_out_adapter: true
).new(context)
- }
+ end
- it_should_behave_like "forwarding calls"
- end
+ it '#[]=' do
+ expect(logged_in_adapter_instance).to receive(:[]=).with('my_key', 'my_value')
+ expect(logged_out_adapter_instance).to receive(:[]=).with('my_key', 'my_value')
+ expect(logged_out_adapter_instance).to receive(:[]).with('my_key') { nil }
+ subject['my_key'] = 'my_value'
+ end
+
+ it '#[]' do
+ expect(logged_in_adapter_instance).to receive(:[]).with('my_key') { 'my_value' }
+ expect_any_instance_of(logged_out_adapter).not_to receive(:[])
+ expect(subject['my_key']).to eq('my_value')
+ end
+
+ it '#delete' do
+ expect(logged_in_adapter_instance).to receive(:delete).with('my_key') { 'my_value' }
+ expect(logged_out_adapter_instance).to receive(:delete).with('my_key') { 'my_value' }
+ expect(subject.delete('my_key')).to eq('my_value')
+ end
+
+ it '#keys' do
+ expect(logged_in_adapter_instance).to receive(:keys) { ['my_value'] }
+ expect(logged_out_adapter_instance).to receive(:keys) { ['my_value', 'my_value2'] }
+ expect(subject.keys).to eq(['my_value', 'my_value2'])
+ end
+ end
- context "when not logged in" do
- subject {
- described_class.with_config(
- logged_in: lambda { |context| false },
- logged_in_adapter: not_selected_adapter,
- logged_out_adapter: selected_adapter
+ context 'when logged out' do
+ subject do
+ described_class.with_config(
+ logged_in: lambda { |context| false },
+ logged_in_adapter: logged_in_adapter,
+ logged_out_adapter: logged_out_adapter,
+ fallback_to_logged_out_adapter: true
).new(context)
- }
+ end
+
+ it '#[]=' do
+ expect_any_instance_of(logged_in_adapter).not_to receive(:[]=)
+ expect(logged_out_adapter_instance).to receive(:[]=).with('my_key', 'my_value')
+ expect(logged_out_adapter_instance).to receive(:[]).with('my_key') { nil }
+ subject['my_key'] = 'my_value'
+ end
+
+ it '#[]' do
+ expect_any_instance_of(logged_in_adapter).not_to receive(:[])
+ expect(logged_out_adapter_instance).to receive(:[]).with('my_key') { 'my_value' }
+ expect(subject['my_key']).to eq('my_value')
+ end
+
+ it '#delete' do
+ expect(logged_in_adapter_instance).to receive(:delete).with('my_key') { 'my_value' }
+ expect(logged_out_adapter_instance).to receive(:delete).with('my_key') { 'my_value' }
+ expect(subject.delete('my_key')).to eq('my_value')
+ end
- it_should_behave_like "forwarding calls"
+ it '#keys' do
+ expect(logged_in_adapter_instance).to receive(:keys) { ['my_value'] }
+ expect(logged_out_adapter_instance).to receive(:keys) { ['my_value', 'my_value2'] }
+ expect(subject.keys).to eq(['my_value', 'my_value2'])
+ end
+ end
end
- describe "when errors in config" do
- before{
- described_class.config.clear
- }
- let(:some_proc){ ->{} }
- it "when no logged in adapter" do
+ describe 'when errors in config' do
+ before { described_class.config.clear }
+ let(:some_proc) { ->{} }
+
+ it 'when no logged in adapter' do
expect{
described_class.with_config(
logged_in: some_proc,
- logged_out_adapter: just_adapter
- ).new(context)
+ logged_out_adapter: logged_out_adapter
+ ).new(context)
}.to raise_error(StandardError, /:logged_in_adapter/)
end
- it "when no logged out adapter" do
+
+ it 'when no logged out adapter' do
expect{
described_class.with_config(
logged_in: some_proc,
- logged_in_adapter: just_adapter
- ).new(context)
+ logged_in_adapter: logged_in_adapter
+ ).new(context)
}.to raise_error(StandardError, /:logged_out_adapter/)
end
- it "when no logged in detector" do
+
+ it 'when no logged in detector' do
expect{
described_class.with_config(
- logged_in_adapter: just_adapter,
- logged_out_adapter: just_adapter
- ).new(context)
+ logged_in_adapter: logged_in_adapter,
+ logged_out_adapter: logged_out_adapter
+ ).new(context)
}.to raise_error(StandardError, /:logged_in$/)
end
end
-
end
| 160 | Merge pull request #588 from santib/improve-dual-adapter | 68 | .rb | rb | mit | splitrb/split |
10072553 | <NME> trial_spec.rb
<BEF> # frozen_string_literal: true
require "spec_helper"
require "split/trial"
describe Split::Trial do
let(:user) { mock_user }
let(:alternatives) { ["basket", "cart"] }
let(:experiment) do
Split::Experiment.new("basket_text", alternatives: alternatives).save
end
it "should be initializeable" do
experiment = double("experiment")
alternative = double("alternative", kind_of?: Split::Alternative)
trial = Split::Trial.new(experiment: experiment, alternative: alternative)
expect(trial.experiment).to eq(experiment)
expect(trial.alternative).to eq(alternative)
end
describe "alternative" do
it "should use the alternative if specified" do
alternative = double("alternative", kind_of?: Split::Alternative)
trial = Split::Trial.new(experiment: double("experiment"),
alternative: alternative, user: user)
expect(trial).not_to receive(:choose)
expect(trial.alternative).to eq(alternative)
end
it "should load the alternative when the alternative name is set" do
experiment = Split::Experiment.new("basket_text", alternatives: ["basket", "cart"])
experiment.save
trial = Split::Trial.new(experiment: experiment, alternative: "basket")
expect(trial.alternative.name).to eq("basket")
end
end
describe "metadata" do
let(:metadata) { Hash[alternatives.map { |k| [k, "Metadata for #{k}"] }] }
let(:experiment) do
Split::Experiment.new("basket_text", alternatives: alternatives, metadata: metadata).save
end
it "has metadata on each trial" do
trial = Split::Trial.new(experiment: experiment, user: user, metadata: metadata["cart"],
override: "cart")
expect(trial.metadata).to eq(metadata["cart"])
end
it "has metadata on each trial from the experiment" do
trial = Split::Trial.new(experiment: experiment, user: user)
trial.choose!
expect(trial.metadata).to eq(metadata[trial.alternative.name])
expect(trial.metadata).to match(/#{trial.alternative.name}/)
end
end
describe "#choose!" do
let(:context) { double(on_trial_callback: "test callback") }
let(:trial) do
Split::Trial.new(user: user, experiment: experiment)
end
shared_examples_for "a trial with callbacks" do
it "does not run if on_trial callback is not respondable" do
Split.configuration.on_trial = :foo
allow(context).to receive(:respond_to?).with(:foo, true).and_return false
expect(context).to_not receive(:foo)
trial.choose! context
end
it "runs on_trial callback" do
Split.configuration.on_trial = :on_trial_callback
expect(context).to receive(:on_trial_callback)
trial.choose! context
end
it "does not run nil on_trial callback" do
Split.configuration.on_trial = nil
expect(context).not_to receive(:on_trial_callback)
trial.choose! context
end
end
def expect_alternative(trial, alternative_name)
3.times do
trial.choose! context
expect(alternative_name).to include(trial.alternative.name)
end
end
context "when override is present" do
let(:override) { "cart" }
let(:trial) do
Split::Trial.new(user: user, experiment: experiment, override: override)
end
it_behaves_like "a trial with callbacks"
it "picks the override" do
expect(experiment).to_not receive(:next_alternative)
expect_alternative(trial, override)
end
context "when alternative doesn't exist" do
let(:override) { nil }
it "falls back on next_alternative" do
expect(experiment).to receive(:next_alternative).and_call_original
expect_alternative(trial, alternatives)
end
end
end
context "when disabled option is true" do
let(:trial) do
Split::Trial.new(user: user, experiment: experiment, disabled: true)
end
it "picks the control", :aggregate_failures do
Split.configuration.on_trial = :on_trial_callback
expect(experiment).to_not receive(:next_alternative)
expect(context).not_to receive(:on_trial_callback)
expect_alternative(trial, "basket")
Split.configuration.on_trial = nil
end
end
context "when Split is globally disabled" do
it "picks the control and does not run on_trial callbacks", :aggregate_failures do
Split.configuration.enabled = false
Split.configuration.on_trial = :on_trial_callback
expect(experiment).to_not receive(:next_alternative)
expect(context).not_to receive(:on_trial_callback)
expect_alternative(trial, "basket")
Split.configuration.enabled = true
Split.configuration.on_trial = nil
end
end
context "when experiment has winner" do
let(:trial) do
Split::Trial.new(user: user, experiment: experiment)
end
it_behaves_like "a trial with callbacks"
it "picks the winner" do
experiment.winner = "cart"
expect(experiment).to_not receive(:next_alternative)
expect_alternative(trial, "cart")
end
end
context "when exclude is true" do
let(:trial) do
Split::Trial.new(user: user, experiment: experiment, exclude: true)
end
it_behaves_like "a trial with callbacks"
it "picks the control" do
expect(experiment).to_not receive(:next_alternative)
expect_alternative(trial, "basket")
end
end
context "when user is already participating" do
it_behaves_like "a trial with callbacks"
it "picks the same alternative" do
user[experiment.key] = "basket"
expect(experiment).to_not receive(:next_alternative)
expect_alternative(trial, "basket")
end
context "when alternative is not found" do
it "falls back on next_alternative" do
user[experiment.key] = "notfound"
expect(experiment).to receive(:next_alternative).and_call_original
expect_alternative(trial, alternatives)
end
end
end
context "when user is a new participant" do
it "picks a new alternative and runs on_trial_choose callback", :aggregate_failures do
Split.configuration.on_trial_choose = :on_trial_choose_callback
expect(experiment).to receive(:next_alternative).and_call_original
expect(context).to receive(:on_trial_choose_callback)
trial.choose! context
expect(trial.alternative.name).to_not be_empty
Split.configuration.on_trial_choose = nil
end
it "assigns user to an alternative" do
trial.choose! context
expect(alternatives).to include(user[experiment.name])
end
context "when cohorting is disabled" do
before(:each) { allow(experiment).to receive(:cohorting_disabled?).and_return(true) }
it "picks the control and does not run on_trial callbacks" do
Split.configuration.on_trial = :on_trial_callback
expect(experiment).to_not receive(:next_alternative)
expect(context).not_to receive(:on_trial_callback)
expect_alternative(trial, "basket")
Split.configuration.enabled = true
Split.configuration.on_trial = nil
end
it "user is not assigned an alternative" do
trial.choose! context
expect(user[experiment]).to eq(nil)
end
end
end
describe "#complete!" do
let(:trial) { Split::Trial.new(:user => user, :experiment => experiment) }
context 'when there are no goals' do
it 'should complete the trial' do
trial.choose!
old_completed_count = trial.alternative.completed_count
trial.complete!
expect(trial.alternative.completed_count).to be(old_completed_count+1)
end
end
context 'when there are many goals' do
let(:goals) { ['first', 'second'] }
let(:trial) { Split::Trial.new(:user => user, :experiment => experiment, :goals => goals) }
shared_examples_for "goal completion" do
it 'should not complete the trial' do
trial.choose!
old_completed_count = trial.alternative.completed_count
trial.complete!(goal)
expect(trial.alternative.completed_count).to_not be(old_completed_count+1)
end
end
describe 'Array of Goals' do
let(:goal) { [goals.first] }
it_behaves_like 'goal completion'
end
describe 'String of Goal' do
let(:goal) { goals.first }
it_behaves_like 'goal completion'
end
end
end
describe "alternative recording" do
before(:each) { Split.configuration.store_override = false }
context "when override is present" do
it "stores when store_override is true" do
trial = Split::Trial.new(user: user, experiment: experiment, override: "basket")
Split.configuration.store_override = true
expect(user).to receive("[]=")
trial.choose!
expect(trial.alternative.participant_count).to eq(1)
end
it "does not store when store_override is false" do
trial = Split::Trial.new(user: user, experiment: experiment, override: "basket")
expect(user).to_not receive("[]=")
trial.choose!
end
end
context "when disabled is present" do
it "stores when store_override is true" do
trial = Split::Trial.new(user: user, experiment: experiment, disabled: true)
Split.configuration.store_override = true
expect(user).to receive("[]=")
trial.choose!
end
it "does not store when store_override is false" do
trial = Split::Trial.new(user: user, experiment: experiment, disabled: true)
expect(user).to_not receive("[]=")
trial.choose!
end
end
context "when exclude is present" do
it "does not store" do
trial = Split::Trial.new(user: user, experiment: experiment, exclude: true)
expect(user).to_not receive("[]=")
trial.choose!
end
end
context "when experiment has winner" do
let(:trial) do
experiment.winner = "cart"
Split::Trial.new(user: user, experiment: experiment)
end
it "does not store" do
expect(user).to_not receive("[]=")
trial.choose!
end
end
end
end
<MSG> Update trial to init with goals instead
<DFF> @@ -229,38 +229,37 @@ describe Split::Trial do
end
describe "#complete!" do
- let(:trial) { Split::Trial.new(:user => user, :experiment => experiment) }
context 'when there are no goals' do
+ let(:trial) { Split::Trial.new(:user => user, :experiment => experiment) }
it 'should complete the trial' do
trial.choose!
old_completed_count = trial.alternative.completed_count
trial.complete!
- expect(trial.alternative.completed_count).to be(old_completed_count+1)
+ expect(trial.alternative.completed_count).to eq(old_completed_count + 1)
end
end
- context 'when there are many goals' do
- let(:goals) { ['first', 'second'] }
+ context "when there are many goals" do
+ let(:goals) { [ "goal1", "second" ] }
let(:trial) { Split::Trial.new(:user => user, :experiment => experiment, :goals => goals) }
- shared_examples_for "goal completion" do
- it 'should not complete the trial' do
- trial.choose!
- old_completed_count = trial.alternative.completed_count
- trial.complete!(goal)
- expect(trial.alternative.completed_count).to_not be(old_completed_count+1)
- end
- end
- describe 'Array of Goals' do
- let(:goal) { [goals.first] }
- it_behaves_like 'goal completion'
+ it "increments the conmpleted count corresponding to the goals" do
+ trial.choose!
+ old_completed_counts = goals.map{ |goal| [goal, trial.alternative.completed_count(goal)] }.to_h
+ trial.complete!
+ goals.each { | goal | expect(trial.alternative.completed_count(goal)).to eq(old_completed_counts[goal] + 1) }
end
+ end
- describe 'String of Goal' do
- let(:goal) { goals.first }
- it_behaves_like 'goal completion'
+ context "when there is 1 goal of type string" do
+ let(:goal) { "first" }
+ let(:trial) { Split::Trial.new(:user => user, :experiment => experiment, :goals => goal) }
+ it "increments the conmpleted count corresponding to the goal" do
+ trial.choose!
+ old_completed_count = trial.alternative.completed_count(goal)
+ trial.complete!
+ expect(trial.alternative.completed_count(goal)).to eq(old_completed_count + 1)
end
-
end
end
| 18 | Update trial to init with goals instead | 19 | .rb | rb | mit | splitrb/split |
10072554 | <NME> trial_spec.rb
<BEF> # frozen_string_literal: true
require "spec_helper"
require "split/trial"
describe Split::Trial do
let(:user) { mock_user }
let(:alternatives) { ["basket", "cart"] }
let(:experiment) do
Split::Experiment.new("basket_text", alternatives: alternatives).save
end
it "should be initializeable" do
experiment = double("experiment")
alternative = double("alternative", kind_of?: Split::Alternative)
trial = Split::Trial.new(experiment: experiment, alternative: alternative)
expect(trial.experiment).to eq(experiment)
expect(trial.alternative).to eq(alternative)
end
describe "alternative" do
it "should use the alternative if specified" do
alternative = double("alternative", kind_of?: Split::Alternative)
trial = Split::Trial.new(experiment: double("experiment"),
alternative: alternative, user: user)
expect(trial).not_to receive(:choose)
expect(trial.alternative).to eq(alternative)
end
it "should load the alternative when the alternative name is set" do
experiment = Split::Experiment.new("basket_text", alternatives: ["basket", "cart"])
experiment.save
trial = Split::Trial.new(experiment: experiment, alternative: "basket")
expect(trial.alternative.name).to eq("basket")
end
end
describe "metadata" do
let(:metadata) { Hash[alternatives.map { |k| [k, "Metadata for #{k}"] }] }
let(:experiment) do
Split::Experiment.new("basket_text", alternatives: alternatives, metadata: metadata).save
end
it "has metadata on each trial" do
trial = Split::Trial.new(experiment: experiment, user: user, metadata: metadata["cart"],
override: "cart")
expect(trial.metadata).to eq(metadata["cart"])
end
it "has metadata on each trial from the experiment" do
trial = Split::Trial.new(experiment: experiment, user: user)
trial.choose!
expect(trial.metadata).to eq(metadata[trial.alternative.name])
expect(trial.metadata).to match(/#{trial.alternative.name}/)
end
end
describe "#choose!" do
let(:context) { double(on_trial_callback: "test callback") }
let(:trial) do
Split::Trial.new(user: user, experiment: experiment)
end
shared_examples_for "a trial with callbacks" do
it "does not run if on_trial callback is not respondable" do
Split.configuration.on_trial = :foo
allow(context).to receive(:respond_to?).with(:foo, true).and_return false
expect(context).to_not receive(:foo)
trial.choose! context
end
it "runs on_trial callback" do
Split.configuration.on_trial = :on_trial_callback
expect(context).to receive(:on_trial_callback)
trial.choose! context
end
it "does not run nil on_trial callback" do
Split.configuration.on_trial = nil
expect(context).not_to receive(:on_trial_callback)
trial.choose! context
end
end
def expect_alternative(trial, alternative_name)
3.times do
trial.choose! context
expect(alternative_name).to include(trial.alternative.name)
end
end
context "when override is present" do
let(:override) { "cart" }
let(:trial) do
Split::Trial.new(user: user, experiment: experiment, override: override)
end
it_behaves_like "a trial with callbacks"
it "picks the override" do
expect(experiment).to_not receive(:next_alternative)
expect_alternative(trial, override)
end
context "when alternative doesn't exist" do
let(:override) { nil }
it "falls back on next_alternative" do
expect(experiment).to receive(:next_alternative).and_call_original
expect_alternative(trial, alternatives)
end
end
end
context "when disabled option is true" do
let(:trial) do
Split::Trial.new(user: user, experiment: experiment, disabled: true)
end
it "picks the control", :aggregate_failures do
Split.configuration.on_trial = :on_trial_callback
expect(experiment).to_not receive(:next_alternative)
expect(context).not_to receive(:on_trial_callback)
expect_alternative(trial, "basket")
Split.configuration.on_trial = nil
end
end
context "when Split is globally disabled" do
it "picks the control and does not run on_trial callbacks", :aggregate_failures do
Split.configuration.enabled = false
Split.configuration.on_trial = :on_trial_callback
expect(experiment).to_not receive(:next_alternative)
expect(context).not_to receive(:on_trial_callback)
expect_alternative(trial, "basket")
Split.configuration.enabled = true
Split.configuration.on_trial = nil
end
end
context "when experiment has winner" do
let(:trial) do
Split::Trial.new(user: user, experiment: experiment)
end
it_behaves_like "a trial with callbacks"
it "picks the winner" do
experiment.winner = "cart"
expect(experiment).to_not receive(:next_alternative)
expect_alternative(trial, "cart")
end
end
context "when exclude is true" do
let(:trial) do
Split::Trial.new(user: user, experiment: experiment, exclude: true)
end
it_behaves_like "a trial with callbacks"
it "picks the control" do
expect(experiment).to_not receive(:next_alternative)
expect_alternative(trial, "basket")
end
end
context "when user is already participating" do
it_behaves_like "a trial with callbacks"
it "picks the same alternative" do
user[experiment.key] = "basket"
expect(experiment).to_not receive(:next_alternative)
expect_alternative(trial, "basket")
end
context "when alternative is not found" do
it "falls back on next_alternative" do
user[experiment.key] = "notfound"
expect(experiment).to receive(:next_alternative).and_call_original
expect_alternative(trial, alternatives)
end
end
end
context "when user is a new participant" do
it "picks a new alternative and runs on_trial_choose callback", :aggregate_failures do
Split.configuration.on_trial_choose = :on_trial_choose_callback
expect(experiment).to receive(:next_alternative).and_call_original
expect(context).to receive(:on_trial_choose_callback)
trial.choose! context
expect(trial.alternative.name).to_not be_empty
Split.configuration.on_trial_choose = nil
end
it "assigns user to an alternative" do
trial.choose! context
expect(alternatives).to include(user[experiment.name])
end
context "when cohorting is disabled" do
before(:each) { allow(experiment).to receive(:cohorting_disabled?).and_return(true) }
it "picks the control and does not run on_trial callbacks" do
Split.configuration.on_trial = :on_trial_callback
expect(experiment).to_not receive(:next_alternative)
expect(context).not_to receive(:on_trial_callback)
expect_alternative(trial, "basket")
Split.configuration.enabled = true
Split.configuration.on_trial = nil
end
it "user is not assigned an alternative" do
trial.choose! context
expect(user[experiment]).to eq(nil)
end
end
end
describe "#complete!" do
let(:trial) { Split::Trial.new(:user => user, :experiment => experiment) }
context 'when there are no goals' do
it 'should complete the trial' do
trial.choose!
old_completed_count = trial.alternative.completed_count
trial.complete!
expect(trial.alternative.completed_count).to be(old_completed_count+1)
end
end
context 'when there are many goals' do
let(:goals) { ['first', 'second'] }
let(:trial) { Split::Trial.new(:user => user, :experiment => experiment, :goals => goals) }
shared_examples_for "goal completion" do
it 'should not complete the trial' do
trial.choose!
old_completed_count = trial.alternative.completed_count
trial.complete!(goal)
expect(trial.alternative.completed_count).to_not be(old_completed_count+1)
end
end
describe 'Array of Goals' do
let(:goal) { [goals.first] }
it_behaves_like 'goal completion'
end
describe 'String of Goal' do
let(:goal) { goals.first }
it_behaves_like 'goal completion'
end
end
end
describe "alternative recording" do
before(:each) { Split.configuration.store_override = false }
context "when override is present" do
it "stores when store_override is true" do
trial = Split::Trial.new(user: user, experiment: experiment, override: "basket")
Split.configuration.store_override = true
expect(user).to receive("[]=")
trial.choose!
expect(trial.alternative.participant_count).to eq(1)
end
it "does not store when store_override is false" do
trial = Split::Trial.new(user: user, experiment: experiment, override: "basket")
expect(user).to_not receive("[]=")
trial.choose!
end
end
context "when disabled is present" do
it "stores when store_override is true" do
trial = Split::Trial.new(user: user, experiment: experiment, disabled: true)
Split.configuration.store_override = true
expect(user).to receive("[]=")
trial.choose!
end
it "does not store when store_override is false" do
trial = Split::Trial.new(user: user, experiment: experiment, disabled: true)
expect(user).to_not receive("[]=")
trial.choose!
end
end
context "when exclude is present" do
it "does not store" do
trial = Split::Trial.new(user: user, experiment: experiment, exclude: true)
expect(user).to_not receive("[]=")
trial.choose!
end
end
context "when experiment has winner" do
let(:trial) do
experiment.winner = "cart"
Split::Trial.new(user: user, experiment: experiment)
end
it "does not store" do
expect(user).to_not receive("[]=")
trial.choose!
end
end
end
end
<MSG> Update trial to init with goals instead
<DFF> @@ -229,38 +229,37 @@ describe Split::Trial do
end
describe "#complete!" do
- let(:trial) { Split::Trial.new(:user => user, :experiment => experiment) }
context 'when there are no goals' do
+ let(:trial) { Split::Trial.new(:user => user, :experiment => experiment) }
it 'should complete the trial' do
trial.choose!
old_completed_count = trial.alternative.completed_count
trial.complete!
- expect(trial.alternative.completed_count).to be(old_completed_count+1)
+ expect(trial.alternative.completed_count).to eq(old_completed_count + 1)
end
end
- context 'when there are many goals' do
- let(:goals) { ['first', 'second'] }
+ context "when there are many goals" do
+ let(:goals) { [ "goal1", "second" ] }
let(:trial) { Split::Trial.new(:user => user, :experiment => experiment, :goals => goals) }
- shared_examples_for "goal completion" do
- it 'should not complete the trial' do
- trial.choose!
- old_completed_count = trial.alternative.completed_count
- trial.complete!(goal)
- expect(trial.alternative.completed_count).to_not be(old_completed_count+1)
- end
- end
- describe 'Array of Goals' do
- let(:goal) { [goals.first] }
- it_behaves_like 'goal completion'
+ it "increments the conmpleted count corresponding to the goals" do
+ trial.choose!
+ old_completed_counts = goals.map{ |goal| [goal, trial.alternative.completed_count(goal)] }.to_h
+ trial.complete!
+ goals.each { | goal | expect(trial.alternative.completed_count(goal)).to eq(old_completed_counts[goal] + 1) }
end
+ end
- describe 'String of Goal' do
- let(:goal) { goals.first }
- it_behaves_like 'goal completion'
+ context "when there is 1 goal of type string" do
+ let(:goal) { "first" }
+ let(:trial) { Split::Trial.new(:user => user, :experiment => experiment, :goals => goal) }
+ it "increments the conmpleted count corresponding to the goal" do
+ trial.choose!
+ old_completed_count = trial.alternative.completed_count(goal)
+ trial.complete!
+ expect(trial.alternative.completed_count(goal)).to eq(old_completed_count + 1)
end
-
end
end
| 18 | Update trial to init with goals instead | 19 | .rb | rb | mit | splitrb/split |
10072555 | <NME> trial_spec.rb
<BEF> # frozen_string_literal: true
require "spec_helper"
require "split/trial"
describe Split::Trial do
let(:user) { mock_user }
let(:alternatives) { ["basket", "cart"] }
let(:experiment) do
Split::Experiment.new("basket_text", alternatives: alternatives).save
end
it "should be initializeable" do
experiment = double("experiment")
alternative = double("alternative", kind_of?: Split::Alternative)
trial = Split::Trial.new(experiment: experiment, alternative: alternative)
expect(trial.experiment).to eq(experiment)
expect(trial.alternative).to eq(alternative)
end
describe "alternative" do
it "should use the alternative if specified" do
alternative = double("alternative", kind_of?: Split::Alternative)
trial = Split::Trial.new(experiment: double("experiment"),
alternative: alternative, user: user)
expect(trial).not_to receive(:choose)
expect(trial.alternative).to eq(alternative)
end
it "should load the alternative when the alternative name is set" do
experiment = Split::Experiment.new("basket_text", alternatives: ["basket", "cart"])
experiment.save
trial = Split::Trial.new(experiment: experiment, alternative: "basket")
expect(trial.alternative.name).to eq("basket")
end
end
describe "metadata" do
let(:metadata) { Hash[alternatives.map { |k| [k, "Metadata for #{k}"] }] }
let(:experiment) do
Split::Experiment.new("basket_text", alternatives: alternatives, metadata: metadata).save
end
it "has metadata on each trial" do
trial = Split::Trial.new(experiment: experiment, user: user, metadata: metadata["cart"],
override: "cart")
expect(trial.metadata).to eq(metadata["cart"])
end
it "has metadata on each trial from the experiment" do
trial = Split::Trial.new(experiment: experiment, user: user)
trial.choose!
expect(trial.metadata).to eq(metadata[trial.alternative.name])
expect(trial.metadata).to match(/#{trial.alternative.name}/)
end
end
describe "#choose!" do
let(:context) { double(on_trial_callback: "test callback") }
let(:trial) do
Split::Trial.new(user: user, experiment: experiment)
end
shared_examples_for "a trial with callbacks" do
it "does not run if on_trial callback is not respondable" do
Split.configuration.on_trial = :foo
allow(context).to receive(:respond_to?).with(:foo, true).and_return false
expect(context).to_not receive(:foo)
trial.choose! context
end
it "runs on_trial callback" do
Split.configuration.on_trial = :on_trial_callback
expect(context).to receive(:on_trial_callback)
trial.choose! context
end
it "does not run nil on_trial callback" do
Split.configuration.on_trial = nil
expect(context).not_to receive(:on_trial_callback)
trial.choose! context
end
end
def expect_alternative(trial, alternative_name)
3.times do
trial.choose! context
expect(alternative_name).to include(trial.alternative.name)
end
end
context "when override is present" do
let(:override) { "cart" }
let(:trial) do
Split::Trial.new(user: user, experiment: experiment, override: override)
end
it_behaves_like "a trial with callbacks"
it "picks the override" do
expect(experiment).to_not receive(:next_alternative)
expect_alternative(trial, override)
end
context "when alternative doesn't exist" do
let(:override) { nil }
it "falls back on next_alternative" do
expect(experiment).to receive(:next_alternative).and_call_original
expect_alternative(trial, alternatives)
end
end
end
context "when disabled option is true" do
let(:trial) do
Split::Trial.new(user: user, experiment: experiment, disabled: true)
end
it "picks the control", :aggregate_failures do
Split.configuration.on_trial = :on_trial_callback
expect(experiment).to_not receive(:next_alternative)
expect(context).not_to receive(:on_trial_callback)
expect_alternative(trial, "basket")
Split.configuration.on_trial = nil
end
end
context "when Split is globally disabled" do
it "picks the control and does not run on_trial callbacks", :aggregate_failures do
Split.configuration.enabled = false
Split.configuration.on_trial = :on_trial_callback
expect(experiment).to_not receive(:next_alternative)
expect(context).not_to receive(:on_trial_callback)
expect_alternative(trial, "basket")
Split.configuration.enabled = true
Split.configuration.on_trial = nil
end
end
context "when experiment has winner" do
let(:trial) do
Split::Trial.new(user: user, experiment: experiment)
end
it_behaves_like "a trial with callbacks"
it "picks the winner" do
experiment.winner = "cart"
expect(experiment).to_not receive(:next_alternative)
expect_alternative(trial, "cart")
end
end
context "when exclude is true" do
let(:trial) do
Split::Trial.new(user: user, experiment: experiment, exclude: true)
end
it_behaves_like "a trial with callbacks"
it "picks the control" do
expect(experiment).to_not receive(:next_alternative)
expect_alternative(trial, "basket")
end
end
context "when user is already participating" do
it_behaves_like "a trial with callbacks"
it "picks the same alternative" do
user[experiment.key] = "basket"
expect(experiment).to_not receive(:next_alternative)
expect_alternative(trial, "basket")
end
context "when alternative is not found" do
it "falls back on next_alternative" do
user[experiment.key] = "notfound"
expect(experiment).to receive(:next_alternative).and_call_original
expect_alternative(trial, alternatives)
end
end
end
context "when user is a new participant" do
it "picks a new alternative and runs on_trial_choose callback", :aggregate_failures do
Split.configuration.on_trial_choose = :on_trial_choose_callback
expect(experiment).to receive(:next_alternative).and_call_original
expect(context).to receive(:on_trial_choose_callback)
trial.choose! context
expect(trial.alternative.name).to_not be_empty
Split.configuration.on_trial_choose = nil
end
it "assigns user to an alternative" do
trial.choose! context
expect(alternatives).to include(user[experiment.name])
end
context "when cohorting is disabled" do
before(:each) { allow(experiment).to receive(:cohorting_disabled?).and_return(true) }
it "picks the control and does not run on_trial callbacks" do
Split.configuration.on_trial = :on_trial_callback
expect(experiment).to_not receive(:next_alternative)
expect(context).not_to receive(:on_trial_callback)
expect_alternative(trial, "basket")
Split.configuration.enabled = true
Split.configuration.on_trial = nil
end
it "user is not assigned an alternative" do
trial.choose! context
expect(user[experiment]).to eq(nil)
end
end
end
describe "#complete!" do
let(:trial) { Split::Trial.new(:user => user, :experiment => experiment) }
context 'when there are no goals' do
it 'should complete the trial' do
trial.choose!
old_completed_count = trial.alternative.completed_count
trial.complete!
expect(trial.alternative.completed_count).to be(old_completed_count+1)
end
end
context 'when there are many goals' do
let(:goals) { ['first', 'second'] }
let(:trial) { Split::Trial.new(:user => user, :experiment => experiment, :goals => goals) }
shared_examples_for "goal completion" do
it 'should not complete the trial' do
trial.choose!
old_completed_count = trial.alternative.completed_count
trial.complete!(goal)
expect(trial.alternative.completed_count).to_not be(old_completed_count+1)
end
end
describe 'Array of Goals' do
let(:goal) { [goals.first] }
it_behaves_like 'goal completion'
end
describe 'String of Goal' do
let(:goal) { goals.first }
it_behaves_like 'goal completion'
end
end
end
describe "alternative recording" do
before(:each) { Split.configuration.store_override = false }
context "when override is present" do
it "stores when store_override is true" do
trial = Split::Trial.new(user: user, experiment: experiment, override: "basket")
Split.configuration.store_override = true
expect(user).to receive("[]=")
trial.choose!
expect(trial.alternative.participant_count).to eq(1)
end
it "does not store when store_override is false" do
trial = Split::Trial.new(user: user, experiment: experiment, override: "basket")
expect(user).to_not receive("[]=")
trial.choose!
end
end
context "when disabled is present" do
it "stores when store_override is true" do
trial = Split::Trial.new(user: user, experiment: experiment, disabled: true)
Split.configuration.store_override = true
expect(user).to receive("[]=")
trial.choose!
end
it "does not store when store_override is false" do
trial = Split::Trial.new(user: user, experiment: experiment, disabled: true)
expect(user).to_not receive("[]=")
trial.choose!
end
end
context "when exclude is present" do
it "does not store" do
trial = Split::Trial.new(user: user, experiment: experiment, exclude: true)
expect(user).to_not receive("[]=")
trial.choose!
end
end
context "when experiment has winner" do
let(:trial) do
experiment.winner = "cart"
Split::Trial.new(user: user, experiment: experiment)
end
it "does not store" do
expect(user).to_not receive("[]=")
trial.choose!
end
end
end
end
<MSG> Update trial to init with goals instead
<DFF> @@ -229,38 +229,37 @@ describe Split::Trial do
end
describe "#complete!" do
- let(:trial) { Split::Trial.new(:user => user, :experiment => experiment) }
context 'when there are no goals' do
+ let(:trial) { Split::Trial.new(:user => user, :experiment => experiment) }
it 'should complete the trial' do
trial.choose!
old_completed_count = trial.alternative.completed_count
trial.complete!
- expect(trial.alternative.completed_count).to be(old_completed_count+1)
+ expect(trial.alternative.completed_count).to eq(old_completed_count + 1)
end
end
- context 'when there are many goals' do
- let(:goals) { ['first', 'second'] }
+ context "when there are many goals" do
+ let(:goals) { [ "goal1", "second" ] }
let(:trial) { Split::Trial.new(:user => user, :experiment => experiment, :goals => goals) }
- shared_examples_for "goal completion" do
- it 'should not complete the trial' do
- trial.choose!
- old_completed_count = trial.alternative.completed_count
- trial.complete!(goal)
- expect(trial.alternative.completed_count).to_not be(old_completed_count+1)
- end
- end
- describe 'Array of Goals' do
- let(:goal) { [goals.first] }
- it_behaves_like 'goal completion'
+ it "increments the conmpleted count corresponding to the goals" do
+ trial.choose!
+ old_completed_counts = goals.map{ |goal| [goal, trial.alternative.completed_count(goal)] }.to_h
+ trial.complete!
+ goals.each { | goal | expect(trial.alternative.completed_count(goal)).to eq(old_completed_counts[goal] + 1) }
end
+ end
- describe 'String of Goal' do
- let(:goal) { goals.first }
- it_behaves_like 'goal completion'
+ context "when there is 1 goal of type string" do
+ let(:goal) { "first" }
+ let(:trial) { Split::Trial.new(:user => user, :experiment => experiment, :goals => goal) }
+ it "increments the conmpleted count corresponding to the goal" do
+ trial.choose!
+ old_completed_count = trial.alternative.completed_count(goal)
+ trial.complete!
+ expect(trial.alternative.completed_count(goal)).to eq(old_completed_count + 1)
end
-
end
end
| 18 | Update trial to init with goals instead | 19 | .rb | rb | mit | splitrb/split |
10072556 | <NME> 7.0.gemfile
<BEF> # This file was generated by Appraisal
source "https://rubygems.org"
gem "appraisal"
gem "codeclimate-test-reporter"
gem "rails", "~> 7.0"
gem "matrix"
gem "matrix"
gemspec path: "../"
<MSG> Merge pull request #676 from splitrb/cleanup-appraisals
Remove appraisals configuration
<DFF> @@ -1,8 +1,5 @@
-# This file was generated by Appraisal
-
source "https://rubygems.org"
-gem "appraisal"
gem "codeclimate-test-reporter"
gem "rails", "~> 7.0"
gem "matrix"
| 0 | Merge pull request #676 from splitrb/cleanup-appraisals | 3 | .gemfile | 0 | mit | splitrb/split |
10072557 | <NME> 7.0.gemfile
<BEF> # This file was generated by Appraisal
source "https://rubygems.org"
gem "appraisal"
gem "codeclimate-test-reporter"
gem "rails", "~> 7.0"
gem "matrix"
gem "matrix"
gemspec path: "../"
<MSG> Merge pull request #676 from splitrb/cleanup-appraisals
Remove appraisals configuration
<DFF> @@ -1,8 +1,5 @@
-# This file was generated by Appraisal
-
source "https://rubygems.org"
-gem "appraisal"
gem "codeclimate-test-reporter"
gem "rails", "~> 7.0"
gem "matrix"
| 0 | Merge pull request #676 from splitrb/cleanup-appraisals | 3 | .gemfile | 0 | mit | splitrb/split |
10072558 | <NME> 7.0.gemfile
<BEF> # This file was generated by Appraisal
source "https://rubygems.org"
gem "appraisal"
gem "codeclimate-test-reporter"
gem "rails", "~> 7.0"
gem "matrix"
gem "matrix"
gemspec path: "../"
<MSG> Merge pull request #676 from splitrb/cleanup-appraisals
Remove appraisals configuration
<DFF> @@ -1,8 +1,5 @@
-# This file was generated by Appraisal
-
source "https://rubygems.org"
-gem "appraisal"
gem "codeclimate-test-reporter"
gem "rails", "~> 7.0"
gem "matrix"
| 0 | Merge pull request #676 from splitrb/cleanup-appraisals | 3 | .gemfile | 0 | mit | splitrb/split |
10072559 | <NME> ariaHelper.js
<BEF> ADDFILE
<MSG> chore(core): Added core module and ariaHelper service
<DFF> @@ -0,0 +1,20 @@
+(function() {
+'use strict';
+
+angular
+ .module('semantic.ui.angular.core')
+
+ .service('ariaHelper', ['$log', function($log) {
+
+ return {
+ hasAttribute: hasAttribute
+ };
+
+ function hasAttribute(node, attrName) {
+ if (!node.hasAttribute(attrName)) {
+ $log.warn('ngAria: Attribute "', attrName, '", required for accessibility, is missing on node:', node);
+ }
+ }
+ }]);
+
+})();
| 20 | chore(core): Added core module and ariaHelper service | 0 | .js | js | mit | Semantic-Org/Semantic-UI-Angular |
10072560 | <NME> ariaHelper.js
<BEF> ADDFILE
<MSG> chore(core): Added core module and ariaHelper service
<DFF> @@ -0,0 +1,20 @@
+(function() {
+'use strict';
+
+angular
+ .module('semantic.ui.angular.core')
+
+ .service('ariaHelper', ['$log', function($log) {
+
+ return {
+ hasAttribute: hasAttribute
+ };
+
+ function hasAttribute(node, attrName) {
+ if (!node.hasAttribute(attrName)) {
+ $log.warn('ngAria: Attribute "', attrName, '", required for accessibility, is missing on node:', node);
+ }
+ }
+ }]);
+
+})();
| 20 | chore(core): Added core module and ariaHelper service | 0 | .js | js | mit | Semantic-Org/Semantic-UI-Angular |
10072561 | <NME> ariaHelper.js
<BEF> ADDFILE
<MSG> chore(core): Added core module and ariaHelper service
<DFF> @@ -0,0 +1,20 @@
+(function() {
+'use strict';
+
+angular
+ .module('semantic.ui.angular.core')
+
+ .service('ariaHelper', ['$log', function($log) {
+
+ return {
+ hasAttribute: hasAttribute
+ };
+
+ function hasAttribute(node, attrName) {
+ if (!node.hasAttribute(attrName)) {
+ $log.warn('ngAria: Attribute "', attrName, '", required for accessibility, is missing on node:', node);
+ }
+ }
+ }]);
+
+})();
| 20 | chore(core): Added core module and ariaHelper service | 0 | .js | js | mit | Semantic-Org/Semantic-UI-Angular |
10072562 | <NME> version.rb
<BEF> # frozen_string_literal: true
module Split
MAJOR = 3
MINOR = 3
PATCH = 0
VERSION = [MAJOR, MINOR, PATCH].join('.')
end
<MSG> Update changelog for 3.3.1
<DFF> @@ -2,6 +2,6 @@
module Split
MAJOR = 3
MINOR = 3
- PATCH = 0
+ PATCH = 1
VERSION = [MAJOR, MINOR, PATCH].join('.')
end
| 1 | Update changelog for 3.3.1 | 1 | .rb | rb | mit | splitrb/split |
10072563 | <NME> version.rb
<BEF> # frozen_string_literal: true
module Split
MAJOR = 3
MINOR = 3
PATCH = 0
VERSION = [MAJOR, MINOR, PATCH].join('.')
end
<MSG> Update changelog for 3.3.1
<DFF> @@ -2,6 +2,6 @@
module Split
MAJOR = 3
MINOR = 3
- PATCH = 0
+ PATCH = 1
VERSION = [MAJOR, MINOR, PATCH].join('.')
end
| 1 | Update changelog for 3.3.1 | 1 | .rb | rb | mit | splitrb/split |
10072564 | <NME> version.rb
<BEF> # frozen_string_literal: true
module Split
MAJOR = 3
MINOR = 3
PATCH = 0
VERSION = [MAJOR, MINOR, PATCH].join('.')
end
<MSG> Update changelog for 3.3.1
<DFF> @@ -2,6 +2,6 @@
module Split
MAJOR = 3
MINOR = 3
- PATCH = 0
+ PATCH = 1
VERSION = [MAJOR, MINOR, PATCH].join('.')
end
| 1 | Update changelog for 3.3.1 | 1 | .rb | rb | mit | splitrb/split |
10072565 | <NME> helper_spec.rb
<BEF> # frozen_string_literal: true
require "spec_helper"
# TODO change some of these tests to use Rack::Test
describe Split::Helper do
include Split::Helper
let(:experiment) {
Split::ExperimentCatalog.find_or_create("link_color", "blue", "red")
}
describe "ab_test" do
it "should not raise an error when passed strings for alternatives" do
expect { ab_test("xyz", "1", "2", "3") }.not_to raise_error
end
it "should not raise an error when passed an array for alternatives" do
expect { ab_test("xyz", ["1", "2", "3"]) }.not_to raise_error
end
it "should raise the appropriate error when passed integers for alternatives" do
expect { ab_test("xyz", 1, 2, 3) }.to raise_error(ArgumentError)
end
it "should raise the appropriate error when passed symbols for alternatives" do
expect { ab_test("xyz", :a, :b, :c) }.to raise_error(ArgumentError)
end
it "should not raise error when passed an array for goals" do
expect { ab_test({ "link_color" => ["purchase", "refund"] }, "blue", "red") }.not_to raise_error
end
it "should not raise error when passed just one goal" do
expect { ab_test({ "link_color" => "purchase" }, "blue", "red") }.not_to raise_error
end
it "raises an appropriate error when processing combined expirements" do
Split.configuration.experiments = {
combined_exp_1: {
alternatives: [ { name: "control", percent: 50 }, { name: "test-alt", percent: 50 } ],
metric: :my_metric,
combined_experiments: [:combined_exp_1_sub_1]
}
}
Split::ExperimentCatalog.find_or_create("combined_exp_1")
expect { ab_test("combined_exp_1") }.to raise_error(Split::InvalidExperimentsFormatError)
end
@params = {'link_color' => 'blue'}
alternative = ab_test('link_color', 'blue', 'red')
alternative.should eql('blue')
end
it "should allow passing a block" do
new_blue_count = Split::Alternative.new("blue", "link_color").participant_count
expect((new_red_count + new_blue_count)).to eq(previous_red_count + previous_blue_count + 1)
end
it "should not increment the counter for an experiment that the user is not participating in" do
ab_test("link_color", "blue", "red")
e = Split::ExperimentCatalog.find_or_create("button_size", "small", "big")
expect {
# User shouldn't participate in this second experiment
ab_test("button_size", "small", "big")
}.not_to change { e.participant_count }
end
it "should not increment the counter for an ended experiment" do
e = Split::ExperimentCatalog.find_or_create("button_size", "small", "big")
e.winner = "small"
expect {
a = ab_test("button_size", "small", "big")
expect(a).to eq("small")
}.not_to change { e.participant_count }
end
it "should not increment the counter for an not started experiment" do
expect(Split.configuration).to receive(:start_manually).and_return(true)
e = Split::ExperimentCatalog.find_or_create("button_size", "small", "big")
expect {
a = ab_test("button_size", "small", "big")
expect(a).to eq("small")
}.not_to change { e.participant_count }
end
it "should return the given alternative for an existing user" do
expect(ab_test("link_color", "blue", "red")).to eq ab_test("link_color", "blue", "red")
end
it "should always return the winner if one is present" do
experiment.winner = "orange"
expect(ab_test("link_color", "blue", "red")).to eq("orange")
end
it "should allow the alternative to be forced by passing it in the params" do
# ?ab_test[link_color]=blue
@params = { "ab_test" => { "link_color" => "blue" } }
alternative = ab_test("link_color", "blue", "red")
expect(alternative).to eq("blue")
alternative = ab_test("link_color", { "blue" => 1 }, "red" => 5)
expect(alternative).to eq("blue")
@params = { "ab_test" => { "link_color" => "red" } }
alternative = ab_test("link_color", "blue", "red")
expect(alternative).to eq("red")
alternative = ab_test("link_color", { "blue" => 5 }, "red" => 1)
expect(alternative).to eq("red")
end
it "should not allow an arbitrary alternative" do
@params = { "ab_test" => { "link_color" => "pink" } }
alternative = ab_test("link_color", "blue")
expect(alternative).to eq("blue")
end
it "should not store the split when a param forced alternative" do
@params = { "ab_test" => { "link_color" => "blue" } }
expect(ab_user).not_to receive(:[]=)
ab_test("link_color", "blue", "red")
end
it "SPLIT_DISABLE query parameter should also force the alternative (uses control)" do
@params = { "SPLIT_DISABLE" => "true" }
alternative = ab_test("link_color", "blue", "red")
expect(alternative).to eq("blue")
alternative = ab_test("link_color", { "blue" => 1 }, "red" => 5)
expect(alternative).to eq("blue")
alternative = ab_test("link_color", "red", "blue")
expect(alternative).to eq("red")
alternative = ab_test("link_color", { "red" => 5 }, "blue" => 1)
expect(alternative).to eq("red")
end
it "should not store the split when Split generically disabled" do
@params = { "SPLIT_DISABLE" => "true" }
expect(ab_user).not_to receive(:[]=)
ab_test("link_color", "blue", "red")
end
context "when store_override is set" do
before { Split.configuration.store_override = true }
it "should store the forced alternative" do
@params = { "ab_test" => { "link_color" => "blue" } }
expect(ab_user).to receive(:[]=).with("link_color", "blue")
ab_test("link_color", "blue", "red")
end
end
context "when on_trial_choose is set" do
before { Split.configuration.on_trial_choose = :some_method }
it "should call the method" do
expect(self).to receive(:some_method)
ab_test("link_color", "blue", "red")
end
end
it "should allow passing a block" do
alt = ab_test("link_color", "blue", "red")
ret = ab_test("link_color", "blue", "red") { |alternative| "shared/#{alternative}" }
expect(ret).to eq("shared/#{alt}")
end
it "should allow the share of visitors see an alternative to be specified" do
ab_test("link_color", { "blue" => 0.8 }, { "red" => 20 })
expect(["red", "blue"]).to include(ab_user["link_color"])
end
it "should allow alternative weighting interface as a single hash" do
ab_test("link_color", { "blue" => 0.01 }, "red" => 0.2)
experiment = Split::ExperimentCatalog.find("link_color")
expect(experiment.alternatives.map(&:name)).to eq(["blue", "red"])
expect(experiment.alternatives.collect { |a| a.weight }).to match_array([0.01, 0.2])
end
it "should only let a user participate in one experiment at a time" do
link_color = ab_test("link_color", "blue", "red")
ab_test("button_size", "small", "big")
expect(ab_user["link_color"]).to eq(link_color)
big = Split::Alternative.new("big", "button_size")
expect(big.participant_count).to eq(0)
small = Split::Alternative.new("small", "button_size")
expect(small.participant_count).to eq(0)
end
it "should let a user participate in many experiment with allow_multiple_experiments option" do
Split.configure do |config|
config.allow_multiple_experiments = true
end
link_color = ab_test("link_color", "blue", "red")
button_size = ab_test("button_size", "small", "big")
expect(ab_user["link_color"]).to eq(link_color)
expect(ab_user["button_size"]).to eq(button_size)
button_size_alt = Split::Alternative.new(button_size, "button_size")
expect(button_size_alt.participant_count).to eq(1)
end
context "with allow_multiple_experiments = 'control'" do
it "should let a user participate in many experiment with one non-'control' alternative" do
Split.configure do |config|
config.allow_multiple_experiments = "control"
end
groups = 100.times.map do |n|
ab_test("test#{n}".to_sym, { "control" => (100 - n) }, { "test#{n}-alt" => n })
end
experiments = ab_user.active_experiments
expect(experiments.size).to be > 1
count_control = experiments.values.count { |g| g == "control" }
expect(count_control).to eq(experiments.size - 1)
count_alts = groups.count { |g| g != "control" }
expect(count_alts).to eq(1)
end
context "when user already has experiment" do
let(:mock_user) { Split::User.new(self, { "test_0" => "test-alt" }) }
before do
Split.configure do |config|
config.allow_multiple_experiments = "control"
end
Split::ExperimentCatalog.find_or_initialize("test_0", "control", "test-alt").save
Split::ExperimentCatalog.find_or_initialize("test_1", "control", "test-alt").save
end
it "should restore previously selected alternative" do
expect(ab_user.active_experiments.size).to eq 1
expect(ab_test(:test_0, { "control" => 100 }, { "test-alt" => 1 })).to eq "test-alt"
expect(ab_test(:test_0, { "control" => 1 }, { "test-alt" => 100 })).to eq "test-alt"
end
it "should select the correct alternatives after experiment resets" do
experiment = Split::ExperimentCatalog.find(:test_0)
experiment.reset
mock_user[experiment.key] = "test-alt"
expect(ab_user.active_experiments.size).to eq 1
expect(ab_test(:test_0, { "control" => 100 }, { "test-alt" => 1 })).to eq "test-alt"
expect(ab_test(:test_0, { "control" => 0 }, { "test-alt" => 100 })).to eq "test-alt"
end
it "lets override existing choice" do
pending "this requires user store reset on first call not depending on whelther it is current trial"
@params = { "ab_test" => { "test_1" => "test-alt" } }
expect(ab_test(:test_0, { "control" => 0 }, { "test-alt" => 100 })).to eq "control"
expect(ab_test(:test_1, { "control" => 100 }, { "test-alt" => 1 })).to eq "test-alt"
end
end
end
it "should not over-write a finished key when an experiment is on a later version" do
experiment.increment_version
ab_user = { experiment.key => "blue", experiment.finished_key => true }
finished_session = ab_user.dup
ab_test("link_color", "blue", "red")
expect(ab_user).to eq(finished_session)
end
end
describe "metadata" do
context "is defined" do
before do
Split.configuration.experiments = {
my_experiment: {
alternatives: ["one", "two"],
resettable: false,
metadata: { "one" => "Meta1", "two" => "Meta2" }
}
}
end
it "should be passed to helper block" do
@params = { "ab_test" => { "my_experiment" => "two" } }
expect(ab_test("my_experiment")).to eq "two"
expect(ab_test("my_experiment") do |alternative, meta|
meta
end).to eq("Meta2")
end
it "should pass control metadata helper block if library disabled" do
Split.configure do |config|
config.enabled = false
end
expect(ab_test("my_experiment")).to eq "one"
expect(ab_test("my_experiment") do |_, meta|
meta
end).to eq("Meta1")
end
end
context "is not defined" do
before do
Split.configuration.experiments = {
my_experiment: {
alternatives: ["one", "two"],
resettable: false,
metadata: nil
}
}
end
it "should be passed to helper block" do
expect(ab_test("my_experiment") do |alternative, meta|
meta
end).to eq({})
end
it "should pass control metadata helper block if library disabled" do
Split.configure do |config|
config.enabled = false
end
expect(ab_test("my_experiment") do |_, meta|
meta
end).to eq({})
end
end
end
describe "ab_finished" do
context "for an experiment that the user participates in" do
before(:each) do
@experiment_name = "link_color"
@alternatives = ["blue", "red"]
@experiment = Split::ExperimentCatalog.find_or_create(@experiment_name, *@alternatives)
@alternative_name = ab_test(@experiment_name, *@alternatives)
@previous_completion_count = Split::Alternative.new(@alternative_name, @experiment_name).completed_count
end
it "should increment the counter for the completed alternative" do
ab_finished(@experiment_name)
new_completion_count = Split::Alternative.new(@alternative_name, @experiment_name).completed_count
expect(new_completion_count).to eq(@previous_completion_count + 1)
end
it "should set experiment's finished key if reset is false" do
ab_finished(@experiment_name, { reset: false })
expect(ab_user[@experiment.key]).to eq(@alternative_name)
expect(ab_user[@experiment.finished_key]).to eq(true)
end
it "should not increment the counter if reset is false and the experiment has been already finished" do
2.times { ab_finished(@experiment_name, { reset: false }) }
new_completion_count = Split::Alternative.new(@alternative_name, @experiment_name).completed_count
expect(new_completion_count).to eq(@previous_completion_count + 1)
end
it "should not increment the counter for an ended experiment" do
e = Split::ExperimentCatalog.find_or_create("button_size", "small", "big")
e.winner = "small"
a = ab_test("button_size", "small", "big")
expect(a).to eq("small")
expect {
ab_finished("button_size")
}.not_to change { Split::Alternative.new(a, "button_size").completed_count }
end
it "should clear out the user's participation from their session" do
expect(ab_user[@experiment.key]).to eq(@alternative_name)
ab_finished(@experiment_name)
expect(ab_user.keys).to be_empty
end
it "should not clear out the users session if reset is false" do
expect(ab_user[@experiment.key]).to eq(@alternative_name)
ab_finished(@experiment_name, { reset: false })
expect(ab_user[@experiment.key]).to eq(@alternative_name)
expect(ab_user[@experiment.finished_key]).to eq(true)
end
it "should reset the users session when experiment is not versioned" do
expect(ab_user[@experiment.key]).to eq(@alternative_name)
ab_finished(@experiment_name)
expect(ab_user.keys).to be_empty
end
it "should reset the users session when experiment is versioned" do
@experiment.increment_version
@alternative_name = ab_test(@experiment_name, *@alternatives)
expect(ab_user[@experiment.key]).to eq(@alternative_name)
ab_finished(@experiment_name)
expect(ab_user.keys).to be_empty
end
context "when on_trial_complete is set" do
before { Split.configuration.on_trial_complete = :some_method }
it "should call the method" do
expect(self).to receive(:some_method)
ab_finished(@experiment_name)
end
it "should not call the method without alternative" do
ab_user[@experiment.key] = nil
expect(self).not_to receive(:some_method)
ab_finished(@experiment_name)
end
end
end
context "for an experiment that the user is excluded from" do
before do
alternative = ab_test("link_color", "blue", "red")
expect(Split::Alternative.new(alternative, "link_color").participant_count).to eq(1)
alternative = ab_test("button_size", "small", "big")
expect(Split::Alternative.new(alternative, "button_size").participant_count).to eq(0)
end
it "should not increment the completed counter" do
# So, user should be participating in the link_color experiment and
# receive the control for button_size. As the user is not participating in
# the button size experiment, finishing it should not increase the
# completion count for that alternative.
expect {
ab_finished("button_size")
}.not_to change { Split::Alternative.new("small", "button_size").completed_count }
end
end
context "for an experiment that the user does not participate in" do
before do
Split::ExperimentCatalog.find_or_create(:not_started_experiment, "control", "alt")
end
it "should not raise an exception" do
expect { ab_finished(:not_started_experiment) }.not_to raise_exception
end
it "should not change the user state when reset is false" do
expect { ab_finished(:not_started_experiment, reset: false) }.not_to change { ab_user.keys }.from([])
end
it "should not change the user state when reset is true" do
expect(self).not_to receive(:reset!)
ab_finished(:not_started_experiment)
end
it "should not increment the completed counter" do
ab_finished(:not_started_experiment)
expect(Split::Alternative.new("control", :not_started_experiment).completed_count).to eq(0)
expect(Split::Alternative.new("alt", :not_started_experiment).completed_count).to eq(0)
end
end
end
context "finished with config" do
it "passes reset option" do
Split.configuration.experiments = {
my_experiment: {
alternatives: ["one", "two"],
resettable: false,
}
}
alternative = ab_test(:my_experiment)
experiment = Split::ExperimentCatalog.find :my_experiment
ab_finished :my_experiment
expect(ab_user[experiment.key]).to eq(alternative)
expect(ab_user[experiment.finished_key]).to eq(true)
end
end
context "finished with metric name" do
before { Split.configuration.experiments = {} }
before { expect(Split::Alternative).to receive(:new).at_least(1).times.and_call_original }
def should_finish_experiment(experiment_name, should_finish = true)
alts = Split.configuration.experiments[experiment_name][:alternatives]
experiment = Split::ExperimentCatalog.find_or_create(experiment_name, *alts)
alt_name = ab_user[experiment.key] = alts.first
alt = double("alternative")
expect(alt).to receive(:name).at_most(1).times.and_return(alt_name)
expect(Split::Alternative).to receive(:new).at_most(1).times.with(alt_name, experiment_name.to_s).and_return(alt)
if should_finish
expect(alt).to receive(:increment_completion).at_most(1).times
else
expect(alt).not_to receive(:increment_completion)
end
end
it "completes the test" do
Split.configuration.experiments[:my_experiment] = {
alternatives: [ "control_opt", "other_opt" ],
metric: :my_metric
}
should_finish_experiment :my_experiment
ab_finished :my_metric
end
it "completes all relevant tests" do
Split.configuration.experiments = {
exp_1: {
alternatives: [ "1-1", "1-2" ],
metric: :my_metric
},
exp_2: {
alternatives: [ "2-1", "2-2" ],
metric: :another_metric
},
exp_3: {
alternatives: [ "3-1", "3-2" ],
metric: :my_metric
},
}
should_finish_experiment :exp_1
should_finish_experiment :exp_2, false
should_finish_experiment :exp_3
ab_finished :my_metric
end
it "passes reset option" do
Split.configuration.experiments = {
my_exp: {
alternatives: ["one", "two"],
metric: :my_metric,
resettable: false,
}
}
alternative_name = ab_test(:my_exp)
exp = Split::ExperimentCatalog.find :my_exp
ab_finished :my_metric
expect(ab_user[exp.key]).to eq(alternative_name)
expect(ab_user[exp.finished_key]).to be_truthy
end
it "passes through options" do
Split.configuration.experiments = {
my_exp: {
alternatives: ["one", "two"],
metric: :my_metric,
}
}
alternative_name = ab_test(:my_exp)
exp = Split::ExperimentCatalog.find :my_exp
ab_finished :my_metric, reset: false
expect(ab_user[exp.key]).to eq(alternative_name)
expect(ab_user[exp.finished_key]).to be_truthy
end
end
describe "conversions" do
it "should return a conversion rate for an alternative" do
alternative_name = ab_test("link_color", "blue", "red")
previous_convertion_rate = Split::Alternative.new(alternative_name, "link_color").conversion_rate
expect(previous_convertion_rate).to eq(0.0)
ab_finished("link_color")
new_convertion_rate = Split::Alternative.new(alternative_name, "link_color").conversion_rate
expect(new_convertion_rate).to eq(1.0)
end
end
describe "active experiments" do
it "should show an active test" do
alternative = ab_test("def", "4", "5", "6")
expect(active_experiments.count).to eq 1
expect(active_experiments.first[0]).to eq "def"
expect(active_experiments.first[1]).to eq alternative
end
it "should show a finished test" do
alternative = ab_test("def", "4", "5", "6")
ab_finished("def", { reset: false })
expect(active_experiments.count).to eq 1
expect(active_experiments.first[0]).to eq "def"
expect(active_experiments.first[1]).to eq alternative
end
it "should show an active test when an experiment is on a later version" do
experiment.reset
expect(experiment.version).to eq(1)
ab_test("link_color", "blue", "red")
expect(active_experiments.count).to eq 1
expect(active_experiments.first[0]).to eq "link_color"
end
it "should show versioned tests properly" do
10.times { experiment.reset }
alternative = ab_test(experiment.name, "blue", "red")
ab_finished(experiment.name, reset: false)
expect(experiment.version).to eq(10)
expect(active_experiments.count).to eq 1
expect(active_experiments).to eq({ "link_color" => alternative })
end
it "should show multiple tests" do
Split.configure do |config|
config.allow_multiple_experiments = true
end
alternative = ab_test("def", "4", "5", "6")
another_alternative = ab_test("ghi", "7", "8", "9")
expect(active_experiments.count).to eq 2
expect(active_experiments["def"]).to eq alternative
expect(active_experiments["ghi"]).to eq another_alternative
end
it "should not show tests with winners" do
Split.configure do |config|
config.allow_multiple_experiments = true
end
e = Split::ExperimentCatalog.find_or_create("def", "4", "5", "6")
e.winner = "4"
ab_test("def", "4", "5", "6")
another_alternative = ab_test("ghi", "7", "8", "9")
expect(active_experiments.count).to eq 1
expect(active_experiments.first[0]).to eq "ghi"
expect(active_experiments.first[1]).to eq another_alternative
end
end
describe "when user is a robot" do
before(:each) do
@request = OpenStruct.new(user_agent: "Googlebot/2.1 (+http://www.google.com/bot.html)")
end
describe "ab_test" do
it "should return the control" do
alternative = ab_test("link_color", "blue", "red")
expect(alternative).to eq experiment.control.name
end
it "should not create a experiment" do
ab_test("link_color", "blue", "red")
expect(Split::Experiment.new("link_color")).to be_a_new_record
end
it "should not increment the participation count" do
previous_red_count = Split::Alternative.new("red", "link_color").participant_count
previous_blue_count = Split::Alternative.new("blue", "link_color").participant_count
ab_test("link_color", "blue", "red")
new_red_count = Split::Alternative.new("red", "link_color").participant_count
new_blue_count = Split::Alternative.new("blue", "link_color").participant_count
expect((new_red_count + new_blue_count)).to eq(previous_red_count + previous_blue_count)
end
end
describe "finished" do
it "should not increment the completed count" do
alternative_name = ab_test("link_color", "blue", "red")
previous_completion_count = Split::Alternative.new(alternative_name, "link_color").completed_count
ab_finished("link_color")
new_completion_count = Split::Alternative.new(alternative_name, "link_color").completed_count
expect(new_completion_count).to eq(previous_completion_count)
end
end
end
describe "when providing custom ignore logic" do
context "using a proc to configure custom logic" do
before(:each) do
Split.configure do |c|
c.ignore_filter = proc { |request| true } # ignore everything
end
end
it "ignores the ab_test" do
ab_test("link_color", "blue", "red")
red_count = Split::Alternative.new("red", "link_color").participant_count
blue_count = Split::Alternative.new("blue", "link_color").participant_count
expect((red_count + blue_count)).to be(0)
end
end
end
shared_examples_for "a disabled test" do
describe "ab_test" do
it "should return the control" do
alternative = ab_test("link_color", "blue", "red")
expect(alternative).to eq experiment.control.name
end
it "should not increment the participation count" do
previous_red_count = Split::Alternative.new("red", "link_color").participant_count
previous_blue_count = Split::Alternative.new("blue", "link_color").participant_count
ab_test("link_color", "blue", "red")
new_red_count = Split::Alternative.new("red", "link_color").participant_count
new_blue_count = Split::Alternative.new("blue", "link_color").participant_count
expect((new_red_count + new_blue_count)).to eq(previous_red_count + previous_blue_count)
end
end
describe "finished" do
it "should not increment the completed count" do
alternative_name = ab_test("link_color", "blue", "red")
previous_completion_count = Split::Alternative.new(alternative_name, "link_color").completed_count
ab_finished("link_color")
new_completion_count = Split::Alternative.new(alternative_name, "link_color").completed_count
expect(new_completion_count).to eq(previous_completion_count)
end
end
end
describe "when ip address is ignored" do
context "individually" do
before(:each) do
@request = OpenStruct.new(ip: "81.19.48.130")
Split.configure do |c|
c.ignore_ip_addresses << "81.19.48.130"
end
end
it_behaves_like "a disabled test"
end
context "for a range" do
before(:each) do
@request = OpenStruct.new(ip: "81.19.48.129")
Split.configure do |c|
c.ignore_ip_addresses << /81\.19\.48\.[0-9]+/
end
end
it_behaves_like "a disabled test"
end
context "using both a range and a specific value" do
before(:each) do
@request = OpenStruct.new(ip: "81.19.48.128")
Split.configure do |c|
c.ignore_ip_addresses << "81.19.48.130"
c.ignore_ip_addresses << /81\.19\.48\.[0-9]+/
end
end
it_behaves_like "a disabled test"
end
context "when ignored other address" do
before do
@request = OpenStruct.new(ip: "1.1.1.1")
Split.configure do |c|
c.ignore_ip_addresses << "81.19.48.130"
end
end
it "works as usual" do
alternative_name = ab_test("link_color", "red", "blue")
expect {
ab_finished("link_color")
}.to change(Split::Alternative.new(alternative_name, "link_color"), :completed_count).by(1)
end
end
end
describe "when user is previewing" do
before(:each) do
@request = OpenStruct.new(headers: { "x-purpose" => "preview" })
end
it_behaves_like "a disabled test"
end
describe "versioned experiments" do
it "should use version zero if no version is present" do
alternative_name = ab_test("link_color", "blue", "red")
expect(experiment.version).to eq(0)
expect(ab_user["link_color"]).to eq(alternative_name)
end
it "should save the version of the experiment to the session" do
experiment.reset
expect(experiment.version).to eq(1)
alternative_name = ab_test("link_color", "blue", "red")
expect(ab_user["link_color:1"]).to eq(alternative_name)
end
it "should load the experiment even if the version is not 0" do
experiment.reset
expect(experiment.version).to eq(1)
alternative_name = ab_test("link_color", "blue", "red")
expect(ab_user["link_color:1"]).to eq(alternative_name)
return_alternative_name = ab_test("link_color", "blue", "red")
expect(return_alternative_name).to eq(alternative_name)
end
it "should reset the session of a user on an older version of the experiment" do
alternative_name = ab_test("link_color", "blue", "red")
expect(ab_user["link_color"]).to eq(alternative_name)
alternative = Split::Alternative.new(alternative_name, "link_color")
expect(alternative.participant_count).to eq(1)
experiment.reset
expect(experiment.version).to eq(1)
alternative = Split::Alternative.new(alternative_name, "link_color")
expect(alternative.participant_count).to eq(0)
new_alternative_name = ab_test("link_color", "blue", "red")
expect(ab_user["link_color:1"]).to eq(new_alternative_name)
new_alternative = Split::Alternative.new(new_alternative_name, "link_color")
expect(new_alternative.participant_count).to eq(1)
end
it "should cleanup old versions of experiments from the session" do
alternative_name = ab_test("link_color", "blue", "red")
expect(ab_user["link_color"]).to eq(alternative_name)
alternative = Split::Alternative.new(alternative_name, "link_color")
expect(alternative.participant_count).to eq(1)
experiment.reset
expect(experiment.version).to eq(1)
alternative = Split::Alternative.new(alternative_name, "link_color")
expect(alternative.participant_count).to eq(0)
new_alternative_name = ab_test("link_color", "blue", "red")
expect(ab_user["link_color:1"]).to eq(new_alternative_name)
end
it "should only count completion of users on the current version" do
alternative_name = ab_test("link_color", "blue", "red")
expect(ab_user["link_color"]).to eq(alternative_name)
Split::Alternative.new(alternative_name, "link_color")
experiment.reset
expect(experiment.version).to eq(1)
ab_finished("link_color")
alternative = Split::Alternative.new(alternative_name, "link_color")
expect(alternative.completed_count).to eq(0)
end
end
context "when redis is not available" do
before(:each) do
expect(Split).to receive(:redis).at_most(5).times.and_raise(Errno::ECONNREFUSED.new)
end
context "and db_failover config option is turned off" do
before(:each) do
Split.configure do |config|
config.db_failover = false
end
end
describe "ab_test" do
it "should raise an exception" do
expect { ab_test("link_color", "blue", "red") }.to raise_error(Errno::ECONNREFUSED)
end
end
describe "finished" do
it "should raise an exception" do
expect { ab_finished("link_color") }.to raise_error(Errno::ECONNREFUSED)
end
end
describe "disable split testing" do
before(:each) do
Split.configure do |config|
config.enabled = false
end
end
it "should not attempt to connect to redis" do
expect { ab_test("link_color", "blue", "red") }.not_to raise_error
end
it "should return control variable" do
expect(ab_test("link_color", "blue", "red")).to eq("blue")
expect { ab_finished("link_color") }.not_to raise_error
end
end
end
context "and db_failover config option is turned on" do
before(:each) do
Split.configure do |config|
config.db_failover = true
end
end
describe "ab_test" do
it "should not raise an exception" do
expect { ab_test("link_color", "blue", "red") }.not_to raise_error
end
it "should call db_failover_on_db_error proc with error as parameter" do
Split.configure do |config|
config.db_failover_on_db_error = proc do |error|
expect(error).to be_a(Errno::ECONNREFUSED)
end
end
expect(Split.configuration.db_failover_on_db_error).to receive(:call).and_call_original
ab_test("link_color", "blue", "red")
end
it "should always use first alternative" do
expect(ab_test("link_color", "blue", "red")).to eq("blue")
expect(ab_test("link_color", { "blue" => 0.01 }, "red" => 0.2)).to eq("blue")
expect(ab_test("link_color", { "blue" => 0.8 }, { "red" => 20 })).to eq("blue")
expect(ab_test("link_color", "blue", "red") do |alternative|
"shared/#{alternative}"
end).to eq("shared/blue")
end
context "and db_failover_allow_parameter_override config option is turned on" do
before(:each) do
Split.configure do |config|
config.db_failover_allow_parameter_override = true
end
end
context "and given an override parameter" do
it "should use given override instead of the first alternative" do
@params = { "ab_test" => { "link_color" => "red" } }
expect(ab_test("link_color", "blue", "red")).to eq("red")
expect(ab_test("link_color", "blue", "red", "green")).to eq("red")
expect(ab_test("link_color", { "blue" => 0.01 }, "red" => 0.2)).to eq("red")
expect(ab_test("link_color", { "blue" => 0.8 }, { "red" => 20 })).to eq("red")
expect(ab_test("link_color", "blue", "red") do |alternative|
"shared/#{alternative}"
end).to eq("shared/red")
end
end
end
context "and preloaded config given" do
before do
Split.configuration.experiments[:link_color] = {
alternatives: [ "blue", "red" ],
}
end
it "uses first alternative" do
expect(ab_test(:link_color)).to eq("blue")
end
end
end
describe "finished" do
it "should not raise an exception" do
expect { ab_finished("link_color") }.not_to raise_error
end
it "should call db_failover_on_db_error proc with error as parameter" do
Split.configure do |config|
config.db_failover_on_db_error = proc do |error|
expect(error).to be_a(Errno::ECONNREFUSED)
end
end
expect(Split.configuration.db_failover_on_db_error).to receive(:call).and_call_original
ab_finished("link_color")
end
end
end
end
context "with preloaded config" do
before { Split.configuration.experiments = {} }
it "pulls options from config file" do
Split.configuration.experiments[:my_experiment] = {
alternatives: [ "control_opt", "other_opt" ],
goals: ["goal1", "goal2"]
}
ab_test :my_experiment
expect(Split::Experiment.new(:my_experiment).alternatives.map(&:name)).to eq([ "control_opt", "other_opt" ])
expect(Split::Experiment.new(:my_experiment).goals).to eq([ "goal1", "goal2" ])
end
it "can be called multiple times" do
Split.configuration.experiments[:my_experiment] = {
alternatives: [ "control_opt", "other_opt" ],
goals: ["goal1", "goal2"]
}
5.times { ab_test :my_experiment }
experiment = Split::Experiment.new(:my_experiment)
expect(experiment.alternatives.map(&:name)).to eq([ "control_opt", "other_opt" ])
expect(experiment.goals).to eq([ "goal1", "goal2" ])
expect(experiment.participant_count).to eq(1)
end
it "accepts multiple goals" do
Split.configuration.experiments[:my_experiment] = {
alternatives: [ "control_opt", "other_opt" ],
goals: [ "goal1", "goal2", "goal3" ]
}
ab_test :my_experiment
experiment = Split::Experiment.new(:my_experiment)
expect(experiment.goals).to eq([ "goal1", "goal2", "goal3" ])
end
it "allow specifying goals to be optional" do
Split.configuration.experiments[:my_experiment] = {
alternatives: [ "control_opt", "other_opt" ]
}
experiment = Split::Experiment.new(:my_experiment)
expect(experiment.goals).to eq([])
end
it "accepts multiple alternatives" do
Split.configuration.experiments[:my_experiment] = {
alternatives: [ "control_opt", "second_opt", "third_opt" ],
}
ab_test :my_experiment
experiment = Split::Experiment.new(:my_experiment)
expect(experiment.alternatives.map(&:name)).to eq([ "control_opt", "second_opt", "third_opt" ])
end
it "accepts probability on alternatives" do
Split.configuration.experiments[:my_experiment] = {
alternatives: [
{ name: "control_opt", percent: 67 },
{ name: "second_opt", percent: 10 },
{ name: "third_opt", percent: 23 },
],
}
ab_test :my_experiment
experiment = Split::Experiment.new(:my_experiment)
expect(experiment.alternatives.collect { |a| [a.name, a.weight] }).to eq([["control_opt", 0.67], ["second_opt", 0.1], ["third_opt", 0.23]])
end
it "accepts probability on some alternatives" do
Split.configuration.experiments[:my_experiment] = {
alternatives: [
{ name: "control_opt", percent: 34 },
"second_opt",
{ name: "third_opt", percent: 23 },
"fourth_opt",
],
}
ab_test :my_experiment
experiment = Split::Experiment.new(:my_experiment)
names_and_weights = experiment.alternatives.collect { |a| [a.name, a.weight] }
expect(names_and_weights).to eq([["control_opt", 0.34], ["second_opt", 0.215], ["third_opt", 0.23], ["fourth_opt", 0.215]])
expect(names_and_weights.inject(0) { |sum, nw| sum + nw[1] }).to eq(1.0)
end
it "allows name param without probability" do
Split.configuration.experiments[:my_experiment] = {
alternatives: [
{ name: "control_opt" },
"second_opt",
{ name: "third_opt", percent: 64 },
],
}
ab_test :my_experiment
experiment = Split::Experiment.new(:my_experiment)
names_and_weights = experiment.alternatives.collect { |a| [a.name, a.weight] }
expect(names_and_weights).to eq([["control_opt", 0.18], ["second_opt", 0.18], ["third_opt", 0.64]])
expect(names_and_weights.inject(0) { |sum, nw| sum + nw[1] }).to eq(1.0)
end
it "fails gracefully if config is missing experiment" do
Split.configuration.experiments = { other_experiment: { foo: "Bar" } }
expect { ab_test :my_experiment }.to raise_error(Split::ExperimentNotFound)
end
it "fails gracefully if config is missing" do
expect { Split.configuration.experiments = nil }.to raise_error(Split::InvalidExperimentsFormatError)
end
it "fails gracefully if config is missing alternatives" do
Split.configuration.experiments[:my_experiment] = { foo: "Bar" }
expect { ab_test :my_experiment }.to raise_error(NoMethodError)
end
end
it "should handle multiple experiments correctly" do
experiment2 = Split::ExperimentCatalog.find_or_create("link_color2", "blue", "red")
ab_test("link_color", "blue", "red")
ab_test("link_color2", "blue", "red")
ab_finished("link_color2")
experiment2.alternatives.each do |alt|
expect(alt.unfinished_count).to eq(0)
end
end
context "with goals" do
before do
@experiment = { "link_color" => ["purchase", "refund"] }
@alternatives = ["blue", "red"]
@experiment_name, @goals = normalize_metric(@experiment)
@goal1 = @goals[0]
@goal2 = @goals[1]
end
it "should normalize experiment" do
expect(@experiment_name).to eq("link_color")
expect(@goals).to eq(["purchase", "refund"])
end
describe "ab_test" do
it "should allow experiment goals interface as a single hash" do
ab_test(@experiment, *@alternatives)
experiment = Split::ExperimentCatalog.find("link_color")
expect(experiment.goals).to eq(["purchase", "refund"])
end
end
describe "ab_finished" do
before do
@alternative_name = ab_test(@experiment, *@alternatives)
end
it "should increment the counter for the specified-goal completed alternative" do
expect { ab_finished({ "link_color" => ["purchase"] }) }
.to change { Split::Alternative.new(@alternative_name, @experiment_name).completed_count(@goal2) }.by(0)
.and change { Split::Alternative.new(@alternative_name, @experiment_name).completed_count(@goal1) }.by(1)
end
end
end
end
<MSG> Merge pull request #34 from layflags/master
Bug fix for ab_test helper
<DFF> @@ -50,6 +50,13 @@ describe Split::Helper do
@params = {'link_color' => 'blue'}
alternative = ab_test('link_color', 'blue', 'red')
alternative.should eql('blue')
+ alternative = ab_test('link_color', 'blue' => 1, 'red' => 5)
+ alternative.should eql('blue')
+ @params = {'link_color' => 'red'}
+ alternative = ab_test('link_color', 'blue', 'red')
+ alternative.should eql('red')
+ alternative = ab_test('link_color', 'blue' => 5, 'red' => 1)
+ alternative.should eql('red')
end
it "should allow passing a block" do
| 7 | Merge pull request #34 from layflags/master | 0 | .rb | rb | mit | splitrb/split |
10072566 | <NME> helper_spec.rb
<BEF> # frozen_string_literal: true
require "spec_helper"
# TODO change some of these tests to use Rack::Test
describe Split::Helper do
include Split::Helper
let(:experiment) {
Split::ExperimentCatalog.find_or_create("link_color", "blue", "red")
}
describe "ab_test" do
it "should not raise an error when passed strings for alternatives" do
expect { ab_test("xyz", "1", "2", "3") }.not_to raise_error
end
it "should not raise an error when passed an array for alternatives" do
expect { ab_test("xyz", ["1", "2", "3"]) }.not_to raise_error
end
it "should raise the appropriate error when passed integers for alternatives" do
expect { ab_test("xyz", 1, 2, 3) }.to raise_error(ArgumentError)
end
it "should raise the appropriate error when passed symbols for alternatives" do
expect { ab_test("xyz", :a, :b, :c) }.to raise_error(ArgumentError)
end
it "should not raise error when passed an array for goals" do
expect { ab_test({ "link_color" => ["purchase", "refund"] }, "blue", "red") }.not_to raise_error
end
it "should not raise error when passed just one goal" do
expect { ab_test({ "link_color" => "purchase" }, "blue", "red") }.not_to raise_error
end
it "raises an appropriate error when processing combined expirements" do
Split.configuration.experiments = {
combined_exp_1: {
alternatives: [ { name: "control", percent: 50 }, { name: "test-alt", percent: 50 } ],
metric: :my_metric,
combined_experiments: [:combined_exp_1_sub_1]
}
}
Split::ExperimentCatalog.find_or_create("combined_exp_1")
expect { ab_test("combined_exp_1") }.to raise_error(Split::InvalidExperimentsFormatError)
end
@params = {'link_color' => 'blue'}
alternative = ab_test('link_color', 'blue', 'red')
alternative.should eql('blue')
end
it "should allow passing a block" do
new_blue_count = Split::Alternative.new("blue", "link_color").participant_count
expect((new_red_count + new_blue_count)).to eq(previous_red_count + previous_blue_count + 1)
end
it "should not increment the counter for an experiment that the user is not participating in" do
ab_test("link_color", "blue", "red")
e = Split::ExperimentCatalog.find_or_create("button_size", "small", "big")
expect {
# User shouldn't participate in this second experiment
ab_test("button_size", "small", "big")
}.not_to change { e.participant_count }
end
it "should not increment the counter for an ended experiment" do
e = Split::ExperimentCatalog.find_or_create("button_size", "small", "big")
e.winner = "small"
expect {
a = ab_test("button_size", "small", "big")
expect(a).to eq("small")
}.not_to change { e.participant_count }
end
it "should not increment the counter for an not started experiment" do
expect(Split.configuration).to receive(:start_manually).and_return(true)
e = Split::ExperimentCatalog.find_or_create("button_size", "small", "big")
expect {
a = ab_test("button_size", "small", "big")
expect(a).to eq("small")
}.not_to change { e.participant_count }
end
it "should return the given alternative for an existing user" do
expect(ab_test("link_color", "blue", "red")).to eq ab_test("link_color", "blue", "red")
end
it "should always return the winner if one is present" do
experiment.winner = "orange"
expect(ab_test("link_color", "blue", "red")).to eq("orange")
end
it "should allow the alternative to be forced by passing it in the params" do
# ?ab_test[link_color]=blue
@params = { "ab_test" => { "link_color" => "blue" } }
alternative = ab_test("link_color", "blue", "red")
expect(alternative).to eq("blue")
alternative = ab_test("link_color", { "blue" => 1 }, "red" => 5)
expect(alternative).to eq("blue")
@params = { "ab_test" => { "link_color" => "red" } }
alternative = ab_test("link_color", "blue", "red")
expect(alternative).to eq("red")
alternative = ab_test("link_color", { "blue" => 5 }, "red" => 1)
expect(alternative).to eq("red")
end
it "should not allow an arbitrary alternative" do
@params = { "ab_test" => { "link_color" => "pink" } }
alternative = ab_test("link_color", "blue")
expect(alternative).to eq("blue")
end
it "should not store the split when a param forced alternative" do
@params = { "ab_test" => { "link_color" => "blue" } }
expect(ab_user).not_to receive(:[]=)
ab_test("link_color", "blue", "red")
end
it "SPLIT_DISABLE query parameter should also force the alternative (uses control)" do
@params = { "SPLIT_DISABLE" => "true" }
alternative = ab_test("link_color", "blue", "red")
expect(alternative).to eq("blue")
alternative = ab_test("link_color", { "blue" => 1 }, "red" => 5)
expect(alternative).to eq("blue")
alternative = ab_test("link_color", "red", "blue")
expect(alternative).to eq("red")
alternative = ab_test("link_color", { "red" => 5 }, "blue" => 1)
expect(alternative).to eq("red")
end
it "should not store the split when Split generically disabled" do
@params = { "SPLIT_DISABLE" => "true" }
expect(ab_user).not_to receive(:[]=)
ab_test("link_color", "blue", "red")
end
context "when store_override is set" do
before { Split.configuration.store_override = true }
it "should store the forced alternative" do
@params = { "ab_test" => { "link_color" => "blue" } }
expect(ab_user).to receive(:[]=).with("link_color", "blue")
ab_test("link_color", "blue", "red")
end
end
context "when on_trial_choose is set" do
before { Split.configuration.on_trial_choose = :some_method }
it "should call the method" do
expect(self).to receive(:some_method)
ab_test("link_color", "blue", "red")
end
end
it "should allow passing a block" do
alt = ab_test("link_color", "blue", "red")
ret = ab_test("link_color", "blue", "red") { |alternative| "shared/#{alternative}" }
expect(ret).to eq("shared/#{alt}")
end
it "should allow the share of visitors see an alternative to be specified" do
ab_test("link_color", { "blue" => 0.8 }, { "red" => 20 })
expect(["red", "blue"]).to include(ab_user["link_color"])
end
it "should allow alternative weighting interface as a single hash" do
ab_test("link_color", { "blue" => 0.01 }, "red" => 0.2)
experiment = Split::ExperimentCatalog.find("link_color")
expect(experiment.alternatives.map(&:name)).to eq(["blue", "red"])
expect(experiment.alternatives.collect { |a| a.weight }).to match_array([0.01, 0.2])
end
it "should only let a user participate in one experiment at a time" do
link_color = ab_test("link_color", "blue", "red")
ab_test("button_size", "small", "big")
expect(ab_user["link_color"]).to eq(link_color)
big = Split::Alternative.new("big", "button_size")
expect(big.participant_count).to eq(0)
small = Split::Alternative.new("small", "button_size")
expect(small.participant_count).to eq(0)
end
it "should let a user participate in many experiment with allow_multiple_experiments option" do
Split.configure do |config|
config.allow_multiple_experiments = true
end
link_color = ab_test("link_color", "blue", "red")
button_size = ab_test("button_size", "small", "big")
expect(ab_user["link_color"]).to eq(link_color)
expect(ab_user["button_size"]).to eq(button_size)
button_size_alt = Split::Alternative.new(button_size, "button_size")
expect(button_size_alt.participant_count).to eq(1)
end
context "with allow_multiple_experiments = 'control'" do
it "should let a user participate in many experiment with one non-'control' alternative" do
Split.configure do |config|
config.allow_multiple_experiments = "control"
end
groups = 100.times.map do |n|
ab_test("test#{n}".to_sym, { "control" => (100 - n) }, { "test#{n}-alt" => n })
end
experiments = ab_user.active_experiments
expect(experiments.size).to be > 1
count_control = experiments.values.count { |g| g == "control" }
expect(count_control).to eq(experiments.size - 1)
count_alts = groups.count { |g| g != "control" }
expect(count_alts).to eq(1)
end
context "when user already has experiment" do
let(:mock_user) { Split::User.new(self, { "test_0" => "test-alt" }) }
before do
Split.configure do |config|
config.allow_multiple_experiments = "control"
end
Split::ExperimentCatalog.find_or_initialize("test_0", "control", "test-alt").save
Split::ExperimentCatalog.find_or_initialize("test_1", "control", "test-alt").save
end
it "should restore previously selected alternative" do
expect(ab_user.active_experiments.size).to eq 1
expect(ab_test(:test_0, { "control" => 100 }, { "test-alt" => 1 })).to eq "test-alt"
expect(ab_test(:test_0, { "control" => 1 }, { "test-alt" => 100 })).to eq "test-alt"
end
it "should select the correct alternatives after experiment resets" do
experiment = Split::ExperimentCatalog.find(:test_0)
experiment.reset
mock_user[experiment.key] = "test-alt"
expect(ab_user.active_experiments.size).to eq 1
expect(ab_test(:test_0, { "control" => 100 }, { "test-alt" => 1 })).to eq "test-alt"
expect(ab_test(:test_0, { "control" => 0 }, { "test-alt" => 100 })).to eq "test-alt"
end
it "lets override existing choice" do
pending "this requires user store reset on first call not depending on whelther it is current trial"
@params = { "ab_test" => { "test_1" => "test-alt" } }
expect(ab_test(:test_0, { "control" => 0 }, { "test-alt" => 100 })).to eq "control"
expect(ab_test(:test_1, { "control" => 100 }, { "test-alt" => 1 })).to eq "test-alt"
end
end
end
it "should not over-write a finished key when an experiment is on a later version" do
experiment.increment_version
ab_user = { experiment.key => "blue", experiment.finished_key => true }
finished_session = ab_user.dup
ab_test("link_color", "blue", "red")
expect(ab_user).to eq(finished_session)
end
end
describe "metadata" do
context "is defined" do
before do
Split.configuration.experiments = {
my_experiment: {
alternatives: ["one", "two"],
resettable: false,
metadata: { "one" => "Meta1", "two" => "Meta2" }
}
}
end
it "should be passed to helper block" do
@params = { "ab_test" => { "my_experiment" => "two" } }
expect(ab_test("my_experiment")).to eq "two"
expect(ab_test("my_experiment") do |alternative, meta|
meta
end).to eq("Meta2")
end
it "should pass control metadata helper block if library disabled" do
Split.configure do |config|
config.enabled = false
end
expect(ab_test("my_experiment")).to eq "one"
expect(ab_test("my_experiment") do |_, meta|
meta
end).to eq("Meta1")
end
end
context "is not defined" do
before do
Split.configuration.experiments = {
my_experiment: {
alternatives: ["one", "two"],
resettable: false,
metadata: nil
}
}
end
it "should be passed to helper block" do
expect(ab_test("my_experiment") do |alternative, meta|
meta
end).to eq({})
end
it "should pass control metadata helper block if library disabled" do
Split.configure do |config|
config.enabled = false
end
expect(ab_test("my_experiment") do |_, meta|
meta
end).to eq({})
end
end
end
describe "ab_finished" do
context "for an experiment that the user participates in" do
before(:each) do
@experiment_name = "link_color"
@alternatives = ["blue", "red"]
@experiment = Split::ExperimentCatalog.find_or_create(@experiment_name, *@alternatives)
@alternative_name = ab_test(@experiment_name, *@alternatives)
@previous_completion_count = Split::Alternative.new(@alternative_name, @experiment_name).completed_count
end
it "should increment the counter for the completed alternative" do
ab_finished(@experiment_name)
new_completion_count = Split::Alternative.new(@alternative_name, @experiment_name).completed_count
expect(new_completion_count).to eq(@previous_completion_count + 1)
end
it "should set experiment's finished key if reset is false" do
ab_finished(@experiment_name, { reset: false })
expect(ab_user[@experiment.key]).to eq(@alternative_name)
expect(ab_user[@experiment.finished_key]).to eq(true)
end
it "should not increment the counter if reset is false and the experiment has been already finished" do
2.times { ab_finished(@experiment_name, { reset: false }) }
new_completion_count = Split::Alternative.new(@alternative_name, @experiment_name).completed_count
expect(new_completion_count).to eq(@previous_completion_count + 1)
end
it "should not increment the counter for an ended experiment" do
e = Split::ExperimentCatalog.find_or_create("button_size", "small", "big")
e.winner = "small"
a = ab_test("button_size", "small", "big")
expect(a).to eq("small")
expect {
ab_finished("button_size")
}.not_to change { Split::Alternative.new(a, "button_size").completed_count }
end
it "should clear out the user's participation from their session" do
expect(ab_user[@experiment.key]).to eq(@alternative_name)
ab_finished(@experiment_name)
expect(ab_user.keys).to be_empty
end
it "should not clear out the users session if reset is false" do
expect(ab_user[@experiment.key]).to eq(@alternative_name)
ab_finished(@experiment_name, { reset: false })
expect(ab_user[@experiment.key]).to eq(@alternative_name)
expect(ab_user[@experiment.finished_key]).to eq(true)
end
it "should reset the users session when experiment is not versioned" do
expect(ab_user[@experiment.key]).to eq(@alternative_name)
ab_finished(@experiment_name)
expect(ab_user.keys).to be_empty
end
it "should reset the users session when experiment is versioned" do
@experiment.increment_version
@alternative_name = ab_test(@experiment_name, *@alternatives)
expect(ab_user[@experiment.key]).to eq(@alternative_name)
ab_finished(@experiment_name)
expect(ab_user.keys).to be_empty
end
context "when on_trial_complete is set" do
before { Split.configuration.on_trial_complete = :some_method }
it "should call the method" do
expect(self).to receive(:some_method)
ab_finished(@experiment_name)
end
it "should not call the method without alternative" do
ab_user[@experiment.key] = nil
expect(self).not_to receive(:some_method)
ab_finished(@experiment_name)
end
end
end
context "for an experiment that the user is excluded from" do
before do
alternative = ab_test("link_color", "blue", "red")
expect(Split::Alternative.new(alternative, "link_color").participant_count).to eq(1)
alternative = ab_test("button_size", "small", "big")
expect(Split::Alternative.new(alternative, "button_size").participant_count).to eq(0)
end
it "should not increment the completed counter" do
# So, user should be participating in the link_color experiment and
# receive the control for button_size. As the user is not participating in
# the button size experiment, finishing it should not increase the
# completion count for that alternative.
expect {
ab_finished("button_size")
}.not_to change { Split::Alternative.new("small", "button_size").completed_count }
end
end
context "for an experiment that the user does not participate in" do
before do
Split::ExperimentCatalog.find_or_create(:not_started_experiment, "control", "alt")
end
it "should not raise an exception" do
expect { ab_finished(:not_started_experiment) }.not_to raise_exception
end
it "should not change the user state when reset is false" do
expect { ab_finished(:not_started_experiment, reset: false) }.not_to change { ab_user.keys }.from([])
end
it "should not change the user state when reset is true" do
expect(self).not_to receive(:reset!)
ab_finished(:not_started_experiment)
end
it "should not increment the completed counter" do
ab_finished(:not_started_experiment)
expect(Split::Alternative.new("control", :not_started_experiment).completed_count).to eq(0)
expect(Split::Alternative.new("alt", :not_started_experiment).completed_count).to eq(0)
end
end
end
context "finished with config" do
it "passes reset option" do
Split.configuration.experiments = {
my_experiment: {
alternatives: ["one", "two"],
resettable: false,
}
}
alternative = ab_test(:my_experiment)
experiment = Split::ExperimentCatalog.find :my_experiment
ab_finished :my_experiment
expect(ab_user[experiment.key]).to eq(alternative)
expect(ab_user[experiment.finished_key]).to eq(true)
end
end
context "finished with metric name" do
before { Split.configuration.experiments = {} }
before { expect(Split::Alternative).to receive(:new).at_least(1).times.and_call_original }
def should_finish_experiment(experiment_name, should_finish = true)
alts = Split.configuration.experiments[experiment_name][:alternatives]
experiment = Split::ExperimentCatalog.find_or_create(experiment_name, *alts)
alt_name = ab_user[experiment.key] = alts.first
alt = double("alternative")
expect(alt).to receive(:name).at_most(1).times.and_return(alt_name)
expect(Split::Alternative).to receive(:new).at_most(1).times.with(alt_name, experiment_name.to_s).and_return(alt)
if should_finish
expect(alt).to receive(:increment_completion).at_most(1).times
else
expect(alt).not_to receive(:increment_completion)
end
end
it "completes the test" do
Split.configuration.experiments[:my_experiment] = {
alternatives: [ "control_opt", "other_opt" ],
metric: :my_metric
}
should_finish_experiment :my_experiment
ab_finished :my_metric
end
it "completes all relevant tests" do
Split.configuration.experiments = {
exp_1: {
alternatives: [ "1-1", "1-2" ],
metric: :my_metric
},
exp_2: {
alternatives: [ "2-1", "2-2" ],
metric: :another_metric
},
exp_3: {
alternatives: [ "3-1", "3-2" ],
metric: :my_metric
},
}
should_finish_experiment :exp_1
should_finish_experiment :exp_2, false
should_finish_experiment :exp_3
ab_finished :my_metric
end
it "passes reset option" do
Split.configuration.experiments = {
my_exp: {
alternatives: ["one", "two"],
metric: :my_metric,
resettable: false,
}
}
alternative_name = ab_test(:my_exp)
exp = Split::ExperimentCatalog.find :my_exp
ab_finished :my_metric
expect(ab_user[exp.key]).to eq(alternative_name)
expect(ab_user[exp.finished_key]).to be_truthy
end
it "passes through options" do
Split.configuration.experiments = {
my_exp: {
alternatives: ["one", "two"],
metric: :my_metric,
}
}
alternative_name = ab_test(:my_exp)
exp = Split::ExperimentCatalog.find :my_exp
ab_finished :my_metric, reset: false
expect(ab_user[exp.key]).to eq(alternative_name)
expect(ab_user[exp.finished_key]).to be_truthy
end
end
describe "conversions" do
it "should return a conversion rate for an alternative" do
alternative_name = ab_test("link_color", "blue", "red")
previous_convertion_rate = Split::Alternative.new(alternative_name, "link_color").conversion_rate
expect(previous_convertion_rate).to eq(0.0)
ab_finished("link_color")
new_convertion_rate = Split::Alternative.new(alternative_name, "link_color").conversion_rate
expect(new_convertion_rate).to eq(1.0)
end
end
describe "active experiments" do
it "should show an active test" do
alternative = ab_test("def", "4", "5", "6")
expect(active_experiments.count).to eq 1
expect(active_experiments.first[0]).to eq "def"
expect(active_experiments.first[1]).to eq alternative
end
it "should show a finished test" do
alternative = ab_test("def", "4", "5", "6")
ab_finished("def", { reset: false })
expect(active_experiments.count).to eq 1
expect(active_experiments.first[0]).to eq "def"
expect(active_experiments.first[1]).to eq alternative
end
it "should show an active test when an experiment is on a later version" do
experiment.reset
expect(experiment.version).to eq(1)
ab_test("link_color", "blue", "red")
expect(active_experiments.count).to eq 1
expect(active_experiments.first[0]).to eq "link_color"
end
it "should show versioned tests properly" do
10.times { experiment.reset }
alternative = ab_test(experiment.name, "blue", "red")
ab_finished(experiment.name, reset: false)
expect(experiment.version).to eq(10)
expect(active_experiments.count).to eq 1
expect(active_experiments).to eq({ "link_color" => alternative })
end
it "should show multiple tests" do
Split.configure do |config|
config.allow_multiple_experiments = true
end
alternative = ab_test("def", "4", "5", "6")
another_alternative = ab_test("ghi", "7", "8", "9")
expect(active_experiments.count).to eq 2
expect(active_experiments["def"]).to eq alternative
expect(active_experiments["ghi"]).to eq another_alternative
end
it "should not show tests with winners" do
Split.configure do |config|
config.allow_multiple_experiments = true
end
e = Split::ExperimentCatalog.find_or_create("def", "4", "5", "6")
e.winner = "4"
ab_test("def", "4", "5", "6")
another_alternative = ab_test("ghi", "7", "8", "9")
expect(active_experiments.count).to eq 1
expect(active_experiments.first[0]).to eq "ghi"
expect(active_experiments.first[1]).to eq another_alternative
end
end
describe "when user is a robot" do
before(:each) do
@request = OpenStruct.new(user_agent: "Googlebot/2.1 (+http://www.google.com/bot.html)")
end
describe "ab_test" do
it "should return the control" do
alternative = ab_test("link_color", "blue", "red")
expect(alternative).to eq experiment.control.name
end
it "should not create a experiment" do
ab_test("link_color", "blue", "red")
expect(Split::Experiment.new("link_color")).to be_a_new_record
end
it "should not increment the participation count" do
previous_red_count = Split::Alternative.new("red", "link_color").participant_count
previous_blue_count = Split::Alternative.new("blue", "link_color").participant_count
ab_test("link_color", "blue", "red")
new_red_count = Split::Alternative.new("red", "link_color").participant_count
new_blue_count = Split::Alternative.new("blue", "link_color").participant_count
expect((new_red_count + new_blue_count)).to eq(previous_red_count + previous_blue_count)
end
end
describe "finished" do
it "should not increment the completed count" do
alternative_name = ab_test("link_color", "blue", "red")
previous_completion_count = Split::Alternative.new(alternative_name, "link_color").completed_count
ab_finished("link_color")
new_completion_count = Split::Alternative.new(alternative_name, "link_color").completed_count
expect(new_completion_count).to eq(previous_completion_count)
end
end
end
describe "when providing custom ignore logic" do
context "using a proc to configure custom logic" do
before(:each) do
Split.configure do |c|
c.ignore_filter = proc { |request| true } # ignore everything
end
end
it "ignores the ab_test" do
ab_test("link_color", "blue", "red")
red_count = Split::Alternative.new("red", "link_color").participant_count
blue_count = Split::Alternative.new("blue", "link_color").participant_count
expect((red_count + blue_count)).to be(0)
end
end
end
shared_examples_for "a disabled test" do
describe "ab_test" do
it "should return the control" do
alternative = ab_test("link_color", "blue", "red")
expect(alternative).to eq experiment.control.name
end
it "should not increment the participation count" do
previous_red_count = Split::Alternative.new("red", "link_color").participant_count
previous_blue_count = Split::Alternative.new("blue", "link_color").participant_count
ab_test("link_color", "blue", "red")
new_red_count = Split::Alternative.new("red", "link_color").participant_count
new_blue_count = Split::Alternative.new("blue", "link_color").participant_count
expect((new_red_count + new_blue_count)).to eq(previous_red_count + previous_blue_count)
end
end
describe "finished" do
it "should not increment the completed count" do
alternative_name = ab_test("link_color", "blue", "red")
previous_completion_count = Split::Alternative.new(alternative_name, "link_color").completed_count
ab_finished("link_color")
new_completion_count = Split::Alternative.new(alternative_name, "link_color").completed_count
expect(new_completion_count).to eq(previous_completion_count)
end
end
end
describe "when ip address is ignored" do
context "individually" do
before(:each) do
@request = OpenStruct.new(ip: "81.19.48.130")
Split.configure do |c|
c.ignore_ip_addresses << "81.19.48.130"
end
end
it_behaves_like "a disabled test"
end
context "for a range" do
before(:each) do
@request = OpenStruct.new(ip: "81.19.48.129")
Split.configure do |c|
c.ignore_ip_addresses << /81\.19\.48\.[0-9]+/
end
end
it_behaves_like "a disabled test"
end
context "using both a range and a specific value" do
before(:each) do
@request = OpenStruct.new(ip: "81.19.48.128")
Split.configure do |c|
c.ignore_ip_addresses << "81.19.48.130"
c.ignore_ip_addresses << /81\.19\.48\.[0-9]+/
end
end
it_behaves_like "a disabled test"
end
context "when ignored other address" do
before do
@request = OpenStruct.new(ip: "1.1.1.1")
Split.configure do |c|
c.ignore_ip_addresses << "81.19.48.130"
end
end
it "works as usual" do
alternative_name = ab_test("link_color", "red", "blue")
expect {
ab_finished("link_color")
}.to change(Split::Alternative.new(alternative_name, "link_color"), :completed_count).by(1)
end
end
end
describe "when user is previewing" do
before(:each) do
@request = OpenStruct.new(headers: { "x-purpose" => "preview" })
end
it_behaves_like "a disabled test"
end
describe "versioned experiments" do
it "should use version zero if no version is present" do
alternative_name = ab_test("link_color", "blue", "red")
expect(experiment.version).to eq(0)
expect(ab_user["link_color"]).to eq(alternative_name)
end
it "should save the version of the experiment to the session" do
experiment.reset
expect(experiment.version).to eq(1)
alternative_name = ab_test("link_color", "blue", "red")
expect(ab_user["link_color:1"]).to eq(alternative_name)
end
it "should load the experiment even if the version is not 0" do
experiment.reset
expect(experiment.version).to eq(1)
alternative_name = ab_test("link_color", "blue", "red")
expect(ab_user["link_color:1"]).to eq(alternative_name)
return_alternative_name = ab_test("link_color", "blue", "red")
expect(return_alternative_name).to eq(alternative_name)
end
it "should reset the session of a user on an older version of the experiment" do
alternative_name = ab_test("link_color", "blue", "red")
expect(ab_user["link_color"]).to eq(alternative_name)
alternative = Split::Alternative.new(alternative_name, "link_color")
expect(alternative.participant_count).to eq(1)
experiment.reset
expect(experiment.version).to eq(1)
alternative = Split::Alternative.new(alternative_name, "link_color")
expect(alternative.participant_count).to eq(0)
new_alternative_name = ab_test("link_color", "blue", "red")
expect(ab_user["link_color:1"]).to eq(new_alternative_name)
new_alternative = Split::Alternative.new(new_alternative_name, "link_color")
expect(new_alternative.participant_count).to eq(1)
end
it "should cleanup old versions of experiments from the session" do
alternative_name = ab_test("link_color", "blue", "red")
expect(ab_user["link_color"]).to eq(alternative_name)
alternative = Split::Alternative.new(alternative_name, "link_color")
expect(alternative.participant_count).to eq(1)
experiment.reset
expect(experiment.version).to eq(1)
alternative = Split::Alternative.new(alternative_name, "link_color")
expect(alternative.participant_count).to eq(0)
new_alternative_name = ab_test("link_color", "blue", "red")
expect(ab_user["link_color:1"]).to eq(new_alternative_name)
end
it "should only count completion of users on the current version" do
alternative_name = ab_test("link_color", "blue", "red")
expect(ab_user["link_color"]).to eq(alternative_name)
Split::Alternative.new(alternative_name, "link_color")
experiment.reset
expect(experiment.version).to eq(1)
ab_finished("link_color")
alternative = Split::Alternative.new(alternative_name, "link_color")
expect(alternative.completed_count).to eq(0)
end
end
context "when redis is not available" do
before(:each) do
expect(Split).to receive(:redis).at_most(5).times.and_raise(Errno::ECONNREFUSED.new)
end
context "and db_failover config option is turned off" do
before(:each) do
Split.configure do |config|
config.db_failover = false
end
end
describe "ab_test" do
it "should raise an exception" do
expect { ab_test("link_color", "blue", "red") }.to raise_error(Errno::ECONNREFUSED)
end
end
describe "finished" do
it "should raise an exception" do
expect { ab_finished("link_color") }.to raise_error(Errno::ECONNREFUSED)
end
end
describe "disable split testing" do
before(:each) do
Split.configure do |config|
config.enabled = false
end
end
it "should not attempt to connect to redis" do
expect { ab_test("link_color", "blue", "red") }.not_to raise_error
end
it "should return control variable" do
expect(ab_test("link_color", "blue", "red")).to eq("blue")
expect { ab_finished("link_color") }.not_to raise_error
end
end
end
context "and db_failover config option is turned on" do
before(:each) do
Split.configure do |config|
config.db_failover = true
end
end
describe "ab_test" do
it "should not raise an exception" do
expect { ab_test("link_color", "blue", "red") }.not_to raise_error
end
it "should call db_failover_on_db_error proc with error as parameter" do
Split.configure do |config|
config.db_failover_on_db_error = proc do |error|
expect(error).to be_a(Errno::ECONNREFUSED)
end
end
expect(Split.configuration.db_failover_on_db_error).to receive(:call).and_call_original
ab_test("link_color", "blue", "red")
end
it "should always use first alternative" do
expect(ab_test("link_color", "blue", "red")).to eq("blue")
expect(ab_test("link_color", { "blue" => 0.01 }, "red" => 0.2)).to eq("blue")
expect(ab_test("link_color", { "blue" => 0.8 }, { "red" => 20 })).to eq("blue")
expect(ab_test("link_color", "blue", "red") do |alternative|
"shared/#{alternative}"
end).to eq("shared/blue")
end
context "and db_failover_allow_parameter_override config option is turned on" do
before(:each) do
Split.configure do |config|
config.db_failover_allow_parameter_override = true
end
end
context "and given an override parameter" do
it "should use given override instead of the first alternative" do
@params = { "ab_test" => { "link_color" => "red" } }
expect(ab_test("link_color", "blue", "red")).to eq("red")
expect(ab_test("link_color", "blue", "red", "green")).to eq("red")
expect(ab_test("link_color", { "blue" => 0.01 }, "red" => 0.2)).to eq("red")
expect(ab_test("link_color", { "blue" => 0.8 }, { "red" => 20 })).to eq("red")
expect(ab_test("link_color", "blue", "red") do |alternative|
"shared/#{alternative}"
end).to eq("shared/red")
end
end
end
context "and preloaded config given" do
before do
Split.configuration.experiments[:link_color] = {
alternatives: [ "blue", "red" ],
}
end
it "uses first alternative" do
expect(ab_test(:link_color)).to eq("blue")
end
end
end
describe "finished" do
it "should not raise an exception" do
expect { ab_finished("link_color") }.not_to raise_error
end
it "should call db_failover_on_db_error proc with error as parameter" do
Split.configure do |config|
config.db_failover_on_db_error = proc do |error|
expect(error).to be_a(Errno::ECONNREFUSED)
end
end
expect(Split.configuration.db_failover_on_db_error).to receive(:call).and_call_original
ab_finished("link_color")
end
end
end
end
context "with preloaded config" do
before { Split.configuration.experiments = {} }
it "pulls options from config file" do
Split.configuration.experiments[:my_experiment] = {
alternatives: [ "control_opt", "other_opt" ],
goals: ["goal1", "goal2"]
}
ab_test :my_experiment
expect(Split::Experiment.new(:my_experiment).alternatives.map(&:name)).to eq([ "control_opt", "other_opt" ])
expect(Split::Experiment.new(:my_experiment).goals).to eq([ "goal1", "goal2" ])
end
it "can be called multiple times" do
Split.configuration.experiments[:my_experiment] = {
alternatives: [ "control_opt", "other_opt" ],
goals: ["goal1", "goal2"]
}
5.times { ab_test :my_experiment }
experiment = Split::Experiment.new(:my_experiment)
expect(experiment.alternatives.map(&:name)).to eq([ "control_opt", "other_opt" ])
expect(experiment.goals).to eq([ "goal1", "goal2" ])
expect(experiment.participant_count).to eq(1)
end
it "accepts multiple goals" do
Split.configuration.experiments[:my_experiment] = {
alternatives: [ "control_opt", "other_opt" ],
goals: [ "goal1", "goal2", "goal3" ]
}
ab_test :my_experiment
experiment = Split::Experiment.new(:my_experiment)
expect(experiment.goals).to eq([ "goal1", "goal2", "goal3" ])
end
it "allow specifying goals to be optional" do
Split.configuration.experiments[:my_experiment] = {
alternatives: [ "control_opt", "other_opt" ]
}
experiment = Split::Experiment.new(:my_experiment)
expect(experiment.goals).to eq([])
end
it "accepts multiple alternatives" do
Split.configuration.experiments[:my_experiment] = {
alternatives: [ "control_opt", "second_opt", "third_opt" ],
}
ab_test :my_experiment
experiment = Split::Experiment.new(:my_experiment)
expect(experiment.alternatives.map(&:name)).to eq([ "control_opt", "second_opt", "third_opt" ])
end
it "accepts probability on alternatives" do
Split.configuration.experiments[:my_experiment] = {
alternatives: [
{ name: "control_opt", percent: 67 },
{ name: "second_opt", percent: 10 },
{ name: "third_opt", percent: 23 },
],
}
ab_test :my_experiment
experiment = Split::Experiment.new(:my_experiment)
expect(experiment.alternatives.collect { |a| [a.name, a.weight] }).to eq([["control_opt", 0.67], ["second_opt", 0.1], ["third_opt", 0.23]])
end
it "accepts probability on some alternatives" do
Split.configuration.experiments[:my_experiment] = {
alternatives: [
{ name: "control_opt", percent: 34 },
"second_opt",
{ name: "third_opt", percent: 23 },
"fourth_opt",
],
}
ab_test :my_experiment
experiment = Split::Experiment.new(:my_experiment)
names_and_weights = experiment.alternatives.collect { |a| [a.name, a.weight] }
expect(names_and_weights).to eq([["control_opt", 0.34], ["second_opt", 0.215], ["third_opt", 0.23], ["fourth_opt", 0.215]])
expect(names_and_weights.inject(0) { |sum, nw| sum + nw[1] }).to eq(1.0)
end
it "allows name param without probability" do
Split.configuration.experiments[:my_experiment] = {
alternatives: [
{ name: "control_opt" },
"second_opt",
{ name: "third_opt", percent: 64 },
],
}
ab_test :my_experiment
experiment = Split::Experiment.new(:my_experiment)
names_and_weights = experiment.alternatives.collect { |a| [a.name, a.weight] }
expect(names_and_weights).to eq([["control_opt", 0.18], ["second_opt", 0.18], ["third_opt", 0.64]])
expect(names_and_weights.inject(0) { |sum, nw| sum + nw[1] }).to eq(1.0)
end
it "fails gracefully if config is missing experiment" do
Split.configuration.experiments = { other_experiment: { foo: "Bar" } }
expect { ab_test :my_experiment }.to raise_error(Split::ExperimentNotFound)
end
it "fails gracefully if config is missing" do
expect { Split.configuration.experiments = nil }.to raise_error(Split::InvalidExperimentsFormatError)
end
it "fails gracefully if config is missing alternatives" do
Split.configuration.experiments[:my_experiment] = { foo: "Bar" }
expect { ab_test :my_experiment }.to raise_error(NoMethodError)
end
end
it "should handle multiple experiments correctly" do
experiment2 = Split::ExperimentCatalog.find_or_create("link_color2", "blue", "red")
ab_test("link_color", "blue", "red")
ab_test("link_color2", "blue", "red")
ab_finished("link_color2")
experiment2.alternatives.each do |alt|
expect(alt.unfinished_count).to eq(0)
end
end
context "with goals" do
before do
@experiment = { "link_color" => ["purchase", "refund"] }
@alternatives = ["blue", "red"]
@experiment_name, @goals = normalize_metric(@experiment)
@goal1 = @goals[0]
@goal2 = @goals[1]
end
it "should normalize experiment" do
expect(@experiment_name).to eq("link_color")
expect(@goals).to eq(["purchase", "refund"])
end
describe "ab_test" do
it "should allow experiment goals interface as a single hash" do
ab_test(@experiment, *@alternatives)
experiment = Split::ExperimentCatalog.find("link_color")
expect(experiment.goals).to eq(["purchase", "refund"])
end
end
describe "ab_finished" do
before do
@alternative_name = ab_test(@experiment, *@alternatives)
end
it "should increment the counter for the specified-goal completed alternative" do
expect { ab_finished({ "link_color" => ["purchase"] }) }
.to change { Split::Alternative.new(@alternative_name, @experiment_name).completed_count(@goal2) }.by(0)
.and change { Split::Alternative.new(@alternative_name, @experiment_name).completed_count(@goal1) }.by(1)
end
end
end
end
<MSG> Merge pull request #34 from layflags/master
Bug fix for ab_test helper
<DFF> @@ -50,6 +50,13 @@ describe Split::Helper do
@params = {'link_color' => 'blue'}
alternative = ab_test('link_color', 'blue', 'red')
alternative.should eql('blue')
+ alternative = ab_test('link_color', 'blue' => 1, 'red' => 5)
+ alternative.should eql('blue')
+ @params = {'link_color' => 'red'}
+ alternative = ab_test('link_color', 'blue', 'red')
+ alternative.should eql('red')
+ alternative = ab_test('link_color', 'blue' => 5, 'red' => 1)
+ alternative.should eql('red')
end
it "should allow passing a block" do
| 7 | Merge pull request #34 from layflags/master | 0 | .rb | rb | mit | splitrb/split |
10072567 | <NME> helper_spec.rb
<BEF> # frozen_string_literal: true
require "spec_helper"
# TODO change some of these tests to use Rack::Test
describe Split::Helper do
include Split::Helper
let(:experiment) {
Split::ExperimentCatalog.find_or_create("link_color", "blue", "red")
}
describe "ab_test" do
it "should not raise an error when passed strings for alternatives" do
expect { ab_test("xyz", "1", "2", "3") }.not_to raise_error
end
it "should not raise an error when passed an array for alternatives" do
expect { ab_test("xyz", ["1", "2", "3"]) }.not_to raise_error
end
it "should raise the appropriate error when passed integers for alternatives" do
expect { ab_test("xyz", 1, 2, 3) }.to raise_error(ArgumentError)
end
it "should raise the appropriate error when passed symbols for alternatives" do
expect { ab_test("xyz", :a, :b, :c) }.to raise_error(ArgumentError)
end
it "should not raise error when passed an array for goals" do
expect { ab_test({ "link_color" => ["purchase", "refund"] }, "blue", "red") }.not_to raise_error
end
it "should not raise error when passed just one goal" do
expect { ab_test({ "link_color" => "purchase" }, "blue", "red") }.not_to raise_error
end
it "raises an appropriate error when processing combined expirements" do
Split.configuration.experiments = {
combined_exp_1: {
alternatives: [ { name: "control", percent: 50 }, { name: "test-alt", percent: 50 } ],
metric: :my_metric,
combined_experiments: [:combined_exp_1_sub_1]
}
}
Split::ExperimentCatalog.find_or_create("combined_exp_1")
expect { ab_test("combined_exp_1") }.to raise_error(Split::InvalidExperimentsFormatError)
end
@params = {'link_color' => 'blue'}
alternative = ab_test('link_color', 'blue', 'red')
alternative.should eql('blue')
end
it "should allow passing a block" do
new_blue_count = Split::Alternative.new("blue", "link_color").participant_count
expect((new_red_count + new_blue_count)).to eq(previous_red_count + previous_blue_count + 1)
end
it "should not increment the counter for an experiment that the user is not participating in" do
ab_test("link_color", "blue", "red")
e = Split::ExperimentCatalog.find_or_create("button_size", "small", "big")
expect {
# User shouldn't participate in this second experiment
ab_test("button_size", "small", "big")
}.not_to change { e.participant_count }
end
it "should not increment the counter for an ended experiment" do
e = Split::ExperimentCatalog.find_or_create("button_size", "small", "big")
e.winner = "small"
expect {
a = ab_test("button_size", "small", "big")
expect(a).to eq("small")
}.not_to change { e.participant_count }
end
it "should not increment the counter for an not started experiment" do
expect(Split.configuration).to receive(:start_manually).and_return(true)
e = Split::ExperimentCatalog.find_or_create("button_size", "small", "big")
expect {
a = ab_test("button_size", "small", "big")
expect(a).to eq("small")
}.not_to change { e.participant_count }
end
it "should return the given alternative for an existing user" do
expect(ab_test("link_color", "blue", "red")).to eq ab_test("link_color", "blue", "red")
end
it "should always return the winner if one is present" do
experiment.winner = "orange"
expect(ab_test("link_color", "blue", "red")).to eq("orange")
end
it "should allow the alternative to be forced by passing it in the params" do
# ?ab_test[link_color]=blue
@params = { "ab_test" => { "link_color" => "blue" } }
alternative = ab_test("link_color", "blue", "red")
expect(alternative).to eq("blue")
alternative = ab_test("link_color", { "blue" => 1 }, "red" => 5)
expect(alternative).to eq("blue")
@params = { "ab_test" => { "link_color" => "red" } }
alternative = ab_test("link_color", "blue", "red")
expect(alternative).to eq("red")
alternative = ab_test("link_color", { "blue" => 5 }, "red" => 1)
expect(alternative).to eq("red")
end
it "should not allow an arbitrary alternative" do
@params = { "ab_test" => { "link_color" => "pink" } }
alternative = ab_test("link_color", "blue")
expect(alternative).to eq("blue")
end
it "should not store the split when a param forced alternative" do
@params = { "ab_test" => { "link_color" => "blue" } }
expect(ab_user).not_to receive(:[]=)
ab_test("link_color", "blue", "red")
end
it "SPLIT_DISABLE query parameter should also force the alternative (uses control)" do
@params = { "SPLIT_DISABLE" => "true" }
alternative = ab_test("link_color", "blue", "red")
expect(alternative).to eq("blue")
alternative = ab_test("link_color", { "blue" => 1 }, "red" => 5)
expect(alternative).to eq("blue")
alternative = ab_test("link_color", "red", "blue")
expect(alternative).to eq("red")
alternative = ab_test("link_color", { "red" => 5 }, "blue" => 1)
expect(alternative).to eq("red")
end
it "should not store the split when Split generically disabled" do
@params = { "SPLIT_DISABLE" => "true" }
expect(ab_user).not_to receive(:[]=)
ab_test("link_color", "blue", "red")
end
context "when store_override is set" do
before { Split.configuration.store_override = true }
it "should store the forced alternative" do
@params = { "ab_test" => { "link_color" => "blue" } }
expect(ab_user).to receive(:[]=).with("link_color", "blue")
ab_test("link_color", "blue", "red")
end
end
context "when on_trial_choose is set" do
before { Split.configuration.on_trial_choose = :some_method }
it "should call the method" do
expect(self).to receive(:some_method)
ab_test("link_color", "blue", "red")
end
end
it "should allow passing a block" do
alt = ab_test("link_color", "blue", "red")
ret = ab_test("link_color", "blue", "red") { |alternative| "shared/#{alternative}" }
expect(ret).to eq("shared/#{alt}")
end
it "should allow the share of visitors see an alternative to be specified" do
ab_test("link_color", { "blue" => 0.8 }, { "red" => 20 })
expect(["red", "blue"]).to include(ab_user["link_color"])
end
it "should allow alternative weighting interface as a single hash" do
ab_test("link_color", { "blue" => 0.01 }, "red" => 0.2)
experiment = Split::ExperimentCatalog.find("link_color")
expect(experiment.alternatives.map(&:name)).to eq(["blue", "red"])
expect(experiment.alternatives.collect { |a| a.weight }).to match_array([0.01, 0.2])
end
it "should only let a user participate in one experiment at a time" do
link_color = ab_test("link_color", "blue", "red")
ab_test("button_size", "small", "big")
expect(ab_user["link_color"]).to eq(link_color)
big = Split::Alternative.new("big", "button_size")
expect(big.participant_count).to eq(0)
small = Split::Alternative.new("small", "button_size")
expect(small.participant_count).to eq(0)
end
it "should let a user participate in many experiment with allow_multiple_experiments option" do
Split.configure do |config|
config.allow_multiple_experiments = true
end
link_color = ab_test("link_color", "blue", "red")
button_size = ab_test("button_size", "small", "big")
expect(ab_user["link_color"]).to eq(link_color)
expect(ab_user["button_size"]).to eq(button_size)
button_size_alt = Split::Alternative.new(button_size, "button_size")
expect(button_size_alt.participant_count).to eq(1)
end
context "with allow_multiple_experiments = 'control'" do
it "should let a user participate in many experiment with one non-'control' alternative" do
Split.configure do |config|
config.allow_multiple_experiments = "control"
end
groups = 100.times.map do |n|
ab_test("test#{n}".to_sym, { "control" => (100 - n) }, { "test#{n}-alt" => n })
end
experiments = ab_user.active_experiments
expect(experiments.size).to be > 1
count_control = experiments.values.count { |g| g == "control" }
expect(count_control).to eq(experiments.size - 1)
count_alts = groups.count { |g| g != "control" }
expect(count_alts).to eq(1)
end
context "when user already has experiment" do
let(:mock_user) { Split::User.new(self, { "test_0" => "test-alt" }) }
before do
Split.configure do |config|
config.allow_multiple_experiments = "control"
end
Split::ExperimentCatalog.find_or_initialize("test_0", "control", "test-alt").save
Split::ExperimentCatalog.find_or_initialize("test_1", "control", "test-alt").save
end
it "should restore previously selected alternative" do
expect(ab_user.active_experiments.size).to eq 1
expect(ab_test(:test_0, { "control" => 100 }, { "test-alt" => 1 })).to eq "test-alt"
expect(ab_test(:test_0, { "control" => 1 }, { "test-alt" => 100 })).to eq "test-alt"
end
it "should select the correct alternatives after experiment resets" do
experiment = Split::ExperimentCatalog.find(:test_0)
experiment.reset
mock_user[experiment.key] = "test-alt"
expect(ab_user.active_experiments.size).to eq 1
expect(ab_test(:test_0, { "control" => 100 }, { "test-alt" => 1 })).to eq "test-alt"
expect(ab_test(:test_0, { "control" => 0 }, { "test-alt" => 100 })).to eq "test-alt"
end
it "lets override existing choice" do
pending "this requires user store reset on first call not depending on whelther it is current trial"
@params = { "ab_test" => { "test_1" => "test-alt" } }
expect(ab_test(:test_0, { "control" => 0 }, { "test-alt" => 100 })).to eq "control"
expect(ab_test(:test_1, { "control" => 100 }, { "test-alt" => 1 })).to eq "test-alt"
end
end
end
it "should not over-write a finished key when an experiment is on a later version" do
experiment.increment_version
ab_user = { experiment.key => "blue", experiment.finished_key => true }
finished_session = ab_user.dup
ab_test("link_color", "blue", "red")
expect(ab_user).to eq(finished_session)
end
end
describe "metadata" do
context "is defined" do
before do
Split.configuration.experiments = {
my_experiment: {
alternatives: ["one", "two"],
resettable: false,
metadata: { "one" => "Meta1", "two" => "Meta2" }
}
}
end
it "should be passed to helper block" do
@params = { "ab_test" => { "my_experiment" => "two" } }
expect(ab_test("my_experiment")).to eq "two"
expect(ab_test("my_experiment") do |alternative, meta|
meta
end).to eq("Meta2")
end
it "should pass control metadata helper block if library disabled" do
Split.configure do |config|
config.enabled = false
end
expect(ab_test("my_experiment")).to eq "one"
expect(ab_test("my_experiment") do |_, meta|
meta
end).to eq("Meta1")
end
end
context "is not defined" do
before do
Split.configuration.experiments = {
my_experiment: {
alternatives: ["one", "two"],
resettable: false,
metadata: nil
}
}
end
it "should be passed to helper block" do
expect(ab_test("my_experiment") do |alternative, meta|
meta
end).to eq({})
end
it "should pass control metadata helper block if library disabled" do
Split.configure do |config|
config.enabled = false
end
expect(ab_test("my_experiment") do |_, meta|
meta
end).to eq({})
end
end
end
describe "ab_finished" do
context "for an experiment that the user participates in" do
before(:each) do
@experiment_name = "link_color"
@alternatives = ["blue", "red"]
@experiment = Split::ExperimentCatalog.find_or_create(@experiment_name, *@alternatives)
@alternative_name = ab_test(@experiment_name, *@alternatives)
@previous_completion_count = Split::Alternative.new(@alternative_name, @experiment_name).completed_count
end
it "should increment the counter for the completed alternative" do
ab_finished(@experiment_name)
new_completion_count = Split::Alternative.new(@alternative_name, @experiment_name).completed_count
expect(new_completion_count).to eq(@previous_completion_count + 1)
end
it "should set experiment's finished key if reset is false" do
ab_finished(@experiment_name, { reset: false })
expect(ab_user[@experiment.key]).to eq(@alternative_name)
expect(ab_user[@experiment.finished_key]).to eq(true)
end
it "should not increment the counter if reset is false and the experiment has been already finished" do
2.times { ab_finished(@experiment_name, { reset: false }) }
new_completion_count = Split::Alternative.new(@alternative_name, @experiment_name).completed_count
expect(new_completion_count).to eq(@previous_completion_count + 1)
end
it "should not increment the counter for an ended experiment" do
e = Split::ExperimentCatalog.find_or_create("button_size", "small", "big")
e.winner = "small"
a = ab_test("button_size", "small", "big")
expect(a).to eq("small")
expect {
ab_finished("button_size")
}.not_to change { Split::Alternative.new(a, "button_size").completed_count }
end
it "should clear out the user's participation from their session" do
expect(ab_user[@experiment.key]).to eq(@alternative_name)
ab_finished(@experiment_name)
expect(ab_user.keys).to be_empty
end
it "should not clear out the users session if reset is false" do
expect(ab_user[@experiment.key]).to eq(@alternative_name)
ab_finished(@experiment_name, { reset: false })
expect(ab_user[@experiment.key]).to eq(@alternative_name)
expect(ab_user[@experiment.finished_key]).to eq(true)
end
it "should reset the users session when experiment is not versioned" do
expect(ab_user[@experiment.key]).to eq(@alternative_name)
ab_finished(@experiment_name)
expect(ab_user.keys).to be_empty
end
it "should reset the users session when experiment is versioned" do
@experiment.increment_version
@alternative_name = ab_test(@experiment_name, *@alternatives)
expect(ab_user[@experiment.key]).to eq(@alternative_name)
ab_finished(@experiment_name)
expect(ab_user.keys).to be_empty
end
context "when on_trial_complete is set" do
before { Split.configuration.on_trial_complete = :some_method }
it "should call the method" do
expect(self).to receive(:some_method)
ab_finished(@experiment_name)
end
it "should not call the method without alternative" do
ab_user[@experiment.key] = nil
expect(self).not_to receive(:some_method)
ab_finished(@experiment_name)
end
end
end
context "for an experiment that the user is excluded from" do
before do
alternative = ab_test("link_color", "blue", "red")
expect(Split::Alternative.new(alternative, "link_color").participant_count).to eq(1)
alternative = ab_test("button_size", "small", "big")
expect(Split::Alternative.new(alternative, "button_size").participant_count).to eq(0)
end
it "should not increment the completed counter" do
# So, user should be participating in the link_color experiment and
# receive the control for button_size. As the user is not participating in
# the button size experiment, finishing it should not increase the
# completion count for that alternative.
expect {
ab_finished("button_size")
}.not_to change { Split::Alternative.new("small", "button_size").completed_count }
end
end
context "for an experiment that the user does not participate in" do
before do
Split::ExperimentCatalog.find_or_create(:not_started_experiment, "control", "alt")
end
it "should not raise an exception" do
expect { ab_finished(:not_started_experiment) }.not_to raise_exception
end
it "should not change the user state when reset is false" do
expect { ab_finished(:not_started_experiment, reset: false) }.not_to change { ab_user.keys }.from([])
end
it "should not change the user state when reset is true" do
expect(self).not_to receive(:reset!)
ab_finished(:not_started_experiment)
end
it "should not increment the completed counter" do
ab_finished(:not_started_experiment)
expect(Split::Alternative.new("control", :not_started_experiment).completed_count).to eq(0)
expect(Split::Alternative.new("alt", :not_started_experiment).completed_count).to eq(0)
end
end
end
context "finished with config" do
it "passes reset option" do
Split.configuration.experiments = {
my_experiment: {
alternatives: ["one", "two"],
resettable: false,
}
}
alternative = ab_test(:my_experiment)
experiment = Split::ExperimentCatalog.find :my_experiment
ab_finished :my_experiment
expect(ab_user[experiment.key]).to eq(alternative)
expect(ab_user[experiment.finished_key]).to eq(true)
end
end
context "finished with metric name" do
before { Split.configuration.experiments = {} }
before { expect(Split::Alternative).to receive(:new).at_least(1).times.and_call_original }
def should_finish_experiment(experiment_name, should_finish = true)
alts = Split.configuration.experiments[experiment_name][:alternatives]
experiment = Split::ExperimentCatalog.find_or_create(experiment_name, *alts)
alt_name = ab_user[experiment.key] = alts.first
alt = double("alternative")
expect(alt).to receive(:name).at_most(1).times.and_return(alt_name)
expect(Split::Alternative).to receive(:new).at_most(1).times.with(alt_name, experiment_name.to_s).and_return(alt)
if should_finish
expect(alt).to receive(:increment_completion).at_most(1).times
else
expect(alt).not_to receive(:increment_completion)
end
end
it "completes the test" do
Split.configuration.experiments[:my_experiment] = {
alternatives: [ "control_opt", "other_opt" ],
metric: :my_metric
}
should_finish_experiment :my_experiment
ab_finished :my_metric
end
it "completes all relevant tests" do
Split.configuration.experiments = {
exp_1: {
alternatives: [ "1-1", "1-2" ],
metric: :my_metric
},
exp_2: {
alternatives: [ "2-1", "2-2" ],
metric: :another_metric
},
exp_3: {
alternatives: [ "3-1", "3-2" ],
metric: :my_metric
},
}
should_finish_experiment :exp_1
should_finish_experiment :exp_2, false
should_finish_experiment :exp_3
ab_finished :my_metric
end
it "passes reset option" do
Split.configuration.experiments = {
my_exp: {
alternatives: ["one", "two"],
metric: :my_metric,
resettable: false,
}
}
alternative_name = ab_test(:my_exp)
exp = Split::ExperimentCatalog.find :my_exp
ab_finished :my_metric
expect(ab_user[exp.key]).to eq(alternative_name)
expect(ab_user[exp.finished_key]).to be_truthy
end
it "passes through options" do
Split.configuration.experiments = {
my_exp: {
alternatives: ["one", "two"],
metric: :my_metric,
}
}
alternative_name = ab_test(:my_exp)
exp = Split::ExperimentCatalog.find :my_exp
ab_finished :my_metric, reset: false
expect(ab_user[exp.key]).to eq(alternative_name)
expect(ab_user[exp.finished_key]).to be_truthy
end
end
describe "conversions" do
it "should return a conversion rate for an alternative" do
alternative_name = ab_test("link_color", "blue", "red")
previous_convertion_rate = Split::Alternative.new(alternative_name, "link_color").conversion_rate
expect(previous_convertion_rate).to eq(0.0)
ab_finished("link_color")
new_convertion_rate = Split::Alternative.new(alternative_name, "link_color").conversion_rate
expect(new_convertion_rate).to eq(1.0)
end
end
describe "active experiments" do
it "should show an active test" do
alternative = ab_test("def", "4", "5", "6")
expect(active_experiments.count).to eq 1
expect(active_experiments.first[0]).to eq "def"
expect(active_experiments.first[1]).to eq alternative
end
it "should show a finished test" do
alternative = ab_test("def", "4", "5", "6")
ab_finished("def", { reset: false })
expect(active_experiments.count).to eq 1
expect(active_experiments.first[0]).to eq "def"
expect(active_experiments.first[1]).to eq alternative
end
it "should show an active test when an experiment is on a later version" do
experiment.reset
expect(experiment.version).to eq(1)
ab_test("link_color", "blue", "red")
expect(active_experiments.count).to eq 1
expect(active_experiments.first[0]).to eq "link_color"
end
it "should show versioned tests properly" do
10.times { experiment.reset }
alternative = ab_test(experiment.name, "blue", "red")
ab_finished(experiment.name, reset: false)
expect(experiment.version).to eq(10)
expect(active_experiments.count).to eq 1
expect(active_experiments).to eq({ "link_color" => alternative })
end
it "should show multiple tests" do
Split.configure do |config|
config.allow_multiple_experiments = true
end
alternative = ab_test("def", "4", "5", "6")
another_alternative = ab_test("ghi", "7", "8", "9")
expect(active_experiments.count).to eq 2
expect(active_experiments["def"]).to eq alternative
expect(active_experiments["ghi"]).to eq another_alternative
end
it "should not show tests with winners" do
Split.configure do |config|
config.allow_multiple_experiments = true
end
e = Split::ExperimentCatalog.find_or_create("def", "4", "5", "6")
e.winner = "4"
ab_test("def", "4", "5", "6")
another_alternative = ab_test("ghi", "7", "8", "9")
expect(active_experiments.count).to eq 1
expect(active_experiments.first[0]).to eq "ghi"
expect(active_experiments.first[1]).to eq another_alternative
end
end
describe "when user is a robot" do
before(:each) do
@request = OpenStruct.new(user_agent: "Googlebot/2.1 (+http://www.google.com/bot.html)")
end
describe "ab_test" do
it "should return the control" do
alternative = ab_test("link_color", "blue", "red")
expect(alternative).to eq experiment.control.name
end
it "should not create a experiment" do
ab_test("link_color", "blue", "red")
expect(Split::Experiment.new("link_color")).to be_a_new_record
end
it "should not increment the participation count" do
previous_red_count = Split::Alternative.new("red", "link_color").participant_count
previous_blue_count = Split::Alternative.new("blue", "link_color").participant_count
ab_test("link_color", "blue", "red")
new_red_count = Split::Alternative.new("red", "link_color").participant_count
new_blue_count = Split::Alternative.new("blue", "link_color").participant_count
expect((new_red_count + new_blue_count)).to eq(previous_red_count + previous_blue_count)
end
end
describe "finished" do
it "should not increment the completed count" do
alternative_name = ab_test("link_color", "blue", "red")
previous_completion_count = Split::Alternative.new(alternative_name, "link_color").completed_count
ab_finished("link_color")
new_completion_count = Split::Alternative.new(alternative_name, "link_color").completed_count
expect(new_completion_count).to eq(previous_completion_count)
end
end
end
describe "when providing custom ignore logic" do
context "using a proc to configure custom logic" do
before(:each) do
Split.configure do |c|
c.ignore_filter = proc { |request| true } # ignore everything
end
end
it "ignores the ab_test" do
ab_test("link_color", "blue", "red")
red_count = Split::Alternative.new("red", "link_color").participant_count
blue_count = Split::Alternative.new("blue", "link_color").participant_count
expect((red_count + blue_count)).to be(0)
end
end
end
shared_examples_for "a disabled test" do
describe "ab_test" do
it "should return the control" do
alternative = ab_test("link_color", "blue", "red")
expect(alternative).to eq experiment.control.name
end
it "should not increment the participation count" do
previous_red_count = Split::Alternative.new("red", "link_color").participant_count
previous_blue_count = Split::Alternative.new("blue", "link_color").participant_count
ab_test("link_color", "blue", "red")
new_red_count = Split::Alternative.new("red", "link_color").participant_count
new_blue_count = Split::Alternative.new("blue", "link_color").participant_count
expect((new_red_count + new_blue_count)).to eq(previous_red_count + previous_blue_count)
end
end
describe "finished" do
it "should not increment the completed count" do
alternative_name = ab_test("link_color", "blue", "red")
previous_completion_count = Split::Alternative.new(alternative_name, "link_color").completed_count
ab_finished("link_color")
new_completion_count = Split::Alternative.new(alternative_name, "link_color").completed_count
expect(new_completion_count).to eq(previous_completion_count)
end
end
end
describe "when ip address is ignored" do
context "individually" do
before(:each) do
@request = OpenStruct.new(ip: "81.19.48.130")
Split.configure do |c|
c.ignore_ip_addresses << "81.19.48.130"
end
end
it_behaves_like "a disabled test"
end
context "for a range" do
before(:each) do
@request = OpenStruct.new(ip: "81.19.48.129")
Split.configure do |c|
c.ignore_ip_addresses << /81\.19\.48\.[0-9]+/
end
end
it_behaves_like "a disabled test"
end
context "using both a range and a specific value" do
before(:each) do
@request = OpenStruct.new(ip: "81.19.48.128")
Split.configure do |c|
c.ignore_ip_addresses << "81.19.48.130"
c.ignore_ip_addresses << /81\.19\.48\.[0-9]+/
end
end
it_behaves_like "a disabled test"
end
context "when ignored other address" do
before do
@request = OpenStruct.new(ip: "1.1.1.1")
Split.configure do |c|
c.ignore_ip_addresses << "81.19.48.130"
end
end
it "works as usual" do
alternative_name = ab_test("link_color", "red", "blue")
expect {
ab_finished("link_color")
}.to change(Split::Alternative.new(alternative_name, "link_color"), :completed_count).by(1)
end
end
end
describe "when user is previewing" do
before(:each) do
@request = OpenStruct.new(headers: { "x-purpose" => "preview" })
end
it_behaves_like "a disabled test"
end
describe "versioned experiments" do
it "should use version zero if no version is present" do
alternative_name = ab_test("link_color", "blue", "red")
expect(experiment.version).to eq(0)
expect(ab_user["link_color"]).to eq(alternative_name)
end
it "should save the version of the experiment to the session" do
experiment.reset
expect(experiment.version).to eq(1)
alternative_name = ab_test("link_color", "blue", "red")
expect(ab_user["link_color:1"]).to eq(alternative_name)
end
it "should load the experiment even if the version is not 0" do
experiment.reset
expect(experiment.version).to eq(1)
alternative_name = ab_test("link_color", "blue", "red")
expect(ab_user["link_color:1"]).to eq(alternative_name)
return_alternative_name = ab_test("link_color", "blue", "red")
expect(return_alternative_name).to eq(alternative_name)
end
it "should reset the session of a user on an older version of the experiment" do
alternative_name = ab_test("link_color", "blue", "red")
expect(ab_user["link_color"]).to eq(alternative_name)
alternative = Split::Alternative.new(alternative_name, "link_color")
expect(alternative.participant_count).to eq(1)
experiment.reset
expect(experiment.version).to eq(1)
alternative = Split::Alternative.new(alternative_name, "link_color")
expect(alternative.participant_count).to eq(0)
new_alternative_name = ab_test("link_color", "blue", "red")
expect(ab_user["link_color:1"]).to eq(new_alternative_name)
new_alternative = Split::Alternative.new(new_alternative_name, "link_color")
expect(new_alternative.participant_count).to eq(1)
end
it "should cleanup old versions of experiments from the session" do
alternative_name = ab_test("link_color", "blue", "red")
expect(ab_user["link_color"]).to eq(alternative_name)
alternative = Split::Alternative.new(alternative_name, "link_color")
expect(alternative.participant_count).to eq(1)
experiment.reset
expect(experiment.version).to eq(1)
alternative = Split::Alternative.new(alternative_name, "link_color")
expect(alternative.participant_count).to eq(0)
new_alternative_name = ab_test("link_color", "blue", "red")
expect(ab_user["link_color:1"]).to eq(new_alternative_name)
end
it "should only count completion of users on the current version" do
alternative_name = ab_test("link_color", "blue", "red")
expect(ab_user["link_color"]).to eq(alternative_name)
Split::Alternative.new(alternative_name, "link_color")
experiment.reset
expect(experiment.version).to eq(1)
ab_finished("link_color")
alternative = Split::Alternative.new(alternative_name, "link_color")
expect(alternative.completed_count).to eq(0)
end
end
context "when redis is not available" do
before(:each) do
expect(Split).to receive(:redis).at_most(5).times.and_raise(Errno::ECONNREFUSED.new)
end
context "and db_failover config option is turned off" do
before(:each) do
Split.configure do |config|
config.db_failover = false
end
end
describe "ab_test" do
it "should raise an exception" do
expect { ab_test("link_color", "blue", "red") }.to raise_error(Errno::ECONNREFUSED)
end
end
describe "finished" do
it "should raise an exception" do
expect { ab_finished("link_color") }.to raise_error(Errno::ECONNREFUSED)
end
end
describe "disable split testing" do
before(:each) do
Split.configure do |config|
config.enabled = false
end
end
it "should not attempt to connect to redis" do
expect { ab_test("link_color", "blue", "red") }.not_to raise_error
end
it "should return control variable" do
expect(ab_test("link_color", "blue", "red")).to eq("blue")
expect { ab_finished("link_color") }.not_to raise_error
end
end
end
context "and db_failover config option is turned on" do
before(:each) do
Split.configure do |config|
config.db_failover = true
end
end
describe "ab_test" do
it "should not raise an exception" do
expect { ab_test("link_color", "blue", "red") }.not_to raise_error
end
it "should call db_failover_on_db_error proc with error as parameter" do
Split.configure do |config|
config.db_failover_on_db_error = proc do |error|
expect(error).to be_a(Errno::ECONNREFUSED)
end
end
expect(Split.configuration.db_failover_on_db_error).to receive(:call).and_call_original
ab_test("link_color", "blue", "red")
end
it "should always use first alternative" do
expect(ab_test("link_color", "blue", "red")).to eq("blue")
expect(ab_test("link_color", { "blue" => 0.01 }, "red" => 0.2)).to eq("blue")
expect(ab_test("link_color", { "blue" => 0.8 }, { "red" => 20 })).to eq("blue")
expect(ab_test("link_color", "blue", "red") do |alternative|
"shared/#{alternative}"
end).to eq("shared/blue")
end
context "and db_failover_allow_parameter_override config option is turned on" do
before(:each) do
Split.configure do |config|
config.db_failover_allow_parameter_override = true
end
end
context "and given an override parameter" do
it "should use given override instead of the first alternative" do
@params = { "ab_test" => { "link_color" => "red" } }
expect(ab_test("link_color", "blue", "red")).to eq("red")
expect(ab_test("link_color", "blue", "red", "green")).to eq("red")
expect(ab_test("link_color", { "blue" => 0.01 }, "red" => 0.2)).to eq("red")
expect(ab_test("link_color", { "blue" => 0.8 }, { "red" => 20 })).to eq("red")
expect(ab_test("link_color", "blue", "red") do |alternative|
"shared/#{alternative}"
end).to eq("shared/red")
end
end
end
context "and preloaded config given" do
before do
Split.configuration.experiments[:link_color] = {
alternatives: [ "blue", "red" ],
}
end
it "uses first alternative" do
expect(ab_test(:link_color)).to eq("blue")
end
end
end
describe "finished" do
it "should not raise an exception" do
expect { ab_finished("link_color") }.not_to raise_error
end
it "should call db_failover_on_db_error proc with error as parameter" do
Split.configure do |config|
config.db_failover_on_db_error = proc do |error|
expect(error).to be_a(Errno::ECONNREFUSED)
end
end
expect(Split.configuration.db_failover_on_db_error).to receive(:call).and_call_original
ab_finished("link_color")
end
end
end
end
context "with preloaded config" do
before { Split.configuration.experiments = {} }
it "pulls options from config file" do
Split.configuration.experiments[:my_experiment] = {
alternatives: [ "control_opt", "other_opt" ],
goals: ["goal1", "goal2"]
}
ab_test :my_experiment
expect(Split::Experiment.new(:my_experiment).alternatives.map(&:name)).to eq([ "control_opt", "other_opt" ])
expect(Split::Experiment.new(:my_experiment).goals).to eq([ "goal1", "goal2" ])
end
it "can be called multiple times" do
Split.configuration.experiments[:my_experiment] = {
alternatives: [ "control_opt", "other_opt" ],
goals: ["goal1", "goal2"]
}
5.times { ab_test :my_experiment }
experiment = Split::Experiment.new(:my_experiment)
expect(experiment.alternatives.map(&:name)).to eq([ "control_opt", "other_opt" ])
expect(experiment.goals).to eq([ "goal1", "goal2" ])
expect(experiment.participant_count).to eq(1)
end
it "accepts multiple goals" do
Split.configuration.experiments[:my_experiment] = {
alternatives: [ "control_opt", "other_opt" ],
goals: [ "goal1", "goal2", "goal3" ]
}
ab_test :my_experiment
experiment = Split::Experiment.new(:my_experiment)
expect(experiment.goals).to eq([ "goal1", "goal2", "goal3" ])
end
it "allow specifying goals to be optional" do
Split.configuration.experiments[:my_experiment] = {
alternatives: [ "control_opt", "other_opt" ]
}
experiment = Split::Experiment.new(:my_experiment)
expect(experiment.goals).to eq([])
end
it "accepts multiple alternatives" do
Split.configuration.experiments[:my_experiment] = {
alternatives: [ "control_opt", "second_opt", "third_opt" ],
}
ab_test :my_experiment
experiment = Split::Experiment.new(:my_experiment)
expect(experiment.alternatives.map(&:name)).to eq([ "control_opt", "second_opt", "third_opt" ])
end
it "accepts probability on alternatives" do
Split.configuration.experiments[:my_experiment] = {
alternatives: [
{ name: "control_opt", percent: 67 },
{ name: "second_opt", percent: 10 },
{ name: "third_opt", percent: 23 },
],
}
ab_test :my_experiment
experiment = Split::Experiment.new(:my_experiment)
expect(experiment.alternatives.collect { |a| [a.name, a.weight] }).to eq([["control_opt", 0.67], ["second_opt", 0.1], ["third_opt", 0.23]])
end
it "accepts probability on some alternatives" do
Split.configuration.experiments[:my_experiment] = {
alternatives: [
{ name: "control_opt", percent: 34 },
"second_opt",
{ name: "third_opt", percent: 23 },
"fourth_opt",
],
}
ab_test :my_experiment
experiment = Split::Experiment.new(:my_experiment)
names_and_weights = experiment.alternatives.collect { |a| [a.name, a.weight] }
expect(names_and_weights).to eq([["control_opt", 0.34], ["second_opt", 0.215], ["third_opt", 0.23], ["fourth_opt", 0.215]])
expect(names_and_weights.inject(0) { |sum, nw| sum + nw[1] }).to eq(1.0)
end
it "allows name param without probability" do
Split.configuration.experiments[:my_experiment] = {
alternatives: [
{ name: "control_opt" },
"second_opt",
{ name: "third_opt", percent: 64 },
],
}
ab_test :my_experiment
experiment = Split::Experiment.new(:my_experiment)
names_and_weights = experiment.alternatives.collect { |a| [a.name, a.weight] }
expect(names_and_weights).to eq([["control_opt", 0.18], ["second_opt", 0.18], ["third_opt", 0.64]])
expect(names_and_weights.inject(0) { |sum, nw| sum + nw[1] }).to eq(1.0)
end
it "fails gracefully if config is missing experiment" do
Split.configuration.experiments = { other_experiment: { foo: "Bar" } }
expect { ab_test :my_experiment }.to raise_error(Split::ExperimentNotFound)
end
it "fails gracefully if config is missing" do
expect { Split.configuration.experiments = nil }.to raise_error(Split::InvalidExperimentsFormatError)
end
it "fails gracefully if config is missing alternatives" do
Split.configuration.experiments[:my_experiment] = { foo: "Bar" }
expect { ab_test :my_experiment }.to raise_error(NoMethodError)
end
end
it "should handle multiple experiments correctly" do
experiment2 = Split::ExperimentCatalog.find_or_create("link_color2", "blue", "red")
ab_test("link_color", "blue", "red")
ab_test("link_color2", "blue", "red")
ab_finished("link_color2")
experiment2.alternatives.each do |alt|
expect(alt.unfinished_count).to eq(0)
end
end
context "with goals" do
before do
@experiment = { "link_color" => ["purchase", "refund"] }
@alternatives = ["blue", "red"]
@experiment_name, @goals = normalize_metric(@experiment)
@goal1 = @goals[0]
@goal2 = @goals[1]
end
it "should normalize experiment" do
expect(@experiment_name).to eq("link_color")
expect(@goals).to eq(["purchase", "refund"])
end
describe "ab_test" do
it "should allow experiment goals interface as a single hash" do
ab_test(@experiment, *@alternatives)
experiment = Split::ExperimentCatalog.find("link_color")
expect(experiment.goals).to eq(["purchase", "refund"])
end
end
describe "ab_finished" do
before do
@alternative_name = ab_test(@experiment, *@alternatives)
end
it "should increment the counter for the specified-goal completed alternative" do
expect { ab_finished({ "link_color" => ["purchase"] }) }
.to change { Split::Alternative.new(@alternative_name, @experiment_name).completed_count(@goal2) }.by(0)
.and change { Split::Alternative.new(@alternative_name, @experiment_name).completed_count(@goal1) }.by(1)
end
end
end
end
<MSG> Merge pull request #34 from layflags/master
Bug fix for ab_test helper
<DFF> @@ -50,6 +50,13 @@ describe Split::Helper do
@params = {'link_color' => 'blue'}
alternative = ab_test('link_color', 'blue', 'red')
alternative.should eql('blue')
+ alternative = ab_test('link_color', 'blue' => 1, 'red' => 5)
+ alternative.should eql('blue')
+ @params = {'link_color' => 'red'}
+ alternative = ab_test('link_color', 'blue', 'red')
+ alternative.should eql('red')
+ alternative = ab_test('link_color', 'blue' => 5, 'red' => 1)
+ alternative.should eql('red')
end
it "should allow passing a block" do
| 7 | Merge pull request #34 from layflags/master | 0 | .rb | rb | mit | splitrb/split |
10072568 | <NME> parser.ts
<BEF> import { strictEqual as equal } from 'assert';
import parser, { ParseOptions, FunctionCall } from '../src/parser';
import tokenizer from '../src/tokenizer';
import stringify from './assets/stringify';
const parse = (abbr: string, opt?: ParseOptions) => parser(tokenizer(abbr), opt);
const expand = (abbr: string) => parse(abbr).map(stringify).join('');
describe('CSS Abbreviation parser', () => {
it('basic', () => {
equal(expand('p10!'), 'p: 10 !important;');
equal(expand('p-10-20'), 'p: -10 20;');
equal(expand('p-10%-20--30'), 'p: -10% -20 -30;');
equal(expand('p.5'), 'p: 0.5;');
equal(expand('p-.5'), 'p: -0.5;');
equal(expand('p.1.2.3'), 'p: 0.1 0.2 0.3;');
equal(expand('p.1-.2.3'), 'p: 0.1 0.2 0.3;');
equal(expand('10'), '?: 10;');
equal(expand('.1'), '?: 0.1;');
equal(expand('lh1.'), 'lh: 1;');
});
it('color', () => {
equal(expand('c#'), 'c: #000000;');
equal(expand('c#1'), 'c: #111111;');
equal(expand('c#f'), 'c: #ffffff;');
equal(expand('c#a#b#c'), 'c: #aaaaaa #bbbbbb #cccccc;');
equal(expand('c#af'), 'c: #afafaf;');
equal(expand('c#fc0'), 'c: #ffcc00;');
equal(expand('c#11.5'), 'c: rgba(17, 17, 17, 0.5);');
equal(expand('c#.99'), 'c: rgba(0, 0, 0, 0.99);');
equal(expand('c#t'), 'c: transparent;');
});
it('keywords', () => {
equal(expand('m:a'), 'm: a;');
equal(expand('m-a'), 'm: a;');
equal(expand('m-abc'), 'm: abc;');
equal(expand('m-a0'), 'm: a 0;');
equal(expand('m-a0-a'), 'm: a 0 a;');
});
it('functions', () => {
equal(expand('bg-lg(top, "red, black",rgb(0, 0, 0) 10%)'), 'bg: lg(top, "red, black", rgb(0, 0, 0) 10%);');
equal(expand('lg(top, "red, black",rgb(0, 0, 0) 10%)'), '?: lg(top, "red, black", rgb(0, 0, 0) 10%);');
});
it('mixed', () => {
equal(expand('bd1-s#fc0'), 'bd: 1 s #ffcc00;');
equal(expand('bd#fc0-1'), 'bd: #ffcc00 1;');
equal(expand('p0+m0'), 'p: 0;m: 0;');
equal(expand('p0!+m0!'), 'p: 0 !important;m: 0 !important;');
});
it('embedded variables', () => {
equal(expand('foo$bar'), 'foo: $bar;');
equal(expand('foo$bar-2'), 'foo: $bar-2;');
equal(expand('foo$bar@bam'), 'foo: $bar @bam;');
});
it('parse value', () => {
const opt: ParseOptions = { value: true };
let prop = parse('${1:foo} ${2:bar}, baz', opt)[0];
equal(prop.name, undefined);
equal(prop.value.length, 2);
equal(prop.value[0].value.length, 2);
equal(prop.value[0].value[0].type, 'Field');
prop = parse('scale3d(${1:x}, ${2:y}, ${3:z})', opt)[0];
const fn = prop.value[0].value[0] as FunctionCall;
equal(prop.value.length, 1);
equal(prop.value[0].value.length, 1);
equal(fn.type, 'FunctionCall');
equal(fn.name, 'scale3d');
});
});
throws(() => expand('p10 '), /Unexpected token/);
});
});
<MSG> Do not allow whitespace in CSS abbreviations
The only exception is argument list inside braces
<DFF> @@ -1,4 +1,4 @@
-import { strictEqual as equal } from 'assert';
+import { strictEqual as equal, throws } from 'assert';
import parser, { ParseOptions, FunctionCall } from '../src/parser';
import tokenizer from '../src/tokenizer';
import stringify from './assets/stringify';
@@ -73,4 +73,8 @@ describe('CSS Abbreviation parser', () => {
equal(fn.type, 'FunctionCall');
equal(fn.name, 'scale3d');
});
+
+ it('errors', () => {
+ throws(() => expand('p10 '), /Unexpected token/);
+ });
});
| 5 | Do not allow whitespace in CSS abbreviations | 1 | .ts | ts | mit | emmetio/emmet |