From 8f016fe5fb42704dd949e77859888fcd898fd985 Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Mon, 28 Jun 2021 21:10:13 +0000 Subject: Add latest changes from gitlab-org/gitlab@master --- spec/experiments/application_experiment_spec.rb | 130 ++++++++++++------------ 1 file changed, 66 insertions(+), 64 deletions(-) (limited to 'spec/experiments') diff --git a/spec/experiments/application_experiment_spec.rb b/spec/experiments/application_experiment_spec.rb index 2d2b911749b..25e91dfdedd 100644 --- a/spec/experiments/application_experiment_spec.rb +++ b/spec/experiments/application_experiment_spec.rb @@ -3,11 +3,10 @@ require 'spec_helper' RSpec.describe ApplicationExperiment, :experiment do - subject { described_class.new('namespaced/stub') } + subject { described_class.new('namespaced/stub', **context) } - let(:feature_definition) do - { name: 'namespaced_stub', type: 'experiment', group: 'group::adoption', default_enabled: false } - end + let(:context) { {} } + let(:feature_definition) { { name: 'namespaced_stub', type: 'experiment', default_enabled: false } } around do |example| Feature::Definition.definitions[:namespaced_stub] = Feature::Definition.new('namespaced_stub.yml', feature_definition) @@ -25,7 +24,7 @@ RSpec.describe ApplicationExperiment, :experiment do expect { experiment('namespaced/stub') { } }.not_to raise_error end - describe "enabled" do + describe "#enabled?" do before do allow(subject).to receive(:enabled?).and_call_original @@ -57,103 +56,100 @@ RSpec.describe ApplicationExperiment, :experiment do end end - describe "publishing results" do - it "doesn't record, track or push data to the client if we shouldn't track", :snowplow do + describe "#publish" do + it "doesn't track or publish to the client or database if we can't track", :snowplow do allow(subject).to receive(:should_track?).and_return(false) - subject.record! - expect(subject).not_to receive(:record_experiment) - expect(subject).not_to receive(:track) - expect(Gon).not_to receive(:push) + expect(subject).not_to receive(:publish_to_client) + expect(subject).not_to receive(:publish_to_database) - subject.publish(:action) + subject.publish expect_no_snowplow_event end - describe 'recording the experiment' do - it 'does not record the experiment if we do not tell it to' do - expect(subject).not_to receive(:record_experiment) - - subject.publish - end - - it 'records the experiment if we tell it to' do - subject.record! - - expect(subject).to receive(:record_experiment) - - subject.publish - end - end - it "tracks the assignment" do expect(subject).to receive(:track).with(:assignment) subject.publish end - it "pushes the experiment knowledge into the client using Gon" do - expect(Gon).to receive(:push).with({ experiment: { 'namespaced/stub' => subject.signature } }, true) + it "publishes the to the client" do + expect(subject).to receive(:publish_to_client) subject.publish end - it "handles when Gon raises exceptions (like when it can't be pushed into)" do - expect(Gon).to receive(:push).and_raise(NoMethodError) + it "publishes to the database if we've opted for that" do + subject.record! + + expect(subject).to receive(:publish_to_database) - expect { subject.publish }.not_to raise_error + subject.publish end - end - it "can exclude from within the block" do - expect(described_class.new('namespaced/stub') { |e| e.exclude! }).to be_excluded - end + describe "#publish_to_client" do + it "adds the data into Gon" do + signature = { key: '86208ac54ca798e11f127e8b23ec396a', variant: 'control' } + expect(Gon).to receive(:push).with({ experiment: { 'namespaced/stub' => hash_including(signature) } }, true) - describe 'recording the experiment subject' do - using RSpec::Parameterized::TableSyntax + subject.publish_to_client + end - subject { described_class.new('namespaced/stub', nil, **context) } + it "handles when Gon raises exceptions (like when it can't be pushed into)" do + expect(Gon).to receive(:push).and_raise(NoMethodError) - before do - subject.record! + expect { subject.publish_to_client }.not_to raise_error + end end - context 'when providing a compatible context' do - where(:context_key, :object_type) do - :namespace | :namespace - :group | :namespace - :project | :project - :user | :user - :actor | :user + describe "#publish_to_database" do + using RSpec::Parameterized::TableSyntax + let(:context) { { context_key => context_value }} + + before do + subject.record! end - with_them do - let(:context) { { context_key => build(object_type) }} + context "when there's a usable subject" do + where(:context_key, :context_value, :object_type) do + :namespace | build(:namespace) | :namespace + :group | build(:namespace) | :namespace + :project | build(:project) | :project + :user | build(:user) | :user + :actor | build(:user) | :user + end - it 'records the experiment and the experiment subject from the context' do - expect { subject.publish }.to change(Experiment, :count).by(1) + with_them do + it "creates an experiment and experiment subject record" do + expect { subject.publish_to_database }.to change(Experiment, :count).by(1) - expect(Experiment.last.name).to eq('namespaced/stub') - expect(ExperimentSubject.last.send(object_type)).to eq(context[context_key]) + expect(Experiment.last.name).to eq('namespaced/stub') + expect(ExperimentSubject.last.send(object_type)).to eq(context[context_key]) + end end end - end - context 'when providing an incompatible or no context' do - where(context_hash: [{ foo: :bar }, {}]) + context "when there's not a usable subject" do + where(:context_key, :context_value) do + :namespace | nil + :foo | :bar + end - with_them do - let(:context) { context_hash } + with_them do + it "doesn't create an experiment record" do + expect { subject.publish_to_database }.not_to change(Experiment, :count) + end - it 'does not record the experiment' do - expect { subject.publish }.not_to change(Experiment, :count) + it "doesn't create an experiment subject record" do + expect { subject.publish_to_database }.not_to change(ExperimentSubject, :count) + end end end end end - describe "tracking events", :snowplow do + describe "#track", :snowplow do it "doesn't track if we shouldn't track" do allow(subject).to receive(:should_track?).and_return(false) @@ -185,7 +181,13 @@ RSpec.describe ApplicationExperiment, :experiment do end end - describe "variant resolution" do + describe "#key_for" do + it "generates MD5 hashes" do + expect(subject.key_for(foo: :bar)).to eq('6f9ac12afdb9b58c2f19a136d09f9153') + end + end + + context "when resolving variants" do it "uses the default value as specified in the yaml" do expect(Feature).to receive(:enabled?).with('namespaced_stub', subject, type: :experiment, default_enabled: :yaml) -- cgit v1.2.3