diff options
Diffstat (limited to 'spec/models/preloaders')
-rw-r--r-- | spec/models/preloaders/project_policy_preloader_spec.rb | 55 | ||||
-rw-r--r-- | spec/models/preloaders/project_root_ancestor_preloader_spec.rb | 99 |
2 files changed, 154 insertions, 0 deletions
diff --git a/spec/models/preloaders/project_policy_preloader_spec.rb b/spec/models/preloaders/project_policy_preloader_spec.rb new file mode 100644 index 00000000000..79f232f5ce2 --- /dev/null +++ b/spec/models/preloaders/project_policy_preloader_spec.rb @@ -0,0 +1,55 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Preloaders::ProjectPolicyPreloader do + let_it_be(:user) { create(:user) } + let_it_be(:root_parent) { create(:group, :private, name: 'root-1', path: 'root-1') } + let_it_be(:guest_project) { create(:project, name: 'public guest', path: 'public-guest') } + let_it_be(:private_maintainer_project) do + create(:project, :private, name: 'b private maintainer', path: 'b-private-maintainer', namespace: root_parent) + end + + let_it_be(:private_developer_project) do + create(:project, :private, name: 'c public developer', path: 'c-public-developer') + end + + let_it_be(:public_maintainer_project) do + create(:project, :private, name: 'a public maintainer', path: 'a-public-maintainer') + end + + let(:base_projects) do + Project.where(id: [guest_project, private_maintainer_project, private_developer_project, public_maintainer_project]) + end + + before_all do + guest_project.add_guest(user) + private_maintainer_project.add_maintainer(user) + private_developer_project.add_developer(user) + public_maintainer_project.add_maintainer(user) + end + + it 'avoids N+1 queries when authorizing a list of projects', :request_store do + preload_projects_for_policy(user) + control = ActiveRecord::QueryRecorder.new { authorize_all_projects(user) } + + new_project1 = create(:project, :private).tap { |project| project.add_maintainer(user) } + new_project2 = create(:project, :private, namespace: root_parent) + + another_root = create(:group, :private, name: 'root-3', path: 'root-3') + new_project3 = create(:project, :private, namespace: another_root).tap { |project| project.add_maintainer(user) } + + pristine_projects = Project.where(id: base_projects + [new_project1, new_project2, new_project3]) + + preload_projects_for_policy(user, pristine_projects) + expect { authorize_all_projects(user, pristine_projects) }.not_to exceed_query_limit(control) + end + + def authorize_all_projects(current_user, project_list = base_projects) + project_list.each { |project| current_user.can?(:read_project, project) } + end + + def preload_projects_for_policy(current_user, project_list = base_projects) + described_class.new(project_list, current_user).execute + end +end diff --git a/spec/models/preloaders/project_root_ancestor_preloader_spec.rb b/spec/models/preloaders/project_root_ancestor_preloader_spec.rb new file mode 100644 index 00000000000..30036a6a033 --- /dev/null +++ b/spec/models/preloaders/project_root_ancestor_preloader_spec.rb @@ -0,0 +1,99 @@ +# frozen_string_literal: true + +require 'spec_helper' + +RSpec.describe Preloaders::ProjectRootAncestorPreloader do + let_it_be(:root_parent1) { create(:group, :private, name: 'root-1', path: 'root-1') } + let_it_be(:root_parent2) { create(:group, :private, name: 'root-2', path: 'root-2') } + let_it_be(:guest_project) { create(:project, name: 'public guest', path: 'public-guest') } + let_it_be(:private_maintainer_project) do + create(:project, :private, name: 'b private maintainer', path: 'b-private-maintainer', namespace: root_parent1) + end + + let_it_be(:private_developer_project) do + create(:project, :private, name: 'c public developer', path: 'c-public-developer') + end + + let_it_be(:public_maintainer_project) do + create(:project, :private, name: 'a public maintainer', path: 'a-public-maintainer', namespace: root_parent2) + end + + let(:root_query_regex) { /\ASELECT.+FROM "namespaces" WHERE "namespaces"."id" = \d+/ } + let(:additional_preloads) { [] } + let(:projects) { [guest_project, private_maintainer_project, private_developer_project, public_maintainer_project] } + let(:pristine_projects) { Project.where(id: projects) } + + shared_examples 'executes N matching DB queries' do |expected_query_count, query_method = nil| + it 'executes the specified root_ancestor queries' do + expect do + pristine_projects.each do |project| + root_ancestor = project.root_ancestor + + root_ancestor.public_send(query_method) if query_method.present? + end + end.to make_queries_matching(root_query_regex, expected_query_count) + end + + it 'strong_memoizes the correct root_ancestor' do + pristine_projects.each do |project| + expected_parent_id = project.root_ancestor&.id + + expect(project.parent_id).to eq(expected_parent_id) + end + end + end + + context 'when use_traversal_ids FF is enabled' do + context 'when the preloader is used' do + before do + preload_ancestors + end + + context 'when no additional preloads are provided' do + it_behaves_like 'executes N matching DB queries', 0 + end + + context 'when additional preloads are provided' do + let(:additional_preloads) { [:route] } + let(:root_query_regex) { /\ASELECT.+FROM "routes" WHERE "routes"."source_id" = \d+/ } + + it_behaves_like 'executes N matching DB queries', 0, :full_path + end + end + + context 'when the preloader is not used' do + it_behaves_like 'executes N matching DB queries', 4 + end + end + + context 'when use_traversal_ids FF is disabled' do + before do + stub_feature_flags(use_traversal_ids: false) + end + + context 'when the preloader is used' do + before do + preload_ancestors + end + + context 'when no additional preloads are provided' do + it_behaves_like 'executes N matching DB queries', 4 + end + + context 'when additional preloads are provided' do + let(:additional_preloads) { [:route] } + let(:root_query_regex) { /\ASELECT.+FROM "routes" WHERE "routes"."source_id" = \d+/ } + + it_behaves_like 'executes N matching DB queries', 4, :full_path + end + end + + context 'when the preloader is not used' do + it_behaves_like 'executes N matching DB queries', 4 + end + end + + def preload_ancestors + described_class.new(pristine_projects, :namespace, additional_preloads).execute + end +end |