2019-12-26 22:10:19 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
RSpec.describe RendersCommits do
|
2019-12-26 22:10:19 +05:30
|
|
|
let_it_be(:project) { create(:project, :public, :repository) }
|
|
|
|
let_it_be(:merge_request) { create(:merge_request, source_project: project) }
|
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
|
|
|
|
controller(ApplicationController) do
|
|
|
|
# `described_class` is not available in this context
|
2020-03-13 15:44:24 +05:30
|
|
|
include RendersCommits
|
2019-12-26 22:10:19 +05:30
|
|
|
|
|
|
|
def index
|
|
|
|
@merge_request = MergeRequest.find(params[:id])
|
|
|
|
@commits = set_commits_for_rendering(
|
|
|
|
@merge_request.recent_commits.with_latest_pipeline(@merge_request.source_branch),
|
2023-05-27 22:25:52 +05:30
|
|
|
commits_count: @merge_request.commits_count
|
2019-12-26 22:10:19 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
render json: { html: view_to_html_string('projects/merge_requests/_commits') }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
def go
|
|
|
|
get :index, params: { id: merge_request.id }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets instance variables for counts' do
|
|
|
|
stub_const("MergeRequestDiff::COMMITS_SAFE_SIZE", 10)
|
|
|
|
|
|
|
|
go
|
|
|
|
|
|
|
|
expect(assigns[:total_commit_count]).to eq(29)
|
|
|
|
expect(assigns[:hidden_commit_count]).to eq(19)
|
|
|
|
expect(assigns[:commits].size).to eq(10)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'rendering commits' do
|
|
|
|
render_views
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
it 'avoids N + 1', :request_store do
|
2019-12-26 22:10:19 +05:30
|
|
|
stub_const("MergeRequestDiff::COMMITS_SAFE_SIZE", 5)
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
control_count = ActiveRecord::QueryRecorder.new do
|
2019-12-26 22:10:19 +05:30
|
|
|
go
|
|
|
|
end.count
|
|
|
|
|
|
|
|
stub_const("MergeRequestDiff::COMMITS_SAFE_SIZE", 15)
|
|
|
|
|
|
|
|
expect do
|
|
|
|
go
|
|
|
|
end.not_to exceed_all_query_limit(control_count)
|
|
|
|
end
|
|
|
|
end
|
2021-04-29 21:17:54 +05:30
|
|
|
|
|
|
|
describe '.prepare_commits_for_rendering' do
|
2023-01-13 00:05:48 +05:30
|
|
|
it 'avoids N+1', :request_store do
|
2021-04-29 21:17:54 +05:30
|
|
|
control = ActiveRecord::QueryRecorder.new do
|
|
|
|
subject.prepare_commits_for_rendering(merge_request.commits.take(1))
|
|
|
|
end
|
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
# Populate Banzai::Filter::References::ReferenceCache
|
|
|
|
subject.prepare_commits_for_rendering(merge_request.commits)
|
|
|
|
|
|
|
|
# Reset lazy_latest_pipeline cache to simulate a new request
|
|
|
|
BatchLoader::Executor.clear_current
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
expect do
|
|
|
|
subject.prepare_commits_for_rendering(merge_request.commits)
|
2021-06-08 01:23:25 +05:30
|
|
|
merge_request.commits.each(&:latest_pipeline)
|
2021-04-29 21:17:54 +05:30
|
|
|
end.not_to exceed_all_query_limit(control.count)
|
|
|
|
end
|
|
|
|
end
|
2019-12-26 22:10:19 +05:30
|
|
|
end
|