diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-09-20 16:18:24 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-09-20 16:18:24 +0300 |
commit | 0653e08efd039a5905f3fa4f6e9cef9f5d2f799c (patch) | |
tree | 4dcc884cf6d81db44adae4aa99f8ec1233a41f55 /spec/models/preloaders/commit_status_preloader_spec.rb | |
parent | 744144d28e3e7fddc117924fef88de5d9674fe4c (diff) |
Add latest changes from gitlab-org/gitlab@14-3-stable-eev14.3.0-rc42
Diffstat (limited to 'spec/models/preloaders/commit_status_preloader_spec.rb')
-rw-r--r-- | spec/models/preloaders/commit_status_preloader_spec.rb | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/spec/models/preloaders/commit_status_preloader_spec.rb b/spec/models/preloaders/commit_status_preloader_spec.rb new file mode 100644 index 00000000000..85ea784335c --- /dev/null +++ b/spec/models/preloaders/commit_status_preloader_spec.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Preloaders::CommitStatusPreloader do + let_it_be(:pipeline) { create(:ci_pipeline) } + + let_it_be(:build1) { create(:ci_build, :tags, pipeline: pipeline) } + let_it_be(:build2) { create(:ci_build, :tags, pipeline: pipeline) } + let_it_be(:bridge1) { create(:ci_bridge, pipeline: pipeline) } + let_it_be(:bridge2) { create(:ci_bridge, pipeline: pipeline) } + let_it_be(:generic_commit_status1) { create(:generic_commit_status, pipeline: pipeline) } + let_it_be(:generic_commit_status2) { create(:generic_commit_status, pipeline: pipeline) } + + describe '#execute' do + let(:relations) { %i[pipeline metadata tags job_artifacts_archive downstream_pipeline] } + let(:statuses) { CommitStatus.where(commit_id: pipeline.id).all } + + subject(:execute) { described_class.new(statuses).execute(relations) } + + it 'prevents N+1 for specified relations', :use_sql_query_cache do + execute + + control_count = ActiveRecord::QueryRecorder.new(skip_cached: false) do + call_each_relation(statuses.sample(3)) + end + + expect do + call_each_relation(statuses) + end.to issue_same_number_of_queries_as(control_count) + end + + private + + def call_each_relation(statuses) + statuses.each do |status| + relations.each { |relation| status.public_send(relation) if status.respond_to?(relation) } + end + end + end +end |