diff options
Diffstat (limited to 'spec/lib/gitlab/data_builder/pipeline_spec.rb')
-rw-r--r-- | spec/lib/gitlab/data_builder/pipeline_spec.rb | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/spec/lib/gitlab/data_builder/pipeline_spec.rb b/spec/lib/gitlab/data_builder/pipeline_spec.rb index 4c23b24aa31..0e574c7aa84 100644 --- a/spec/lib/gitlab/data_builder/pipeline_spec.rb +++ b/spec/lib/gitlab/data_builder/pipeline_spec.rb @@ -131,5 +131,41 @@ RSpec.describe Gitlab::DataBuilder::Pipeline do expect(build_environment_data[:deployment_tier]).to eq(build.persisted_environment.try(:tier)) end end + + context 'avoids N+1 database queries' do + it "with multiple builds" do + # Preparing the pipeline with the minimal builds + pipeline = create(:ci_pipeline, user: user, project: project) + create(:ci_build, user: user, project: project, pipeline: pipeline) + create(:ci_build, :deploy_to_production, :with_deployment, user: user, project: project, pipeline: pipeline) + + # We need `.to_json` as the build hook data is wrapped within `Gitlab::Lazy` + control_count = ActiveRecord::QueryRecorder.new { described_class.build(pipeline.reload).to_json }.count + + # Adding more builds to the pipeline and serializing the data again + create_list(:ci_build, 3, user: user, project: project, pipeline: pipeline) + create(:ci_build, :start_review_app, :with_deployment, user: user, project: project, pipeline: pipeline) + create(:ci_build, :stop_review_app, :with_deployment, user: user, project: project, pipeline: pipeline) + + expect { described_class.build(pipeline.reload).to_json }.not_to exceed_query_limit(control_count) + end + + it "with multiple retried builds" do + # Preparing the pipeline with the minimal builds + pipeline = create(:ci_pipeline, user: user, project: project) + create(:ci_build, :retried, user: user, project: project, pipeline: pipeline) + create(:ci_build, :deploy_to_production, :retried, :with_deployment, user: user, project: project, pipeline: pipeline) + + # We need `.to_json` as the build hook data is wrapped within `Gitlab::Lazy` + control_count = ActiveRecord::QueryRecorder.new { described_class.build(pipeline.reload).with_retried_builds.to_json }.count + + # Adding more builds to the pipeline and serializing the data again + create_list(:ci_build, 3, :retried, user: user, project: project, pipeline: pipeline) + create(:ci_build, :start_review_app, :retried, :with_deployment, user: user, project: project, pipeline: pipeline) + create(:ci_build, :stop_review_app, :retried, :with_deployment, user: user, project: project, pipeline: pipeline) + + expect { described_class.build(pipeline.reload).with_retried_builds.to_json }.not_to exceed_query_limit(control_count) + end + end end end |