Commit 74a2e3a5 authored by Tan Le's avatar Tan Le

Reduce database calls in approval rules specs

This commit converts `let` to `let_it_be` to reduce the number of
database calls when setting up test data. All Rubocop violations on
touched files are also fixed.
parent db66f1cf
......@@ -5,10 +5,23 @@ require 'spec_helper'
RSpec.describe 'Merge request > User edits MR with approval rules', :js do
include Select2Helper
include_context 'project with approval rules'
include_context 'with project with approval rules'
let_it_be(:merge_request) { create(:merge_request, source_project: project) }
let_it_be(:approver) { create(:user) }
let_it_be(:mr_rule_names) { %w[foo lorem ipsum] }
let_it_be(:mr_rules) do
mr_rule_names.map do |name|
create(
:approval_merge_request_rule,
merge_request: merge_request,
approvals_required: 1,
name: name,
users: [approver]
)
end
end
let(:merge_request) { create(:merge_request, source_project: project) }
let(:mr_rule_names) { %w[foo lorem ipsum] }
let(:modal_id) { '#mr-edit-approvals-create-modal' }
let(:members_selector) { "#{modal_id} input[name=members]" }
let(:members_search_selector) { "#{modal_id} .select2-input" }
......@@ -25,15 +38,9 @@ RSpec.describe 'Merge request > User edits MR with approval rules', :js do
end
before do
project.update_attribute(:disable_overriding_approvers_per_merge_request, false)
project.update!(disable_overriding_approvers_per_merge_request: false)
stub_licensed_features(multiple_approval_rules: true)
approver = create(:user)
mr_rule_names.each do |name|
create(:approval_merge_request_rule,
merge_request: merge_request, approvals_required: 1, name: name, users: [approver])
end
sign_in(author)
visit(edit_project_merge_request_path(project, merge_request))
......@@ -66,7 +73,7 @@ RSpec.describe 'Merge request > User edits MR with approval rules', :js do
end
context "with public group" do
let!(:group) { create(:group, :public) }
let_it_be(:group) { create(:group, :public) }
before do
group.add_developer create(:user)
......
......@@ -5,22 +5,25 @@ require 'spec_helper'
RSpec.describe 'Merge request > User sets approval rules', :js do
include ProjectForksHelper
include_context 'project with approval rules'
include_context 'with project with approval rules'
def page_rule_names
page.all('.js-approval-rules table .js-name')
end
context "with project approval rules" do
context "from a fork" do
let(:forked_project) { fork_project(project, nil, repository: true) }
context "when from a fork" do
let_it_be(:forked_project) { fork_project(project, nil, repository: true) }
before do
forked_project.add_maintainer(author)
allow(forked_project).to receive(:multiple_approval_rules_available?).and_return(false)
sign_in(author)
visit project_new_merge_request_path(forked_project, merge_request: { target_branch: 'master', target_project_id: project.id, source_branch: 'feature' })
visit project_new_merge_request_path(
forked_project,
merge_request: { target_branch: 'master', target_project_id: project.id, source_branch: 'feature' }
)
wait_for_requests
end
......@@ -35,15 +38,21 @@ RSpec.describe 'Merge request > User sets approval rules', :js do
end
context "with a private group rule" do
let!(:private_group) { create(:group, :private) }
let!(:private_rule) { create(:approval_project_rule, project: project, groups: [private_group], name: 'Private Rule') }
let!(:rules) { regular_rules + [private_rule] }
let_it_be(:private_group) { create(:group, :private) }
let_it_be(:private_rule) do
create(:approval_project_rule, project: project, groups: [private_group], name: 'Private Rule')
end
let_it_be(:rules) { regular_rules + [private_rule] }
before do
private_group.add_developer(approver)
sign_in(author)
visit project_new_merge_request_path(project, merge_request: { target_branch: 'master', source_branch: 'feature' })
visit project_new_merge_request_path(
project,
merge_request: { target_branch: 'master', source_branch: 'feature' }
)
wait_for_requests
end
......
......@@ -3,12 +3,12 @@
require 'spec_helper'
RSpec.describe ApprovalRules::GroupFinder do
let(:rule) { create(:approval_project_rule) }
let(:user) { create(:user) }
let_it_be_with_reload(:rule) { create(:approval_project_rule) }
let_it_be(:user) { create(:user) }
let(:public_group) { create(:group, name: 'public_group') }
let(:private_inaccessible_group) { create(:group, :private, name: 'private_inaccessible_group') }
let(:private_accessible_group) { create(:group, :private, name: 'private_accessible_group') }
let_it_be(:public_group) { create(:group, name: 'public_group') }
let_it_be(:private_inaccessible_group) { create(:group, :private, name: 'private_inaccessible_group') }
let_it_be(:private_accessible_group) { create(:group, :private, name: 'private_accessible_group') }
subject { described_class.new(rule, user) }
......
# frozen_string_literal: true
RSpec.shared_context 'project with approval rules' do
let(:approver) { create(:user) }
let(:author) { create(:user) }
let(:project) { create(:project, :public, :repository) }
RSpec.shared_context 'with project with approval rules' do
let_it_be(:approver) { create(:user) }
let_it_be(:author) { create(:user) }
let_it_be(:project) { create(:project, :public, :repository) }
before do
stub_licensed_features(multiple_approval_rules: true)
......@@ -13,7 +13,7 @@ RSpec.shared_context 'project with approval rules' do
end
end
let!(:regular_rules) do
let_it_be(:regular_rules) do
Array.new(3) do |i|
create(:approval_project_rule, project: project, users: [approver], name: "Regular Rule #{i}")
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment