2021-04-29 21:17:54 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
RSpec.describe Preloaders::UserMaxAccessLevelInProjectsPreloader do
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
let_it_be(:project_1) { create(:project) }
|
|
|
|
let_it_be(:project_2) { create(:project) }
|
|
|
|
let_it_be(:project_3) { create(:project) }
|
2022-07-23 23:45:48 +05:30
|
|
|
let_it_be(:project_4) { create(:project) }
|
|
|
|
let_it_be(:project_5) { create(:project) }
|
2021-04-29 21:17:54 +05:30
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
let(:projects) { [project_1, project_2, project_3, project_4, project_5] }
|
2022-07-16 23:28:13 +05:30
|
|
|
let(:query) { projects.each { |project| user.can?(:read_project, project) } }
|
2021-04-29 21:17:54 +05:30
|
|
|
|
|
|
|
before do
|
|
|
|
project_1.add_developer(user)
|
|
|
|
project_2.add_developer(user)
|
|
|
|
end
|
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
context 'without preloader' do
|
2022-07-23 23:45:48 +05:30
|
|
|
it 'runs some queries' do
|
|
|
|
# we have an existing N+1, one for each project for which user is not a member
|
|
|
|
# in this spec, project_3, project_4, project_5
|
|
|
|
# https://gitlab.com/gitlab-org/gitlab/-/issues/362890
|
|
|
|
expect { query }.to make_queries(projects.size + 3)
|
2022-07-16 23:28:13 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#execute', :request_store do
|
|
|
|
let(:projects_arg) { projects }
|
|
|
|
|
|
|
|
before do
|
|
|
|
Preloaders::UserMaxAccessLevelInProjectsPreloader.new(projects_arg, user).execute
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'avoids N+1 queries' do
|
|
|
|
expect { query }.not_to make_queries
|
|
|
|
end
|
2021-04-29 21:17:54 +05:30
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
context 'when projects is an array of IDs' do
|
2022-07-23 23:45:48 +05:30
|
|
|
let(:projects_arg) { projects.map(&:id) }
|
2021-04-29 21:17:54 +05:30
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
it 'avoids N+1 queries' do
|
|
|
|
expect { query }.not_to make_queries
|
|
|
|
end
|
2021-04-29 21:17:54 +05:30
|
|
|
end
|
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
# Test for handling of SQL table name clashes.
|
|
|
|
context 'when projects is a relation including project_authorizations' do
|
|
|
|
let(:projects_arg) do
|
|
|
|
Project.where(id: ProjectAuthorization.where(project_id: projects).select(:project_id))
|
|
|
|
end
|
2021-04-29 21:17:54 +05:30
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
it 'avoids N+1 queries' do
|
|
|
|
expect { query }.not_to make_queries
|
|
|
|
end
|
2021-04-29 21:17:54 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|