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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
refactor: Make update ancestors spec more maintainable
Christophe Bliard36faf2d87fa5
diff --git a/spec/services/work_packages/update_ancestors_service_spec.rb b/spec/services/work_packages/update_ancestors_service_spec.rbindex b549fca80e5..1361de2f830 100644--- a/spec/services/work_packages/update_ancestors_service_spec.rb+++ b/spec/services/work_packages/update_ancestors_service_spec.rb@@ -36,14 +36,8 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model doshared_let(:closed_status) { create(:closed_status, name: "Closed", default_done_ratio: 100) }shared_let(:user) { create(:user) }- let(:estimated_hours) { [nil, nil, nil] }- let(:done_ratios) { [0, 0, 0] }- let(:statuses) { %i(open open open) }- let(:aggregate_done_ratio) { 0.0 }- let(:ignore_non_working_days) { [false, false, false] }-# In order to have dependent values computed, this leverages- # the SetAttributesService to mimick how attributes are set+ # the SetAttributesService to mimic how attributes are set# on work packages prior to the UpdateAncestorsService being# executed.def set_attributes_on(work_package, attributes)@@ -54,82 +48,67 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do.call(attributes)end- describe "done_ratio/estimated_hours/remaining_hours propagation" do- context "when setting the status of a work package" do- context 'when using the "status-based" % complete mode',- with_settings: { work_package_done_ratio: "status" } do- def call_update_ancestors_service(work_package)- changed_attributes = work_package.changes.keys.map(&:to_sym)- described_class.new(user:, work_package:)- .call(changed_attributes)- end+ def call_update_ancestors_service(work_package)+ changed_attributes = work_package.changes.keys.map(&:to_sym)+ described_class.new(user:, work_package:)+ .call(changed_attributes)+ end- context "with both parent and children having estimated hours set" do- shared_let(:parent) do- create(:work_package,- subject: "parent",- 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,- subject: "child",- parent:,- estimated_hours: 5.0,- remaining_hours: 5.0,- status: open_status)- end+ describe "work, remaining work, and % complete propagation" do+ context 'when using the "status-based" progress calculation mode',+ with_settings: { work_package_done_ratio: "status" } do+ context "with both parent and children having work set" do+ shared_let_work_packages(<<~TABLE)+ hierarchy | status | work | ∑ work | remaining work | ∑ remaining work | % complete | ∑ % complete+ parent | Open | 10h | 15h | 10h | 15h | 0% | 0%+ child | Open | 5h | | 5h | | 0% |+ TABLE- context "when changing child status to a status with a default done ratio" do- %i[status status_id].each do |field|- context "with the #{field} field" do- it "recomputes child remaining work and updates ancestors total % complete accordingly" do- value =- case field- when :status then closed_status- when :status_id then closed_status.id- end- set_attributes_on(child, field => value)- call_update_ancestors_service(child)-- expect_work_packages([parent, child], <<~TABLE)- | subject | work | total work | remaining work | total remaining work | % complete | total % complete |- | parent | 10h | 15h | 10h | 10h | 0% | 33% |- | child | 5h | | 0h | | 100% | |- TABLE- end+ context "when changing child status to a status with a default % complete ratio" do+ %i[status status_id].each do |field|+ context "with the #{field} field" do+ it "recomputes child remaining work and updates ancestors total % complete accordingly" do+ value =+ case field+ when :status then closed_status+ when :status_id then closed_status.id+ end+ set_attributes_on(child, field => value)+ call_update_ancestors_service(child)++ expect_work_packages([parent, child], <<~TABLE)+ | subject | status | work | ∑ work | remaining work | ∑ remaining work | % complete | ∑ % complete |+ | parent | Open | 10h | 15h | 10h | 10h | 0% | 33% |+ | child | Closed | 5h | | 0h | | 100% | |+ TABLEendendendend+ end- context "with parent having nothing set, and 2 children having values set (bug #54179)" do- let_work_packages(<<~TABLE)- hierarchy | status | work | ∑ work | remaining work | ∑ remaining work | % complete | ∑ % complete- parent | Open | | 15h | | 10h | 0% | 33%- child1 | Open | 10h | | 10h | | 0% |- child2 | Closed | 5h | | 0h | | 100% |- TABLE+ context "with parent having nothing set, and 2 children having values set (bug #54179)" do+ shared_let_work_packages(<<~TABLE)+ hierarchy | status | work | ∑ work | remaining work | ∑ remaining work | % complete | ∑ % complete+ parent | Open | | 15h | | 10h | 0% | 33%+ child1 | Open | 10h | | 10h | | 0% |+ child2 | Closed | 5h | | 0h | | 100% |+ TABLE- context "when changing children to all have 100% complete" do- before do- set_attributes_on(child1, status: closed_status)- call_update_ancestors_service(child1)- end+ context "when changing children to all have 100% complete" do+ before do+ set_attributes_on(child1, status: closed_status)+ call_update_ancestors_service(child1)+ end- it "sets parent total % complete to 100% and its total remaining work to 0h, " \- "and computes totals for the updated children too" do- table_work_packages.map(&:reload)- expect_work_packages(table_work_packages, <<~TABLE)- hierarchy | status | work | ∑ work | remaining work | ∑ remaining work | % complete | ∑ % complete- parent | Open | | 15h | | 0h | 0% | 100%- child1 | Closed | 10h | | 0h | | 100% |- child2 | Closed | 5h | | 0h | | 100% |- TABLE- end+ it "sets parent total % complete to 100% and its total remaining work to 0h" do+ table_work_packages.map(&:reload)+ expect_work_packages(table_work_packages, <<~TABLE)+ hierarchy | status | work | ∑ work | remaining work | ∑ remaining work | % complete | ∑ % complete+ parent | Open | | 15h | | 0h | 0% | 100%+ child1 | Closed | 10h | | 0h | | 100% |+ child2 | Closed | 5h | | 0h | | 100% |+ TABLEendendend@@ -137,459 +116,228 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model docontext "for the new ancestor chain" doshared_examples "attributes of parent having children" do- before do- children- end-it "is a success" doexpect(subject).to be_successend- it "updated one work package - the parent" do+ it "updates one work package - the parent" doexpect(subject.dependent_results.map(&:result)).to contain_exactly(parent)end- it "has the expected derived done ratio" do+ it "has the expected total % complete" doexpect(subject.dependent_results.first.result.derived_done_ratio)- .to eq aggregate_done_ratio+ .to eq expected_total_p_completeend- it "has the expected derived estimated_hours" do+ it "has the expected total work" doexpect(subject.dependent_results.first.result.derived_estimated_hours)- .to eq aggregate_estimated_hours+ .to eq expected_total_workend- it "has the expected derived remaining_hours" do+ it "has the expected total remaining work" doexpect(subject.dependent_results.first.result.derived_remaining_hours)- .to eq aggregate_remaining_hours+ .to eq expected_total_remaining_workendend- 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+ shared_context "when work is changed" do+ subject do+ # On work-based mode, changing estimated_hours+ # entails done_ratio also changing when going+ # through the SetAttributesService+ described_class+ .new(user:,+ work_package: child1)+ .call(%i(estimated_hours done_ratio))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 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 "does not update the parent" do- expect(subject.dependent_results)- .to be_empty- end- end+ shared_context "when remaining work is changed" do+ subject do+ # On work-based mode, changing remaining work+ # entails % complete also changing when going+ # through the SetAttributesService+ described_class+ .new(user:,+ work_package: child1)+ .call(%i(remaining_hours done_ratio))+ end+ end- 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+ context "without any work or % complete being set" do+ shared_let_work_packages(<<~TABLE)+ hierarchy | work | remaining work | % complete+ parent | | |+ child1 | | |+ child2 | | |+ child3 | | |+ TABLE- let(:aggregate_estimated_hours) do- 30.0- end- let(:aggregate_remaining_hours) do- 10.0- end- let(:aggregate_done_ratio) do- 67- end- end+ for_each_context "when work is changed",+ "when remaining work is changed" do+ it "is a success" do+ expect(subject)+ .to be_successend- 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+ it "does not update the parent" do+ expect(subject.dependent_results)+ .to be_emptyend+ end+ end- context "with all tasks having estimated hours and no tasks having any remaining hours" 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+ context "with all children tasks having work and remaining work set" do+ shared_let_work_packages(<<~TABLE)+ hierarchy | work | remaining work | % complete+ parent | | |+ child1 | 10h | 0h | 100%+ child2 | 10h | 0h | 100%+ child3 | 10h | 10h | 0%+ TABLE- let(:aggregate_estimated_hours) do- 15.0- end- let(:aggregate_remaining_hours) do- 0.0- end- let(:aggregate_done_ratio) do- 100- end+ for_each_context "when work is changed",+ "when remaining work is changed" do+ it_behaves_like "attributes of parent having children" do+ let(:expected_total_work) do+ 30.0end- end-- 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- nil- end+ let(:expected_total_remaining_work) do+ 10.0end- 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- nil- end+ let(:expected_total_p_complete) do+ 67endend+ end+ end- 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+ context "with all children tasks having work and remaining work set (second example)" do+ shared_let_work_packages(<<~TABLE)+ hierarchy | work | remaining work | % complete+ parent | | |+ child1 | 10h | 2.5h | 75%+ child2 | 10h | 2.5h | 75%+ child3 | 10h | 10h | 0%+ TABLE- 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)+ for_each_context "when work is changed",+ "when remaining work is changed" do+ it_behaves_like "attributes of parent having children" do+ let(:expected_total_work) do+ 30.0end-- 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+ let(:expected_total_remaining_work) do+ 15.0end-- 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+ let(:expected_total_p_complete) do+ 50endendend+ 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 no progress" do- let(:estimated_hours) { [nil, nil, nil] }- let(:remaining_hours) { [nil, nil, nil] }- # let(:statuses) { %i(open open open) }+ context "with all children tasks having work set to positive value, and having remaining work set to 0h" do+ shared_let_work_packages(<<~TABLE)+ hierarchy | work | remaining work | % complete+ parent | | |+ child1 | 10h | 0h | 100%+ child2 | 2h | 0h | 100%+ child3 | 3h | 0h | 100%+ TABLE- it "is a success" do- expect(subject)- .to be_success+ for_each_context "when work is changed",+ "when remaining work is changed" do+ it_behaves_like "attributes of parent having children" do+ let(:expected_total_work) do+ 15.0end-- it "does not update the parent" do- expect(subject.dependent_results)- .to be_empty+ let(:expected_total_remaining_work) do+ 0.0end- end-- 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+ let(:expected_total_p_complete) do+ 100endend+ end+ end- 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+ context "with some children tasks having work and remaining work set" do+ shared_let_work_packages(<<~TABLE)+ hierarchy | work | remaining work | % complete+ parent | | |+ child1 | 10h | 2.5h | 75%+ child2 | | |+ child3 | | |+ TABLE- let(:aggregate_estimated_hours) do- 30.0- end- let(:aggregate_remaining_hours) do- 15.0- end- let(:aggregate_done_ratio) do- 50- end+ for_each_context "when work is changed",+ "when remaining work is changed" do+ it_behaves_like "attributes of parent having children" do+ let(:expected_total_work) do+ 10.0end- end-- context "with all tasks having estimated hours and no tasks having remaining hours" 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- 0.0- end- let(:aggregate_done_ratio) do- 100- end+ let(:expected_total_remaining_work) do+ 2.5end- end-- 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- nil- end+ let(:expected_total_p_complete) do+ 75endend+ end+ 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+ context "with the parent having work and % complete set " \+ "and some children tasks having work and % complete set" do+ shared_let_work_packages(<<~TABLE)+ hierarchy | work | remaining work | % complete+ parent | 10h | 5h | 50%+ child1 | 10h | 2.5h | 75%+ child2 | | |+ child3 | | |+ TABLE- let(:aggregate_estimated_hours) do- 10.0- end- let(:aggregate_remaining_hours) do- nil- end- let(:aggregate_done_ratio) do- nil- end+ for_each_context "when work is changed",+ "when remaining work is changed" do+ it_behaves_like "attributes of parent having children" do+ # Parent's work and remaining work are taken into account+ let(:expected_total_work) do+ 20.0end- end-- 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+ let(:expected_total_remaining_work) do+ 7.5+ end+ let(:expected_total_p_complete) do+ 63endend+ end+ end- 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 "with the parent having work and % complete set " \+ "and no children tasks having work or % complete set" do+ shared_let_work_packages(<<~TABLE)+ hierarchy | work | remaining work | % complete+ parent | 10h | 5h | 50%+ child1 | | |+ child2 | | |+ child3 | | |+ TABLE- 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+ for_each_context "when work is changed",+ "when remaining work is changed" do+ it_behaves_like "attributes of parent having children" do+ # Parent's work and remaining work become the total values+ let(:expected_total_work) do+ 10.0end-- 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+ let(:expected_total_remaining_work) do+ 5.0+ end+ let(:expected_total_p_complete) do+ 50endendend@@ -597,37 +345,13 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model doendcontext "for the previous ancestors" do- 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",- estimated_hours: grandparent_estimated_hours,- remaining_hours: grandparent_remaining_hours)- end- shared_let(:parent) do- create(:work_package,- subject: "parent",- estimated_hours: parent_estimated_hours,- parent: grandparent)- end- shared_let(:sibling) do- create(:work_package,- subject: "sibling",- parent:,- estimated_hours: sibling_estimated_hours,- remaining_hours: sibling_remaining_hours)- end-- shared_let(:work_package) do- create(:work_package,- subject: "subject - loses its parent",- parent:)- end+ shared_let_work_packages(<<~TABLE)+ hierarchy | work | remaining work | % complete+ grandparent | 3h | 1.5h | 50%+ parent | 3h | 0h | 100%+ work package | | |+ sibling | 7h | 3.5h | 50%+ TABLEsubject dowork_package.parent = nil@@ -644,93 +368,25 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do.to be_successend+ it "updates the totals of the ancestors" do+ subject+ expect_work_packages([grandparent, parent, sibling, work_package], <<~TABLE)+ hierarchy | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % complete+ grandparent | 3h | 1.5h | 50% | 13h | 5h | 62%+ parent | 3h | 0h | 100% | 10h | 3.5h | 65%+ sibling | 7h | 3.5h | 50% | | |+ work package | | | | | |+ TABLE+ end+it "returns the former ancestors in the dependent results" doexpect(subject.dependent_results.map(&:result)).to contain_exactly(parent, grandparent)end-- it "updates the derived_done_ratio, derived_estimated_hours, and derived_remaining_hours of the former parent" do- expect do- subject- parent.reload- end- .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-- it "updates the derived_done_ratio, derived_estimated_hours, and derived_remaining_hours of the former grandparent" do- expect do- subject- grandparent.reload- end- .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)- endendcontext "for new ancestors" do- shared_let(:estimated_hours) { 7.0 }- shared_let(:remaining_hours) { 3.5 }- shared_let(:parent_estimated_hours) { 3.0 }- shared_let(:parent_remaining_hours) { 1.5 }-- shared_let(:grandparent) do- create(:work_package,- subject: "grandparent")- end- shared_let(:parent) do- create(:work_package,- subject: "parent",- estimated_hours: parent_estimated_hours,- remaining_hours: parent_remaining_hours,- parent: grandparent)- end- shared_let(:work_package) do- create(:work_package,- subject: "subject - gains a new parent and grandparent",- estimated_hours:,- remaining_hours:)- end-- shared_examples_for "updates the attributes within the new hierarchy" do- it "is successful" do- expect(subject)- .to be_success- end-- it "returns the new ancestors in the dependent results" do- expect(subject.dependent_results.map(&:result))- .to contain_exactly(parent, grandparent)- end-- it "updates the derived_done_ratio, derived_estimated_hours, and derived_remaining_hours of the new parent" do- expect do- subject- parent.reload- end- .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-- it "updates the derived_done_ratio, derived_estimated_hours, and derived_remaining_hours " \- "of the new grandparent" do- expect do- subject- grandparent.reload- end- .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- end-- context "if setting the parent" do+ shared_context "if setting the parent" dosubject dowork_package.parent = parentwork_package.save!@@ -740,11 +396,9 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model dowork_package:).call(%i(parent))end-- it_behaves_like "updates the attributes within the new hierarchy"end- context "if setting the parent_id" do+ shared_context "if setting the parent_id" dosubject dowork_package.parent_id = parent.idwork_package.save!@@ -754,59 +408,61 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model dowork_package:).call(%i(parent_id))end+ end++ shared_let_work_packages(<<~TABLE)+ hierarchy | work | remaining work | % complete+ grandparent | | |+ parent | 3h | 1.5h | 50%+ work package | 7h | 3.5h | 50%+ TABLE++ for_each_context "if setting the parent",+ "if setting the parent_id" do+ it "is successful" do+ expect(subject)+ .to be_success+ end++ it "returns the new ancestors in the dependent results" do+ expect(subject.dependent_results.map(&:result))+ .to contain_exactly(parent, grandparent)+ end- it_behaves_like "updates the attributes within the new hierarchy"+ it "updates the totals of the ancestors" do+ subject+ expect_work_packages([grandparent, parent, work_package], <<~TABLE)+ hierarchy | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % complete+ grandparent | | | | 10h | 5h | 50%+ parent | 3h | 1.5h | 50% | 10h | 5h | 50%+ work package | 7h | 3.5h | 50% | | |+ TABLE+ endendendcontext "with old and new parent having a common ancestor" do- shared_let(:estimated_hours) { 7.0 }- shared_let(:remaining_hours) { 3.5 }- 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: 50, # two children having [done_ratio, 0]- derived_estimated_hours: estimated_hours,- derived_remaining_hours: remaining_hours)- end- shared_let(:old_parent) do- create(:work_package,- subject: "old parent",- parent: grandparent,- derived_done_ratio: 50,- derived_estimated_hours: estimated_hours,- derived_remaining_hours: remaining_hours)- end- shared_let(:new_parent) do- create(:work_package,- subject: "new parent",- parent: grandparent)- end- shared_let(:work_package) do- create(:work_package,- subject: "subject - parent changes from old parent to new parent, same grandparent",- parent: old_parent,- estimated_hours:,- remaining_hours:)- end+ # work_package's parent will change from old parent to new parent, same grandparent+ shared_let_work_packages(<<~TABLE)+ hierarchy | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % complete+ grandparent | | | | 7h | 3.5h | 50%+ old parent | | | | 7h | 3.5h | 50%+ work package | 7h | 3.5h | 50% | | |+ new parent | | | | | |+ TABLEsubject do- work_package.parent = new_parent# In this test case, done_ratio, derived_estimated_hours, and- # derived_remaining_hours would not inherently change on grandparent.- # However, if work_package has siblings then changing its parent could- # cause done_ratio, derived_estimated_hours, and/or- # derived_remaining_hours on grandparent to inherently change. To verify- # that grandparent can be properly updated in that case without making- # 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.+ # derived_remaining_hours would not inherently change on grandparent+ # if work package keeps the same progress values.+ #+ # To verify that grandparent can be properly updated in this scenario,+ # work and remaining work are also changed on the work package to force+ # grandparent totals to be updated.set_attributes_on(work_package,- estimated_hours: new_estimated_hours,- remaining_hours: new_remaining_hours)+ parent: new_parent,+ estimated_hours: 10,+ remaining_hours: 2)work_package.save!described_class@@ -820,49 +476,29 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do.to be_successend+ it "updates the totals of the new parent and the former parent" do+ subject+ expect_work_packages([grandparent, old_parent, new_parent, work_package], <<~TABLE)+ hierarchy | work | remaining work | % complete | ∑ work | ∑ remaining work | ∑ % complete+ grandparent | | | | 10h | 2h | 80%+ old parent | | | | | |+ new parent | | | | 10h | 2h | 80%+ work package | 10h | 2h | 80% | | |+ TABLE+ end+it "returns both the former and new ancestors in the dependent results without duplicates" doexpect(subject.dependent_results.map(&:result)).to contain_exactly(new_parent, grandparent, old_parent)end-- it "updates the derived_done_ratio, derived_estimated_hours, and derived_remaining_hours " \- "of the former parent to nil" do- expect do- subject- old_parent.reload- end- .to change(old_parent, :derived_done_ratio).to(nil)- .and change(old_parent, :derived_estimated_hours).to(nil)- .and change(old_parent, :derived_remaining_hours).to(nil)- end-- it "updates the derived_done_ratio, derived_estimated_hours, and derived_remaining_hours of the new parent" do- expect do- subject- new_parent.reload- end- .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- expect do- subject- grandparent.reload- end- .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)- endendenddescribe "work propagation" do- shared_let(:parent) { create(:work_package, subject: "parent") }- shared_let(:child) { create(:work_package, subject: "child", parent:) }-context "when setting work of a work package having children without any work value" do+ shared_let(:parent) { create(:work_package, subject: "parent") }+ shared_let(:child) { create(:work_package, subject: "child", parent:) }+before doparent.estimated_hours = 2.0end@@ -1050,7 +686,7 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model doendend- describe "done_ratio propagation" do+ describe "% complete propagation" doshared_let(:parent) { create(:work_package, subject: "parent") }context "given child with work, when remaining work being set on parent" dodiff --git a/spec/support/for_each_context.rb b/spec/support/for_each_context.rbnew file mode 100644index 00000000000..78fdee1f77d--- /dev/null+++ b/spec/support/for_each_context.rb@@ -0,0 +1,73 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 2012-2024 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 RSpecOpExt+ module ForEachContext+ # Runs the same example group multiple times: once for each given named+ # context.+ #+ # For each named context, the context is applied and the tests are run. It+ # allows to have multiple shared contexts and only one example group,+ # instead of having shared examples and multiple contexts including the+ # shared examples. It generally reads better.+ #+ # @example+ #+ # RSpec.describe "something" do+ # shared_context "early in the morning" do+ # let(:time) { "06:30" }+ # end+ #+ # shared_context "late in the evening" do+ # let(:time) { "23:00" }+ # end+ #+ # for_each_context "early in the morning",+ # "late in the evening" do+ # it "has energy" do+ # expect(body.energy_level).to eq(100)+ # end+ # end+ # end+ def for_each_context(*context_names, &blk)+ context_names.each do |context_name|+ context context_name do+ include_context context_name++ instance_exec(&blk)+ end+ end+ end+ end+end++RSpec.configure do |config|+ config.extend RSpecOpExt::ForEachContext+enddiff --git a/spec/support/table_helpers/example_methods.rb b/spec/support/table_helpers/example_methods.rbindex 5843b2744ac..f7ea096f8eb 100644--- a/spec/support/table_helpers/example_methods.rb+++ b/spec/support/table_helpers/example_methods.rb@@ -51,7 +51,8 @@ module TableHelpers# Expect the given work packages to match a visual table representation.#- # It uses +match_table+ internally.+ # It uses +match_table+ internally and reloads the work packages from+ # database before comparing.## For instance:#@@ -66,6 +67,7 @@ module TableHelpers# is equivalent to:## it 'is scheduled' do+ # work_packages.each(&:reload)# expect(work_packages).to match_table(<<~TABLE)# subject | work | derived work |# parent | 1h | 3h |@@ -73,6 +75,7 @@ module TableHelpers# TABLE# enddef expect_work_packages(work_packages, table_representation)+ work_packages.each(&:reload)expect(work_packages).to match_table(table_representation)endenddiff --git a/spec/support/table_helpers/let_work_packages.rb b/spec/support/table_helpers/let_work_packages.rbindex c7982c267a7..bc62f2cf3da 100644--- a/spec/support/table_helpers/let_work_packages.rb+++ b/spec/support/table_helpers/let_work_packages.rb@@ -49,7 +49,7 @@ module TableHelpers# let!(:_table) do# create_table(table_representation)# end- # let!(:table_work_packages) do+ # let(:table_work_packages) do# _table.work_packages# end# let(:parent) do@@ -70,5 +70,48 @@ module TableHelperslet(identifier) { _table.work_package(identifier) }endend++ # Declare work packages and relations from a visual chart representation.+ #+ # It uses +create_table+ internally and is useful to have direct access+ # to the created work packages.+ #+ # To see supported columns, see +TableHelpers::Column+.+ #+ # For instance:+ #+ # shared_let_work_packages(<<~TABLE)+ # hierarchy | work |+ # parent | 1h |+ # child | 2.5h |+ # another one | |+ # TABLE+ #+ # is equivalent to:+ #+ # shared_let(:_table) do+ # create_table(table_representation)+ # end+ # shared_let(:table_work_packages) do+ # _table.work_packages+ # end+ # shared_let(:parent) do+ # _table.work_package(:parent)+ # end+ # shared_let(:child) do+ # _table.work_package(:child)+ # end+ # shared_let(:another_one) do+ # _table.work_package(:another_one)+ # end+ def shared_let_work_packages(table_representation)+ shared_let(:_table) { create_table(table_representation) }++ table_data = TableData.for(table_representation)+ shared_let(:table_work_packages) { _table.work_packages }+ table_data.work_package_identifiers.each do |identifier|+ shared_let(identifier) { _table.work_package(identifier) }+ end+ endendend