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
[55802] Add % Complete sum to work packages table sums
Christophe Bliard74f142622727
diff --git a/app/models/queries/work_packages/selects/property_select.rb b/app/models/queries/work_packages/selects/property_select.rbindex 0a7da338fe3..7a1bfd2bc22 100644--- a/app/models/queries/work_packages/selects/property_select.rb+++ b/app/models/queries/work_packages/selects/property_select.rb@@ -118,7 +118,18 @@ class Queries::WorkPackages::Selects::PropertySelect < Queries::WorkPackages::Se},done_ratio: {sortable: "#{WorkPackage.table_name}.done_ratio",- groupable: true+ groupable: true,+ summable: true,+ summable_select: <<~SQL.squish+ CASE+ WHEN estimated_hours IS NULL OR remaining_hours IS NULL OR estimated_hours <= 0 THEN NULL+ WHEN remaining_hours <= 0 THEN 100+ WHEN remaining_hours <= estimated_hours * 0.01 THEN 99+ WHEN remaining_hours >= estimated_hours THEN 0+ WHEN remaining_hours >= estimated_hours * 0.99 THEN 1+ ELSE ROUND( ((1 - (remaining_hours / estimated_hours)) * 100)::numeric )::integer+ END as done_ratio+ SQL},created_at: {sortable: "#{WorkPackage.table_name}.created_at",diff --git a/app/models/work_package/exports/formatters/done_ratio.rb b/app/models/work_package/exports/formatters/done_ratio.rbindex a85a5b9c547..e84757523f4 100644--- a/app/models/work_package/exports/formatters/done_ratio.rb+++ b/app/models/work_package/exports/formatters/done_ratio.rb@@ -34,8 +34,14 @@ module WorkPackage::Exportsdef format(work_package, **)derived_done_ratio = work_package.derived_done_ratio- derived = derived_done_ratio&.positive? ? " · Σ #{derived_done_ratio}%" : ""- "#{work_package.done_ratio}%#{derived}"+ derived = derived_done_ratio&.positive? ? " · Σ #{format_value(derived_done_ratio)}" : ""+ "#{format_value(work_package.done_ratio)}#{derived}"+ end++ def format_value(value, _options = {})+ return "" if value.nil?++ "#{value}%"endendenddiff --git a/lib/api/v3/work_packages/schema/work_package_sums_schema_representer.rb b/lib/api/v3/work_packages/schema/work_package_sums_schema_representer.rbindex a9fb9465e01..3f291aebaf5 100644--- a/lib/api/v3/work_packages/schema/work_package_sums_schema_representer.rb+++ b/lib/api/v3/work_packages/schema/work_package_sums_schema_representer.rb@@ -64,6 +64,12 @@ module APIrequired: false,writable: false+ schema :percentage_done,+ type: "Integer",+ name_source: :done_ratio,+ required: false,+ writable: false+schema :overall_costs,type: "String",required: false,diff --git a/lib/api/v3/work_packages/work_package_sums_representer.rb b/lib/api/v3/work_packages/work_package_sums_representer.rbindex e5161fd809c..ec507487779 100644--- a/lib/api/v3/work_packages/work_package_sums_representer.rb+++ b/lib/api/v3/work_packages/work_package_sums_representer.rb@@ -29,6 +29,12 @@ module APIproperty :story_points,render_nil: true+ property :percentage_done,+ render_nil: true,+ getter: ->(*) {+ done_ratio+ }+property :remaining_time,render_nil: true,exec_context: :decorator,diff --git a/spec/features/work_packages/index_sums_spec.rb b/spec/features/work_packages/index_sums_spec.rbindex 257f2e3d2a0..3911dc1787a 100644--- a/spec/features/work_packages/index_sums_spec.rb+++ b/spec/features/work_packages/index_sums_spec.rb@@ -123,22 +123,17 @@ RSpec.describe "Work package index sums", :js dovisit project_work_packages_path(project)wp_table.expect_work_package_listed work_package1, work_package2- # Add work column- columns.add "Work"- # Add remaining work column- columns.add "Remaining work"- # Add int cf column- columns.add int_cf.name- # Add float cf column- columns.add float_cf.name- # Add labor costs column- columns.add "Labor costs"- # Add unit costs column- columns.add "Unit costs"- # Add overall costs column- columns.add "Overall costs"+ # Add summable columns, without saving changes to save time+ columns.add "Work", save_changes: false+ columns.add "Remaining work", save_changes: false+ columns.add "% Complete", save_changes: false+ columns.add int_cf.name, save_changes: false+ columns.add float_cf.name, save_changes: false+ columns.add "Labor costs", save_changes: false+ columns.add "Unit costs", save_changes: false+ columns.add "Overall costs", save_changes: false- # Trigger action from action menu dropdown+ # Activate the display of sums, and save the changesmodal.set_display_sums enable: truewp_table.expect_work_package_listed work_package1, work_package2@@ -148,6 +143,7 @@ RSpec.describe "Work package index sums", :js dowithin(:row, "Total sum") do |row|expect(row).to have_css(".estimatedTime", text: "25h")expect(row).to have_css(".remainingTime", text: "12.5h")+ expect(row).to have_css(".percentageDone", text: "50%")expect(row).to have_css(".#{int_cf.attribute_name(:camel_case)}", text: "12")expect(row).to have_css(".#{float_cf.attribute_name(:camel_case)}", text: "13.2")expect(row).to have_css(".laborCosts", text: "15.00 EUR")@@ -156,16 +152,17 @@ RSpec.describe "Work package index sums", :js doendend- # Update the sum- wp_table.edit_field(work_package1, :estimatedTime)- .update "20"- wp_table.edit_field(work_package1, :remainingTime)- .update "12"+ # Change some progress values to test that sums are recalculated+ progress_popover = wp_table.progress_popover(work_package1)+ progress_popover.open+ progress_popover.set_values(work: "20h", remaining_work: "12h")+ progress_popover.saveaggregate_failures dowithin(:row, "Total sum") do |row|expect(row).to have_css(".estimatedTime", text: "35h")expect(row).to have_css(".remainingTime", text: "19.5h")+ expect(row).to have_css(".percentageDone", text: "44%")expect(row).to have_css(".#{int_cf.attribute_name(:camel_case)}", text: "12")expect(row).to have_css(".#{float_cf.attribute_name(:camel_case)}", text: "13.2")expect(row).to have_css(".laborCosts", text: "15.00 EUR")@@ -186,6 +183,7 @@ RSpec.describe "Work package index sums", :js doaggregate_failures doexpect(first_sum_row).to have_css(".estimatedTime", text: "20h")expect(first_sum_row).to have_css(".remainingTime", text: "12h")+ expect(first_sum_row).to have_css(".percentageDone", text: "40%")expect(first_sum_row).to have_css(".#{int_cf.attribute_name(:camel_case)}", text: "5")expect(first_sum_row).to have_css(".#{float_cf.attribute_name(:camel_case)}", text: "5.5")expect(first_sum_row).to have_css(".laborCosts", text: "15.00 EUR")@@ -197,6 +195,7 @@ RSpec.describe "Work package index sums", :js doaggregate_failures doexpect(second_sum_row).to have_css(".estimatedTime", text: "15h")expect(second_sum_row).to have_css(".remainingTime", text: "7.5h")+ expect(second_sum_row).to have_css(".percentageDone", text: "50%")expect(second_sum_row).to have_css(".#{int_cf.attribute_name(:camel_case)}", text: "7")expect(second_sum_row).to have_css(".#{float_cf.attribute_name(:camel_case)}", text: "7.7")expect(second_sum_row).to have_css(".laborCosts", text: "", exact_text: true)@@ -209,6 +208,7 @@ RSpec.describe "Work package index sums", :js dowithin(:row, "Total sum") do |row|expect(row).to have_css(".estimatedTime", text: "35h")expect(row).to have_css(".remainingTime", text: "19.5h")+ expect(row).to have_css(".percentageDone", text: "44%")expect(row).to have_css(".#{int_cf.attribute_name(:camel_case)}", text: "12")expect(row).to have_css(".#{float_cf.attribute_name(:camel_case)}", text: "13.2")expect(row).to have_css(".laborCosts", text: "15.00 EUR")@@ -218,9 +218,9 @@ RSpec.describe "Work package index sums", :js doend# Collapsing groups will also hide the sums row- page.find(".expander.icon-minus2", match: :first).click+ first(".expander.icon-minus2").clicksleep 1- page.find(".expander.icon-minus2", match: :first).click+ first(".expander.icon-minus2").click# Expect to have only the final sumsexpect(page).not_to have_row("Sum")@@ -273,7 +273,7 @@ RSpec.describe "Work package index sums", :js doproject:,user:,display_sums: true,- column_names: [:id, :subject, :type, :status, :estimated_hours, :remaining_hours,+ column_names: [:id, :subject, :type, :status, :estimated_hours, :remaining_hours, :done_ratio,:"#{int_cf.column_name}", :"#{float_cf.column_name}",:labor_costs, :material_costs, :overall_costs])wp_table.visit_query query@@ -284,6 +284,7 @@ RSpec.describe "Work package index sums", :js dowithin(:row, "Total sum") do |row|expect(row).to have_css(".estimatedTime", text: "50h")expect(row).to have_css(".remainingTime", text: "25h")+ expect(row).to have_css(".percentageDone", text: "50%")expect(row).to have_css(".#{int_cf.attribute_name(:camel_case)}", text: "24")expect(row).to have_css(".#{float_cf.attribute_name(:camel_case)}", text: "26.4")expect(row).to have_css(".laborCosts", text: "40.00 EUR")@@ -304,6 +305,7 @@ RSpec.describe "Work package index sums", :js dowithin(:row, "Total sum") do |row|expect(row).to have_css(".estimatedTime", text: "30h")expect(row).to have_css(".remainingTime", text: "15h")+ expect(row).to have_css(".percentageDone", text: "50%")expect(row).to have_css(".#{int_cf.attribute_name(:camel_case)}", text: "14")expect(row).to have_css(".#{float_cf.attribute_name(:camel_case)}", text: "15.4")expect(row).to have_css(".laborCosts", text: "", exact_text: true)@@ -329,6 +331,7 @@ RSpec.describe "Work package index sums", :js doaggregate_failures doexpect(first_sum_row).to have_css(".estimatedTime", text: "10h")expect(first_sum_row).to have_css(".remainingTime", text: "5h")+ expect(first_sum_row).to have_css(".percentageDone", text: "50%")expect(first_sum_row).to have_css(".#{int_cf.attribute_name(:camel_case)}", text: "5")expect(first_sum_row).to have_css(".#{float_cf.attribute_name(:camel_case)}", text: "5.5")expect(first_sum_row).to have_css(".laborCosts", text: "15.00 EUR")@@ -340,6 +343,7 @@ RSpec.describe "Work package index sums", :js doaggregate_failures doexpect(second_sum_row).to have_css(".estimatedTime", text: "15h")expect(second_sum_row).to have_css(".remainingTime", text: "7.5h")+ expect(second_sum_row).to have_css(".percentageDone", text: "50%")expect(second_sum_row).to have_css(".#{int_cf.attribute_name(:camel_case)}", text: "7")expect(second_sum_row).to have_css(".#{float_cf.attribute_name(:camel_case)}", text: "7.7")expect(second_sum_row).to have_css(".laborCosts", text: "", exact_text: true)@@ -352,6 +356,7 @@ RSpec.describe "Work package index sums", :js dowithin(:row, "Total sum") do |row|expect(row).to have_css(".estimatedTime", text: "25h")expect(row).to have_css(".remainingTime", text: "12.5h")+ expect(row).to have_css(".percentageDone", text: "50%")expect(row).to have_css(".#{int_cf.attribute_name(:camel_case)}", text: "12")expect(row).to have_css(".#{float_cf.attribute_name(:camel_case)}", text: "13.2")expect(row).to have_css(".laborCosts", text: "15.00 EUR")diff --git a/spec/lib/api/v3/work_packages/work_package_collection_representer_spec.rb b/spec/lib/api/v3/work_packages/work_package_collection_representer_spec.rbindex a6bd379769c..92c6666eee8 100644--- a/spec/lib/api/v3/work_packages/work_package_collection_representer_spec.rb+++ b/spec/lib/api/v3/work_packages/work_package_collection_representer_spec.rb@@ -509,11 +509,12 @@ RSpec.describe API::V3::WorkPackages::WorkPackageCollectionRepresenter doendcontext "when passing sums" do- let(:total_sums) { OpenStruct.new(estimated_hours: 1) }+ let(:total_sums) { API::ParserStruct.new(estimated_hours: 1) }it "renders the groups object as json" doexpected = { "estimatedTime" => "PT1H","remainingTime" => nil,+ "percentageDone" => nil,"storyPoints" => nil }expect(collection).to be_json_eql(expected.to_json).at_path("totalSums")enddiff --git a/spec/lib/api/v3/work_packages/work_package_sums_representer_spec.rb b/spec/lib/api/v3/work_packages/work_package_sums_representer_spec.rbindex 37909dca498..bc31262b29e 100644--- a/spec/lib/api/v3/work_packages/work_package_sums_representer_spec.rb+++ b/spec/lib/api/v3/work_packages/work_package_sums_representer_spec.rb@@ -41,6 +41,7 @@ RSpec.describe API::V3::WorkPackages::WorkPackageSumsRepresenter dostory_points: 5,remaining_hours: 10,estimated_hours: 5,+ done_ratio: 50,material_costs: 5,labor_costs: 10,overall_costs: 15,@@ -69,6 +70,13 @@ RSpec.describe API::V3::WorkPackages::WorkPackageSumsRepresenter doendend+ describe "percentageDone" do+ it "is represented" do+ expected = 50+ expect(subject).to be_json_eql(expected.to_json).at_path("percentageDone")+ end+ end+describe "storyPoints" doit "is represented" doexpect(subject).to be_json_eql(sums.story_points.to_json).at_path("storyPoints")diff --git a/spec/models/query/results_sums_integration_spec.rb b/spec/models/query/results_sums_integration_spec.rbindex f46f8f59cfa..fdb7b380cf1 100644--- a/spec/models/query/results_sums_integration_spec.rb+++ b/spec/models/query/results_sums_integration_spec.rb@@ -44,6 +44,7 @@ RSpec.describe Query::Results, "sums" dolet(:labor_costs_column) { query.displayable_columns.detect { |c| c.name.to_s == "labor_costs" } }let(:overall_costs_column) { query.displayable_columns.detect { |c| c.name.to_s == "overall_costs" } }let(:remaining_hours_column) { query.displayable_columns.detect { |c| c.name.to_s == "remaining_hours" } }+ let(:done_ratio_column) { query.displayable_columns.detect { |c| c.name.to_s == "done_ratio" } }let(:story_points_column) { query.displayable_columns.detect { |c| c.name.to_s == "story_points" } }let(:other_project) docreate(:project) do |p|@@ -158,14 +159,15 @@ RSpec.describe Query::Results, "sums" dodescribe "#all_total_sums" doit "is a hash of all summable columns" doexpect(query_results.all_total_sums)- .to eql(estimated_hours_column => 20.0,- int_cf_column => 30,- float_cf_column => 10.24,- material_costs_column => 400.0,- labor_costs_column => 600.0,- overall_costs_column => 1000.0,- remaining_hours_column => 14.0,- story_points_column => 21)+ .to eq(estimated_hours_column => 20.0,+ int_cf_column => 30,+ float_cf_column => 10.24,+ material_costs_column => 400.0,+ labor_costs_column => 600.0,+ overall_costs_column => 1000.0,+ remaining_hours_column => 14.0,+ done_ratio_column => 30,+ story_points_column => 21)endcontext "when filtering" do@@ -175,14 +177,15 @@ RSpec.describe Query::Results, "sums" doit "is a hash of all summable columns and includes only the work packages matching the filter" doexpect(query_results.all_total_sums)- .to eql(estimated_hours_column => 10.0,- int_cf_column => 20,- float_cf_column => 6.83,- material_costs_column => 200.0,- labor_costs_column => 300.0,- overall_costs_column => 500.0,- remaining_hours_column => 5.0,- story_points_column => 14)+ .to eq(estimated_hours_column => 10.0,+ int_cf_column => 20,+ float_cf_column => 6.83,+ material_costs_column => 200.0,+ labor_costs_column => 300.0,+ overall_costs_column => 500.0,+ remaining_hours_column => 5.0,+ done_ratio_column => 50,+ story_points_column => 14)endendend@@ -192,23 +195,27 @@ RSpec.describe Query::Results, "sums" dolet(:group_by) { :assigned_to }it "is a hash of sums grouped by user values (and nil) and grouped columns" do- expect(query_results.all_group_sums)- .to eql(current_user => { estimated_hours_column => 10.0,- int_cf_column => 20,- float_cf_column => 6.83,- material_costs_column => 200.0,- labor_costs_column => 300.0,- overall_costs_column => 500.0,- remaining_hours_column => 5.0,- story_points_column => 14 },- nil => { estimated_hours_column => 10.0,- int_cf_column => 10,- float_cf_column => 3.41,- material_costs_column => 200.0,- labor_costs_column => 300.0,- overall_costs_column => 500.0,- remaining_hours_column => 9.0,- story_points_column => 7 })+ expect(query_results.all_group_sums.keys).to contain_exactly(current_user, nil)+ expect(query_results.all_group_sums[current_user])+ .to eq(estimated_hours_column => 10.0,+ int_cf_column => 20,+ float_cf_column => 6.83,+ material_costs_column => 200.0,+ labor_costs_column => 300.0,+ overall_costs_column => 500.0,+ remaining_hours_column => 5.0,+ done_ratio_column => 50,+ story_points_column => 14)+ expect(query_results.all_group_sums[nil])+ .to eq(estimated_hours_column => 10.0,+ int_cf_column => 10,+ float_cf_column => 3.41,+ material_costs_column => 200.0,+ labor_costs_column => 300.0,+ overall_costs_column => 500.0,+ remaining_hours_column => 9.0,+ done_ratio_column => 10,+ story_points_column => 7)endcontext "when filtering" do@@ -217,15 +224,17 @@ RSpec.describe Query::Results, "sums" doendit "is a hash of sums grouped by user values and grouped columns" do- expect(query_results.all_group_sums)- .to eql(current_user => { estimated_hours_column => 5.0,- int_cf_column => 10,- float_cf_column => 3.41,- material_costs_column => 0.0,- labor_costs_column => 0.0,- overall_costs_column => 0.0,- story_points_column => 7,- remaining_hours_column => 2.5 })+ expect(query_results.all_group_sums.keys).to contain_exactly(current_user)+ expect(query_results.all_group_sums[current_user])+ .to eq(estimated_hours_column => 5.0,+ int_cf_column => 10,+ float_cf_column => 3.41,+ material_costs_column => 0.0,+ labor_costs_column => 0.0,+ overall_costs_column => 0.0,+ remaining_hours_column => 2.5,+ done_ratio_column => 50,+ story_points_column => 7)endendend@@ -234,23 +243,27 @@ RSpec.describe Query::Results, "sums" dolet(:group_by) { :done_ratio }it "is a hash of sums grouped by done_ratio values and grouped columns" do- expect(query_results.all_group_sums)- .to eql(50 => { estimated_hours_column => 10.0,- int_cf_column => 20,- float_cf_column => 6.83,- material_costs_column => 200.0,- labor_costs_column => 300.0,- overall_costs_column => 500.0,- remaining_hours_column => 5.0,- story_points_column => 14 },- 10 => { estimated_hours_column => 10.0,- int_cf_column => 10,- float_cf_column => 3.41,- material_costs_column => 200.0,- labor_costs_column => 300.0,- overall_costs_column => 500.0,- remaining_hours_column => 9.0,- story_points_column => 7 })+ expect(query_results.all_group_sums.keys).to contain_exactly(50, 10)+ expect(query_results.all_group_sums[50])+ .to eq(estimated_hours_column => 10.0,+ int_cf_column => 20,+ float_cf_column => 6.83,+ material_costs_column => 200.0,+ labor_costs_column => 300.0,+ overall_costs_column => 500.0,+ remaining_hours_column => 5.0,+ done_ratio_column => 50,+ story_points_column => 14)+ expect(query_results.all_group_sums[10])+ .to eq(estimated_hours_column => 10.0,+ int_cf_column => 10,+ float_cf_column => 3.41,+ material_costs_column => 200.0,+ labor_costs_column => 300.0,+ overall_costs_column => 500.0,+ remaining_hours_column => 9.0,+ done_ratio_column => 10,+ story_points_column => 7)endcontext "when filtering" do@@ -259,15 +272,17 @@ RSpec.describe Query::Results, "sums" doendit "is a hash of sums grouped by done_ratio values and grouped columns" do- expect(query_results.all_group_sums)- .to eql(50 => { estimated_hours_column => 5.0,- int_cf_column => 10,- float_cf_column => 3.41,- material_costs_column => 0.0,- labor_costs_column => 0.0,- overall_costs_column => 0.0,- story_points_column => 7,- remaining_hours_column => 2.5 })+ expect(query_results.all_group_sums.keys).to contain_exactly(50)+ expect(query_results.all_group_sums[50])+ .to eq(estimated_hours_column => 5.0,+ int_cf_column => 10,+ float_cf_column => 3.41,+ material_costs_column => 0.0,+ labor_costs_column => 0.0,+ overall_costs_column => 0.0,+ remaining_hours_column => 2.5,+ done_ratio_column => 50,+ story_points_column => 7)endendenddiff --git a/spec/models/work_package/exports/formatters/done_ratio_spec.rb b/spec/models/work_package/exports/formatters/done_ratio_spec.rbnew file mode 100644index 00000000000..d0aa2c50f9f--- /dev/null+++ b/spec/models/work_package/exports/formatters/done_ratio_spec.rb@@ -0,0 +1,67 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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 WorkPackage::Exports::Formatters::DoneRatio do+ subject { described_class.new(:done_ratio) }++ describe "#format" do+ it "returns empty string if neither done_ratio or derived_done_ratio is set" do+ work_package = build(:work_package, done_ratio: nil, derived_done_ratio: nil)+ expect(subject.format(work_package)).to eq("")+ end++ it "returns something like '40%' when only done_ratio is set" do+ work_package = build(:work_package, done_ratio: 40)+ expect(subject.format(work_package)).to eq("40%")+ end++ it "returns something like ' · Σ 70%' when only derived_done_ratio is set" do+ work_package = build(:work_package, done_ratio: nil, derived_done_ratio: 70)+ expect(subject.format(work_package)).to eq(" · Σ 70%")+ end++ it "returns something like '40% · Σ 70%' when both done_ratio and derived_done_ratio are set" do+ work_package = build(:work_package, done_ratio: 40, derived_done_ratio: 70)+ expect(subject.format(work_package)).to eq("40% · Σ 70%")+ end+ end++ describe "#format_value" do+ it "returns the value as a percentage" do+ expect(subject.format_value(40)).to eq("40%")+ end++ it "returns empty string if value is nil" do+ expect(subject.format_value(nil)).to eq("")+ end+ end+enddiff --git a/spec/models/work_packages/pdf_export/work_package_list_to_pdf_spec.rb b/spec/models/work_packages/pdf_export/work_package_list_to_pdf_spec.rbindex 2d454cf06bb..de6ca2d483d 100644--- a/spec/models/work_packages/pdf_export/work_package_list_to_pdf_spec.rb+++ b/spec/models/work_packages/pdf_export/work_package_list_to_pdf_spec.rb@@ -31,46 +31,50 @@ require "spec_helper"RSpec.describe WorkPackage::PDFExport::WorkPackageListToPdf doinclude Redmine::I18ninclude PDFExportSpecUtils- let(:type_standard) { create(:type_standard) }- let(:type_bug) { create(:type_bug) }- let!(:list_custom_field) do+ shared_let(:type_standard) { create(:type_standard) }+ shared_let(:type_bug) { create(:type_bug) }+ shared_let(:list_custom_field) docreate(:list_wp_custom_field,types: [type_standard, type_bug],multi_value: true,possible_values: %w[Foo Bar])end- let!(:text_custom_field_a) do+ shared_let(:text_custom_field_a) docreate(:issue_custom_field, :text, types: [type_standard, type_bug], name: "Notes A")end- let!(:text_custom_field_b) do+ shared_let(:text_custom_field_b) docreate(:issue_custom_field, :text, types: [type_standard, type_bug], name: "Notes B")end- let(:custom_value_first) do+ shared_let(:custom_value_first) docreate(:work_package_custom_value,custom_field: list_custom_field,value: list_custom_field.custom_options.first.id)end- let(:types) { [type_standard, type_bug] }- let(:project) do+ shared_let(:types) { [type_standard, type_bug] }+ shared_let(:project) docreate(:project,name: "Foo Bla. Report No. 4/2021 with/for Case 42",types:,work_package_custom_fields: [list_custom_field, text_custom_field_a, text_custom_field_b])end- let(:user) do+ shared_let(:user) docreate(:user,member_with_permissions: { project => %w[view_work_packages export_work_packages] })end- let(:export_time) { DateTime.new(2023, 6, 30, 23, 59) }- let(:export_time_formatted) { format_time(export_time, include_date: true) }- let(:work_package_parent) do+ shared_let(:export_time) { DateTime.new(2023, 6, 30, 23, 59) }+ shared_let(:export_time_formatted) { format_time(export_time, include_date: true) }+ shared_let(:work_package_parent) docreate(:work_package,project:,type: type_standard,subject: "Work package 1",story_points: 1,+ estimated_hours: 10,+ derived_estimated_hours: 20,+ remaining_hours: 7.5,+ derived_remaining_hours: 12.5,done_ratio: 25,- derived_done_ratio: 50,+ derived_done_ratio: 38,description: "This is a description",text_custom_field_a.attribute_name => "Rich text 1.A",text_custom_field_b.attribute_name => "Rich text 1.B",@@ -79,12 +83,14 @@ RSpec.describe WorkPackage::PDFExport::WorkPackageListToPdf dolist_custom_field.value_of("Bar")])end- let(:work_package_child) do+ shared_let(:work_package_child) docreate(:work_package,project:,parent: work_package_parent,type: type_bug,subject: "Work package 2",+ estimated_hours: 10,+ remaining_hours: 5,done_ratio: 50,story_points: 2,description: "This is work package 2",@@ -92,12 +98,9 @@ RSpec.describe WorkPackage::PDFExport::WorkPackageListToPdf dotext_custom_field_b.attribute_name => "Rich text 2.B",list_custom_field.attribute_name => list_custom_field.value_of("Foo"))end- let(:work_packages) do- [work_package_parent, work_package_child]- endlet(:query_attributes) { {} }let!(:query) do- build(:query, user:, project:, **query_attributes).tap do |q|+ build(:query, user:, project:, **query_attributes) do |q|q.column_names = column_namesq.sort_criteria = [%w[id asc]]end@@ -106,7 +109,6 @@ RSpec.describe WorkPackage::PDFExport::WorkPackageListToPdf dolet(:options) { {} }let(:export) dologin_as(user)- work_packagesdescribed_class.new(query, options)endlet(:export_pdf) do@@ -131,7 +133,7 @@ RSpec.describe WorkPackage::PDFExport::WorkPackageListToPdf doenddef work_package_done_ratio(work_package)- work_package.done_ratio == 25 ? "25% · Σ 50%" : "50%"+ work_package.done_ratio == 25 ? "25% · Σ 38%" : "50%"enddef work_package_details(work_package, index, ltfs = [])@@ -218,11 +220,11 @@ RSpec.describe WorkPackage::PDFExport::WorkPackageListToPdf dowork_package_parent.type.name,*column_titles,*work_package_columns(work_package_parent),- I18n.t("js.label_sum"), work_package_parent.story_points.to_s,+ I18n.t("js.label_sum"), work_package_parent.story_points.to_s, "25%",work_package_child.type.name,*column_titles,*work_package_columns(work_package_child),- I18n.t("js.label_sum"), work_package_child.story_points.to_s,+ I18n.t("js.label_sum"), work_package_child.story_points.to_s, "50%","1/1", export_time_formatted, query.name].join(" ")end@@ -237,11 +239,11 @@ RSpec.describe WorkPackage::PDFExport::WorkPackageListToPdf do"Foo",*column_titles,*work_package_columns(work_package_child),- I18n.t("js.label_sum"), work_package_child.story_points.to_s,+ I18n.t("js.label_sum"), work_package_child.story_points.to_s, "50%","Foo, Bar",*column_titles,*work_package_columns(work_package_parent),- I18n.t("js.label_sum"), work_package_parent.story_points.to_s,+ I18n.t("js.label_sum"), work_package_parent.story_points.to_s, "25%","1/1", export_time_formatted, query.name].join(" ")end@@ -334,7 +336,8 @@ RSpec.describe WorkPackage::PDFExport::WorkPackageListToPdf do"1/2", export_time_formatted, query.name,I18n.t("js.work_packages.tabs.overview"),column_title(:story_points),- I18n.t("js.label_sum"), work_packages_sum.to_s,+ column_title(:done_ratio),+ I18n.t("js.label_sum"), work_packages_sum.to_s, "38%",*work_package_details(work_package_parent, "1", long_text_fields),*work_package_details(work_package_child, "2", long_text_fields),"2/2", export_time_formatted, query.name@@ -353,10 +356,10 @@ RSpec.describe WorkPackage::PDFExport::WorkPackageListToPdf do"2.", "2", work_package_child.subject,"1/2", export_time_formatted, query.name,I18n.t("js.work_packages.tabs.overview"),- column_title(:type), column_title(:story_points),- work_package_parent.type.name, work_package_parent.story_points.to_s,- work_package_child.type.name, work_package_child.story_points.to_s,- I18n.t("js.label_sum"), work_packages_sum.to_s,+ column_title(:type), column_title(:story_points), column_title(:done_ratio),+ work_package_parent.type.name, work_package_parent.story_points.to_s, "25%",+ work_package_child.type.name, work_package_child.story_points.to_s, "50%",+ I18n.t("js.label_sum"), work_packages_sum.to_s, "38%",*work_package_details(work_package_parent, "1", long_text_fields),*work_package_details(work_package_child, "2", long_text_fields),"2/2", export_time_formatted, query.name@@ -375,11 +378,11 @@ RSpec.describe WorkPackage::PDFExport::WorkPackageListToPdf do"2.", "2", work_package_parent.subject,"1/2", export_time_formatted, query.name,I18n.t("js.work_packages.tabs.overview"),- list_custom_field.name, column_title(:story_points),+ list_custom_field.name, column_title(:story_points), column_title(:done_ratio),- "Foo", work_package_child.story_points.to_s,- "Foo, Bar", work_package_parent.story_points.to_s,- I18n.t("js.label_sum"), work_packages_sum.to_s,+ "Foo", work_package_child.story_points.to_s, "50%",+ "Foo, Bar", work_package_parent.story_points.to_s, "25%",+ I18n.t("js.label_sum"), work_packages_sum.to_s, "38%",*work_package_details(work_package_child, "1", long_text_fields),*work_package_details(work_package_parent, "2", long_text_fields),diff --git a/spec/requests/api/v3/work_packages/by_project_index_resource_spec.rb b/spec/requests/api/v3/work_packages/by_project_index_resource_spec.rbindex 9a36620383b..797a6616d44 100644--- a/spec/requests/api/v3/work_packages/by_project_index_resource_spec.rb+++ b/spec/requests/api/v3/work_packages/by_project_index_resource_spec.rb@@ -32,9 +32,9 @@ RSpec.describe API::V3::WorkPackages::WorkPackagesByProjectAPI, content_type: :jinclude Rack::Test::Methodsinclude API::V3::Utilities::PathHelper+ shared_let(:project) { create(:project_with_types, public: false) }let(:role) { create(:project_role, permissions:) }let(:permissions) { [:view_work_packages] }- let(:project) { create(:project_with_types, public: false) }let(:base_path) { api_v3_paths.work_packages_by_project project.id }let(:query) { {} }let(:path) { "#{base_path}?#{query.to_query}" }@@ -210,11 +210,115 @@ RSpec.describe API::V3::WorkPackages::WorkPackagesByProjectAPI, content_type: :jlaborCosts: "0.00 EUR",materialCosts: "0.00 EUR",overallCosts: "0.00 EUR",+ percentageDone: nil,remainingTime: nil,storyPoints: nil}expect(subject.body).to be_json_eql(expected.to_json).at_path("totalSums")end++ describe "percentageDone/done_ratio sum" do+ shared_let(:work_package) { create(:work_package, project:) }+ shared_let(:work_packages) { [work_package] }++ subject(:percentage_done_sum) { JSON.parse(last_response.body)["totalSums"]["percentageDone"] }++ context "when work sum and remaining work sum are not set" do+ it "is not set" do+ expect(percentage_done_sum).to be_nil+ end+ end++ context "when work sum and remaining work sum are set to valid values (W=10h, RW=4h)" do+ before_all do+ work_package.update_columns(estimated_hours: 10, remaining_hours: 4)+ end++ it "is calculated from them (60%)" do+ expect(percentage_done_sum).to eq 60+ end+ end++ context "when only work sum is set" do+ before_all do+ work_package.update_columns(estimated_hours: 10)+ end++ it "is not set" do+ expect(percentage_done_sum).to be_nil+ end+ end++ context "when only remaining work sum is set" do+ before_all do+ work_package.update_columns(remaining_hours: 4)+ end++ it "is not set" do+ expect(percentage_done_sum).to be_nil+ end+ end++ context "when work sum and remaining work sum are 0h" do+ before_all do+ work_package.update_columns(estimated_hours: 0, remaining_hours: 0)+ end++ it "is not set" do+ expect(percentage_done_sum).to be_nil+ end+ end++ context "when remaining work sum is greater than work sum (bad data, like W=10h RW=15h)" do+ before_all do+ work_package.update_columns(estimated_hours: 10, remaining_hours: 15)+ end++ it "is set to 0% (and not -50%)" do+ expect(percentage_done_sum).to eq 0+ end+ end++ context "when remaining work sum is 0h and work sum is positive" do+ before_all do+ work_package.update_columns(estimated_hours: 10, remaining_hours: 0)+ end++ it "is set to 100%" do+ expect(percentage_done_sum).to eq 100+ end+ end++ context "when calculated % complete sum is xx.5% (like 50.5% or 42.5%)" do+ before_all do+ work_package.update_columns(estimated_hours: 1000, remaining_hours: 495)+ end++ it "is rounded up (like 51% or 43%)" do+ expect(percentage_done_sum).to eq 51+ end+ end++ context "when calculated % complete sum is almost 0% (like 0.4% or 0.01%)" do+ before_all do+ work_package.update_columns(estimated_hours: 1000, remaining_hours: 999)+ end++ it "is rounded to 1% (because 0% would be wrong)" do+ expect(percentage_done_sum).to eq 1+ end+ end++ context "when % complete sum is almost 100% (like 99.5% or 99.99%)" do+ before_all do+ work_package.update_columns(estimated_hours: 1000, remaining_hours: 1)+ end++ it "is rounded to 99% (because 100% would be wrong)" do+ expect(percentage_done_sum).to eq 99+ end+ end+ endendend