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
[51193] Move remaining_time tests to core
Christophe Bliard7623dfcd8707
diff --git a/app/services/work_packages/update_ancestors/loader.rb b/app/services/work_packages/update_ancestors/loader.rbindex 44d94180897..6e745869763 100644--- a/app/services/work_packages/update_ancestors/loader.rb+++ b/app/services/work_packages/update_ancestors/loader.rb@@ -131,8 +131,7 @@ class WorkPackages::UpdateAncestors::Loaderdef former_ancestors@former_ancestors ||= if previous_parent_id && include_former_ancestorsparent = WorkPackage.find(previous_parent_id)-- [parent] + parent.ancestors+ parent.self_and_ancestorselse[]end@@ -171,7 +170,7 @@ class WorkPackages::UpdateAncestors::Loaderdef previous_change_parent_idprevious = work_package.previous_changes- previous_parent_changes = (previous[:parent_id] || previous[:parent])+ previous_parent_changes = previous[:parent_id] || previous[:parent]previous_parent_changes ? previous_parent_changes.first : nilenddiff --git a/app/services/work_packages/update_ancestors_service.rb b/app/services/work_packages/update_ancestors_service.rbindex c1736be440e..6e3040c038d 100644--- a/app/services/work_packages/update_ancestors_service.rb+++ b/app/services/work_packages/update_ancestors_service.rb@@ -57,8 +57,9 @@ class WorkPackages::UpdateAncestorsServiceprivatedef update_current_and_former_ancestors(attributes)+ include_former_ancestors = attributes.intersect?(%i[parent_id parent])WorkPackages::UpdateAncestors::Loader- .new(work_package, (%i(parent_id parent) & attributes).any?)+ .new(work_package, include_former_ancestors).select do |ancestor, loader|inherit_attributes(ancestor, loader, attributes)diff --git a/modules/backlogs/spec/api/work_package_resource_spec.rb b/modules/backlogs/spec/api/work_package_resource_spec.rbindex 70d77e7ebf5..7ee6bcb2ac1 100644--- a/modules/backlogs/spec/api/work_package_resource_spec.rb+++ b/modules/backlogs/spec/api/work_package_resource_spec.rb@@ -61,10 +61,6 @@ RSpec.describe 'API v3 Work package resource' doinclude_context 'query work package'it { is_expected.to be_json_eql(work_package.story_points.to_json).at_path('storyPoints') }-- it { is_expected.to be_json_eql('PT5H'.to_json).at_path('remainingTime') }-- it { is_expected.to be_json_eql(nil.to_json).at_path('derivedRemainingTime') }endcontext 'backlogs deactivated' do@@ -77,10 +73,6 @@ RSpec.describe 'API v3 Work package resource' doit { expect(last_response.status).to be 200 }it { is_expected.not_to have_json_path('storyPoints') }-- it { is_expected.not_to have_json_path('remainingTime') }-- it { is_expected.not_to have_json_path('derivedRemainingTime') }endend@@ -105,12 +97,5 @@ RSpec.describe 'API v3 Work package resource' doit { expect(subject.status).to eq(200) }it { expect(subject.body).to be_json_eql(12.to_json).at_path('storyPoints') }end-- describe 'remainingTime' do- let(:params) { valid_params.merge(remainingTime: 'PT12H30M') }-- it { expect(subject.status).to eq(200) }- it { expect(subject.body).to be_json_eql('PT12H30M'.to_json).at_path('remainingTime') }- endendenddiff --git a/modules/backlogs/spec/api/work_packages/form_resource_spec.rb b/modules/backlogs/spec/api/work_packages/form_resource_spec.rbindex f8d1799dd08..b102d90e860 100644--- a/modules/backlogs/spec/api/work_packages/form_resource_spec.rb+++ b/modules/backlogs/spec/api/work_packages/form_resource_spec.rb@@ -136,32 +136,5 @@ RSpec.describe 'API v3 Work package form resource' doit_behaves_like 'having an error', 'storyPoints'endend-- describe 'remainingTime' do- include_context 'post request'-- context 'valid remainingTime' do- let(:params) { valid_params.merge(remainingTime: 'PT2H45M') }-- it_behaves_like 'valid payload'-- it_behaves_like 'having no errors'-- it 'responds with updated story points' do- expect(subject.body).to be_json_eql('PT2H45M'.to_json)- .at_path('_embedded/payload/remainingTime')- end- end-- context 'invalid remainingTime' do- let(:params) { valid_params.merge(remainingTime: 3) }-- it_behaves_like 'format error',- I18n.t('api_v3.errors.invalid_format',- property: 'remainingTime',- expected_format: 'ISO 8601 duration',- actual: '3')- end- endendenddiff --git a/modules/backlogs/spec/api/work_packages/work_package_schema_representer_spec.rb b/modules/backlogs/spec/api/work_packages/work_package_schema_representer_spec.rbindex d374e573e34..701ef14d147 100644--- a/modules/backlogs/spec/api/work_packages/work_package_schema_representer_spec.rb+++ b/modules/backlogs/spec/api/work_packages/work_package_schema_representer_spec.rb@@ -61,7 +61,7 @@ RSpec.describe API::V3::WorkPackages::Schema::WorkPackageSchemaRepresenter dolet(:writable) { true }end- context 'backlogs disabled' do+ context 'when backlogs module is disabled' dobefore doallow(schema.project).to receive(:backlogs_enabled?).and_return(false)end@@ -81,69 +81,4 @@ RSpec.describe API::V3::WorkPackages::Schema::WorkPackageSchemaRepresenter doendendend-- describe 'remainingTime & derivedRemainingtime' do- subject { representer.to_json }-- shared_examples_for 'has schema for remainingTime' do- it_behaves_like 'has basic schema properties' do- let(:path) { 'remainingTime' }- let(:type) { 'Duration' }- let(:name) { I18n.t('activerecord.attributes.work_package.remaining_hours') }- let(:required) { false }- let(:writable) { true }- end- end-- shared_examples_for 'has schema for derivedRemainingTime' do- it_behaves_like 'has basic schema properties' do- let(:path) { 'derivedRemainingTime' }- let(:type) { 'Duration' }- let(:name) { I18n.t('activerecord.attributes.work_package.derived_remaining_hours') }- let(:required) { false }- let(:writable) { false }- end- end-- it_behaves_like 'has schema for remainingTime'- it_behaves_like 'has schema for derivedRemainingTime'-- context 'backlogs disabled' do- before do- allow(schema.project).to receive(:backlogs_enabled?).and_return(false)- end-- it 'has no schema for remainingTime' do- expect(subject).not_to have_json_path('remainingTime')- end-- it 'has no schema for derivedRemainingTime' do- expect(subject).not_to have_json_path('derivedRemainingTime')- end- end-- context 'not a story' do- before do- allow(schema.type).to receive(:story?).and_return(false)- end-- it_behaves_like 'has schema for remainingTime'- it_behaves_like 'has schema for derivedRemainingTime'- end-- context 'remainingTime not writable' do- before do- allow(schema).to receive(:writable?).and_call_original- allow(schema).to receive(:writable?).with('remaining_hours').and_return(false)- end-- it_behaves_like 'has basic schema properties' do- let(:path) { 'remainingTime' }- let(:type) { 'Duration' }- let(:name) { I18n.t('activerecord.attributes.work_package.remaining_hours') }- let(:required) { false }- let(:writable) { false }- end- end- endenddiff --git a/modules/backlogs/spec/services/work_packages/update_ancestors_service_spec.rb b/modules/backlogs/spec/services/work_packages/update_ancestors_service_spec.rbdeleted file mode 100644index 38046fe54f6..00000000000--- a/modules/backlogs/spec/services/work_packages/update_ancestors_service_spec.rb+++ /dev/null@@ -1,93 +0,0 @@-#-- copyright-# OpenProject is an open source project management software.-# Copyright (C) 2012-2023 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 'spec_helper'--RSpec.describe WorkPackages::UpdateAncestorsService do- let(:user) { create(:user) }-- let(:sibling_remaining_hours) { 7.0 }- let(:parent_remaining_hours) { 2.0 }- let(:work_package_remaining_hours) { 5.0 }-- let!(:grandparent) { create(:work_package) }- let!(:parent) { create(:work_package, parent: grandparent, remaining_hours: parent_remaining_hours) }- let!(:sibling) { create(:work_package, parent:, remaining_hours: sibling_remaining_hours) }-- context 'for a new ancestors' do- let!(:work_package) { create(:work_package, remaining_hours: work_package_remaining_hours, parent:) }-- before do- described_class.new(user:, work_package:).call(%i(parent))- end-- it 'recalculates the remaining_hours for new parent and grandparent' do- [grandparent, parent, sibling].each(&:reload)-- expect(grandparent.remaining_hours).to be_nil- expect(grandparent.derived_remaining_hours)- .to eql(sibling_remaining_hours + work_package_remaining_hours + parent_remaining_hours)-- expect(parent.remaining_hours).to eql(parent_remaining_hours)- expect(parent.derived_remaining_hours).to eql(sibling_remaining_hours + work_package_remaining_hours)-- expect(sibling.remaining_hours).to eql(sibling_remaining_hours)- expect(sibling.derived_remaining_hours).to be_nil-- expect(work_package.remaining_hours).to eql(work_package_remaining_hours)- expect(work_package.derived_remaining_hours).to be_nil- end- end-- context 'for the previous ancestors' do- let(:work_package) { create(:work_package, remaining_hours: work_package_remaining_hours, parent:) }-- before do- work_package.parent = nil- work_package.save!-- described_class.new(user:, work_package:).call(%i(parent))- end-- it 'recalculates the derived_remaining_hours for former parent and grandparent' do- [grandparent, parent, sibling, work_package].each(&:reload)-- expect(grandparent.remaining_hours).to be_nil- expect(grandparent.derived_remaining_hours).to eql sibling_remaining_hours + parent_remaining_hours-- expect(parent.remaining_hours).to eql(parent_remaining_hours)- expect(parent.derived_remaining_hours).to eql sibling_remaining_hours-- expect(sibling.remaining_hours).to eql sibling_remaining_hours- expect(sibling.derived_remaining_hours).to be_nil-- expect(work_package.remaining_hours).to eql work_package_remaining_hours- expect(work_package.derived_remaining_hours).to be_nil- end- end-enddiff --git a/spec/factories/work_package_factory.rb b/spec/factories/work_package_factory.rbindex 8e3721d2fd9..1c4d1d49da2 100644--- a/spec/factories/work_package_factory.rb+++ b/spec/factories/work_package_factory.rb@@ -106,6 +106,7 @@ FactoryBot.define do.reverse_merge(journable: work_package,created_at: timestamp,updated_at: timestamp,+ user: work_package.author,version: version + 1)data_attributes = work_package_attributesdiff --git a/spec/lib/api/v3/work_packages/schema/work_package_schema_representer_spec.rb b/spec/lib/api/v3/work_packages/schema/work_package_schema_representer_spec.rbindex 708cbd1c4cb..a6c47140899 100644--- a/spec/lib/api/v3/work_packages/schema/work_package_schema_representer_spec.rb+++ b/spec/lib/api/v3/work_packages/schema/work_package_schema_representer_spec.rb@@ -603,6 +603,26 @@ RSpec.describe API::V3::WorkPackages::Schema::WorkPackageSchemaRepresenter doendend+ describe 'remainingTime' do+ it_behaves_like 'has basic schema properties' do+ let(:path) { 'remainingTime' }+ let(:type) { 'Duration' }+ let(:name) { I18n.t('activerecord.attributes.work_package.remaining_hours') }+ let(:required) { false }+ let(:writable) { true }+ end+ end++ describe 'derivedRemainingTime' do+ it_behaves_like 'has basic schema properties' do+ let(:path) { 'derivedRemainingTime' }+ let(:type) { 'Duration' }+ let(:name) { I18n.t('activerecord.attributes.work_package.derived_remaining_hours') }+ let(:required) { false }+ let(:writable) { false }+ end+ end+describe 'spentTime' docontext 'with the view_time_entries permission' dolet(:permissions) { %i[edit_work_packages view_time_entries] }diff --git a/spec/requests/api/v3/work_packages/form/work_package_form_resource_spec.rb b/spec/requests/api/v3/work_packages/form/work_package_form_resource_spec.rbindex 84b29509369..28c9d226806 100644--- a/spec/requests/api/v3/work_packages/form/work_package_form_resource_spec.rb+++ b/spec/requests/api/v3/work_packages/form/work_package_form_resource_spec.rb@@ -340,6 +340,33 @@ RSpec.describe 'API v3 Work package form resource' doendend+ describe 'remaining work' do+ include_context 'with post request'++ context 'for valid duration' do+ let(:params) { valid_params.merge(remainingTime: 'PT12H45M') }++ it_behaves_like 'valid payload'++ it_behaves_like 'having no errors'++ it 'responds with updated work package' do+ expect(subject.body).to be_json_eql('PT12H45M'.to_json)+ .at_path('_embedded/payload/remainingTime')+ end+ end++ context 'for invalid duration' do+ let(:params) { valid_params.merge(remainingTime: 'not a duration') }++ it_behaves_like 'format error',+ I18n.t('api_v3.errors.invalid_format',+ property: 'remainingTime',+ expected_format: I18n.t('api_v3.errors.expected.duration'),+ actual: 'not a duration')+ end+ end+describe 'status' dolet(:path) { '_embedded/payload/_links/status/href' }let(:target_status) { create(:status) }diff --git a/spec/requests/api/v3/work_packages/show_resource_spec.rb b/spec/requests/api/v3/work_packages/show_resource_spec.rbindex ec8c0d2a2a3..74560de95f6 100644--- a/spec/requests/api/v3/work_packages/show_resource_spec.rb+++ b/spec/requests/api/v3/work_packages/show_resource_spec.rb@@ -35,34 +35,36 @@ RSpec.describe 'API v3 Work package resource',include Capybara::RSpecMatchersinclude API::V3::Utilities::PathHelper- let(:closed_status) { create(:closed_status) }+ shared_let(:project) { create(:project, identifier: 'test_project', public: false) }+ shared_let(:closed_status) { create(:closed_status) }+ shared_let(:priority) { create(:priority) }+ shared_let(:status) { create(:status) }+ shared_let(:user) do+ create(:user,+ member_with_permissions: { project => %i[view_work_packages edit_work_packages assign_versions] })+ end++ before_all do+ set_factory_default(:priority, priority)+ set_factory_default(:project, project)+ set_factory_default(:project_with_types, project)+ set_factory_default(:status, status)+ set_factory_default(:user, user)+ endlet(:work_package) docreate(:work_package,project_id: project.id,description: 'lorem ipsum')end- let(:project) do- create(:project, identifier: 'test_project', public: false)- end- let(:role) { create(:project_role, permissions:) }- let(:permissions) { %i[view_work_packages edit_work_packages assign_versions] }- let(:current_user) do- create(:user, member_with_roles: { project => role })- end- let(:unauthorize_user) { create(:user) }- let(:type) { create(:type) }- before do- login_as(current_user)- end+ current_user { user }describe 'GET /api/v3/work_packages/:id' dolet(:get_path) { api_v3_paths.work_package work_package.id }context 'when acting as a user with permission to view work package' dobefore do- login_as(current_user)get get_pathend@@ -73,15 +75,11 @@ RSpec.describe 'API v3 Work package resource',describe 'response body' dosubject { last_response.body }- let!(:other_wp) do- create(:work_package,- project_id: project.id,- status: closed_status)- end+ shared_let(:other_wp) { create(:work_package, status: closed_status) }let(:work_package) docreate(:work_package,- project_id: project.id,- description:).tap do |wp|+ description:,+ remaining_hours: 5) do |wp|wp.children << childrenendend@@ -118,15 +116,15 @@ RSpec.describe 'API v3 Work package resource',subject { JSON.parse(last_response.body)['description'] }it 'renders to html' do- expect(subject).to have_selector('h1')- expect(subject).to have_selector('h2')+ expect(subject).to have_css('h1')+ expect(subject).to have_css('h2')# resolves linksexpect(subject['html']).to have_selector("opce-macro-wp-quickinfo[data-id='#{other_wp.id}']")# resolves macros, e.g. tocexpect(subject['html'])- .to have_selector('.op-uc-toc--list-item', text: "OpenProject Masterplan for 2015")+ .to have_css('.op-uc-toc--list-item', text: "OpenProject Masterplan for 2015")endend@@ -176,9 +174,17 @@ RSpec.describe 'API v3 Work package resource',.at_path('_embedded/relations/_embedded/elements/0/_links/to/href')endend++ describe 'remaining time' do+ it { is_expected.to be_json_eql('PT5H'.to_json).at_path('remainingTime') }+ end++ describe 'derived remaining time' do+ it { is_expected.to be_json_eql(nil.to_json).at_path('derivedRemainingTime') }+ endend- context 'requesting nonexistent work package' do+ context 'when requesting nonexistent work package' dolet(:get_path) { api_v3_paths.work_package 909090 }it_behaves_like 'not found',@@ -187,8 +193,11 @@ RSpec.describe 'API v3 Work package resource',endcontext 'when acting as a user without permission to view work package' do+ shared_let(:unauthorized_user) { create(:user) }++ current_user { unauthorized_user }+before do- allow(User).to receive(:current).and_return unauthorize_userget get_pathend@@ -197,8 +206,9 @@ RSpec.describe 'API v3 Work package resource',endcontext 'when acting as an anonymous user' do+ current_user { User.anonymous }+before do- allow(User).to receive(:current).and_return User.anonymousget get_pathend@@ -213,7 +223,6 @@ RSpec.describe 'API v3 Work package resource',describe 'response body' dosubject do- login_as current_userget get_pathlast_response.bodyend@@ -226,7 +235,6 @@ RSpec.describe 'API v3 Work package resource',let(:work_package) docreate(:work_package,subject: "The current work package",- project:,journals: {created_at => { subject: "The original work package" },# This journal creation conflicts with the timestamp "P-1D" due to timing issues.@@ -350,8 +358,6 @@ RSpec.describe 'API v3 Work package resource',endcontext "with caching" do- before { login_as current_user }-context "with relative timestamps" dolet(:timestamps) { [Timestamp.parse("P-2D"), Timestamp.now] }let(:created_at) { Date.parse('2015-01-01') }@@ -436,8 +442,6 @@ RSpec.describe 'API v3 Work package resource',endcontext "with caching" do- before { login_as current_user }-context "with relative timestamps" dolet(:timestamps) { [Timestamp.parse("oneDayAgo@00:00+00:00"), Timestamp.now] }let(:created_at) { Date.parse('2015-01-01') }diff --git a/spec/requests/api/v3/work_packages/update_resource_spec.rb b/spec/requests/api/v3/work_packages/update_resource_spec.rbindex 0687ab17747..16ca5a39555 100644--- a/spec/requests/api/v3/work_packages/update_resource_spec.rb+++ b/spec/requests/api/v3/work_packages/update_resource_spec.rb@@ -47,7 +47,6 @@ RSpec.describe 'API v3 Work package resource',endlet(:role) { create(:project_role, permissions:) }let(:permissions) { %i[view_work_packages edit_work_packages assign_versions work_package_assigned] }- let(:type) { create(:type) }current_user docreate(:user, member_with_roles: { project => role })@@ -235,30 +234,45 @@ RSpec.describe 'API v3 Work package resource',endcontext 'start date' do- let(:dateString) { Date.today.to_date.iso8601 }- let(:params) { valid_params.merge(startDate: dateString) }+ let(:date_string) { Date.current.iso8601 }+ let(:params) { valid_params.merge(startDate: date_string) }include_context 'patch request'it { expect(response.status).to eq(200) }it 'responds with updated start date' do- expect(subject.body).to be_json_eql(dateString.to_json).at_path('startDate')+ expect(subject.body).to be_json_eql(date_string.to_json).at_path('startDate')endit_behaves_like 'lock version updated'endcontext 'finish date' do- let(:dateString) { Date.today.to_date.iso8601 }- let(:params) { valid_params.merge(dueDate: dateString) }+ let(:date_string) { Date.current.iso8601 }+ let(:params) { valid_params.merge(dueDate: date_string) }include_context 'patch request'it { expect(response.status).to eq(200) }it 'responds with updated finish date' do- expect(subject.body).to be_json_eql(dateString.to_json).at_path('dueDate')+ expect(subject.body).to be_json_eql(date_string.to_json).at_path('dueDate')+ end++ it_behaves_like 'lock version updated'+ end++ describe 'remaining time' do+ let(:duration) { 'PT12H30M' }+ let(:params) { valid_params.merge(remainingTime: duration) }++ include_context 'patch request'++ it { expect(response.status).to eq(200) } # rubocop:disable RSpec/Rails/HaveHttpStatus++ it 'responds with updated finish date' do+ expect(subject.body).to be_json_eql(duration.to_json).at_path('remainingTime')endit_behaves_like 'lock version updated'diff --git a/spec/services/work_packages/update_ancestors_service_spec.rb b/spec/services/work_packages/update_ancestors_service_spec.rbindex 5b6086f5234..62f32e5de08 100644--- a/spec/services/work_packages/update_ancestors_service_spec.rb+++ b/spec/services/work_packages/update_ancestors_service_spec.rb@@ -42,13 +42,18 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model dolet(:aggregate_done_ratio) { 0.0 }let(:ignore_non_working_days) { [false, false, false] }- describe 'done_ratio/estimated_hours propagation' do+ describe 'done_ratio/estimated_hours/remaining_hours propagation' docontext 'for the new ancestor chain' doshared_examples 'attributes of parent having children' dobefore dochildrenend+ it 'is a success' do+ expect(subject)+ .to be_success+ end+it 'updated one work package - the parent' doexpect(subject.dependent_results.map(&:result)).to contain_exactly(parent)@@ -63,11 +68,6 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model doexpect(subject.dependent_results.first.result.derived_estimated_hours).to eq aggregate_estimated_hoursend-- it 'is a success' do- expect(subject)- .to be_success- endendlet(:children) do@@ -217,24 +217,30 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model doshared_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(:grandparent) do- create(:work_package)+ create(:work_package,+ subject: 'grandparent')endshared_let(:parent) docreate(:work_package,+ subject: 'parent',parent: grandparent)endshared_let(:sibling) docreate(:work_package,+ subject: 'sibling',parent:,status: sibling_status,+ done_ratio: sibling_done_ratio,estimated_hours: sibling_estimated_hours,- done_ratio: sibling_done_ratio)+ remaining_hours: sibling_remaining_hours)endshared_let(:work_package) docreate(:work_package,+ subject: 'subject - loses its parent',parent:)end@@ -248,10 +254,6 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do.call(%i(parent))end- before do- subject- end-it 'is successful' doexpect(subject).to be_success@@ -262,24 +264,24 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do.to contain_exactly(parent, grandparent)end- it 'updates the done_ratio of the former parent' do- expect(parent.reload(select: :done_ratio).done_ratio)- .to eql sibling_done_ratio- end-- it 'updates the estimated_hours of the former parent' do- expect(parent.reload(select: :derived_estimated_hours).derived_estimated_hours)- .to eql sibling_estimated_hours- end-- it 'updates the done_ratio of the former grandparent' do- expect(grandparent.reload(select: :done_ratio).done_ratio)- .to eql sibling_done_ratio+ it 'updates the done_ratio, derived_estimated_hours, and derived_remaining_hours of the former parent' do+ expect do+ subject+ parent.reload+ end+ .to change(parent, :done_ratio).to(sibling_done_ratio)+ .and change(parent, :derived_estimated_hours).to(sibling_estimated_hours)+ .and change(parent, :derived_remaining_hours).to(sibling_remaining_hours)end- it 'updates the estimated_hours of the former grandparent' do- expect(grandparent.reload(select: :derived_estimated_hours).derived_estimated_hours)- .to eql sibling_estimated_hours+ it 'updates the done_ratio, derived_estimated_hours, and derived_remaining_hours of the former grandparent' do+ expect do+ subject+ grandparent.reload+ end+ .to change(grandparent, :done_ratio).to(sibling_done_ratio)+ .and change(grandparent, :derived_estimated_hours).to(sibling_estimated_hours)+ .and change(grandparent, :derived_remaining_hours).to(sibling_remaining_hours)endend@@ -287,26 +289,27 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model doshared_let(:status) { open_status }shared_let(:done_ratio) { 50 }shared_let(:estimated_hours) { 7.0 }+ shared_let(:remaining_hours) { 3.5 }shared_let(:grandparent) do- create(:work_package)+ create(:work_package,+ subject: 'grandparent')endshared_let(:parent) docreate(:work_package,+ subject: 'parent',parent: grandparent)endshared_let(:work_package) docreate(:work_package,+ subject: 'subject - gains a new parent and grandparent',status:,+ done_ratio:,estimated_hours:,- done_ratio:)+ remaining_hours:)endshared_examples_for 'updates the attributes within the new hierarchy' do- before do- subject- end-it 'is successful' doexpect(subject).to be_success@@ -317,24 +320,24 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do.to contain_exactly(parent, grandparent)end- it 'updates the done_ratio of the new parent' do- expect(parent.reload(select: :done_ratio).done_ratio)- .to eql done_ratio- end-- it 'updates the estimated_hours of the new parent' do- expect(parent.reload(select: :derived_estimated_hours).derived_estimated_hours)- .to eql estimated_hours- end-- it 'updates the done_ratio of the new grandparent' do- expect(grandparent.reload(select: :done_ratio).done_ratio)- .to eql done_ratio+ it 'updates the done_ratio, derived_estimated_hours, and derived_remaining_hours of the new parent' do+ expect do+ subject+ parent.reload+ end+ .to change(parent, :done_ratio).to(done_ratio)+ .and change(parent, :derived_estimated_hours).to(estimated_hours)+ .and change(parent, :derived_remaining_hours).to(remaining_hours)end- it 'updates the estimated_hours of the new grandparent' do- expect(grandparent.reload(select: :derived_estimated_hours).derived_estimated_hours)- .to eql estimated_hours+ it 'updates the done_ratio, derived_estimated_hours, and derived_remaining_hours of the new grandparent' do+ expect do+ subject+ grandparent.reload+ end+ .to change(grandparent, :done_ratio).to(done_ratio)+ .and change(grandparent, :derived_estimated_hours).to(estimated_hours)+ .and change(grandparent, :derived_remaining_hours).to(remaining_hours)endend@@ -342,7 +345,6 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model dosubject dowork_package.parent = parentwork_package.save!- work_package.parent_id_wasdescribed_class.new(user:,@@ -357,7 +359,6 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model dosubject dowork_package.parent_id = parent.idwork_package.save!- work_package.parent_id_wasdescribed_class.new(user:,@@ -373,43 +374,55 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model doshared_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) docreate(:work_package,+ subject: 'common grandparent',+ done_ratio: done_ratio / 2, # two children having [done_ratio, 0]derived_estimated_hours: estimated_hours,- done_ratio:)+ derived_remaining_hours: remaining_hours)endshared_let(:old_parent) docreate(:work_package,+ subject: 'old parent',parent: grandparent,+ done_ratio:,derived_estimated_hours: estimated_hours,- done_ratio:)+ derived_remaining_hours: remaining_hours)endshared_let(:new_parent) docreate(:work_package,+ subject: 'new parent',parent: grandparent)endshared_let(:work_package) docreate(:work_package,+ subject: 'subject - parent changes from old parent to new parent, same grandparent',parent: old_parent,status:,+ done_ratio:,estimated_hours:,- done_ratio:)+ remaining_hours:)endsubject dowork_package.parent = new_parent- # In this test case, derived_estimated_hours and done_ratio will not- # inherently change on grandparent. However, if work_package has siblings- # then changing its parent could cause derived_estimated_hours and/or- # done_ratio 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- # derived_estimated_hours and done_ratio calculations, force- # derived_estimated_hours and done_ratio to change at the same time as the- # parent.- work_package.estimated_hours = (estimated_hours + 1)- work_package.done_ratio = (done_ratio + 1)+ # 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.+ work_package.done_ratio = new_done_ratio+ work_package.estimated_hours = new_estimated_hours+ work_package.remaining_hours = new_remaining_hourswork_package.save!described_class@@ -418,10 +431,6 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do.call(%i(parent))end- before do- subject- end-it 'is successful' doexpect(subject).to be_success@@ -432,14 +441,86 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do.to contain_exactly(new_parent, grandparent, old_parent)end- it 'updates the done_ratio of the former parent' do- expect(old_parent.reload(select: :done_ratio).done_ratio)- .to be 0+ it 'updates the done_ratio, derived_estimated_hours, and derived_remaining_hours of the former parent to 0' do+ expect do+ subject+ old_parent.reload+ end+ .to change(old_parent, :done_ratio).to(0)+ .and change(old_parent, :derived_estimated_hours).to(nil)+ .and change(old_parent, :derived_remaining_hours).to(nil)+ end++ it 'updates the 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, :done_ratio).to(new_done_ratio)+ .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 done_ratio, derived_estimated_hours, and derived_remaining_hours of the grandparent' do+ expect do+ subject+ grandparent.reload+ end+ .to change(grandparent, :done_ratio).to(new_done_ratio / 2) # two children having [new_done_ratio, 0] now+ .and change(grandparent, :derived_estimated_hours).to(new_estimated_hours)+ .and change(grandparent, :derived_remaining_hours).to(new_remaining_hours)+ end+ end+ end++ describe 'remaining_hours propagation' do+ shared_let(:parent) { create(:work_package, status: open_status) }++ context 'for the new ancestors chain' do+ shared_context 'when called with children' do |children_remaining_hours:|+ let(:children) do+ (children_remaining_hours.size - 1).downto(0).map do |i|+ create(:work_package,+ parent:,+ status: open_status,+ remaining_hours: children_remaining_hours[i])+ end+ end+ subject(:call_result) do+ described_class.new(user:, work_package: children.first)+ .call(%i(remaining_hours))+ end+ end++ shared_examples 'derived remaining hours' do |children_remaining_hours:, expected_derived_remaining_hours:|+ context "for #{children_remaining_hours.count} children with remaining hours being #{children_remaining_hours.inspect}" do+ include_context('when called with children', children_remaining_hours:)++ it "sets parent derived remaining hours to #{expected_derived_remaining_hours}", :aggregate_failures do+ expect(call_result).to be_success+ expect(call_result.dependent_results.map(&:result))+ .to contain_exactly(parent)+ expect(call_result.dependent_results.first.result.derived_remaining_hours)+ .to eq(expected_derived_remaining_hours)+ end+ endend- it 'updates the estimated_hours of the former parent' do- expect(old_parent.reload(select: :derived_estimated_hours).derived_estimated_hours)- .to be_nil+ include_examples 'derived remaining hours',+ children_remaining_hours: [0.0, 2.0, nil],+ expected_derived_remaining_hours: 2.0++ include_examples 'derived remaining hours',+ children_remaining_hours: [1, 2, 5, 42],+ expected_derived_remaining_hours: 50++ context 'with no remaining hours' do+ include_context('when called with children', children_remaining_hours: [nil, nil, nil])++ it 'does not update the parent derived remaining hours' do+ expect(call_result).to be_success+ expect(call_result.dependent_results).to be_empty+ endendendend