debian-mirror-gitlab/spec/models/preloaders/labels_preloader_spec.rb

64 lines
1.7 KiB
Ruby
Raw Permalink Normal View History

2021-04-29 21:17:54 +05:30
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Preloaders::LabelsPreloader do
let_it_be(:user) { create(:user) }
shared_examples 'an efficient database query' do
2022-08-27 11:52:29 +05:30
let(:subscriptions) { labels.each { |l| create(:subscription, subscribable: l, project: l.project, user: user) } }
2021-04-29 21:17:54 +05:30
it 'does not make n+1 queries' do
first_label = labels_with_preloaded_data.first
clean_labels = labels_with_preloaded_data
expect { access_data(clean_labels) }.to issue_same_number_of_queries_as { access_data([first_label]) }
end
end
context 'project labels' do
let_it_be(:projects) { create_list(:project, 3, :public, :repository) }
2023-06-20 00:43:36 +05:30
let_it_be(:labels) { projects.map { |p| create(:label, project: p) } }
2021-04-29 21:17:54 +05:30
it_behaves_like 'an efficient database query'
2023-06-20 00:43:36 +05:30
it 'preloads the max access level', :request_store do
labels_with_preloaded_data
query_count = ActiveRecord::QueryRecorder.new do
projects.first.team.max_member_access_for_user_ids([user.id])
end.count
expect(query_count).to eq(0)
end
2021-04-29 21:17:54 +05:30
end
context 'group labels' do
let_it_be(:groups) { create_list(:group, 3) }
2023-06-20 00:43:36 +05:30
let_it_be(:labels) { groups.map { |g| create(:group_label, group: g) } }
2021-04-29 21:17:54 +05:30
it_behaves_like 'an efficient database query'
end
private
def labels_with_preloaded_data
l = Label.where(id: labels.map(&:id))
described_class.new(l, user).preload_all
l
end
def access_data(labels)
labels.each do |label|
2023-01-13 00:05:48 +05:30
case label
when ProjectLabel
2021-04-29 21:17:54 +05:30
label.project.project_feature
label.lazy_subscription(user, label.project)
2023-01-13 00:05:48 +05:30
when GroupLabel
2021-04-29 21:17:54 +05:30
label.group.route
label.lazy_subscription(user)
end
end
end
end