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 work packages with delay when working days change

Christophe Bliardf59dea8ce512
diff --git a/app/models/relation.rb b/app/models/relation.rb
index 2d7cb3b53e0..bc361f5e6cb 100644
--- a/app/models/relation.rb
+++ b/app/models/relation.rb
@@ -99,6 +99,9 @@ class Relation < ApplicationRecord
scope :of_work_package,
->(work_package) { where(from: work_package).or(where(to: work_package)) }
+ scope :follows_with_delay,
+ -> { follows.where("delay > 0") }
+
validates :delay, numericality: { allow_nil: true }
validates :to, uniqueness: { scope: :from }
diff --git a/app/models/work_package.rb b/app/models/work_package.rb
index 83580a1ca71..72bea97d630 100644
--- a/app/models/work_package.rb
+++ b/app/models/work_package.rb
@@ -120,7 +120,8 @@ class WorkPackage < ApplicationRecord
where(author_id: author.id)
}
- scopes :for_scheduling,
+ scopes :covering_days_of_week,
+ :for_scheduling,
:include_derived_dates,
:include_spent_time,
:left_join_self_and_descendants,
diff --git a/app/models/work_packages/scopes/covering_days_of_week.rb b/app/models/work_packages/scopes/covering_days_of_week.rb
new file mode 100644
index 00000000000..35995b71223
--- /dev/null
+++ b/app/models/work_packages/scopes/covering_days_of_week.rb
@@ -0,0 +1,96 @@
+#-- 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 WorkPackages::Scopes::CoveringDaysOfWeek
+ extend ActiveSupport::Concern
+ using CoreExtensions::SquishSql
+
+ class_methods do
+ # Fetches all work packages that cover specific days of the week.
+ #
+ # The period considered is from the work package start date to the due date.
+ #
+ # @param days_of_week number[] An array of the ISO days of the week to
+ # consider. 1 is Monday, 7 is Sunday.
+ def covering_days_of_week(days_of_week)
+ days_of_week = Array(days_of_week)
+ return none if days_of_week.empty?
+
+ where("id IN (#{query(days_of_week)})")
+ end
+
+ private
+
+ def query(days_of_week)
+ sql = <<~SQL.squish
+ -- select work packages dates with their followers dates
+ WITH work_packages_with_dates AS (
+ SELECT work_packages.id,
+ work_packages.start_date AS work_package_start_date,
+ work_packages.due_date AS work_package_due_date
+ FROM work_packages
+ WHERE work_packages.ignore_non_working_days = false
+ AND (
+ work_packages.start_date IS NOT NULL
+ OR work_packages.due_date IS NOT NULL
+ )
+ ORDER BY work_packages.id
+ ),
+ -- coalesce non-existing dates of work package to get period start/end
+ work_packages_periods AS (
+ SELECT id,
+ LEAST(work_package_start_date, work_package_due_date) AS start_date,
+ GREATEST(work_package_start_date, work_package_due_date) AS end_date
+ FROM work_packages_with_dates
+ ),
+ -- expand period into days of the week. Limit to 7 days (more would be useless).
+ work_packages_days_of_week AS (
+ SELECT id,
+ extract(
+ isodow
+ from generate_series(
+ work_packages_periods.start_date,
+ LEAST(
+ work_packages_periods.start_date + 6,
+ work_packages_periods.end_date
+ ),
+ '1 day'
+ )
+ ) AS dow
+ FROM work_packages_periods
+ )
+ -- select id of work packages covering the given days
+ SELECT DISTINCT id
+ FROM work_packages_days_of_week
+ WHERE dow IN (:days_of_week)
+ SQL
+
+ sanitize_sql([sql, { days_of_week: }])
+ end
+ end
+end
diff --git a/app/workers/work_packages/apply_working_days_change_job.rb b/app/workers/work_packages/apply_working_days_change_job.rb
index 97370b00b73..bb88ad67be9 100644
--- a/app/workers/work_packages/apply_working_days_change_job.rb
+++ b/app/workers/work_packages/apply_working_days_change_job.rb
@@ -29,50 +29,55 @@
class WorkPackages::ApplyWorkingDaysChangeJob < ApplicationJob
queue_with_priority :above_normal
- def perform(user_id:)
+ def perform(user_id:, working_days_changes:)
user = User.find(user_id)
- each_applicable_work_package do |work_package|
- WorkPackages::UpdateService
- .new(user:, model: work_package, contract_class: EmptyContract)
- .call(duration: work_package.duration)
+ each_applicable_work_package(working_days_changes.keys) do |work_package|
+ apply_change_to_work_package(user, work_package)
+ end
+ each_applicable_follows_relation do |relation|
+ apply_change_to_relation(user, relation)
end
end
private
- def each_applicable_work_package
+ def apply_change_to_work_package(user, work_package)
+ WorkPackages::UpdateService
+ .new(user:, model: work_package, contract_class: EmptyContract)
+ .call(duration: work_package.duration) # trigger a recomputation of start and due date
+ end
+
+ def apply_change_to_relation(user, relation)
+ predecessor = relation.to
+ schedule_result = WorkPackages::SetScheduleService
+ .new(user:, work_package: predecessor)
+ .call
+
+ # The SetScheduleService does not save. It has to be done by the caller.
+ schedule_result.dependent_results.each do |dependent_result|
+ work_package = dependent_result.result
+ work_package.save
+ end
+ end
+
+ def each_applicable_work_package(days_of_week)
WorkPackage
- .where(ignore_non_working_days: false)
- .where.not(start_date: nil, due_date: nil)
+ .covering_days_of_week(days_of_week)
.order(WorkPackage.arel_table[:start_date].asc.nulls_first,
WorkPackage.arel_table[:due_date].asc)
.pluck(:id)
.each do |id|
- work_package = WorkPackage.find(id)
- next unless dates_and_duration_mismatch?(work_package)
-
- yield work_package
+ yield WorkPackage.find(id)
end
end
- def dates_and_duration_mismatch?(work_package)
- # precondition: ignore_non_working_days is false
- non_working?(work_package.start_date) \
- || non_working?(work_package.due_date) \
- || wrong_duration?(work_package)
- end
-
- def non_working?(date)
- date && !days.working?(date)
- end
-
- def wrong_duration?(work_package)
- computed_duration = days.duration(work_package.start_date, work_package.due_date)
- computed_duration && work_package.duration != computed_duration
- end
-
- def days
- @days ||= WorkPackages::Shared::WorkingDays.new
+ def each_applicable_follows_relation
+ Relation
+ .follows_with_delay
+ .pluck(:id)
+ .each do |id|
+ yield Relation.find(id)
+ end
end
end
diff --git a/spec/models/work_packages/scopes/covering_days_of_week_spec.rb b/spec/models/work_packages/scopes/covering_days_of_week_spec.rb
new file mode 100644
index 00000000000..66821e2ae41
--- /dev/null
+++ b/spec/models/work_packages/scopes/covering_days_of_week_spec.rb
@@ -0,0 +1,129 @@
+#-- 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.
+#++
+
+require 'rails_helper'
+
+RSpec.describe WorkPackages::Scopes::CoveringDaysOfWeek do
+ create_shared_association_defaults_for_work_package_factory
+
+ it 'returns work packages having start date or due date being in the given days of week' do
+ schedule =
+ create_schedule(<<~CHART)
+ days | MTWTFSS |
+ covered1 | XX |
+ covered2 | XX |
+ covered3 | X |
+ covered4 | [ |
+ covered5 | ] |
+ not_covered1 | X |
+ not_covered2 | X |
+ not_covered3 | XX |
+ not_covered4 | |
+ CHART
+
+ expect(WorkPackage.covering_days_of_week(2))
+ .to contain_exactly(
+ schedule.work_package("covered1"),
+ schedule.work_package("covered2"),
+ schedule.work_package("covered3"),
+ schedule.work_package("covered4"),
+ schedule.work_package("covered5")
+ )
+ end
+
+ it 'returns work packages having days between start date and due date being in the given days of week' do
+ schedule =
+ create_schedule(<<~CHART)
+ days | MTWTFSS |
+ covered1 | XXXX |
+ covered2 | XXX |
+ not_covered1 | XX |
+ not_covered2 | X |
+ CHART
+
+ expect(WorkPackage.covering_days_of_week([2, 3]))
+ .to contain_exactly(
+ schedule.work_package("covered1"),
+ schedule.work_package("covered2")
+ )
+ end
+
+ context 'if work package ignores non working days' do
+ it 'does not returns it' do
+ create_schedule(<<~CHART)
+ days | MTWTFSS |
+ not_covered | XXXXXXX | working days include weekends
+ CHART
+
+ expect(WorkPackage.covering_days_of_week(3))
+ .to eq([])
+ end
+ end
+
+ it 'does not return work packages having follows relation covering the given days of week' do
+ create_schedule(<<~CHART)
+ days | MTWTFSS |
+ not_covered1 | X |
+ follower1 | X | follows not_covered1
+ not_covered2 | X |
+ follower2 | X | follows not_covered2
+ CHART
+
+ expect(WorkPackage.covering_days_of_week([2, 4]))
+ .to eq([])
+ end
+
+ it 'does not return work packages having follows relation with delay covering the given days of week' do
+ create_schedule(<<~CHART)
+ days | MTWTFSS |
+ not_covered1 | X |
+ follower1 | X | follows not_covered1 with delay 3
+ not_covered2 | X |
+ follower2 | X | follows not_covered2 with delay 1
+ CHART
+
+ expect(WorkPackage.covering_days_of_week([2, 4]))
+ .to eq([])
+ end
+
+ it 'accepts a single day of week or an array of days' do
+ schedule =
+ create_schedule(<<~CHART)
+ days | MTWTFSS |
+ covered | X |
+ not_covered | X |
+ CHART
+
+ expect(WorkPackage.covering_days_of_week(2))
+ .to eq([schedule.work_package("covered")])
+ expect(WorkPackage.covering_days_of_week([2]))
+ .to eq([schedule.work_package("covered")])
+ expect(WorkPackage.covering_days_of_week([2, 3]))
+ .to eq([schedule.work_package("covered")])
+ end
+end
diff --git a/spec/workers/work_packages/apply_working_days_change_job_spec.rb b/spec/workers/work_packages/apply_working_days_change_job_spec.rb
index ba051eb10b4..c2fcaec5a9a 100644
--- a/spec/workers/work_packages/apply_working_days_change_job_spec.rb
+++ b/spec/workers/work_packages/apply_working_days_change_job_spec.rb
@@ -29,35 +29,58 @@
require 'rails_helper'
RSpec.describe WorkPackages::ApplyWorkingDaysChangeJob do
+ create_shared_association_defaults_for_work_package_factory
+
subject(:job) { described_class }
shared_let(:user) { create(:user) }
let!(:week) { week_with_saturday_and_sunday_as_weekend }
+ def set_non_working_week_days_returning_changes(*days)
+ set_week_days_returning_changes(*days, working: false)
+ end
+
+ def set_working_week_days_returning_changes(*days)
+ set_week_days_returning_changes(*days, working: true)
+ end
+
+ def set_week_days_returning_changes(*days, working:)
+ previous_working_days = Setting.working_days
+ set_week_days(*days, working:)
+ current_working_days = Setting.working_days
+
+ changes = {}
+ (current_working_days - previous_working_days).each do |day|
+ changes[day] = [false, true]
+ end
+ (previous_working_days - current_working_days).each do |day|
+ changes[day] = [true, false]
+ end
+ changes
+ end
+
context 'when a work package includes a date that is now a non-working day' do
let_schedule(<<~CHART)
days | MTWTFSS |
- work_package | XXXX |
- work_package_on_start | XX |
- work_package_on_due | XXX |
- wp_start_only | [ |
- wp_due_only | ] |
+ work_package | XXXX ░░ |
+ work_package_on_start | XX ░░ |
+ work_package_on_due | XXX ░░ |
+ wp_start_only | [ ░░ |
+ wp_due_only | ] ░░ |
CHART
-
- before do
- set_non_working_week_days('wednesday')
- end
+ let(:working_days_changes) { set_non_working_week_days_returning_changes('wednesday') }
it 'moves the finish date to the corresponding number of now-excluded days to maintain duration [#31992]' do
- job.perform_now(user_id: user.id)
+ job.perform_now(user_id: user.id, working_days_changes:)
+
expect(WorkPackage.all).to match_schedule(<<~CHART)
days | MTWTFSS |
- work_package | XX.XX |
- work_package_on_start | XX |
- work_package_on_due | XX.X |
- wp_start_only | [ |
- wp_due_only | ] |
+ work_package | XX▓XX░░ |
+ work_package_on_start | ░XX░░ |
+ work_package_on_due | XX▓X ░░ |
+ wp_start_only | ░[ ░░ |
+ wp_due_only | ░] ░░ |
CHART
end
end
@@ -65,19 +88,16 @@ RSpec.describe WorkPackages::ApplyWorkingDaysChangeJob do
context 'when a work package was scheduled to start on a date that is now a non-working day' do
let_schedule(<<~CHART)
days | MTWTFSS |
- work_package | XX |
+ work_package | XX ░░ |
CHART
-
- before do
- set_non_working_week_days('wednesday')
- end
+ let(:working_days_changes) { set_non_working_week_days_returning_changes('wednesday') }
it 'moves the start date to the earliest working day in the future, ' \
'and the finish date changes by consequence [#31992]' do
- job.perform_now(user_id: user.id)
+ job.perform_now(user_id: user.id, working_days_changes:)
expect(WorkPackage.all).to match_schedule(<<~CHART)
days | MTWTFSS |
- work_package | XX |
+ work_package | ░XX░░ |
CHART
end
end
@@ -85,18 +105,15 @@ RSpec.describe WorkPackages::ApplyWorkingDaysChangeJob do
context 'when a work package includes a date that is no more a non-working day' do
let_schedule(<<~CHART)
days | fssMTWTFSS |
- work_package | X..XX |
+ work_package | X▓▓XX ░░ |
CHART
-
- before do
- set_working_week_days('saturday')
- end
+ let(:working_days_changes) { set_working_week_days_returning_changes('saturday') }
it 'moves the finish date backwards to the corresponding number of now-included days to maintain duration [#31992]' do
- job.perform_now(user_id: user.id)
+ job.perform_now(user_id: user.id, working_days_changes:)
expect(WorkPackage.all).to match_schedule(<<~CHART)
days | fssMTWTFSS |
- work_package | XX.X |
+ work_package | XX▓X ░ |
CHART
end
end
@@ -104,20 +121,17 @@ RSpec.describe WorkPackages::ApplyWorkingDaysChangeJob do
context 'when a follower has a predecessor with dates covering a day that is now a non-working day' do
let_schedule(<<~CHART)
days | MTWTFSS |
- predecessor | XX | working days work week
- follower | XXX | working days include weekends, follows predecessor
+ predecessor | XX ░░ | working days work week
+ follower | XXX░ | working days include weekends, follows predecessor
CHART
-
- before do
- set_non_working_week_days('wednesday')
- end
+ let(:working_days_changes) { set_non_working_week_days_returning_changes('wednesday') }
it 'moves the follower start date by consequence of the predecessor dates shift [#31992]' do
- job.perform_now(user_id: user.id)
+ job.perform_now(user_id: user.id, working_days_changes:)
expect(WorkPackage.all).to match_schedule(<<~CHART)
days | MTWTFSS |
- predecessor | X.X | working days work week
- follower | XXX | working days include weekends
+ predecessor | X▓X ░░ | working days work week
+ follower | ░ XXX | working days include weekends
CHART
end
end
@@ -125,66 +139,90 @@ RSpec.describe WorkPackages::ApplyWorkingDaysChangeJob do
context 'when a follower has a predecessor with delay covering a day that is now a non-working day' do
let_schedule(<<~CHART)
days | MTWTFSS |
- predecessor | XX |
- follower | X | follows predecessor with delay 1
+ predecessor | XX ░░ |
+ follower | X ░░ | follows predecessor with delay 1
CHART
+ let(:working_days_changes) { set_non_working_week_days_returning_changes('wednesday') }
- before do
- set_non_working_week_days('wednesday')
+ it 'moves the follower start date forward to keep the delay to 1 day' do
+ job.perform_now(user_id: user.id, working_days_changes:)
+ expect(WorkPackage.all).to match_schedule(<<~CHART)
+ days | MTWTFSS |
+ predecessor | XX░ ░░ |
+ follower | ░ X░░ |
+ CHART
end
+ end
+
+ context 'with work packages without dates following each other with delay' do
+ let_schedule(<<~CHART)
+ days | MTWTFSS |
+ predecessor | ░░ |
+ follower | ░░ | follows predecessor with delay 5
+ CHART
+ let(:working_days_changes) { set_non_working_week_days_returning_changes('wednesday') }
- it 'moves the follower start date to keep the delay to 1 day' do
- job.perform_now(user_id: user.id)
+ it 'does not move anything (obviously) and does not crash either' do
+ job.perform_now(user_id: user.id, working_days_changes:)
expect(WorkPackage.all).to match_schedule(<<~CHART)
days | MTWTFSS |
- predecessor | XX |
- follower | X |
+ predecessor | ░ ░░ |
+ follower | ░ ░░ |
CHART
end
end
- context 'when a follower has a predecessor with dates covering a day that is now a working day' do
- let!(:week) { set_work_week('monday', 'tuesday', 'thursday', 'friday') }
-
+ context 'when a follower has a predecessor with delay covering multiple days with different working changes' do
let_schedule(<<~CHART)
- days | MTWTFSS |
- predecessor | X.X | working days work week
- follower | XXX | working days include weekends, follows predecessor
+ days | MTWTFSS |
+ predecessor | X ░ ░░ |
+ follower | ░ X░░ | follows predecessor with delay 2
CHART
-
- before do
- set_working_week_days('wednesday')
+ let!(:week) { set_work_week('monday', 'tuesday', 'thursday', 'friday') }
+ let(:working_days_changes) do
+ set_non_working_week_days_returning_changes('tuesday')
+ .merge(set_working_week_days_returning_changes('wednesday'))
end
- it 'does not move the follower backwards' do
- job.perform_now(user_id: user.id)
+ it 'correctly handles the changes' do
+ job.perform_now(user_id: user.id, working_days_changes:)
expect(WorkPackage.all).to match_schedule(<<~CHART)
- days | MTWTFSS |
- predecessor | XX | working days work week
- follower | XXX | working days include weekends
+ days | MTWTFSS |
+ predecessor | X░ ░░ |
+ follower | ░ X░░ |
CHART
end
end
- context 'when a follower has a predecessor with a non-working day between them that is now a working day' do
- let!(:week) { set_work_week('monday', 'tuesday', 'thursday', 'friday') }
-
+ context 'when a follower has a predecessor with dates covering a day that is now a working day' do
let_schedule(<<~CHART)
days | MTWTFSS |
- predecessor | XX |
- follower | XX | follows predecessor
+ predecessor | X▓X ░░ | working days work week
+ follower | ░ XXX | working days include weekends, follows predecessor
CHART
+ let!(:week) { set_work_week('monday', 'tuesday', 'thursday', 'friday') }
+ let(:working_days_changes) { set_working_week_days_returning_changes('wednesday') }
- before do
- set_working_week_days('wednesday')
+ it 'does not move the follower backwards' do
+ job.perform_now(user_id: user.id, working_days_changes:)
end
+ end
+
+ context 'when a follower has a predecessor with a non-working day between them that is now a working day' do
+ let_schedule(<<~CHART)
+ days | MTWTFSS |
+ predecessor | XX░ ░░ |
+ follower | ░XX░░ | follows predecessor
+ CHART
+ let!(:week) { set_work_week('monday', 'tuesday', 'thursday', 'friday') }
+ let(:working_days_changes) { set_working_week_days_returning_changes('wednesday') }
it 'does not move the follower' do
- job.perform_now(user_id: user.id)
+ job.perform_now(user_id: user.id, working_days_changes:)
expect(WorkPackage.all).to match_schedule(<<~CHART)
days | MTWTFSS |
predecessor | XX |
- follower | XX |
+ follower | XX░░ |
CHART
end
end
@@ -192,18 +230,15 @@ RSpec.describe WorkPackages::ApplyWorkingDaysChangeJob do
context 'when a work package has working days include weekends, and includes a date that is now a non-working day' do
let_schedule(<<~CHART)
days | MTWTFSS |
- work_package | XXXX | working days include weekends
+ work_package | XXXX ░░ | working days include weekends
CHART
-
- before do
- set_non_working_week_days('wednesday')
- end
+ let(:working_days_changes) { set_non_working_week_days_returning_changes('wednesday') }
it 'does not move any dates' do
- job.perform_now(user_id: user.id)
+ job.perform_now(user_id: user.id, working_days_changes:)
expect(WorkPackage.all).to match_schedule(<<~CHART)
days | MTWTFSS |
- work_package | XXXX | working days include weekends
+ work_package | XXXX ░░ | working days include weekends
CHART
end
end
@@ -211,15 +246,12 @@ RSpec.describe WorkPackages::ApplyWorkingDaysChangeJob do
context 'when a work package only has a duration' do
let_schedule(<<~CHART)
days | MTWTFSS |
- work_package | | duration 3 days
+ work_package | ░░ | duration 3 days
CHART
-
- before do
- set_non_working_week_days('wednesday')
- end
+ let(:working_days_changes) { set_non_working_week_days_returning_changes('wednesday') }
it 'does not change anything' do
- job.perform_now(user_id: user.id)
+ job.perform_now(user_id: user.id, working_days_changes:)
expect(work_package.duration).to eq(3)
end
end
@@ -227,50 +259,74 @@ RSpec.describe WorkPackages::ApplyWorkingDaysChangeJob do
context 'when having multiple work packages following each other, and having days becoming non working days' do
let_schedule(<<~CHART)
days | MTWTFSS |
- wp1 | X..XX | follows wp2
- wp2 | X | follows wp3
- wp3 | XXX |
+ wp1 | X▓▓XX | follows wp2
+ wp2 | X ░░ | follows wp3
+ wp3 | XXX ░░ |
CHART
+ let(:working_days_changes) { set_non_working_week_days_returning_changes('tuesday', 'wednesday', 'friday') }
- before do
- set_non_working_week_days('tuesday', 'wednesday', 'friday')
- end
-
- it 'updates them only once' do
- expect { job.perform_now(user_id: user.id) }
+ it 'updates them only once most of the time' do
+ expect { job.perform_now(user_id: user.id, working_days_changes:) }
.to change { WorkPackage.pluck(:lock_version) }
.from([0, 0, 0])
.to([1, 1, 1])
expect(WorkPackage.all).to match_schedule(<<~CHART)
days | MTWTFSSmtwtfssmtwtfss |
- wp1 | X..X...X |
- wp2 | X |
- wp3 | X..X...X |
+ wp1 | ░░ ░░░ ░░ ░░░X▓▓X▓▓▓X |
+ wp2 | ░░ ░░░ ░░X░░░ ░░ ░░░ |
+ wp3 | X▓▓X▓▓▓X░░ ░░░ ░░ ░░░ |
CHART
end
end
- context 'when having multiple work packages following each other, and having days becoming working days' do
- let!(:week) { set_work_week('monday', 'thursday') }
-
+ context 'with multiple predecessors for same follower' do
+ # Given below schedule, as work packages are processed by order of start_date
+ # ascending, the processing order will be wp1, wp2, wp3.
+ #
+ # So when Tuesday, Wednesday and Friday become non-working days:
+ # * wp1 will move from Tuesday to Thursday and its followers will be rescheduled too
+ # * follower wp3 gets rescheduled and moves to next Monday
+ # * wp2 will move from Wednesday-Thursday to Thursday-nextMonday too and its followers will be rescheduled too
+ # * follower wp3 gets rescheduled *again* and moves to next Thursday
let_schedule(<<~CHART)
- days | MTWTFSSmtwtfssmtwtfss |
- wp1 | X..X...X | follows wp2
- wp2 | X | follows wp3
- wp3 | X..X...X |
+ days | MTWTFSS |
+ wp1 | X ░░ |
+ wp2 | XX ░░ |
+ wp3 | X░░ | follows wp1, follows wp2
CHART
+ let(:working_days_changes) { set_non_working_week_days_returning_changes('tuesday', 'wednesday', 'friday') }
- before do
- set_working_week_days('tuesday', 'wednesday', 'friday')
+ it 'can update some followers twice sometimes' do
+ expect { job.perform_now(user_id: user.id, working_days_changes:) }
+ .to change { WorkPackage.order(:subject).pluck(:lock_version) }
+ .from([0, 0, 0])
+ .to([1, 1, 2])
+ expect(WorkPackage.all).to match_schedule(<<~CHART)
+ days | MTWTFSSmtwt |
+ wp1 | ░░X░░░ ░░ |
+ wp2 | ░░X▓▓▓X░░ |
+ wp3 | ░░ ░░░ ░░X |
+ CHART
end
+ end
+
+ context 'when having multiple work packages following each other, and having days becoming working days' do
+ let_schedule(<<~CHART)
+ days | MTWTFSSmtwtfssmtwtfss |
+ wp1 | ░░ ░░░ ░░ ░░░X▓▓X▓▓▓X | follows wp2
+ wp2 | ░░ ░░░ ░░X░░░ ░░ ░░░ | follows wp3
+ wp3 | X▓▓X▓▓▓X░░ ░░░ ░░ ░░░ |
+ CHART
+ let!(:week) { set_work_week('monday', 'thursday') }
+ let(:working_days_changes) { set_working_week_days_returning_changes('tuesday', 'wednesday', 'friday') }
it 'keeps the same start dates and updates them only once' do
- job.perform_now(user_id: user.id)
+ job.perform_now(user_id: user.id, working_days_changes:)
expect(WorkPackage.all).to match_schedule(<<~CHART)
days | MTWTFSSmtwtfssmtwtfss |
- wp1 | XXX | follows wp2
- wp2 | X | follows wp3
- wp3 | XXX |
+ wp1 | ░░ ░░XXX ░░ | follows wp2
+ wp2 | ░░ X ░░ ░░ | follows wp3
+ wp3 | XXX ░░ ░░ ░░ |
CHART
expect(WorkPackage.pluck(:lock_version)).to all(be <= 1)
end
@@ -279,25 +335,23 @@ RSpec.describe WorkPackages::ApplyWorkingDaysChangeJob do
context 'when having multiple work packages following each other and first one only has a due date' do
let_schedule(<<~CHART)
days | MTWTFSS |
- wp1 | X..XX | follows wp2
- wp2 | XX | follows wp3
- wp3 | ] |
+ wp1 | X▓▓XX | follows wp2
+ wp2 | XX ░░ | follows wp3
+ wp3 | ] ░░ |
CHART
- before do
- set_non_working_week_days('tuesday', 'wednesday', 'friday')
- end
+ let(:working_days_changes) { set_non_working_week_days_returning_changes('tuesday', 'wednesday', 'friday') }
- it 'updates them only once' do
- expect { job.perform_now(user_id: user.id) }
+ it 'updates all of them correctly' do
+ expect { job.perform_now(user_id: user.id, working_days_changes:) }
.to change { WorkPackage.pluck(:lock_version) }
.from([0, 0, 0])
.to([1, 1, 1])
expect(WorkPackage.all).to match_schedule(<<~CHART)
days | MTWTFSSm t ssm t ssm |
- wp1 | X..X...X |
- wp2 | X..X |
- wp3 | ] |
+ wp1 | ░░ ░░░ ░░ ░░░X▓▓X▓▓▓X |
+ wp2 | ░░ ░░░X▓▓X░░░ ░░ ░░░ |
+ wp3 | ░░]░░░ ░░ ░░░ ░░ ░░░ |
CHART
end
end