Commit cd2e298b authored by Eulyeon Ko's avatar Eulyeon Ko

Refactor milestone related specs for performance

Refactor to use let_it_be and build wherever suitable
to improve test performance.
parent 5695800c
...@@ -3,46 +3,62 @@ ...@@ -3,46 +3,62 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe MilestonesFinder do RSpec.describe MilestonesFinder do
let(:now) { Time.now } let_it_be(:now) { Time.now }
let(:group) { create(:group) } let_it_be(:group) { create(:group) }
let(:project_1) { create(:project, namespace: group) } let_it_be(:project_1) { create(:project, namespace: group) }
let(:project_2) { create(:project, namespace: group) } let_it_be(:project_2) { create(:project, namespace: group) }
let!(:milestone_1) { create(:milestone, group: group, title: 'one test', start_date: now - 1.day, due_date: now) }
let!(:milestone_2) { create(:milestone, group: group, start_date: now + 1.day, due_date: now + 2.days) }
let!(:milestone_3) { create(:milestone, project: project_1, state: 'active', start_date: now + 2.days, due_date: now + 3.days) }
let!(:milestone_4) { create(:milestone, project: project_2, state: 'active', start_date: now + 4.days, due_date: now + 5.days) }
it 'returns milestones for projects' do
result = described_class.new(project_ids: [project_1.id, project_2.id], state: 'all').execute
expect(result).to contain_exactly(milestone_3, milestone_4)
end
it 'returns milestones for groups' do context 'without filters' do
result = described_class.new(group_ids: group.id, state: 'all').execute let_it_be(:milestone_1) { create(:milestone, group: group, title: 'one test', start_date: now - 1.day, due_date: now) }
let_it_be(:milestone_2) { create(:milestone, group: group, start_date: now + 1.day, due_date: now + 2.days) }
let_it_be(:milestone_3) { create(:milestone, project: project_1, state: 'active', start_date: now + 2.days, due_date: now + 3.days) }
let_it_be(:milestone_4) { create(:milestone, project: project_2, state: 'active', start_date: now + 4.days, due_date: now + 5.days) }
expect(result).to contain_exactly(milestone_1, milestone_2) it 'returns milestones for projects' do
end result = described_class.new(project_ids: [project_1.id, project_2.id], state: 'all').execute
expect(result).to contain_exactly(milestone_3, milestone_4)
end
it 'returns milestones for groups' do
result = described_class.new(group_ids: group.id, state: 'all').execute
context 'milestones for groups and project' do expect(result).to contain_exactly(milestone_1, milestone_2)
let(:result) do
described_class.new(project_ids: [project_1.id, project_2.id], group_ids: group.id, state: 'all').execute
end end
it 'returns milestones for groups and projects' do context 'milestones for groups and project' do
expect(result).to contain_exactly(milestone_1, milestone_2, milestone_3, milestone_4) let(:result) do
described_class.new(project_ids: [project_1.id, project_2.id], group_ids: group.id, state: 'all').execute
end
it 'returns milestones for groups and projects' do
expect(result).to contain_exactly(milestone_1, milestone_2, milestone_3, milestone_4)
end
it 'orders milestones by due date' do
milestone = create(:milestone, group: group, due_date: now - 2.days)
expect(result.first).to eq(milestone)
expect(result.second).to eq(milestone_1)
expect(result.third).to eq(milestone_2)
end
end end
it 'orders milestones by due date' do describe '#find_by' do
milestone = create(:milestone, group: group, due_date: now - 2.days) it 'finds a single milestone' do
finder = described_class.new(project_ids: [project_1.id], state: 'all')
expect(result.first).to eq(milestone) expect(finder.find_by(iid: milestone_3.iid)).to eq(milestone_3)
expect(result.second).to eq(milestone_1) end
expect(result.third).to eq(milestone_2)
end end
end end
context 'with filters' do context 'with filters' do
let_it_be(:milestone_1) { create(:milestone, group: group, state: 'closed', title: 'one test', start_date: now - 1.day, due_date: now) }
let_it_be(:milestone_2) { create(:milestone, group: group, start_date: now + 1.day, due_date: now + 2.days) }
let_it_be(:milestone_3) { create(:milestone, project: project_1, state: 'closed', start_date: now + 2.days, due_date: now + 3.days) }
let_it_be(:milestone_4) { create(:milestone, project: project_2, state: 'active', start_date: now + 4.days, due_date: now + 5.days) }
let(:params) do let(:params) do
{ {
project_ids: [project_1.id, project_2.id], project_ids: [project_1.id, project_2.id],
...@@ -51,11 +67,6 @@ RSpec.describe MilestonesFinder do ...@@ -51,11 +67,6 @@ RSpec.describe MilestonesFinder do
} }
end end
before do
milestone_1.close
milestone_3.close
end
it 'filters by id' do it 'filters by id' do
params[:ids] = [milestone_1.id, milestone_2.id] params[:ids] = [milestone_1.id, milestone_2.id]
...@@ -118,12 +129,4 @@ RSpec.describe MilestonesFinder do ...@@ -118,12 +129,4 @@ RSpec.describe MilestonesFinder do
end end
end end
end end
describe '#find_by' do
it 'finds a single milestone' do
finder = described_class.new(project_ids: [project_1.id], state: 'all')
expect(finder.find_by(iid: milestone_3.iid)).to eq(milestone_3)
end
end
end end
...@@ -3,10 +3,9 @@ ...@@ -3,10 +3,9 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Milestone do RSpec.describe Milestone do
let(:user) { create(:user) } let_it_be(:user) { create(:user) }
let(:issue) { create(:issue, project: project) } let_it_be(:project) { create(:project, :public) }
let(:milestone) { create(:milestone, project: project) } let_it_be(:issue) { create(:issue, project: project) }
let(:project) { create(:project, :public) }
it_behaves_like 'a timebox', :milestone do it_behaves_like 'a timebox', :milestone do
describe "#uniqueness_of_title" do describe "#uniqueness_of_title" do
...@@ -92,6 +91,8 @@ RSpec.describe Milestone do ...@@ -92,6 +91,8 @@ RSpec.describe Milestone do
end end
describe '.predefined_id?' do describe '.predefined_id?' do
let_it_be(:milestone) { create(:milestone, project: project) }
it 'returns true for a predefined Milestone ID' do it 'returns true for a predefined Milestone ID' do
expect(Milestone.predefined_id?(described_class::Upcoming.id)).to be true expect(Milestone.predefined_id?(described_class::Upcoming.id)).to be true
end end
...@@ -129,6 +130,8 @@ RSpec.describe Milestone do ...@@ -129,6 +130,8 @@ RSpec.describe Milestone do
end end
describe "#percent_complete" do describe "#percent_complete" do
let(:milestone) { create(:milestone, project: project) }
it "does not count open issues" do it "does not count open issues" do
milestone.issues << issue milestone.issues << issue
expect(milestone.percent_complete).to eq(0) expect(milestone.percent_complete).to eq(0)
...@@ -146,13 +149,15 @@ RSpec.describe Milestone do ...@@ -146,13 +149,15 @@ RSpec.describe Milestone do
end end
describe '#expired?' do describe '#expired?' do
let_it_be(:milestone) { build(:milestone, project: project) }
context "expired" do context "expired" do
before do before do
allow(milestone).to receive(:due_date).and_return(Date.today.prev_year) allow(milestone).to receive(:due_date).and_return(Date.today.prev_year)
end end
it 'returns true when due_date is in the past' do it 'returns true when due_date is in the past' do
expect(milestone.expired?).to be_truthy expect(milestone.expired?).to be_truthy
end end
end end
...@@ -162,7 +167,7 @@ RSpec.describe Milestone do ...@@ -162,7 +167,7 @@ RSpec.describe Milestone do
end end
it 'returns false when due_date is in the future' do it 'returns false when due_date is in the future' do
expect(milestone.expired?).to be_falsey expect(milestone.expired?).to be_falsey
end end
end end
end end
...@@ -180,10 +185,14 @@ RSpec.describe Milestone do ...@@ -180,10 +185,14 @@ RSpec.describe Milestone do
end end
describe '#can_be_closed?' do describe '#can_be_closed?' do
let_it_be(:milestone) { build(:milestone, project: project) }
it { expect(milestone.can_be_closed?).to be_truthy } it { expect(milestone.can_be_closed?).to be_truthy }
end end
describe '#can_be_closed?' do describe '#can_be_closed?' do
let_it_be(:milestone) { build(:milestone, project: project) }
before do before do
milestone = create :milestone, project: project milestone = create :milestone, project: project
create :closed_issue, milestone: milestone, project: project create :closed_issue, milestone: milestone, project: project
...@@ -335,10 +344,10 @@ RSpec.describe Milestone do ...@@ -335,10 +344,10 @@ RSpec.describe Milestone do
it_behaves_like '#for_projects_and_groups' it_behaves_like '#for_projects_and_groups'
describe '.upcoming_ids' do describe '.upcoming_ids' do
let(:group_1) { create(:group) } let_it_be(:group_1) { create(:group) }
let(:group_2) { create(:group) } let_it_be(:group_2) { create(:group) }
let(:group_3) { create(:group) } let_it_be(:group_3) { create(:group) }
let(:groups) { [group_1, group_2, group_3] } let_it_be(:groups) { [group_1, group_2, group_3] }
let!(:past_milestone_group_1) { create(:milestone, group: group_1, due_date: Time.current - 1.day) } let!(:past_milestone_group_1) { create(:milestone, group: group_1, due_date: Time.current - 1.day) }
let!(:current_milestone_group_1) { create(:milestone, group: group_1, due_date: Time.current + 1.day) } let!(:current_milestone_group_1) { create(:milestone, group: group_1, due_date: Time.current + 1.day) }
...@@ -350,10 +359,10 @@ RSpec.describe Milestone do ...@@ -350,10 +359,10 @@ RSpec.describe Milestone do
let!(:past_milestone_group_3) { create(:milestone, group: group_3, due_date: Time.current - 1.day) } let!(:past_milestone_group_3) { create(:milestone, group: group_3, due_date: Time.current - 1.day) }
let(:project_1) { create(:project) } let_it_be(:project_1) { create(:project) }
let(:project_2) { create(:project) } let_it_be(:project_2) { create(:project) }
let(:project_3) { create(:project) } let_it_be(:project_3) { create(:project) }
let(:projects) { [project_1, project_2, project_3] } let_it_be(:projects) { [project_1, project_2, project_3] }
let!(:past_milestone_project_1) { create(:milestone, project: project_1, due_date: Time.current - 1.day) } let!(:past_milestone_project_1) { create(:milestone, project: project_1, due_date: Time.current - 1.day) }
let!(:current_milestone_project_1) { create(:milestone, project: project_1, due_date: Time.current + 1.day) } let!(:current_milestone_project_1) { create(:milestone, project: project_1, due_date: Time.current + 1.day) }
......
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