blob: 7b241fc29afb04d63424c2606490efc19c19042c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe RendersCommits do
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
include RendersCommits
def index
@merge_request = MergeRequest.find(params[:id])
@commits = set_commits_for_rendering(
@merge_request.recent_commits.with_latest_pipeline(@merge_request.source_branch),
commits_count: @merge_request.commits_count
)
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
it 'avoids N + 1' do
stub_const("MergeRequestDiff::COMMITS_SAFE_SIZE", 5)
control_count = ActiveRecord::QueryRecorder.new do
go
end.count
stub_const("MergeRequestDiff::COMMITS_SAFE_SIZE", 15)
expect do
go
end.not_to exceed_all_query_limit(control_count)
end
end
describe '.prepare_commits_for_rendering' do
it 'avoids N+1' do
control = ActiveRecord::QueryRecorder.new do
subject.prepare_commits_for_rendering(merge_request.commits.take(1))
end
expect do
subject.prepare_commits_for_rendering(merge_request.commits)
end.not_to exceed_all_query_limit(control.count)
end
end
end
|