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,14 +3,16 @@
require 'spec_helper'
RSpec.describe MilestonesFinder do
let(:now) { Time.now }
let(:group) { create(:group) }
let(:project_1) { create(:project, namespace: group) }
let(: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) }
let_it_be(:now) { Time.now }
let_it_be(:group) { create(:group) }
let_it_be(:project_1) { create(:project, namespace: group) }
let_it_be(:project_2) { create(:project, namespace: group) }
context 'without filters' do
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) }
it 'returns milestones for projects' do
result = described_class.new(project_ids: [project_1.id, project_2.id], state: 'all').execute
......@@ -42,7 +44,21 @@ RSpec.describe MilestonesFinder do
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
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
{
project_ids: [project_1.id, project_2.id],
......@@ -51,11 +67,6 @@ RSpec.describe MilestonesFinder do
}
end
before do
milestone_1.close
milestone_3.close
end
it 'filters by id' do
params[:ids] = [milestone_1.id, milestone_2.id]
......@@ -118,12 +129,4 @@ RSpec.describe MilestonesFinder do
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
......@@ -3,10 +3,9 @@
require 'spec_helper'
RSpec.describe Milestone do
let(:user) { create(:user) }
let(:issue) { create(:issue, project: project) }
let(:milestone) { create(:milestone, project: project) }
let(:project) { create(:project, :public) }
let_it_be(:user) { create(:user) }
let_it_be(:project) { create(:project, :public) }
let_it_be(:issue) { create(:issue, project: project) }
it_behaves_like 'a timebox', :milestone do
describe "#uniqueness_of_title" do
......@@ -92,6 +91,8 @@ RSpec.describe Milestone do
end
describe '.predefined_id?' do
let_it_be(:milestone) { create(:milestone, project: project) }
it 'returns true for a predefined Milestone ID' do
expect(Milestone.predefined_id?(described_class::Upcoming.id)).to be true
end
......@@ -129,6 +130,8 @@ RSpec.describe Milestone do
end
describe "#percent_complete" do
let(:milestone) { create(:milestone, project: project) }
it "does not count open issues" do
milestone.issues << issue
expect(milestone.percent_complete).to eq(0)
......@@ -146,6 +149,8 @@ RSpec.describe Milestone do
end
describe '#expired?' do
let_it_be(:milestone) { build(:milestone, project: project) }
context "expired" do
before do
allow(milestone).to receive(:due_date).and_return(Date.today.prev_year)
......@@ -180,10 +185,14 @@ RSpec.describe Milestone do
end
describe '#can_be_closed?' do
let_it_be(:milestone) { build(:milestone, project: project) }
it { expect(milestone.can_be_closed?).to be_truthy }
end
describe '#can_be_closed?' do
let_it_be(:milestone) { build(:milestone, project: project) }
before do
milestone = create :milestone, project: project
create :closed_issue, milestone: milestone, project: project
......@@ -335,10 +344,10 @@ RSpec.describe Milestone do
it_behaves_like '#for_projects_and_groups'
describe '.upcoming_ids' do
let(:group_1) { create(:group) }
let(:group_2) { create(:group) }
let(:group_3) { create(:group) }
let(:groups) { [group_1, group_2, group_3] }
let_it_be(:group_1) { create(:group) }
let_it_be(:group_2) { create(:group) }
let_it_be(:group_3) { create(:group) }
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!(:current_milestone_group_1) { create(:milestone, group: group_1, due_date: Time.current + 1.day) }
......@@ -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(:project_1) { create(:project) }
let(:project_2) { create(:project) }
let(:project_3) { create(:project) }
let(:projects) { [project_1, project_2, project_3] }
let_it_be(:project_1) { create(:project) }
let_it_be(:project_2) { create(:project) }
let_it_be(:project_3) { create(:project) }
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!(: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