2019-07-25 01:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-05-26 07:16:43 -04:00
|
|
|
module ReferenceParserHelpers
|
|
|
|
def empty_html_link
|
|
|
|
Nokogiri::HTML.fragment('<a></a>').children[0]
|
|
|
|
end
|
2018-04-02 11:14:20 -04:00
|
|
|
|
2021-08-26 14:10:35 -04:00
|
|
|
def expect_gathered_references(result, visible, nodes, visible_nodes)
|
2020-01-30 16:08:47 -05:00
|
|
|
expect(result[:visible]).to eq(visible)
|
2021-08-26 14:10:35 -04:00
|
|
|
expect(result[:nodes]).to eq(nodes)
|
|
|
|
expect(result[:visible_nodes]).to eq(visible_nodes)
|
2020-01-30 16:08:47 -05:00
|
|
|
end
|
|
|
|
|
2020-06-24 11:08:50 -04:00
|
|
|
RSpec.shared_examples 'no project N+1 queries' do
|
2021-05-31 08:10:34 -04:00
|
|
|
it 'avoids N+1 queries in #nodes_visible_to_user' do
|
2018-04-03 09:45:17 -04:00
|
|
|
context = Banzai::RenderContext.new(project, user)
|
|
|
|
|
2021-05-31 08:10:34 -04:00
|
|
|
request = lambda do |links|
|
|
|
|
described_class.new(context).nodes_visible_to_user(user, links)
|
2018-04-02 11:14:20 -04:00
|
|
|
end
|
|
|
|
|
2021-05-31 08:10:34 -04:00
|
|
|
control = ActiveRecord::QueryRecorder.new { request.call(control_links) }
|
2021-05-26 08:10:41 -04:00
|
|
|
|
|
|
|
create(:group_member, group: project.group) if project.group
|
|
|
|
create(:project_member, project: project)
|
|
|
|
create(:project_group_link, project: project)
|
|
|
|
|
2021-05-31 08:10:34 -04:00
|
|
|
expect { request.call(actual_links) }.not_to exceed_query_limit(control)
|
2018-04-02 11:14:20 -04:00
|
|
|
end
|
2018-09-30 13:54:21 -04:00
|
|
|
end
|
|
|
|
|
2020-06-24 11:08:50 -04:00
|
|
|
RSpec.shared_examples 'no N+1 queries' do
|
2018-09-30 13:54:21 -04:00
|
|
|
it_behaves_like 'no project N+1 queries'
|
2018-04-02 11:14:20 -04:00
|
|
|
|
|
|
|
it 'avoids N+1 queries in #records_for_nodes', :request_store do
|
2018-04-03 09:45:17 -04:00
|
|
|
context = Banzai::RenderContext.new(project, user)
|
|
|
|
|
2018-04-02 11:14:20 -04:00
|
|
|
record_queries = lambda do |links|
|
|
|
|
ActiveRecord::QueryRecorder.new do
|
2018-04-03 09:45:17 -04:00
|
|
|
described_class.new(context).records_for_nodes(links)
|
2018-04-02 11:14:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
control = record_queries.call(control_links)
|
|
|
|
actual = record_queries.call(actual_links)
|
|
|
|
|
|
|
|
expect(actual.count).to be <= control.count
|
|
|
|
expect(actual.cached_count).to be <= control.cached_count
|
|
|
|
end
|
|
|
|
end
|
2016-05-26 07:16:43 -04:00
|
|
|
end
|