From c871faa3e42edf7c1d92669cf18a5b0de7c84ace Mon Sep 17 00:00:00 2001 From: Chantal Rollison Date: Mon, 10 Sep 2018 10:26:33 -0700 Subject: Add preload in issues controller --- spec/controllers/boards/issues_controller_spec.rb | 49 ++++++++++++++++++++++- 1 file changed, 47 insertions(+), 2 deletions(-) (limited to 'spec/controllers/boards') diff --git a/spec/controllers/boards/issues_controller_spec.rb b/spec/controllers/boards/issues_controller_spec.rb index d98e6ff0df8..c365988a100 100644 --- a/spec/controllers/boards/issues_controller_spec.rb +++ b/spec/controllers/boards/issues_controller_spec.rb @@ -30,6 +30,15 @@ describe Boards::IssuesController do context 'when list id is present' do context 'with valid list id' do + let(:group) { create(:group, :private, projects: [project]) } + let(:group_board) { create(:board, group: group) } + let!(:list3) { create(:list, board: group_board, label: development, position: 2) } + let(:sub_group_1) { create(:group, :private, parent: group) } + + before do + group.add_maintainer(user) + end + it 'returns issues that have the list label applied' do issue = create(:labeled_issue, project: project, labels: [planning]) create(:labeled_issue, project: project, labels: [planning]) @@ -56,6 +65,39 @@ describe Boards::IssuesController do expect { list_issues(user: user, board: board, list: list2) }.not_to exceed_query_limit(control_count) end + + it 'avoids N+1 database queries when adding a project', :request_store do + create(:labeled_issue, project: project, labels: [development]) + control_count = ActiveRecord::QueryRecorder.new { list_issues(user: user, board: group_board, list: list3) }.count + + 2.times do + p = create(:project, group: group) + create(:labeled_issue, project: p, labels: [development]) + end + + project_2 = create(:project, group: group) + create(:labeled_issue, project: project_2, labels: [development], assignees: [johndoe]) + + # because each issue without relative_position must be updated with + # a different value, we have 8 extra queries per issue + expect { list_issues(user: user, board: group_board, list: list3) }.not_to exceed_query_limit(control_count + (2 * 8 - 1)) + end + + it 'avoids N+1 database queries when adding a subgroup, project, and issue', :nested_groups do + create(:project, group: sub_group_1) + create(:labeled_issue, project: project, labels: [development]) + control_count = ActiveRecord::QueryRecorder.new { list_issues(user: user, board: group_board, list: list3) }.count + project_2 = create(:project, group: group) + + 2.times do + p = create(:project, group: sub_group_1) + create(:labeled_issue, project: p, labels: [development]) + end + + create(:labeled_issue, project: project_2, labels: [development], assignees: [johndoe]) + + expect { list_issues(user: user, board: group_board, list: list3) }.not_to exceed_query_limit(control_count + (2 * 8 - 1)) + end end context 'with invalid list id' do @@ -102,12 +144,15 @@ describe Boards::IssuesController do sign_in(user) params = { - namespace_id: project.namespace.to_param, - project_id: project, board_id: board.to_param, list_id: list.try(:to_param) } + unless board.try(:parent)&.is_a?(Group) + params[:namespace_id] = project.namespace.to_param + params[:project_id] = project + end + get :index, params.compact end end -- cgit v1.2.1