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

Re-work UpdateAncestorsService specs

Aaron Contreras87fb412d3a0b
diff --git a/app/services/work_packages/update_ancestors_service.rb b/app/services/work_packages/update_ancestors_service.rb
index 80b6ac43424..8211a9d6996 100644
--- a/app/services/work_packages/update_ancestors_service.rb
+++ b/app/services/work_packages/update_ancestors_service.rb
@@ -99,8 +99,8 @@ class WorkPackages::UpdateAncestorsService
# or the derived remaining hours, depending on the % Complete mode
# currently active.
#
- %i[estimated_hours] => :derive_estimated_hours,
- %i[remaining_hours] => :derive_remaining_hours,
+ %i[estimated_hours remaining_hours] => :derive_estimated_hours,
+ %i[remaining_hours estimated_hours] => :derive_remaining_hours,
%i[estimated_hours done_ratio status status_id] => :derive_done_ratio,
%i[ignore_non_working_days] => :derive_ignore_non_working_days
}.each do |derivative_attributes, method|
diff --git a/spec/services/work_packages/update_ancestors_service_spec.rb b/spec/services/work_packages/update_ancestors_service_spec.rb
index fee7201ae30..43cf0bb7d6a 100644
--- a/spec/services/work_packages/update_ancestors_service_spec.rb
+++ b/spec/services/work_packages/update_ancestors_service_spec.rb
@@ -42,58 +42,112 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
let(:aggregate_done_ratio) { 0.0 }
let(:ignore_non_working_days) { [false, false, false] }
- describe 'done_ratio/estimated_hours/remaining_hours propagation' do
- context 'when setting estimated hours of a work package' do
- shared_let(:work_package) { create(:work_package, status: open_status) }
+ # In order to have dependent values computed, this leverages
+ # the SetAttributesService to mimick how attributes are set
+ # on work packages prior to the UpdateAncestorsService being
+ # executed.
+ def set_attributes_on(work_package, attributes)
+ WorkPackages::SetAttributesService
+ .new(user:,
+ model: work_package,
+ contract_class: WorkPackages::UpdateContract)
+ .call(attributes)
+ end
- before do
- work_package.estimated_hours = 2.0
- end
+ describe 'done_ratio/estimated_hours/remaining_hours propagation' do
+ context 'when setting the status of a work package' do
+ shared_let(:open_status) { create(:status) }
+ shared_let(:new_status_with_done_ratio) { create(:status, default_done_ratio: 100) }
+ shared_let(:new_status_without_done_ratio) { create(:status) }
- subject(:call_result) do
- described_class.new(user:, work_package:)
- .call(%i(estimated_hours))
- end
+ shared_examples 'updates % complete of ancestors' do
+ it 'considers the work package as the % complete ' \
+ 'attached to the status and sets the ' \
+ 'derived % complete value of the ancestors accordingly' do
+ expect(child.done_ratio)
+ .to eq(new_status_with_done_ratio.default_done_ratio)
- it 'sets its derived value to the same value' do
- expect { call_result }
- .to change(work_package, :derived_estimated_hours).from(nil).to(2.0)
- expect(call_result).to be_success
- expect(call_result.dependent_results).to be_empty
+ expect do
+ updated_attributes = child.changes.keys.map(&:to_sym)
+ described_class.new(user:, work_package: child)
+ .call(updated_attributes)
+ parent.reload
+ end
+ .to change(parent, :derived_done_ratio).from(0).to(33)
+ end
end
- end
-
- context 'when setting closed status to a work package' do
- shared_let(:parent) { create(:work_package) }
- shared_let(:child) { create(:work_package, parent:, status: open_status) }
- shared_examples 'updates % complete of ancestors' do
- it 'considers the work package as 100 % complete and sets the ' \
+ shared_examples 'does not update % complete of ancestors' do
+ it 'does not consider the closed status a special value and ' \
+ 'does not set % complete of the work package or set the ' \
'derived % complete value of the ancestors accordingly' do
+ expect(child.done_ratio).to eq(0)
+
expect do
updated_attributes = child.changes.keys.map(&:to_sym)
described_class.new(user:, work_package: child)
- .call(updated_attributes)
+ .call(updated_attributes)
parent.reload
end
- .to change(parent, :derived_done_ratio).from(0).to(100)
+ .not_to change(parent, :derived_done_ratio)
end
end
- context 'with the status field' do
- before do
- child.status = closed_status
- end
+ context 'when using the "status-based" % complete mode',
+ with_settings: { work_package_done_ratio: 'status' } do
+ context 'with both parent and children having estimated hours set' do
+ shared_let(:parent) do
+ create(:work_package,
+ estimated_hours: 10.0,
+ remaining_hours: 10.0,
+ derived_estimated_hours: 15.0,
+ derived_remaining_hours: 15.0,
+ status: open_status)
+ end
+ shared_let(:child) do
+ create(:work_package,
+ parent:,
+ estimated_hours: 5.0,
+ remaining_hours: 5.0,
+ status: open_status)
+ end
- include_examples 'updates % complete of ancestors'
- end
+ context 'when the new status has its default done ratio set' do
+ context 'with the status field' do
+ before do
+ set_attributes_on(child, status: new_status_with_done_ratio)
+ end
- context 'with the status_id field' do
- before do
- child.status_id = closed_status.id
- end
+ include_examples 'updates % complete of ancestors'
+ end
+
+ context 'with the status_id field' do
+ before do
+ set_attributes_on(child, status_id: new_status_with_done_ratio.id)
+ end
+
+ include_examples 'updates % complete of ancestors'
+ end
+ end
- include_examples 'updates % complete of ancestors'
+ context 'when the closed status has no default done ratio set' do
+ context 'with the status field' do
+ before do
+ set_attributes_on(child, status: new_status_without_done_ratio)
+ end
+
+ include_examples 'does not update % complete of ancestors'
+ end
+
+ context 'with the status_id field' do
+ before do
+ set_attributes_on(child, status_id: new_status_without_done_ratio.id)
+ end
+
+ include_examples 'does not update % complete of ancestors'
+ end
+ end
+ end
end
end
@@ -122,182 +176,454 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
expect(subject.dependent_results.first.result.derived_estimated_hours)
.to eq aggregate_estimated_hours
end
- end
- let(:children) do
- (statuses.size - 1).downto(0).map do |i|
- create(:work_package,
- parent:,
- subject: "child #{i}",
- status: statuses[i] == :open ? open_status : closed_status,
- estimated_hours: estimated_hours[i],
- done_ratio: done_ratios[i],
- ignore_non_working_days:)
+ it 'has the expected derived remaining_hours' do
+ expect(subject.dependent_results.first.result.derived_remaining_hours)
+ .to eq aggregate_remaining_hours
end
end
- shared_let(:parent) { create(:work_package, subject: 'parent', status: open_status) }
-
- subject do
- # In the call we only use estimated_hours (instead of also adding
- # done_ratio) in order to test that changes in estimated hours
- # trigger a recalculation of done_ratio, because estimated hours
- # act as weights in this calculation.
- described_class
- .new(user:,
- work_package: children.first)
- .call(%i(estimated_hours))
- end
-
- context 'with no estimated hours and no progress' do
- let(:statuses) { %i(open open open) }
-
- it 'is a success' do
- expect(subject)
- .to be_success
+ context 'when on field-based mode for % complete' do
+ let(:children) do
+ (statuses.size - 1).downto(0).map do |i|
+ create(:work_package,
+ parent:,
+ subject: "child #{i}",
+ estimated_hours: estimated_hours[i],
+ remaining_hours: remaining_hours[i],
+ ignore_non_working_days:)
+ end
end
- it 'does not update the parent' do
- expect(subject.dependent_results)
- .to be_empty
- end
- end
+ shared_let(:parent) { create(:work_package, subject: 'parent', status: open_status) }
+
+ context 'when estimated_hours is changed' do
+ subject do
+ # On field-based mode, changing estimated_hours
+ # entails done_ratio also changing when going
+ # through the SetAttributesService
+ described_class
+ .new(user:,
+ work_package: children.first)
+ .call(%i(estimated_hours done_ratio))
+ end
- context 'with 1 out of 3 tasks having estimated hours and 2 out of 3 tasks done' do
- let(:statuses) do
- %i(open closed closed)
- end
+ context 'with no estimated hours and no progress' do
+ let(:estimated_hours) { [nil, nil, nil] }
+ let(:remaining_hours) { [nil, nil, nil] }
+
+ it 'is a success' do
+ expect(subject)
+ .to be_success
+ end
- it_behaves_like 'attributes of parent having children' do
- let(:estimated_hours) do
- [0.0, 2.0, 0.0]
+ it 'does not update the parent' do
+ expect(subject.dependent_results)
+ .to be_empty
+ end
end
- # 66.67 rounded - previous wrong result: 133
- let(:aggregate_done_ratio) do
- 67
+ context 'with all tasks having estimated hours and some having progress done already' do
+ it_behaves_like 'attributes of parent having children' do
+ let(:estimated_hours) do
+ [10.0, 10.0, 10.0]
+ end
+ let(:remaining_hours) do
+ [0.0, 0.0, 10.0]
+ end
+
+ let(:aggregate_estimated_hours) do
+ 30.0
+ end
+ let(:aggregate_remaining_hours) do
+ 10.0
+ end
+ let(:aggregate_done_ratio) do
+ 67
+ end
+ end
end
- let(:aggregate_estimated_hours) do
- 2.0
+
+ context 'with all tasks having estimated hours and all having progress done already' do
+ it_behaves_like 'attributes of parent having children' do
+ let(:estimated_hours) do
+ [10.0, 10.0, 10.0]
+ end
+ let(:remaining_hours) do
+ [2.5, 2.5, 10.0]
+ end
+
+ let(:aggregate_estimated_hours) do
+ 30.0
+ end
+ let(:aggregate_remaining_hours) do
+ 15.0
+ end
+ let(:aggregate_done_ratio) do
+ 50
+ end
+ end
end
- end
- context 'with mixed nil and 0 values for estimated hours' do
- it_behaves_like 'attributes of parent having children' do
- let(:estimated_hours) do
- [nil, 2.0, 0.0]
+ context 'with all tasks having estimated hours and no tasks having any progress done yet' do
+ it_behaves_like 'attributes of parent having children' do
+ let(:estimated_hours) do
+ [10.0, 2.0, 3.0]
+ end
+ let(:remaining_hours) do
+ [0.0, 0.0, 0.0]
+ end
+
+ let(:aggregate_estimated_hours) do
+ 15.0
+ end
+ let(:aggregate_remaining_hours) do
+ nil # zero-values aren't accounted for
+ end
+ let(:aggregate_done_ratio) do
+ 0
+ end
end
+ end
- # 66.67 rounded - previous wrong result: 100
- let(:aggregate_done_ratio) do
- 67
+ context 'with all tasks having estimated hours and no tasks having progress set' do
+ it_behaves_like 'attributes of parent having children' do
+ let(:estimated_hours) do
+ [10.0, 2.0, 3.0]
+ end
+ let(:remaining_hours) do
+ [nil, nil, nil]
+ end
+
+ let(:aggregate_estimated_hours) do
+ 15.0
+ end
+ let(:aggregate_remaining_hours) do
+ nil
+ end
+ let(:aggregate_done_ratio) do
+ 0
+ end
end
- let(:aggregate_estimated_hours) do
- 2.0
+ end
+
+ context 'with some tasks having estimated hours and none having progress set' do
+ it_behaves_like 'attributes of parent having children' do
+ let(:estimated_hours) do
+ [10.0, nil, nil]
+ end
+ let(:remaining_hours) do
+ [nil, nil, nil]
+ end
+
+ let(:aggregate_estimated_hours) do
+ 10.0
+ end
+ let(:aggregate_remaining_hours) do
+ nil
+ end
+ let(:aggregate_done_ratio) do
+ 0
+ end
end
end
- end
- context 'with parent having estimated hours' do
- before do
- parent.update(estimated_hours: 5.0)
+ context 'with some tasks having estimated hours and those that do also having progress done' do
+ it_behaves_like 'attributes of parent having children' do
+ let(:estimated_hours) do
+ [10.0, nil, nil]
+ end
+ let(:remaining_hours) do
+ [2.5, nil, nil]
+ end
+
+ let(:aggregate_estimated_hours) do
+ 10.0
+ end
+ let(:aggregate_remaining_hours) do
+ 2.5
+ end
+ let(:aggregate_done_ratio) do
+ 75
+ end
+ end
end
- it_behaves_like 'attributes of parent having children' do
- let(:estimated_hours) do
- [nil, 2.0, 0.0]
+ context 'with the parent having estimated hours and progress' do
+ shared_let(:parent) do
+ create(:work_package,
+ subject: 'parent',
+ estimated_hours: 10.0,
+ remaining_hours: 5.0)
end
- # 66.67 rounded - previous wrong result: 100
- let(:aggregate_done_ratio) do
- 67
+ context 'and some tasks having estimated hours and some progress' do
+ it_behaves_like 'attributes of parent having children' do
+ let(:estimated_hours) do
+ [10.0, nil, nil]
+ end
+ let(:remaining_hours) do
+ [2.5, nil, nil]
+ end
+
+ # Parent's estimated and remaining hours are taken into account
+ let(:aggregate_estimated_hours) do
+ 20.0
+ end
+ let(:aggregate_remaining_hours) do
+ 7.5
+ end
+ let(:aggregate_done_ratio) do
+ 63
+ end
+ end
end
- let(:aggregate_estimated_hours) do
- 7.0
+
+ context 'and no tasks having estimated hours or progress' do
+ it_behaves_like 'attributes of parent having children' do
+ let(:estimated_hours) do
+ [nil, nil, nil]
+ end
+ let(:remaining_hours) do
+ [nil, nil, nil]
+ end
+
+ # Parent's estimated hours and remaining hours become the aggregated values
+ let(:aggregate_estimated_hours) do
+ 10.0
+ end
+ let(:aggregate_remaining_hours) do
+ 5.0
+ end
+ let(:aggregate_done_ratio) do
+ 50
+ end
+ end
end
end
end
- end
-
- context 'with some values same for done ratio' do
- it_behaves_like 'attributes of parent having children' do
- let(:done_ratios) { [20, 20, 50] }
- let(:estimated_hours) { [nil, nil, nil] }
- let(:aggregate_done_ratio) { 30 }
- let(:aggregate_estimated_hours) { nil }
- end
- end
+ context 'when remaining_hours is changed' do
+ subject do
+ # On field-based mode, changing remaining_hours
+ # entails done_ratio also changing when going
+ # through the SetAttributesService
+ described_class
+ .new(user:,
+ work_package: children.first)
+ .call(%i(remaining_hours done_ratio))
+ end
- context 'with no estimated hours and 1.5 of the tasks done' do
- it_behaves_like 'attributes of parent having children' do
- let(:done_ratios) { [0, 50, 100] }
+ context 'with no estimated hours and no progress' do
+ let(:estimated_hours) { [nil, nil, nil] }
+ let(:remaining_hours) { [nil, nil, nil] }
+ # let(:statuses) { %i(open open open) }
- let(:aggregate_done_ratio) { 50 }
- let(:aggregate_estimated_hours) { nil }
- end
- end
+ it 'is a success' do
+ expect(subject)
+ .to be_success
+ end
- context 'with estimated hours being 1, 2 and 5' do
- let(:estimated_hours) { [1, 2, 5] }
+ it 'does not update the parent' do
+ expect(subject.dependent_results)
+ .to be_empty
+ end
+ end
- context 'with the last 2 tasks at 100% progress' do
- it_behaves_like 'attributes of parent having children' do
- let(:done_ratios) { [0, 100, 100] }
+ context 'with all tasks having estimated hours and some having progress done already' do
+ it_behaves_like 'attributes of parent having children' do
+ let(:estimated_hours) do
+ [10.0, 10.0, 10.0]
+ end
+ let(:remaining_hours) do
+ [0.0, 0.0, 10.0]
+ end
+
+ let(:aggregate_estimated_hours) do
+ 30.0
+ end
+ let(:aggregate_remaining_hours) do
+ 10.0
+ end
+ let(:aggregate_done_ratio) do
+ 67
+ end
+ end
+ end
- # (2 + 5 = 7) / 8 estimated hours done
- let(:aggregate_done_ratio) { 88 } # 87.5 rounded
- let(:aggregate_estimated_hours) { estimated_hours.sum }
+ context 'with all tasks having estimated hours and all having progress done already' do
+ it_behaves_like 'attributes of parent having children' do
+ let(:estimated_hours) do
+ [10.0, 10.0, 10.0]
+ end
+ let(:remaining_hours) do
+ [2.5, 2.5, 10.0]
+ end
+
+ let(:aggregate_estimated_hours) do
+ 30.0
+ end
+ let(:aggregate_remaining_hours) do
+ 15.0
+ end
+ let(:aggregate_done_ratio) do
+ 50
+ end
+ end
end
- end
- context 'with the last 2 tasks closed (therefore at 100%)' do
- it_behaves_like 'attributes of parent having children' do
- let(:statuses) { %i(open closed closed) }
+ context 'with all tasks having estimated hours and no tasks having any progress done yet' do
+ it_behaves_like 'attributes of parent having children' do
+ let(:estimated_hours) do
+ [10.0, 2.0, 3.0]
+ end
+ let(:remaining_hours) do
+ [0.0, 0.0, 0.0]
+ end
+
+ let(:aggregate_estimated_hours) do
+ 15.0
+ end
+ let(:aggregate_remaining_hours) do
+ nil # zero-values aren't accounted for
+ end
+ let(:aggregate_done_ratio) do
+ 0
+ end
+ end
+ end
- # (2 + 5 = 7) / 8 estimated hours done
- let(:aggregate_done_ratio) { 88 } # 87.5 rounded
- let(:aggregate_estimated_hours) { estimated_hours.sum }
+ context 'with all tasks having estimated hours and no tasks having progress set' do
+ it_behaves_like 'attributes of parent having children' do
+ let(:estimated_hours) do
+ [10.0, 2.0, 3.0]
+ end
+ let(:remaining_hours) do
+ [nil, nil, nil]
+ end
+
+ let(:aggregate_estimated_hours) do
+ 15.0
+ end
+ let(:aggregate_remaining_hours) do
+ nil
+ end
+ let(:aggregate_done_ratio) do
+ 0
+ end
+ end
end
- end
- context 'with mixed done ratios, statuses' do
- it_behaves_like 'attributes of parent having children' do
- let(:done_ratios) { [50, 75, 42] }
- let(:statuses) { %i(open open closed) }
+ context 'with some tasks having estimated hours and none having progress set' do
+ it_behaves_like 'attributes of parent having children' do
+ let(:estimated_hours) do
+ [10.0, nil, nil]
+ end
+ let(:remaining_hours) do
+ [nil, nil, nil]
+ end
+
+ let(:aggregate_estimated_hours) do
+ 10.0
+ end
+ let(:aggregate_remaining_hours) do
+ nil
+ end
+ let(:aggregate_done_ratio) do
+ 0
+ end
+ end
+ end
- # 50% 75% 100% (42 ignored)
- # (0.5 * 1 + 0.75 * 2 + 1 * 5 [since closed] = 7)
- # (0.5 + 1.5 + 5 = 7) / 8 estimated hours done
- let(:aggregate_done_ratio) { 88 } # 87.5 rounded
- let(:aggregate_estimated_hours) { estimated_hours.sum }
+ context 'with some tasks having estimated hours and those that do also having progress done' do
+ it_behaves_like 'attributes of parent having children' do
+ let(:estimated_hours) do
+ [10.0, nil, nil]
+ end
+ let(:remaining_hours) do
+ [2.5, nil, nil]
+ end
+
+ let(:aggregate_estimated_hours) do
+ 10.0
+ end
+ let(:aggregate_remaining_hours) do
+ 2.5
+ end
+ let(:aggregate_done_ratio) do
+ 75
+ end
+ end
end
- end
- end
- context 'with everything playing together' do
- it_behaves_like 'attributes of parent having children' do
- let(:statuses) { %i(open open closed open) }
- let(:done_ratios) { [0, 0, 0, 50] }
- let(:estimated_hours) { [0.0, 3.0, nil, 7.0] }
+ context 'with the parent having estimated hours and progress' do
+ shared_let(:parent) do
+ create(:work_package,
+ subject: 'parent',
+ estimated_hours: 10.0,
+ remaining_hours: 5.0)
+ end
+
+ context 'and some tasks having estimated hours and some progress' do
+ it_behaves_like 'attributes of parent having children' do
+ let(:estimated_hours) do
+ [10.0, nil, nil]
+ end
+ let(:remaining_hours) do
+ [2.5, nil, nil]
+ end
+
+ # Parent's estimated and remaining hours are taken into account
+ let(:aggregate_estimated_hours) do
+ 20.0
+ end
+ let(:aggregate_remaining_hours) do
+ 7.5
+ end
+ let(:aggregate_done_ratio) do
+ 63
+ end
+ end
+ end
- # (0 * 5 + 0 * 3 + 1 * 5 + 0.5 * 7 = 8.5) / 20 est. hours done
- let(:aggregate_done_ratio) { 43 } # 42.5 rounded
- let(:aggregate_estimated_hours) { 10.0 }
+ context 'and no tasks having estimated hours or progress' do
+ it_behaves_like 'attributes of parent having children' do
+ let(:estimated_hours) do
+ [nil, nil, nil]
+ end
+ let(:remaining_hours) do
+ [nil, nil, nil]
+ end
+
+ # Parent's estimated hours and remaining hours become the aggregated values
+ let(:aggregate_estimated_hours) do
+ 10.0
+ end
+ let(:aggregate_remaining_hours) do
+ 5.0
+ end
+ let(:aggregate_done_ratio) do
+ 50
+ end
+ end
+ end
+ end
end
end
end
context 'for the previous ancestors' do
- shared_let(:sibling_status) { open_status }
- shared_let(:sibling_done_ratio) { 50 }
shared_let(:sibling_estimated_hours) { 7.0 }
shared_let(:sibling_remaining_hours) { 3.5 }
shared_let(:parent_estimated_hours) { 3.0 }
+ shared_let(:grandparent_estimated_hours) { 3.0 }
+ shared_let(:grandparent_remaining_hours) { 1.5 }
shared_let(:grandparent) do
create(:work_package,
- subject: 'grandparent')
+ subject: 'grandparent',
+ estimated_hours: grandparent_estimated_hours,
+ remaining_hours: grandparent_remaining_hours)
end
shared_let(:parent) do
create(:work_package,
@@ -309,8 +635,6 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
create(:work_package,
subject: 'sibling',
parent:,
- status: sibling_status,
- done_ratio: sibling_done_ratio,
estimated_hours: sibling_estimated_hours,
remaining_hours: sibling_remaining_hours)
end
@@ -346,7 +670,7 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
subject
parent.reload
end
- .to change(parent, :derived_done_ratio).to(sibling_done_ratio)
+ .to change(parent, :derived_done_ratio).to(65) # 6.5h derived_work_done / 10.0h derived_estimated_hours
.and change(parent, :derived_estimated_hours).to(parent_estimated_hours + sibling_estimated_hours)
.and change(parent, :derived_remaining_hours).to(sibling_remaining_hours)
end
@@ -356,15 +680,16 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
subject
grandparent.reload
end
- .to change(grandparent, :derived_done_ratio).to(sibling_done_ratio)
- .and change(grandparent, :derived_estimated_hours).to(parent_estimated_hours + sibling_estimated_hours)
- .and change(grandparent, :derived_remaining_hours).to(sibling_remaining_hours)
+ .to change(grandparent, :derived_done_ratio).to(62) # 8.0h derived_work_done / 13.0h derived_estimated_hours
+ .and change(grandparent, :derived_estimated_hours).to(grandparent_estimated_hours +
+ parent_estimated_hours +
+ sibling_estimated_hours)
+ .and change(grandparent, :derived_remaining_hours).to(sibling_remaining_hours +
+ grandparent_remaining_hours)
end
end
context 'for new ancestors' do
- shared_let(:status) { open_status }
- shared_let(:done_ratio) { 50 }
shared_let(:estimated_hours) { 7.0 }
shared_let(:remaining_hours) { 3.5 }
shared_let(:parent_estimated_hours) { 3.0 }
@@ -384,8 +709,6 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
shared_let(:work_package) do
create(:work_package,
subject: 'subject - gains a new parent and grandparent',
- status:,
- done_ratio:,
estimated_hours:,
remaining_hours:)
end
@@ -406,7 +729,7 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
subject
parent.reload
end
- .to change(parent, :derived_done_ratio).to(done_ratio)
+ .to change(parent, :derived_done_ratio).to(50) # 5.0h derived_work_done / 10.0h derived_estimated_hours
.and change(parent, :derived_estimated_hours).to(parent_estimated_hours + estimated_hours)
.and change(parent, :derived_remaining_hours).to(parent_remaining_hours + remaining_hours)
end
@@ -417,7 +740,7 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
subject
grandparent.reload
end
- .to change(grandparent, :derived_done_ratio).to(done_ratio)
+ .to change(grandparent, :derived_done_ratio).to(50) # 5.0h derived_work_done / 10.0h derived_estimated_hours
.and change(grandparent, :derived_estimated_hours).to(parent_estimated_hours + estimated_hours)
.and change(grandparent, :derived_remaining_hours).to(parent_remaining_hours + remaining_hours)
end
@@ -453,18 +776,15 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
end
context 'with old and new parent having a common ancestor' do
- shared_let(:status) { open_status }
- shared_let(:done_ratio) { 50 }
shared_let(:estimated_hours) { 7.0 }
shared_let(:remaining_hours) { 3.5 }
- shared_let(:new_done_ratio) { 80 }
shared_let(:new_estimated_hours) { 10.0 }
shared_let(:new_remaining_hours) { 2 }
shared_let(:grandparent) do
create(:work_package,
subject: 'common grandparent',
- derived_done_ratio: done_ratio / 2, # two children having [done_ratio, 0]
+ derived_done_ratio: 50, # two children having [done_ratio, 0]
derived_estimated_hours: estimated_hours,
derived_remaining_hours: remaining_hours)
end
@@ -472,7 +792,7 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
create(:work_package,
subject: 'old parent',
parent: grandparent,
- derived_done_ratio: done_ratio,
+ derived_done_ratio: 50,
derived_estimated_hours: estimated_hours,
derived_remaining_hours: remaining_hours)
end
@@ -485,8 +805,6 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
create(:work_package,
subject: 'subject - parent changes from old parent to new parent, same grandparent',
parent: old_parent,
- status:,
- done_ratio:,
estimated_hours:,
remaining_hours:)
end
@@ -502,9 +820,9 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
# this test dependent on the implementation details of the calculations,
# done_ratio, derived_estimated_hours, and derived_remaining_hours are
# forced to change at the same time as the parent.
- work_package.done_ratio = new_done_ratio
- work_package.estimated_hours = new_estimated_hours
- work_package.remaining_hours = new_remaining_hours
+ set_attributes_on(work_package,
+ estimated_hours: new_estimated_hours,
+ remaining_hours: new_remaining_hours)
work_package.save!
described_class
@@ -539,25 +857,120 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
subject
new_parent.reload
end
- .to change(new_parent, :derived_done_ratio).to(new_done_ratio)
+ .to change(new_parent, :derived_done_ratio).to(80)
.and change(new_parent, :derived_estimated_hours).to(new_estimated_hours)
.and change(new_parent, :derived_remaining_hours).to(new_remaining_hours)
end
- it 'updates the derived_done_ratio, derived_estimated_hours, and derived_remaining_hours of the grandparent' do
+ it 'updates the derived_done_ratio, erived_estimated_hours, and derived_remaining_hours of the grandparent' do
expect do
subject
grandparent.reload
end
- .to change(grandparent, :derived_done_ratio).to(new_done_ratio / 2) # two children having [new_done_ratio, 0] now
+ .to change(grandparent, :derived_done_ratio).to(80)
.and change(grandparent, :derived_estimated_hours).to(new_estimated_hours)
.and change(grandparent, :derived_remaining_hours).to(new_remaining_hours)
end
end
end
+ describe 'estimated_hours propagation' do
+ shared_let(:parent) { create(:work_package, subject: 'parent') }
+
+ context 'when setting estimated hours of a work package' do
+ before do
+ parent.estimated_hours = 2.0
+ end
+
+ subject(:call_result) do
+ described_class.new(user:, work_package: parent)
+ .call(%i(estimated_hours))
+ end
+
+ it 'sets its derived value to the same value' do
+ expect { call_result }
+ .to change(parent, :derived_estimated_hours).from(nil).to(2.0)
+ expect(call_result).to be_success
+ expect(call_result.dependent_results).to be_empty
+ end
+ end
+
+ context 'for the new ancestors chain' do
+ context 'with parent having no work' do
+ let_work_packages(<<~TABLE)
+ hierarchy | work |
+ parent | |
+ child1 | 0h |
+ child2 | |
+ child3 | 2.5h |
+ TABLE
+
+ subject(:call_result) do
+ described_class.new(user:, work_package: child1)
+ .call(%i(estimated_hours))
+ end
+
+ it 'sets parent derived remaining work to the sum of children remaining work' do
+ expect(call_result).to be_success
+ updated_work_packages = call_result.dependent_results.map(&:result)
+ expect_work_packages(updated_work_packages, <<~TABLE)
+ subject | derived work |
+ parent | 2.5h |
+ TABLE
+ end
+ end
+
+ context 'with parent having some remaining work' do
+ let_work_packages(<<~TABLE)
+ hierarchy | work |
+ parent | 5.25h |
+ child1 | 0h |
+ child2 | |
+ child3 | 2.5h |
+ TABLE
+
+ subject(:call_result) do
+ described_class.new(user:, work_package: child1)
+ .call(%i(estimated_hours))
+ end
+
+ it 'sets parent derived work to the sum of itself and children remaining work' do
+ expect(call_result).to be_success
+ updated_work_packages = call_result.dependent_results.map(&:result)
+ expect_work_packages(updated_work_packages, <<~TABLE)
+ subject | derived work |
+ parent | 7.75h |
+ TABLE
+ end
+ end
+
+ context 'with parent and children having no work' do
+ let_work_packages(<<~TABLE)
+ hierarchy | work |
+ parent | |
+ child1 | |
+ child2 | |
+ TABLE
+
+ subject(:call_result) do
+ described_class.new(user:, work_package: child1)
+ .call(%i(estimated_hours))
+ end
+
+ it 'does not update the parent derived work' do
+ expect(call_result).to be_success
+ expect(call_result.dependent_results).to be_empty
+ expect_work_packages([parent.reload], <<~TABLE)
+ subject | derived work |
+ parent | |
+ TABLE
+ end
+ end
+ end
+ end
+
describe 'remaining_hours propagation' do
- shared_let(:parent) { create(:work_package, subject: 'parent', status: open_status) }
+ shared_let(:parent) { create(:work_package, subject: 'parent') }
context 'when setting remaining hours of a work package' do
before do