Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Introduce create_schedule rspec helper

Christophe Bliardf6a1f2c1036b
diff --git a/spec/support/schedule_helpers/chart.rb b/spec/support/schedule_helpers/chart.rb
index e85b38fb274..61a325defb0 100644
--- a/spec/support/schedule_helpers/chart.rb
+++ b/spec/support/schedule_helpers/chart.rb
@@ -64,7 +64,7 @@ module ScheduleHelpers
end
def initialize
- self.monday = next_monday
+ self.monday = Date.current.next_occurring(:monday)
self.id_column_size = FIRST_CELL_TEXT.length
end
@@ -230,12 +230,6 @@ module ScheduleHelpers
end
end
- def next_monday
- date = Time.zone.today
- date += 1.day while date.wday != 1
- date
- end
-
def predecessors_by_followers
@predecessors_by_followers ||= Hash.new { |h, k| h[k] = [] }
end
diff --git a/spec/support/schedule_helpers/chart_builder.rb b/spec/support/schedule_helpers/chart_builder.rb
index 570ba8089ac..5f830893052 100644
--- a/spec/support/schedule_helpers/chart_builder.rb
+++ b/spec/support/schedule_helpers/chart_builder.rb
@@ -119,7 +119,19 @@ module ScheduleHelpers
when /^working days include weekends$/
chart.set_ignore_non_working_days(name, true)
else
- raise "unable to parse property #{property.inspect} for line #{name.inspect}"
+ spell_checker = DidYouMean::SpellChecker.new(
+ dictionary: [
+ "follows :wp",
+ "follows :wp with delay :int",
+ "child of :wp",
+ "duration :int",
+ "working days work week",
+ "working days include weekends"
+ ]
+ )
+ suggestions = spell_checker.correct(property).map(&:inspect).join(' ')
+ did_you_mean = " Did you mean #{suggestions} instead?" if suggestions.present?
+ raise "unable to parse property #{property.inspect} for line #{name.inspect}.#{did_you_mean}"
end
end
diff --git a/spec/support/schedule_helpers/example_methods.rb b/spec/support/schedule_helpers/example_methods.rb
index 0205620dcfe..77f856d1ebf 100644
--- a/spec/support/schedule_helpers/example_methods.rb
+++ b/spec/support/schedule_helpers/example_methods.rb
@@ -28,6 +28,31 @@
module ScheduleHelpers
module ExampleMethods
+ # Create work packages and relations from a visual chart representation.
+ #
+ # For instance:
+ #
+ # create_schedule(<<~CHART)
+ # days | MTWTFSS |
+ # main | XX |
+ # follower | XXX | follows main
+ # start_only | [ |
+ # due_only | ] |
+ # CHART
+ #
+ # is equivalent to:
+ #
+ # create(:work_package, subject: 'main', start_date: next_monday, due_date: next_monday + 1.day)
+ # create(:work_package, subject: 'follower', start_date: next_monday + 2.days, due_date: next_monday + 4.days) }
+ # create(:work_package, subject: 'start_only', start_date: next_monday + 1.day) }
+ # create(:work_package, subject: 'due_only', due_date: next_monday + 3.days) }
+ # create(:follows_relation, from: follower, to: main, delay: 0) }
+ #
+ def create_schedule(chart_representation)
+ chart = Chart.for(chart_representation)
+ ScheduleBuilder.from_chart(chart, self)
+ end
+
# Change the given work packages according to the given chart representation.
# Work packages are changed without being saved.
#
diff --git a/spec/support/schedule_helpers/let_schedule.rb b/spec/support/schedule_helpers/let_schedule.rb
index 453e5ee0c04..2867368f413 100644
--- a/spec/support/schedule_helpers/let_schedule.rb
+++ b/spec/support/schedule_helpers/let_schedule.rb
@@ -30,6 +30,9 @@ module ScheduleHelpers
module LetSchedule
# Declare work packages and relations from a visual chart representation.
#
+ # It uses +create_schedule+ internally and is useful to have direct access
+ # to the created work packages.
+ #
# For instance:
#
# let_schedule(<<~CHART)
@@ -42,65 +45,29 @@ module ScheduleHelpers
#
# is equivalent to:
#
- # let!(:schedule_chart) do
- # chart = <...parse_chart(CHART)...>
- # main
- # follower
- # start_only
- # due_only
- # relation_follower_follows_main
- # chart
+ # let!(:schedule) do
+ # create_schedule(chart)
# end
# let(:main) do
- # create(:work_package, subject: 'main', start_date: next_monday, due_date: next_monday + 1.day)
+ # schedule.work_package(:main)
# end
# let(:follower) do
- # create(:work_package, subject: 'follower', start_date: next_monday + 2.days, due_date: next_monday + 4.days) }
- # end
- # let(:relation_follower_follows_main) do
- # create(:follows_relation, from: follower, to: main, delay: 0) }
+ # schedule.work_package(:follower)
# end
# let(:start_only) do
- # create(:work_package, subject: 'start_only', start_date: next_monday + 1.day) }
+ # schedule.work_package(:start_only)
# end
# let(:due_only) do
- # create(:work_package, subject: 'due_only', due_date: next_monday + 3.days) }
+ # schedule.work_package(:due_only)
# end
- def let_schedule(chart_representation, **extra_attributes)
+ def let_schedule(chart_representation)
# To be able to use `travel_to` in a before hook, the dates in the chart
# must be lazy evaluated in a let statement.
- let(:schedule_chart) { Chart.for(chart_representation) }
- let!(:__evaluate_work_packages_from_schedule_chart) do
- schedule_chart.work_package_names.each do |name|
- # force evaluation of work package
- send(name)
- schedule_chart.predecessors_by_follower(name).each do |predecessor|
- # force evaluation of relation
- send("relation_#{name}_follows_#{predecessor}")
- end
- end
- end
+ let!(:schedule) { create_schedule(chart_representation) }
- # we still need to parse the chart to get the work package names and relations
chart = Chart.for(chart_representation)
chart.work_package_names.each do |name|
- let(name) do
- attributes = schedule_chart
- .work_package_attributes(name)
- .excluding(:name)
- .reverse_merge(extra_attributes)
- .merge(parent: schedule_chart.parent(name) ? send(schedule_chart.parent(name)) : nil)
- create(:work_package, attributes)
- end
- chart.predecessors_by_follower(name).each do |predecessor|
- relation_alias = "relation_#{name}_follows_#{predecessor}"
- let(relation_alias) do
- create(:follows_relation,
- from: send(name),
- to: send(predecessor),
- delay: schedule_chart.delay_between(predecessor:, follower: name))
- end
- end
+ let(name) { schedule.work_package(name) }
end
end
end
diff --git a/spec/support/schedule_helpers/schedule.rb b/spec/support/schedule_helpers/schedule.rb
new file mode 100644
index 00000000000..9bcf7566040
--- /dev/null
+++ b/spec/support/schedule_helpers/schedule.rb
@@ -0,0 +1,59 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+module ScheduleHelpers
+ class Schedule
+ def initialize(work_packages, follows_relations)
+ @work_packages = work_packages
+ @follows_relations = follows_relations
+ end
+
+ def work_package(name)
+ name = normalize_name(name)
+ @work_packages[name]
+ end
+
+ def follows_relation(from:, to:)
+ from = normalize_name(from)
+ to = normalize_name(to)
+ @follows_relations[from:, to:]
+ end
+
+ private
+
+ def normalize_name(name)
+ symbolic_name = name.to_sym
+ return symbolic_name if @work_packages.has_key?(symbolic_name)
+
+ spell_checker = DidYouMean::SpellChecker.new(dictionary: @work_packages.keys.map(&:to_s))
+ suggestions = spell_checker.correct(name).map(&:inspect).join(' ')
+ did_you_mean = " Did you mean #{suggestions} instead?" if suggestions.present?
+ raise "No work package with name #{name.inspect} in schedule.#{did_you_mean}"
+ end
+ end
+end
diff --git a/spec/support/schedule_helpers/schedule_builder.rb b/spec/support/schedule_helpers/schedule_builder.rb
new file mode 100644
index 00000000000..e2f1fa444bf
--- /dev/null
+++ b/spec/support/schedule_helpers/schedule_builder.rb
@@ -0,0 +1,75 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2022 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+module ScheduleHelpers
+ class ScheduleBuilder
+ def self.from_chart(chart, rspec_example)
+ creator = new(chart, rspec_example)
+ chart.work_package_names.each do |name|
+ creator.create_work_package(name)
+ creator.create_follows_relations(name)
+ end
+ Schedule.new(creator.work_packages, creator.follows_relations)
+ end
+
+ attr_reader :chart, :work_packages, :follows_relations
+
+ def initialize(chart, rspec_example)
+ @chart = chart
+ @rspec_example = rspec_example
+ @work_packages = {}
+ @follows_relations = {}
+ end
+
+ def create_work_package(name)
+ work_packages[name] ||= begin
+ attributes = chart
+ .work_package_attributes(name)
+ .excluding(:name)
+ .merge(parent: parent_of(name))
+ @rspec_example.create(:work_package, attributes)
+ end
+ end
+
+ def create_follows_relations(follower)
+ chart.predecessors_by_follower(follower).each do |predecessor|
+ follows_relations[from: follower, to: predecessor] =
+ @rspec_example.create(:follows_relation,
+ from: create_work_package(follower),
+ to: create_work_package(predecessor),
+ delay: chart.delay_between(predecessor:, follower:))
+ end
+ end
+
+ def parent_of(name)
+ if chart.parent(name)
+ create_work_package(chart.parent(name))
+ end
+ end
+ end
+end
diff --git a/spec/support_spec/schedule_helpers/example_methods_spec.rb b/spec/support_spec/schedule_helpers/example_methods_spec.rb
index 4ca696d7fd7..6673cee3197 100644
--- a/spec/support_spec/schedule_helpers/example_methods_spec.rb
+++ b/spec/support_spec/schedule_helpers/example_methods_spec.rb
@@ -29,18 +29,120 @@
require 'spec_helper'
describe ScheduleHelpers::ExampleMethods do
- include ActiveSupport::Testing::TimeHelpers
-
create_shared_association_defaults_for_work_package_factory
- let(:fake_today) { Date.new(2022, 6, 16) } # Thursday 16 June 2022
- let(:monday) { Date.new(2022, 6, 20) } # Monday 20 June
- let(:tuesday) { Date.new(2022, 6, 21) }
- let(:wednesday) { Date.new(2022, 6, 22) }
- let(:thursday) { Date.new(2022, 6, 23) }
- let(:friday) { Date.new(2022, 6, 24) }
- let(:saturday) { Date.new(2022, 6, 25) }
- let(:sunday) { Date.new(2022, 6, 26) }
+ describe 'create_schedule' do
+ let(:monday) { Date.current.next_occurring(:monday) }
+ let(:tuesday) { monday + 1.day }
+
+ # rubocop:disable RSpec/ExampleLength
+ it 'creates work packages from the given chart' do
+ schedule = create_schedule(<<~CHART)
+ days | MTWTFSS |
+ main | XX |
+ start_only | [ |
+ due_only | ] |
+ no_dates | |
+ CHART
+
+ expect(WorkPackage.count).to eq(4)
+ expect(schedule.work_package("main")).to have_attributes(
+ subject: "main",
+ start_date: monday,
+ due_date: tuesday,
+ duration: 2
+ )
+ expect(schedule.work_package("start_only")).to have_attributes(
+ subject: "start_only",
+ start_date: monday,
+ due_date: nil,
+ duration: nil
+ )
+ expect(schedule.work_package("due_only")).to have_attributes(
+ subject: "due_only",
+ start_date: nil,
+ due_date: tuesday,
+ duration: nil
+ )
+ expect(schedule.work_package("no_dates")).to have_attributes(
+ subject: "no_dates",
+ start_date: nil,
+ due_date: nil,
+ duration: nil
+ )
+ end
+ # rubocop:enable RSpec/ExampleLength
+
+ it 'creates parent/child relations from the given chart' do
+ schedule = create_schedule(<<~CHART)
+ days | MTWTFSS |
+ main | |
+ child | | child of main
+ CHART
+ expect(schedule.work_package("main")).to have_attributes(
+ children: [schedule.work_package("child")]
+ )
+ expect(schedule.work_package("child")).to have_attributes(
+ parent: schedule.work_package("main")
+ )
+ end
+
+ it 'creates follows relations from the given chart' do
+ schedule = create_schedule(<<~CHART)
+ days | MTWTFSS |
+ predecessor | XX |
+ follower | X | follows predecessor with delay 2
+ CHART
+ expect(Relation.count).to eq(1)
+ expect(schedule.follows_relation(from: "follower", to: "predecessor")).to be_an_instance_of(Relation)
+ expect(schedule.follows_relation(from: "follower", to: "predecessor")).to have_attributes(
+ relation_type: "follows",
+ delay: 2,
+ from: schedule.work_package("follower"),
+ to: schedule.work_package("predecessor")
+ )
+ end
+ end
+
+ describe 'change_schedule' do
+ include ActiveSupport::Testing::TimeHelpers
+
+ let(:fake_today) { Date.new(2022, 6, 16) } # Thursday 16 June 2022
+ let(:monday) { Date.new(2022, 6, 20) } # Monday 20 June
+ let(:tuesday) { Date.new(2022, 6, 21) }
+ let(:thursday) { Date.new(2022, 6, 23) }
+ let(:friday) { Date.new(2022, 6, 24) }
+
+ before do
+ travel_to(fake_today)
+ end
+
+ it 'applies dates changes to a group of work packages from a visual chart representation' do
+ main = build_stubbed(:work_package, subject: 'main')
+ second = build_stubbed(:work_package, subject: 'second')
+ change_schedule([main, second], <<~CHART)
+ days | MTWTFSS |
+ main | XX |
+ second | XX |
+ CHART
+ expect(main.start_date).to eq(monday)
+ expect(main.due_date).to eq(tuesday)
+ expect(second.start_date).to eq(thursday)
+ expect(second.due_date).to eq(friday)
+ end
+
+ it 'does not save changes' do
+ main = create(:work_package, subject: 'main')
+ expect(main.persisted?).to be(true)
+ expect(main.has_changes_to_save?).to be(false)
+ change_schedule([main], <<~CHART)
+ days | MTWTFSS |
+ main | XX |
+ CHART
+ expect(main.has_changes_to_save?).to be(true)
+ expect(main.changes).to eq('start_date' => [nil, monday], 'due_date' => [nil, tuesday])
+ end
+ end
describe 'expect_schedule' do
let_schedule(<<~CHART)
@@ -118,36 +220,4 @@ describe ScheduleHelpers::ExampleMethods do
end.not_to raise_error
end
end
-
- describe 'change_schedule' do
- before do
- travel_to(fake_today)
- end
-
- it 'applies dates changes to a group of work packages from a visual chart representation' do
- main = build_stubbed(:work_package, subject: 'main')
- second = build_stubbed(:work_package, subject: 'second')
- change_schedule([main, second], <<~CHART)
- days | MTWTFSS |
- main | XX |
- second | XX |
- CHART
- expect(main.start_date).to eq(monday)
- expect(main.due_date).to eq(tuesday)
- expect(second.start_date).to eq(thursday)
- expect(second.due_date).to eq(friday)
- end
-
- it 'does not save changes' do
- main = create(:work_package, subject: 'main')
- expect(main.persisted?).to be(true)
- expect(main.has_changes_to_save?).to be(false)
- change_schedule([main], <<~CHART)
- days | MTWTFSS |
- main | XX |
- CHART
- expect(main.has_changes_to_save?).to be(true)
- expect(main.changes).to eq('start_date' => [nil, monday], 'due_date' => [nil, tuesday])
- end
- end
end
diff --git a/spec/support_spec/schedule_helpers/let_schedule_spec.rb b/spec/support_spec/schedule_helpers/let_schedule_spec.rb
index 97fdcb5842d..07d387f4ad3 100644
--- a/spec/support_spec/schedule_helpers/let_schedule_spec.rb
+++ b/spec/support_spec/schedule_helpers/let_schedule_spec.rb
@@ -29,19 +29,8 @@
require 'spec_helper'
describe ScheduleHelpers::LetSchedule do
- include ActiveSupport::Testing::TimeHelpers
-
create_shared_association_defaults_for_work_package_factory
- let(:fake_today) { Date.new(2022, 6, 16) } # Thursday 16 June 2022
- let(:monday) { Date.new(2022, 6, 20) } # Monday 20 June
- let(:tuesday) { Date.new(2022, 6, 21) }
- let(:wednesday) { Date.new(2022, 6, 22) }
- let(:thursday) { Date.new(2022, 6, 23) }
- let(:friday) { Date.new(2022, 6, 24) }
- let(:saturday) { Date.new(2022, 6, 25) }
- let(:sunday) { Date.new(2022, 6, 26) }
-
describe 'let_schedule' do
let_schedule(<<~CHART)
days | MTWTFSS |
@@ -50,23 +39,18 @@ describe ScheduleHelpers::LetSchedule do
child | | child of main
CHART
- it 'creates let! call for :schedule_chart which returns the chart' do
- next_monday = (Time.zone.today..(Time.zone.today + 7.days)).find { |d| d.wday == 1 }
- expect(schedule_chart.first_day).to eq(next_monday)
- end
-
- it 'creates let! calls for each work package' do
+ it 'creates let calls for each work package' do
expect([main, follower, child]).to all(be_an_instance_of(WorkPackage))
expect([main, follower, child]).to all(be_persisted)
expect(main).to have_attributes(
subject: 'main',
- start_date: schedule_chart.monday,
- due_date: schedule_chart.monday + 1.day
+ start_date: schedule.monday,
+ due_date: schedule.tuesday
)
expect(follower).to have_attributes(
subject: 'follower',
- start_date: schedule_chart.monday + 2.days,
- due_date: schedule_chart.monday + 4.days
+ start_date: schedule.wednesday,
+ due_date: schedule.friday
)
expect(child).to have_attributes(
subject: 'child',
@@ -75,26 +59,13 @@ describe ScheduleHelpers::LetSchedule do
)
end
- it 'creates let! calls for follows relations between work packages' do
+ it 'creates follows relations between work packages' do
expect(follower.follows_relations.count).to eq(1)
- expect(relation_follower_follows_main).to be_an_instance_of(Relation)
- expect(relation_follower_follows_main.delay).to eq(2)
+ expect(follower.follows_relations.first.to).to eq(main)
end
it 'creates parent / child relations' do
expect(child.parent).to eq(main)
end
-
- context 'with additional attributes' do
- let_schedule(<<~CHART, done_ratio: 50, schedule_manually: true)
- days | MTWTFSS |
- main | XX |
- follower | XXX | follows main
- CHART
-
- it 'applies additional attributes to all created work packages' do
- expect([main, follower]).to all(have_attributes(done_ratio: 50, schedule_manually: true))
- end
- end
end
end