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

[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.rb
index 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::Loader
def former_ancestors
@former_ancestors ||= if previous_parent_id && include_former_ancestors
parent = WorkPackage.find(previous_parent_id)
-
- [parent] + parent.ancestors
+ parent.self_and_ancestors
else
[]
end
@@ -171,7 +170,7 @@ class WorkPackages::UpdateAncestors::Loader
def previous_change_parent_id
previous = 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 : nil
end
diff --git a/app/services/work_packages/update_ancestors_service.rb b/app/services/work_packages/update_ancestors_service.rb
index 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::UpdateAncestorsService
private
def 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.rb
index 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' do
include_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') }
end
context 'backlogs deactivated' do
@@ -77,10 +73,6 @@ RSpec.describe 'API v3 Work package resource' do
it { 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') }
end
end
@@ -105,12 +97,5 @@ RSpec.describe 'API v3 Work package resource' do
it { 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') }
- end
end
end
diff --git a/modules/backlogs/spec/api/work_packages/form_resource_spec.rb b/modules/backlogs/spec/api/work_packages/form_resource_spec.rb
index 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' do
it_behaves_like 'having an error', 'storyPoints'
end
end
-
- 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
- end
end
end
diff --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.rb
index 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 do
let(:writable) { true }
end
- context 'backlogs disabled' do
+ context 'when backlogs module is disabled' do
before do
allow(schema.project).to receive(:backlogs_enabled?).and_return(false)
end
@@ -81,69 +81,4 @@ RSpec.describe API::V3::WorkPackages::Schema::WorkPackageSchemaRepresenter do
end
end
end
-
- 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
- end
end
diff --git a/modules/backlogs/spec/services/work_packages/update_ancestors_service_spec.rb b/modules/backlogs/spec/services/work_packages/update_ancestors_service_spec.rb
deleted file mode 100644
index 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
-end
diff --git a/spec/factories/work_package_factory.rb b/spec/factories/work_package_factory.rb
index 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_attributes
diff --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.rb
index 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 do
end
end
+ 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' do
context 'with the view_time_entries permission' do
let(: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.rb
index 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' do
end
end
+ 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' do
let(: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.rb
index 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::RSpecMatchers
include 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)
+ end
let(:work_package) do
create(: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' do
let(:get_path) { api_v3_paths.work_package work_package.id }
context 'when acting as a user with permission to view work package' do
before do
- login_as(current_user)
get get_path
end
@@ -73,15 +75,11 @@ RSpec.describe 'API v3 Work package resource',
describe 'response body' do
subject { 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) do
create(:work_package,
- project_id: project.id,
- description:).tap do |wp|
+ description:,
+ remaining_hours: 5) do |wp|
wp.children << children
end
end
@@ -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 links
expect(subject['html'])
.to have_selector("opce-macro-wp-quickinfo[data-id='#{other_wp.id}']")
# resolves macros, e.g. toc
expect(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")
end
end
@@ -176,9 +174,17 @@ RSpec.describe 'API v3 Work package resource',
.at_path('_embedded/relations/_embedded/elements/0/_links/to/href')
end
end
+
+ 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') }
+ end
end
- context 'requesting nonexistent work package' do
+ context 'when requesting nonexistent work package' do
let(:get_path) { api_v3_paths.work_package 909090 }
it_behaves_like 'not found',
@@ -187,8 +193,11 @@ RSpec.describe 'API v3 Work package resource',
end
context '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_user
get get_path
end
@@ -197,8 +206,9 @@ RSpec.describe 'API v3 Work package resource',
end
context 'when acting as an anonymous user' do
+ current_user { User.anonymous }
+
before do
- allow(User).to receive(:current).and_return User.anonymous
get get_path
end
@@ -213,7 +223,6 @@ RSpec.describe 'API v3 Work package resource',
describe 'response body' do
subject do
- login_as current_user
get get_path
last_response.body
end
@@ -226,7 +235,6 @@ RSpec.describe 'API v3 Work package resource',
let(:work_package) do
create(: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',
end
context "with caching" do
- before { login_as current_user }
-
context "with relative timestamps" do
let(: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',
end
context "with caching" do
- before { login_as current_user }
-
context "with relative timestamps" do
let(: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.rb
index 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',
end
let(: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 do
create(:user, member_with_roles: { project => role })
@@ -235,30 +234,45 @@ RSpec.describe 'API v3 Work package resource',
end
context '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')
end
it_behaves_like 'lock version updated'
end
context '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')
end
it_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.rb
index 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 do
let(: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' do
context 'for the new ancestor chain' do
shared_examples 'attributes of parent having children' do
before do
children
end
+ it 'is a success' do
+ expect(subject)
+ .to be_success
+ end
+
it 'updated one work package - the parent' do
expect(subject.dependent_results.map(&:result))
.to contain_exactly(parent)
@@ -63,11 +68,6 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
expect(subject.dependent_results.first.result.derived_estimated_hours)
.to eq aggregate_estimated_hours
end
-
- it 'is a success' do
- expect(subject)
- .to be_success
- end
end
let(:children) do
@@ -217,24 +217,30 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model 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(:grandparent) do
- create(:work_package)
+ create(:work_package,
+ subject: 'grandparent')
end
shared_let(:parent) do
create(:work_package,
+ subject: 'parent',
parent: grandparent)
end
shared_let(:sibling) do
create(: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)
end
shared_let(:work_package) do
create(: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' do
expect(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)
end
end
@@ -287,26 +289,27 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model 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(:grandparent) do
- create(:work_package)
+ create(:work_package,
+ subject: 'grandparent')
end
shared_let(:parent) do
create(:work_package,
+ subject: 'parent',
parent: grandparent)
end
shared_let(:work_package) do
create(:work_package,
+ subject: 'subject - gains a new parent and grandparent',
status:,
+ done_ratio:,
estimated_hours:,
- done_ratio:)
+ remaining_hours:)
end
shared_examples_for 'updates the attributes within the new hierarchy' do
- before do
- subject
- end
-
it 'is successful' do
expect(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)
end
end
@@ -342,7 +345,6 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
subject do
work_package.parent = parent
work_package.save!
- work_package.parent_id_was
described_class
.new(user:,
@@ -357,7 +359,6 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model do
subject do
work_package.parent_id = parent.id
work_package.save!
- work_package.parent_id_was
described_class
.new(user:,
@@ -373,43 +374,55 @@ RSpec.describe WorkPackages::UpdateAncestorsService, type: :model 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',
+ done_ratio: done_ratio / 2, # two children having [done_ratio, 0]
derived_estimated_hours: estimated_hours,
- done_ratio:)
+ derived_remaining_hours: remaining_hours)
end
shared_let(:old_parent) do
create(:work_package,
+ subject: 'old parent',
parent: grandparent,
+ done_ratio:,
derived_estimated_hours: estimated_hours,
- done_ratio:)
+ 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,
status:,
+ done_ratio:,
estimated_hours:,
- done_ratio:)
+ remaining_hours:)
end
subject do
work_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_hours
work_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' do
expect(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
+ end
end
- 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
+ end
end
end
end