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

Reschedule correctly if delta covers non-working days

Christophe Bliard30cf1e430765
diff --git a/app/services/work_packages/set_schedule_service.rb b/app/services/work_packages/set_schedule_service.rb
index 7da9c979625..75796ea5fb8 100644
--- a/app/services/work_packages/set_schedule_service.rb
+++ b/app/services/work_packages/set_schedule_service.rb
@@ -156,7 +156,8 @@ class WorkPackages::SetScheduleService
end
def reschedule_by_delta(scheduled, delta, min_start_date)
- required_delta = [min_start_date - (scheduled.start_date || min_start_date), [delta, 0].min].max
+ moved_delta = min_start_date - (scheduled.start_date || min_start_date)
+ required_delta = [moved_delta, [delta, 0].min].max
new_start_date = WorkPackages::Shared::Days.for(scheduled).add_days(scheduled.start_date, required_delta)
new_due_date = WorkPackages::Shared::Days.for(scheduled).add_days(scheduled.due_date, required_delta) if scheduled.due_date
@@ -166,17 +167,20 @@ class WorkPackages::SetScheduleService
def follows_delta(dependency)
if dependency.moving_predecessors.any?
- date_rescheduling_delta(dependency.moving_predecessors.first)
+ date_rescheduling_delta(dependency.moving_predecessors.first, dependency.work_package)
else
0
end
end
- def date_rescheduling_delta(predecessor)
+ def date_rescheduling_delta(predecessor, follower)
+ days = WorkPackages::Shared::Days.for(follower)
if predecessor.due_date.present?
- predecessor.due_date - (predecessor.due_date_before_last_save || predecessor.due_date_was || predecessor.due_date)
+ previous_due_date = predecessor.due_date_before_last_save || predecessor.due_date_was || predecessor.due_date
+ days.delta(previous: previous_due_date, current: predecessor.due_date)
elsif predecessor.start_date.present?
- predecessor.start_date - (predecessor.start_date_before_last_save || predecessor.start_date_was || predecessor.start_date)
+ previous_start_date = predecessor.start_date_before_last_save || predecessor.start_date_was || predecessor.start_date
+ days.delta(previous: previous_start_date, current: predecessor.start_date)
else
0
end
diff --git a/app/services/work_packages/shared/all_days.rb b/app/services/work_packages/shared/all_days.rb
index df4797cba85..662848168b2 100644
--- a/app/services/work_packages/shared/all_days.rb
+++ b/app/services/work_packages/shared/all_days.rb
@@ -44,6 +44,10 @@ module WorkPackages
date
end
+ def delta(previous:, current:)
+ current - previous
+ end
+
private
def no_duration
diff --git a/app/services/work_packages/shared/working_days.rb b/app/services/work_packages/shared/working_days.rb
index 63720d59d29..fd2f80f8494 100644
--- a/app/services/work_packages/shared/working_days.rb
+++ b/app/services/work_packages/shared/working_days.rb
@@ -56,6 +56,20 @@ module WorkPackages
date
end
+ def delta(previous:, current:)
+ delta = 0
+ direction = previous < current ? 1 : -1
+ pos = last_pos = previous
+ while pos != current
+ pos += direction
+ if working?(last_pos) && working?(pos)
+ delta += direction
+ last_pos = pos
+ end
+ end
+ delta
+ end
+
private
def no_duration
diff --git a/spec/services/work_packages/set_schedule_service_working_days_spec.rb b/spec/services/work_packages/set_schedule_service_working_days_spec.rb
index 70e78b111e4..0de354a2267 100644
--- a/spec/services/work_packages/set_schedule_service_working_days_spec.rb
+++ b/spec/services/work_packages/set_schedule_service_working_days_spec.rb
@@ -40,23 +40,23 @@ describe WorkPackages::SetScheduleService, 'working days', with_flag: { work_pac
subject { instance.call(changed_attributes) }
- context 'when moved successor covers non-working days' do
+ context 'when moving successor covers non-working days' do
let_schedule(<<~CHART, ignore_non_working_days: false)
- days | MTWTFss |
+ days | MTWTFSS |
work_package | XX |
- follower | XXX | after work_package
+ follower | XXX | follows work_package
CHART
before do
change_schedule([work_package], <<~CHART)
- days | MTWTFss |
+ days | MTWTFSS |
work_package | XXXX |
CHART
end
it 'extends to a later due date to keep the same duration' do
expect_schedule(subject.all_results, <<~CHART)
- days | MTWTFss |
+ days | MTWTFSS |
work_package | XXXX |
follower | X..XX |
CHART
@@ -64,165 +64,264 @@ describe WorkPackages::SetScheduleService, 'working days', with_flag: { work_pac
end
end
- context 'with a single successor' do
- context 'when moved forward with the follower having no due date' do
- let_schedule(<<~CHART, ignore_non_working_days: false)
- days | MTWTFss |
- work_package | X |
- follower | [ | after work_package
+ context 'when moved predecessor covers non-working days' do
+ let_schedule(<<~CHART, ignore_non_working_days: false)
+ days | MTWTFSS |
+ work_package | XX |
+ follower | XXX | follows work_package
+ CHART
+
+ before do
+ change_schedule([work_package], <<~CHART)
+ days | MTWTFSS |
+ work_package | XX..XX |
+ CHART
+ end
+
+ it 'extends to a later due date to keep the same duration' do
+ expect_schedule(subject.all_results, <<~CHART)
+ days | MTWTFSS |
+ work_package | XX..XX |
+ follower | XXX |
CHART
+ expect(follower.duration).to eq(3)
+ end
+ end
+
+ context 'with a single successor' do
+ context 'when predecessor moved forward' do
+ context 'on a day in the middle on working days with the follower having only start date' do
+ let_schedule(<<~CHART, ignore_non_working_days: false)
+ days | MTWTFSS |
+ work_package | X |
+ follower | [ | follows work_package
+ CHART
- context 'on a day in the middle on working days' do
before do
change_schedule([work_package], <<~CHART)
- days | MTWTFss |
+ days | MTWTFSS |
work_package | XXXX |
CHART
end
it 'reschedules follower to start the next day after its predecessor due date' do
expect_schedule(subject.all_results, <<~CHART)
- | MTWTFss |
+ | MTWTFSS |
work_package | XXXX |
follower | [ |
CHART
end
end
- context 'on a day just before non working days' do
+ context 'on a day just before non working days with the follower having only start date' do
+ let_schedule(<<~CHART, ignore_non_working_days: false)
+ days | MTWTFSS |
+ work_package | X |
+ follower | [ | follows work_package
+ CHART
+
before do
change_schedule([work_package], <<~CHART)
- days | MTWTFss |
+ days | MTWTFSS |
work_package | XXXXX |
CHART
end
it 'reschedules follower to start after the non working days' do
expect_schedule(subject.all_results, <<~CHART)
- | MTWTFss |
+ | MTWTFSS |
work_package | XXXXX |
follower | [ |
CHART
end
end
- end
- context 'when moving forward with the follower having only due date' do
- let_schedule(<<~CHART, ignore_non_working_days: false)
- days | MTWTFss |
- work_package | ] |
- follower | ] | after work_package
- CHART
+ context 'on a day in the middle of working days with the follower having only due date' do
+ let_schedule(<<~CHART, ignore_non_working_days: false)
+ days | MTWTFSS |
+ work_package | ] |
+ follower | ] | follows work_package
+ CHART
- context 'on a day in the middle on working days' do
before do
change_schedule([work_package], <<~CHART)
- days | MTWTFss |
+ days | MTWTFSS |
work_package | ] |
CHART
end
it 'reschedules follower to start and end right after its predecessor with a default duration of 1 day' do
expect_schedule(subject.all_results, <<~CHART)
- | MTWTFss |
+ | MTWTFSS |
work_package | ] |
follower | X |
CHART
end
end
- context 'on a day just before non working days' do
+ context 'on a day just before non-working day with the follower having only due date' do
+ let_schedule(<<~CHART, ignore_non_working_days: false)
+ days | MTWTFSS |
+ work_package | ] |
+ follower | ] | follows work_package
+ CHART
+
before do
change_schedule([work_package], <<~CHART)
- days | MTWTFss |
+ days | MTWTFSS |
work_package | ] |
CHART
end
it 'reschedules follower to start and end after the non working days with a default duration of 1 day' do
expect_schedule(subject.all_results, <<~CHART)
- | MTWTFss |
+ | MTWTFSS |
work_package | ] |
follower | X |
CHART
end
end
- end
- # context 'when moving forward with the follower having some space left' do
- # let(:follower1_start_date) { Time.zone.today + 3.days }
- # let(:follower1_due_date) { Time.zone.today + 5.days }
+ context 'with the follower having some space left' do
+ let_schedule(<<~CHART, ignore_non_working_days: false)
+ days | MTWTFSS |
+ work_package | X |
+ follower | X..XX | follows work_package
+ CHART
- # before do
- # work_package.due_date = Time.zone.today + 5.days
- # end
+ before do
+ change_schedule([work_package], <<~CHART)
+ days | MTWTFSS |
+ work_package | XXXXX |
+ CHART
+ end
- # it_behaves_like 'reschedules' do
- # let(:expected) do
- # { following_work_package1 => [Time.zone.today + 6.days, Time.zone.today + 8.days] }
- # end
- # end
- # end
+ it 'reschedules follower to start the next working day after its predecessor due date' do
+ expect_schedule(subject.all_results, <<~CHART)
+ | MTWTFSS |
+ work_package | XXXXX |
+ follower | XXX |
+ CHART
+ end
+ end
- # context 'when moving forward with the follower having enough space left to not be moved at all' do
- # let(:follower1_start_date) { Time.zone.today + 10.days }
- # let(:follower1_due_date) { Time.zone.today + 12.days }
+ context 'with the follower having enough space left to not be moved at all' do
+ let_schedule(<<~CHART, ignore_non_working_days: false)
+ days | MTWTFSS |
+ work_package | X |
+ follower | XXX | follows work_package
+ CHART
- # before do
- # work_package.due_date = Time.zone.today + 5.days
- # end
+ before do
+ change_schedule([work_package], <<~CHART)
+ days | MTWTFSS |
+ work_package | XXXXX..X |
+ CHART
+ end
- # it_behaves_like 'reschedules' do
- # let(:expected) do
- # {}
- # end
- # end
- # end
+ it 'does not move follower' do
+ expect_schedule(subject.all_results, <<~CHART)
+ | MTWTFSS |
+ work_package | XXXXX..X |
+ follower | XXX |
+ CHART
+ end
+ end
- # context 'when moving forward with the follower having some space left and a delay' do
- # let(:follower1_start_date) { Time.zone.today + 5.days }
- # let(:follower1_due_date) { Time.zone.today + 7.days }
- # let(:follower1_delay) { 3 }
+ context 'with the follower having some space left and a delay' do
+ let_schedule(<<~CHART, ignore_non_working_days: false)
+ days | MTWTFSSmtwtfss |
+ work_package | X |
+ follower | XXX | follows work_package with delay 3
+ CHART
- # before do
- # work_package.due_date = Time.zone.today + 5.days
- # end
+ before do
+ change_schedule([work_package], <<~CHART)
+ days | MTWTFSS |
+ work_package | XXXXX..X |
+ CHART
+ end
- # it_behaves_like 'reschedules' do
- # let(:expected) do
- # { following_work_package1 => [Time.zone.today + 9.days, Time.zone.today + 11.days] }
- # end
- # end
- # end
+ it 'reschedules the follower to start after the delay' do
+ expect_schedule(subject.all_results, <<~CHART)
+ | MTWTFSSmtwtfss |
+ work_package | XXXXX..X |
+ follower | X..XX |
+ CHART
+ end
+ end
+ end
- # context 'when moving forward with the follower not needing to be moved' do
- # let(:follower1_start_date) { Time.zone.today + 6.days }
- # let(:follower1_due_date) { Time.zone.today + 8.days }
+ context 'when predecessor moved backwards' do
+ context 'on a day right before some non-working days' do
+ let_schedule(<<~CHART, ignore_non_working_days: false)
+ days | MTWTFSS |
+ work_package | X |
+ follower | XX | follows work_package
+ CHART
- # before do
- # work_package.due_date = Time.zone.today + 5.days
- # end
+ before do
+ change_schedule([work_package], <<~CHART)
+ days | MTWTFSS |
+ work_package | X |
+ CHART
+ end
- # it_behaves_like 'reschedules' do
- # # Nothing should be rescheduled
- # let(:expected) do
- # {}
- # end
- # end
- # end
+ it 'reschedules the follower to start after the non-working days' do
+ expect_schedule(subject.all_results, <<~CHART)
+ | MTWTFSS |
+ work_package | X |
+ follower | XX |
+ CHART
+ end
+ end
- # context 'when moving backwards' do
- # before do
- # work_package.due_date = Time.zone.today - 5.days
- # end
+ context 'on a day in the middle of working days' do
+ let_schedule(<<~CHART, ignore_non_working_days: false)
+ days | MTWTFSS |
+ work_package | X |
+ follower | XX | follows work_package
+ CHART
- # it_behaves_like 'reschedules' do
- # let(:expected) do
- # { following_work_package1 => [Time.zone.today - 4.days, Time.zone.today - 2.days] }
- # end
- # end
- # end
+ before do
+ change_schedule([work_package], <<~CHART)
+ days | mtwtfssmtwtfssMTWTFSS |
+ work_package | X |
+ CHART
+ end
+
+ it 'reschedules the follower to move by the same delta' do
+ expect_schedule(subject.all_results, <<~CHART)
+ days | mtwtfssmtwtfssMTWTFSS |
+ work_package | X |
+ follower | X..X |
+ CHART
+ end
+ end
+
+ context 'on a day in the middle on working days with the follower having no due date' do
+ let_schedule(<<~CHART, ignore_non_working_days: false)
+ days | MTWTFSS |
+ work_package | X |
+ follower | [ | follows work_package
+ CHART
+ before do
+ change_schedule([work_package], <<~CHART)
+ days | MTWTFSS |
+ work_package | X |
+ CHART
+ end
+
+ it 'reschedules follower to start the next day after its predecessor due date' do
+ expect_schedule(subject.all_results, <<~CHART)
+ | MTWTFSS |
+ work_package | X |
+ follower | [ |
+ CHART
+ end
+ end
+ end
# context 'when moving backwards with space between' do
# let(:follower1_start_date) { Time.zone.today + 3.days }
# let(:follower1_due_date) { Time.zone.today + 5.days }
@@ -799,16 +898,16 @@ describe WorkPackages::SetScheduleService, 'working days', with_flag: { work_pac
# let_schedule(<<~CHART)
# | MTWTFSS |
# work_package | X |
- # follower1 | XXX | after work_package
- # follower2 | XXXXX | after follower1
- # follower3 | XXXX | after work_package
- # follower4 | XX | after follower2, after follower3
+ # follower1 | XXX | follows work_package
+ # follower2 | XXXXX | follows follower1
+ # follower3 | XXXX | follows work_package
+ # follower4 | XX | follows follower2, after follower3
# CHART
# context 'when moving forward' do
# before do
# change_schedule(<<~CHART)
- # | MTWTFss |
+ # | MTWTFSS |
# work_package | X |
# CHART
# end
diff --git a/spec/services/work_packages/shared/all_days_spec.rb b/spec/services/work_packages/shared/all_days_spec.rb
index c33fb8b8f6c..237d1adc97c 100644
--- a/spec/services/work_packages/shared/all_days_spec.rb
+++ b/spec/services/work_packages/shared/all_days_spec.rb
@@ -126,4 +126,23 @@ RSpec.describe WorkPackages::Shared::AllDays do
end
end
end
+
+ describe '#delta' do
+ it 'returns the number of shift from one day to another between two dates' do
+ expect(subject.delta(previous: Date.new(2022, 8, 1), current: Date.new(2022, 8, 30))).to eq(29)
+ expect(subject.delta(previous: Date.new(2022, 8, 15), current: Date.new(2022, 8, 1))).to eq(-14)
+ end
+
+ context 'with weekend days (Saturday and Sunday)', :weekend_saturday_sunday do
+ it 'returns the same delta as without them' do
+ expect(subject.delta(previous: Date.new(2022, 8, 1), current: Date.new(2022, 8, 30))).to eq(29)
+ end
+ end
+
+ context 'with non working days (Christmas 2022-12-25 and new year\'s day 2023-01-01)', :christmas_2022_new_year_2023 do
+ it 'returns the same delta as without them' do
+ expect(subject.delta(previous: Date.new(2022, 12, 1), current: Date.new(2023, 1, 1))).to eq(31)
+ end
+ end
+ end
end
diff --git a/spec/services/work_packages/shared/shared_examples_days.rb b/spec/services/work_packages/shared/shared_examples_days.rb
index 54449922843..379341b9cd5 100644
--- a/spec/services/work_packages/shared/shared_examples_days.rb
+++ b/spec/services/work_packages/shared/shared_examples_days.rb
@@ -69,3 +69,16 @@ RSpec.shared_examples 'soonest working day' do |date:, expected:|
expect(subject.soonest_working_day(date)).to eq(expected)
end
end
+
+RSpec.shared_examples 'delta' do |previous:, current:, expected:|
+ date_format = '%a %-d %b %Y'
+
+ it "delta(previous: #{previous.strftime(date_format)}, current: #{current.strftime(date_format)}) => #{expected} days" do
+ expect(subject.delta(previous:, current:)).to eq(expected)
+ end
+
+ # check inverse: delta(a, b) == -delta(b, a)
+ it "delta(previous: #{current.strftime(date_format)}, current: #{previous.strftime(date_format)}) => #{-expected} days" do
+ expect(subject.delta(previous: current, current: previous)).to eq(-expected)
+ end
+end
diff --git a/spec/services/work_packages/shared/working_days_spec.rb b/spec/services/work_packages/shared/working_days_spec.rb
index 7ccfd37e648..5e116408004 100644
--- a/spec/services/work_packages/shared/working_days_spec.rb
+++ b/spec/services/work_packages/shared/working_days_spec.rb
@@ -32,6 +32,7 @@ require_relative 'shared_examples_days'
RSpec.describe WorkPackages::Shared::WorkingDays do
subject { described_class.new }
+ friday_2022_07_29 = Date.new(2022, 7, 29)
saturday_2022_07_30 = Date.new(2022, 7, 30)
sunday_2022_07_31 = Date.new(2022, 7, 31)
monday_2022_08_01 = Date.new(2022, 8, 1)
@@ -130,11 +131,11 @@ RSpec.describe WorkPackages::Shared::WorkingDays do
context 'with weekend days (Saturday and Sunday)', :weekend_saturday_sunday do
include_examples 'add_days returns date', date: saturday_2022_07_30, count: 0, expected: saturday_2022_07_30
include_examples 'add_days returns date', date: saturday_2022_07_30, count: 1, expected: monday_2022_08_01
- include_examples 'add_days returns date', date: saturday_2022_07_30, count: -1, expected: Date.new(2022, 7, 29)
+ include_examples 'add_days returns date', date: saturday_2022_07_30, count: -1, expected: friday_2022_07_29
include_examples 'add_days returns date', date: sunday_2022_07_31, count: 0, expected: sunday_2022_07_31
include_examples 'add_days returns date', date: sunday_2022_07_31, count: 1, expected: monday_2022_08_01
- include_examples 'add_days returns date', date: sunday_2022_07_31, count: -1, expected: Date.new(2022, 7, 29)
+ include_examples 'add_days returns date', date: sunday_2022_07_31, count: -1, expected: friday_2022_07_29
include_examples 'add_days returns date', date: Date.new(2022, 6, 15), count: 100, expected: Date.new(2022, 11, 2)
include_examples 'add_days returns date', date: Date.new(2022, 6, 15), count: -100, expected: Date.new(2022, 1, 26)
@@ -158,7 +159,7 @@ RSpec.describe WorkPackages::Shared::WorkingDays do
end
context 'with weekend days (Saturday and Sunday)', :weekend_saturday_sunday do
- include_examples 'soonest working day', date: Date.new(2022, 7, 29), expected: Date.new(2022, 7, 29)
+ include_examples 'soonest working day', date: friday_2022_07_29, expected: friday_2022_07_29
include_examples 'soonest working day', date: saturday_2022_07_30, expected: monday_2022_08_01
include_examples 'soonest working day', date: sunday_2022_07_31, expected: monday_2022_08_01
include_examples 'soonest working day', date: monday_2022_08_01, expected: monday_2022_08_01
@@ -170,4 +171,24 @@ RSpec.describe WorkPackages::Shared::WorkingDays do
include_examples 'soonest working day', date: Date.new(2023, 1, 1), expected: Date.new(2023, 1, 2)
end
end
+
+ describe '#delta' do
+ it 'returns the number of shift from one working day to another between two dates' do
+ expect(subject.delta(previous: monday_2022_08_01, current: wednesday_2022_08_03)).to eq(2)
+ expect(subject.delta(previous: wednesday_2022_08_03, current: monday_2022_08_01)).to eq(-2)
+ end
+
+ context 'with weekend days (Saturday and Sunday)', :weekend_saturday_sunday do
+ include_examples 'delta', previous: friday_2022_07_29, current: saturday_2022_07_30, expected: 0
+ include_examples 'delta', previous: saturday_2022_07_30, current: monday_2022_08_01, expected: 0
+ include_examples 'delta', previous: sunday_2022_07_31, current: monday_2022_08_01, expected: 0
+ include_examples 'delta', previous: friday_2022_07_29, current: monday_2022_08_01, expected: 1
+ include_examples 'delta', previous: friday_2022_07_29, current: Date.new(2022, 8, 5), expected: 5
+ include_examples 'delta', previous: friday_2022_07_29, current: Date.new(2022, 8, 8), expected: 6
+ end
+
+ context 'with non working days (Christmas 2022-12-25 and new year\'s day 2023-01-01)', :christmas_2022_new_year_2023 do
+ include_examples 'delta', previous: Date.new(2022, 12, 27), current: Date.new(2022, 12, 20), expected: -6
+ end
+ end
end
diff --git a/spec/support/schedule_helpers.rb b/spec/support/schedule_helpers.rb
index 159dac286f4..9af5d529e17 100644
--- a/spec/support/schedule_helpers.rb
+++ b/spec/support/schedule_helpers.rb
@@ -49,37 +49,50 @@ module ScheduleHelpers
# * +_+: ignored but useful as a placeholder to highlight particular days, for
# instance to highlight the previous dates of a work package.
class Chart
- attr_accessor :first_day
+ attr_writer :first_day
def self.for(representation)
builder = ChartBuilder.new
builder.parse(representation)
end
+ def initialize
+ @monday = next_monday
+ end
+
+ def first_day
+ @first_day ||= monday
+ end
+
def work_packages
@work_packages ||= {}
end
+ def work_package_attributes(name)
+ work_packages[name.to_sym]
+ end
+
+ def delay_between(predecessor:, follower:)
+ delays_between.fetch([predecessor, follower])
+ end
+
def predecessors_by_follower(follower)
@predecessors_by_follower ||= Hash.new { |h, k| h[k] = [] }
@predecessors_by_follower[follower]
end
def add_work_package(attributes)
- key = attributes[:subject].to_sym
- work_packages[key] = attributes
+ name = attributes[:subject].to_sym
+ work_packages[name] = attributes
end
- def add_follows_relation(predecessor:, follower:)
+ def add_follows_relation(predecessor:, follower:, delay:)
predecessors_by_follower(follower) << predecessor
+ delays_between[[predecessor, follower]] = delay
end
# Returns the date for the Monday represented on the chart.
def monday
- unless defined?(@monday)
- @monday = first_day
- @monday += 1 until @monday.wday == 1
- end
@monday
end
@@ -112,6 +125,18 @@ module ScheduleHelpers
def sunday
monday + 6.days
end
+
+ private
+
+ def next_monday
+ date = Time.zone.today
+ date += 1.day while date.wday != 1
+ date
+ end
+
+ def delays_between
+ @delays_between ||= Hash.new(0)
+ end
end
# Builds a +Chart+ instance from a visual chart representation.
@@ -119,9 +144,9 @@ module ScheduleHelpers
# Example:
#
# ChartBuilder.new.parse(<<~CHART)
- # days | MTWTFss |
+ # days | MTWTFSS |
# main | XX |
- # follower | XXX | after main
+ # follower | XXX | follows main
# start_only | [ |
# due_only | ] |
# no_dates | |
@@ -147,20 +172,12 @@ module ScheduleHelpers
def parse_header(header)
_, week_days = header.split(' | ', 2)
- unless week_days.match?(/mtwtfss/i)
- raise ArgumentError, "First header line of schedule chart must contain MTWTFSS to indicate day names"
+ unless week_days.include?('MTWTFSS')
+ raise ArgumentError, "First header line of schedule chart must contain MTWTFSS to indicate day names and have an origin"
end
- nb_days_from_monday = week_days.index(/m/i)
- chart.first_day = next_monday - nb_days_from_monday.days
- end
-
- def next_monday
- date = Time.zone.today
- while date.wday != 1
- date += 1.day
- end
- date
+ nb_days_from_monday = week_days.index('M')
+ chart.first_day = chart.monday - nb_days_from_monday.days
end
def parse_line(line)
@@ -175,28 +192,29 @@ module ScheduleHelpers
end
def parse_work_package_line(line)
- name, timespan, modifiers = line.split(' | ', 3)
+ name, timespan, properties = line.split(' | ', 3)
name.strip!
attributes = { subject: name }
attributes.update(parse_timespan(timespan))
chart.add_work_package(attributes)
- modifiers.to_s.split(',').map(&:strip).each do |modifier|
- parse_modifier(name, modifier)
+ properties.to_s.split(',').map(&:strip).each do |property|
+ parse_properties(name, property)
end
end
- def parse_modifier(name, modifier)
- case modifier
- when /^after (\w+)/
+ def parse_properties(name, property)
+ case property
+ when /^follows (\w+)(?: with delay (\d+))?/
predecessor = $1.to_sym
+ delay = $2.to_i
if !chart.work_packages.has_key?(predecessor)
- raise "unable to find work package #{predecessor.inspect} in modifier #{modifier.inspect} for line #{name.inspect}"
+ raise "unable to find work package #{predecessor.inspect} in property #{property.inspect} for line #{name.inspect}"
end
- chart.add_follows_relation(predecessor: $1.to_sym, follower: name.to_sym)
+ chart.add_follows_relation(predecessor:, follower: name.to_sym, delay:)
else
- raise "unable to parse modifier #{modifier.inspect} for line #{name.inspect}"
+ raise "unable to parse property #{property.inspect} for line #{name.inspect}"
end
end
@@ -227,9 +245,9 @@ module ScheduleHelpers
# For instance:
#
# let_schedule(<<~CHART)
- # days | MTWTFss |
+ # days | MTWTFSS |
# main | XX |
- # follower | XXX | after main
+ # follower | XXX | follows main
# start_only | [ |
# due_only | ] |
# CHART
@@ -243,7 +261,7 @@ module ScheduleHelpers
# 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) }
+ # create(:follows_relation, from: follower, to: main, delay: 0) }
# end
# let!(:start_only) do
# create(:work_package, subject: 'start_only', start_date: next_monday + 1.day) }
@@ -251,16 +269,25 @@ module ScheduleHelpers
# let!(:due_only) do
# create(:work_package, subject: 'due_only', due_date: next_monday + 3.days) }
# end
- def let_schedule(chart, **extra_attributes)
- chart = Chart.for(chart)
- let(:schedule_chart) { chart }
- chart.work_packages.each do |name, attributes|
+ def let_schedule(chart_representation, **extra_attributes)
+ # 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) }
+
+ # we still need to parse the chart to get the work package names and relations
+ chart = Chart.for(chart_representation)
+ chart.work_packages.each_key do |name|
let!(name) do
- create(:work_package, **attributes.reverse_merge(extra_attributes))
+ create(:work_package, schedule_chart.work_package_attributes(name).reverse_merge(extra_attributes))
end
chart.predecessors_by_follower(name).each do |predecessor|
relation_alias = "relation_#{name}_follows_#{predecessor}"
- let!(relation_alias) { create(:follows_relation, from: send(name), to: send(predecessor)) }
+ let!(relation_alias) do
+ create(:follows_relation,
+ from: send(name),
+ to: send(predecessor),
+ delay: schedule_chart.delay_between(predecessor:, follower: name))
+ end
end
end
end
@@ -274,7 +301,7 @@ module ScheduleHelpers
#
# before do
# change_schedule([main], <<~CHART)
- # days | MTWTFss |
+ # days | MTWTFSS |
# main | XX |
# CHART
# end
@@ -297,7 +324,7 @@ module ScheduleHelpers
#
# before do
# change_schedule([main], <<~CHART)
- # days | MTWTFss |
+ # days | MTWTFSS |
# main | XX |
# CHART
# end
@@ -328,7 +355,7 @@ module ScheduleHelpers
#
# it 'is scheduled' do
# expect_schedule(work_packages, <<~CHART)
- # days | MTWTFss |
+ # days | MTWTFSS |
# main | XX |
# follower | XXX |
# CHART
diff --git a/spec/support_spec/schedule_helpers_spec.rb b/spec/support_spec/schedule_helpers_spec.rb
index 38e3d079527..9489aa56db9 100644
--- a/spec/support_spec/schedule_helpers_spec.rb
+++ b/spec/support_spec/schedule_helpers_spec.rb
@@ -33,7 +33,7 @@ describe ScheduleHelpers do
create_shared_association_defaults_for_work_package_factory
- let(:today) { Date.new(2022, 6, 16) } # Thursday 16 June 2022
+ 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) }
@@ -46,12 +46,17 @@ describe ScheduleHelpers do
let(:chart) { described_class.new }
before do
- chart.first_day = today
+ travel_to(fake_today)
end
describe '#first_day' do
- it 'returns the first day represented on the graph' do
- expect(chart.first_day).to eq(today)
+ it 'returns the first day represented on the graph, which is next monday by default' do
+ expect(chart.first_day).to eq(monday)
+ end
+
+ it 'can be set' do
+ chart.first_day = fake_today
+ expect(chart.first_day).to eq(fake_today)
end
end
@@ -72,15 +77,15 @@ describe ScheduleHelpers do
let(:next_tuesday) { tuesday + 7.days }
before do
- travel_to(today)
+ travel_to(fake_today)
end
it 'reads a chart and convert it into objects with attributes' do
chart = builder.parse(<<~CHART)
- days | MTWTFss |
+ days | MTWTFSS |
main | XX |
other | XX..XX |
- follower | XXX | after main
+ follower | XXX | follows main
start_only | [ |
due_only | ] |
no_dates | |
@@ -101,23 +106,85 @@ describe ScheduleHelpers do
end
end
+ describe 'origin day' do
+ before do
+ travel_to(fake_today)
+ end
+
+ it 'is identified by the M in MTWTFSS and corresponds to next monday' do
+ chart = builder.parse(<<~CHART)
+ days | MTWTFSS |
+ CHART
+ expect(chart.monday).to eq(monday)
+ expect(chart.monday).to eq(chart.first_day)
+ end
+
+ it 'defines the first day by the number of characters separating the M from the cell left border' do
+ chart = builder.parse(<<~CHART)
+ days | 12MTWTFSS |
+ CHART
+ expect(chart.monday).to eq(monday)
+ expect(chart.first_day).to eq(chart.monday - 2.days)
+
+ chart = builder.parse(<<~CHART)
+ days | 123456789MTWTFSS |
+ CHART
+ expect(chart.monday).to eq(monday)
+ expect(chart.first_day).to eq(chart.monday - 9.days)
+ end
+
+ it 'is not identified by mtwtfss which can be used as documentation instead' do
+ chart = builder.parse(<<~CHART)
+ days | mtwtfssMTWTFSSmtwtfss |
+ CHART
+ expect(chart.monday).to eq(monday)
+ expect(chart.monday).to eq(chart.first_day + 7.days)
+ end
+ end
+
+ describe 'properties' do
+ describe 'follows <name>' do
+ it 'adds a follows relation to the named' do
+ chart = builder.parse(<<~CHART)
+ days | MTWTFSS |
+ main | |
+ follower | | follows main
+ CHART
+ expect(chart.predecessors_by_follower(:follower)).to eq([:main])
+ expect(chart.delay_between(predecessor: :main, follower: :follower)).to eq(0)
+ end
+ end
+
+ describe 'follows <name> with delay <n>' do
+ it 'adds a follows relation to the named with a delay' do
+ chart = builder.parse(<<~CHART)
+ days | MTWTFSS |
+ main | |
+ follower | | follows main with delay 3
+ CHART
+ expect(chart.predecessors_by_follower(:follower)).to eq([:main])
+ expect(chart.delay_between(predecessor: :main, follower: :follower)).to eq(3)
+ end
+ end
+ end
+
describe 'error handling' do
it 'raises an error if the relation references a non-existing work package predecessor' do
expect do
builder.parse(<<~CHART)
- | MTWTFss |
- follower | XX | after main
+ | MTWTFSS |
+ follower | XX | follows main
CHART
- end.to raise_error(RuntimeError, /unable to find work package :main in modifier "after main" for line "follower"/)
+ end.to raise_error(RuntimeError, /unable to find work package :main in property "follows main" for line "follower"/)
end
end
end
describe 'let_schedule helper' do
let_schedule(<<~CHART)
- days | MTWTFss |
+ days | MTWTFSS |
main | XX |
- follower | XXX | after main
+ follower | XXX | follows main with delay 2
CHART
it 'creates let! call for :schedule_chart which returns the chart' do
@@ -143,13 +210,14 @@ describe ScheduleHelpers do
it 'creates let! calls for 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)
end
context 'with additional attributes' do
let_schedule(<<~CHART, done_ratio: 50, schedule_manually: true)
- days | MTWTFss |
+ days | MTWTFSS |
main | XX |
- follower | XXX | after main
+ follower | XXX | follows main
CHART
it 'applies additional attributes to all created work packages' do
@@ -160,7 +228,7 @@ describe ScheduleHelpers do
describe 'expect_schedule helper' do
let_schedule(<<~CHART)
- | MTWTFss |
+ | MTWTFSS |
main | XX |
other | XXX |
CHART
@@ -168,7 +236,7 @@ describe ScheduleHelpers do
it 'checks the work packages properties according to the given work packages and chart representation' do
expect do
expect_schedule([main, other], <<~CHART)
- | MTWTFss |
+ | MTWTFSS |
main | XX |
other | XXX |
CHART
@@ -178,7 +246,7 @@ describe ScheduleHelpers do
it 'raises an error if start_date is wrong' do
expect do
expect_schedule([main], <<~CHART)
- | MTWTFss |
+ | MTWTFSS |
main | X |
CHART
end.to raise_error(RSpec::Expectations::ExpectationNotMetError)
@@ -187,7 +255,7 @@ describe ScheduleHelpers do
it 'raises an error if due_date is wrong' do
expect do
expect_schedule([main], <<~CHART)
- | MTWTFss |
+ | MTWTFSS |
main | XXXXX |
CHART
end.to raise_error(RSpec::Expectations::ExpectationNotMetError)
@@ -196,7 +264,7 @@ describe ScheduleHelpers do
it 'raises an error if no work package exists for a given name' do
expect do
expect_schedule([main], <<~CHART)
- | MTWTFss |
+ | MTWTFSS |
unknown | XX |
CHART
end.to raise_error(ArgumentError, "unable to find WorkPackage :unknown")
@@ -205,7 +273,7 @@ describe ScheduleHelpers do
it 'checks against the given work packages rather than the ones from the let! definitions' do
expect do
expect_schedule([], <<~CHART)
- | MTWTFss |
+ | MTWTFSS |
main | XX |
CHART
end.not_to raise_error
@@ -216,19 +284,19 @@ describe ScheduleHelpers do
a_modified_instance_of_main.due_date += 2.days
expect do
expect_schedule([main], <<~CHART)
- | MTWTFss |
+ | MTWTFSS |
main | XX |
CHART
end.not_to raise_error
expect do
expect_schedule([a_modified_instance_of_main], <<~CHART)
- | MTWTFss |
+ | MTWTFSS |
main | XXXX |
CHART
end.not_to raise_error
expect do
expect_schedule([], <<~CHART)
- | MTWTFss |
+ | MTWTFSS |
main | XX |
CHART
end.not_to raise_error