|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "spec_helper" |
| 4 | + |
| 5 | +describe Split::ExperimentStorage do |
| 6 | + let(:experiment) do |
| 7 | + { |
| 8 | + resettable: "false", |
| 9 | + algorithm: "Split::Algorithms::WeightedSample", |
| 10 | + alternatives: [ "foo", "bar" ], |
| 11 | + goals: ["purchase", "refund"], |
| 12 | + metadata: { |
| 13 | + "foo" => { "text" => "Something bad" }, |
| 14 | + "bar" => { "text" => "Something good" } |
| 15 | + } |
| 16 | + } |
| 17 | + end |
| 18 | + before do |
| 19 | + Split.configuration.experiments = { |
| 20 | + my_exp: experiment |
| 21 | + } |
| 22 | + end |
| 23 | + |
| 24 | + context "ConfigStorage" do |
| 25 | + let(:config_store) { Split::ExperimentStorage::ConfigStorage.new("my_exp") } |
| 26 | + |
| 27 | + it "loads an experiment from the configuration" do |
| 28 | + stored_data = config_store.load |
| 29 | + expect(stored_data).to match(experiment) |
| 30 | + end |
| 31 | + |
| 32 | + it "checks if an experiment exists on the configuration" do |
| 33 | + expect(config_store.exists?).to be_truthy |
| 34 | + expect(Split::ExperimentStorage::ConfigStorage.new("whatever").exists?).to be_falsy |
| 35 | + end |
| 36 | + |
| 37 | + it "memoizes data from the configuration by default" do |
| 38 | + expect(config_store).to receive(:load!).once.and_call_original |
| 39 | + config_store.load |
| 40 | + config_store.load |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | + context "from Redis" do |
| 45 | + before { Split::ExperimentCatalog.find_or_create(:my_exp) } |
| 46 | + let(:config_store) { Split::ExperimentStorage::RedisStorage.new("my_exp") } |
| 47 | + |
| 48 | + it "loads an experiment from the configuration" do |
| 49 | + stored_data = config_store.load |
| 50 | + stored_data[:alternatives].map! { |alternative| alternative.name } |
| 51 | + expect(stored_data).to match(experiment) |
| 52 | + end |
| 53 | + |
| 54 | + it "checks if an experiment exists on the configuration" do |
| 55 | + expect(config_store.exists?).to be_truthy |
| 56 | + expect(Split::ExperimentStorage::ConfigStorage.new("whatever").exists?).to be_falsy |
| 57 | + end |
| 58 | + end |
| 59 | +end |
0 commit comments