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

calculated value feature specs

Ivan Kuchinf9c15139cf17
diff --git a/spec/features/projects/project_custom_fields/overview_page/dialog/inputs_spec.rb b/spec/features/projects/project_custom_fields/overview_page/dialog/inputs_spec.rb
index 33217723166..4a38c6de25c 100644
--- a/spec/features/projects/project_custom_fields/overview_page/dialog/inputs_spec.rb
+++ b/spec/features/projects/project_custom_fields/overview_page/dialog/inputs_spec.rb
@@ -220,6 +220,44 @@ RSpec.describe "Edit project custom fields on project overview page", :js do
it_behaves_like "a rich text custom field input"
end
+
+ describe "with calculated value CFs" do
+ shared_examples "a calculated value custom field input" do
+ it "shows the correct value if given" do
+ overview_page.open_edit_dialog_for_section(section)
+
+ dialog.within_async_content(close_after_yield: true) do
+ expect(page).to have_field(custom_field.name, disabled: true, with: expected_initial_value)
+ end
+ end
+
+ it "shows a blank input if no value is given" do
+ custom_field.custom_values.destroy_all
+
+ overview_page.open_edit_dialog_for_section(section)
+
+ dialog.within_async_content(close_after_yield: true) do
+ expect(page).to have_field(custom_field.name, disabled: true, with: expected_blank_value)
+ end
+ end
+ end
+
+ describe "using int" do
+ let(:custom_field) { calculated_from_int_project_custom_field }
+ let(:expected_blank_value) { "" }
+ let(:expected_initial_value) { 234 }
+
+ it_behaves_like "a calculated value custom field input"
+ end
+
+ describe "using int and float" do
+ let(:custom_field) { calculated_from_int_and_float_project_custom_field }
+ let(:expected_blank_value) { "" }
+ let(:expected_initial_value) { 15185.088 }
+
+ it_behaves_like "a calculated value custom field input"
+ end
+ end
end
describe "with single select fields" do
diff --git a/spec/features/projects/project_custom_fields/overview_page/dialog/render_spec.rb b/spec/features/projects/project_custom_fields/overview_page/dialog/render_spec.rb
index 8b8853eb9c8..7d2cc3b553b 100644
--- a/spec/features/projects/project_custom_fields/overview_page/dialog/render_spec.rb
+++ b/spec/features/projects/project_custom_fields/overview_page/dialog/render_spec.rb
@@ -125,6 +125,8 @@ RSpec.describe "Edit project custom fields on project overview page", :js do
expect(containers[4].text).to include("Date field")
expect(containers[5].text).to include("Link field")
expect(containers[6].text).to include("Text field")
+ expect(containers[7].text).to include("Calculated field using int")
+ expect(containers[8].text).to include("Calculated field using int and float")
end
boolean_project_custom_field.move_to_bottom
@@ -140,7 +142,9 @@ RSpec.describe "Edit project custom fields on project overview page", :js do
expect(containers[3].text).to include("Date field")
expect(containers[4].text).to include("Link field")
expect(containers[5].text).to include("Text field")
- expect(containers[6].text).to include("Boolean field")
+ expect(containers[6].text).to include("Calculated field using int")
+ expect(containers[7].text).to include("Calculated field using int and float")
+ expect(containers[8].text).to include("Boolean field")
end
end
diff --git a/spec/features/projects/project_custom_fields/overview_page/dialog/update_spec.rb b/spec/features/projects/project_custom_fields/overview_page/dialog/update_spec.rb
index d40ca5de9b3..0b922c8e613 100644
--- a/spec/features/projects/project_custom_fields/overview_page/dialog/update_spec.rb
+++ b/spec/features/projects/project_custom_fields/overview_page/dialog/update_spec.rb
@@ -162,6 +162,63 @@ RSpec.describe "Edit project custom fields on project overview page", :js do
end
end
+ shared_examples "affecting calculated value" do
+ it "calculates value based on referenced value" do
+ overview_page.visit_page
+
+ overview_page.within_custom_field_container(calculated_value_custom_field) do
+ expect(page).to have_content expected_initial_calculated_value
+ end
+
+ overview_page.open_edit_dialog_for_section(section)
+
+ field.fill_in(with: update_value)
+
+ dialog.submit
+ dialog.expect_closed
+
+ overview_page.within_custom_field_container(calculated_value_custom_field) do
+ expect(page).to have_content expected_updated_calculated_value
+ end
+ end
+
+ it "does not recalculate value if referenced value doesn't change" do
+ overview_page.visit_page
+
+ overview_page.within_custom_field_container(calculated_value_custom_field) do
+ expect(page).to have_content expected_initial_calculated_value
+ end
+
+ overview_page.open_edit_dialog_for_section(section)
+
+ # don't touch the input
+
+ dialog.submit
+ dialog.expect_closed
+
+ overview_page.within_custom_field_container(calculated_value_custom_field) do
+ expect(page).to have_content expected_initial_calculated_value
+ end
+ end
+
+ it "blanks the value if referenced value gets removed" do
+ overview_page.within_custom_field_container(calculated_value_custom_field) do
+ expect(page).to have_content expected_initial_calculated_value
+ end
+
+ overview_page.open_edit_dialog_for_section(section)
+
+ field.fill_in(with: "")
+
+ dialog.submit
+ dialog.expect_closed
+
+ overview_page.within_custom_field_container(calculated_value_custom_field) do
+ expect(page).to have_content I18n.t("placeholders.default")
+ end
+ end
+ end
+
shared_examples "a rich text custom field input" do
it "saves the value properly" do
custom_field.custom_values.destroy_all
@@ -246,6 +303,22 @@ RSpec.describe "Edit project custom fields on project overview page", :js do
let(:expected_updated_value) { update_value }
it_behaves_like "a custom field input"
+
+ describe "affecting calculated value using int" do
+ let(:calculated_value_custom_field) { calculated_from_int_project_custom_field }
+ let(:expected_initial_calculated_value) { 234 }
+ let(:expected_updated_calculated_value) { 912 }
+
+ include_examples "affecting calculated value"
+ end
+
+ describe "affecting calculated value using int and float" do
+ let(:calculated_value_custom_field) { calculated_from_int_and_float_project_custom_field }
+ let(:expected_initial_calculated_value) { "15,185.088" }
+ let(:expected_updated_calculated_value) { "56,295.936" }
+
+ include_examples "affecting calculated value"
+ end
end
describe "with float CF" do
@@ -256,6 +329,14 @@ RSpec.describe "Edit project custom fields on project overview page", :js do
let(:expected_updated_value) { update_value }
it_behaves_like "a custom field input"
+
+ describe "affecting calculated value using int and float" do
+ let(:calculated_value_custom_field) { calculated_from_int_and_float_project_custom_field }
+ let(:expected_initial_calculated_value) { "15,185.088" }
+ let(:expected_updated_calculated_value) { "56,185.047" }
+
+ include_examples "affecting calculated value"
+ end
end
describe "with date CF" do
diff --git a/spec/features/projects/project_custom_fields/overview_page/dialog/validation_spec.rb b/spec/features/projects/project_custom_fields/overview_page/dialog/validation_spec.rb
index 0177c83b73a..021ef8d8473 100644
--- a/spec/features/projects/project_custom_fields/overview_page/dialog/validation_spec.rb
+++ b/spec/features/projects/project_custom_fields/overview_page/dialog/validation_spec.rb
@@ -63,6 +63,8 @@ RSpec.describe "Edit project custom fields on project overview page", :js, :sele
expect(containers[4].text).to include("Date field")
expect(containers[5].text).to include("Link field")
expect(containers[6].text).to include("Text field")
+ expect(containers[7].text).to include("Calculated field using int")
+ expect(containers[8].text).to include("Calculated field using int and float")
expect(page).to have_no_text(boolean_project_custom_field_activated_in_other_project.name)
end
@@ -83,6 +85,8 @@ RSpec.describe "Edit project custom fields on project overview page", :js, :sele
expect(containers[4].text).to include("Date field")
expect(containers[5].text).to include("Link field")
expect(containers[6].text).to include("Text field")
+ expect(containers[7].text).to include("Calculated field using int")
+ expect(containers[8].text).to include("Calculated field using int and float")
expect(page).to have_no_text(boolean_project_custom_field_activated_in_other_project.name)
end
@@ -334,6 +338,27 @@ RSpec.describe "Edit project custom fields on project overview page", :js, :sele
it_behaves_like "a custom field input"
end
+
+ describe "with calculated value CFs" do
+ before do
+ # prevent calculation from happening
+ integer_project_custom_field.custom_values.delete_all
+ end
+
+ describe "using int" do
+ let(:custom_field) { calculated_from_int_project_custom_field }
+ let(:field) { FormFields::Primerized::InputField.new(custom_field) }
+
+ it_behaves_like "a custom field input"
+ end
+
+ describe "using int and float" do
+ let(:custom_field) { calculated_from_int_and_float_project_custom_field }
+ let(:field) { FormFields::Primerized::InputField.new(custom_field) }
+
+ it_behaves_like "a custom field input"
+ end
+ end
end
describe "with select fields" do
diff --git a/spec/features/projects/project_custom_fields/overview_page/shared_context.rb b/spec/features/projects/project_custom_fields/overview_page/shared_context.rb
index a2b79a2b2c8..83d4cd8af87 100644
--- a/spec/features/projects/project_custom_fields/overview_page/shared_context.rb
+++ b/spec/features/projects/project_custom_fields/overview_page/shared_context.rb
@@ -177,6 +177,36 @@ RSpec.shared_context "with seeded projects, members and project custom fields" d
field
end
+ let!(:calculated_from_int_project_custom_field) do
+ field = create(
+ :calculated_value_project_custom_field,
+ :skip_validations,
+ formula: "{{cf_#{integer_project_custom_field.id}}} * 2",
+ projects: [project],
+ name: "Calculated field using int",
+ project_custom_field_section: section_for_input_fields
+ )
+
+ create(:custom_value, customized: project, custom_field: field, value: 234)
+
+ field
+ end
+
+ let!(:calculated_from_int_and_float_project_custom_field) do
+ field = create(
+ :calculated_value_project_custom_field,
+ :skip_validations,
+ formula: "{{cf_#{float_project_custom_field.id}}} * {{cf_#{integer_project_custom_field.id}}}",
+ projects: [project],
+ name: "Calculated field using int and float",
+ project_custom_field_section: section_for_input_fields
+ )
+
+ create(:custom_value, customized: project, custom_field: field, value: 123 * 123.456)
+
+ field
+ end
+
let!(:list_project_custom_field) do
field = create(:list_project_custom_field, projects: [project],
name: "List field",
@@ -273,7 +303,14 @@ RSpec.shared_context "with seeded projects, members and project custom fields" d
]
end
- let(:all_fields) { input_fields + select_fields + multi_select_fields }
+ let!(:calculated_value_fields) do
+ [
+ calculated_from_int_project_custom_field,
+ calculated_from_int_and_float_project_custom_field
+ ]
+ end
+
+ let(:all_fields) { input_fields + select_fields + multi_select_fields + calculated_value_fields }
let!(:boolean_project_custom_field_activated_in_other_project) do
create(:boolean_project_custom_field, projects: [other_project],
diff --git a/spec/features/projects/project_custom_fields/overview_page/sidebar_spec.rb b/spec/features/projects/project_custom_fields/overview_page/sidebar_spec.rb
index f4d80a851b5..e26ff82becc 100644
--- a/spec/features/projects/project_custom_fields/overview_page/sidebar_spec.rb
+++ b/spec/features/projects/project_custom_fields/overview_page/sidebar_spec.rb
@@ -84,7 +84,7 @@ RSpec.describe "Show project custom fields on project overview page", :js do
overview_page.within_custom_field_section_container(section_for_input_fields) do
fields = page.all(".op-project-custom-field-container")
- expect(fields.size).to eq(7)
+ expect(fields.size).to eq(9)
expect(fields[0].text).to include("Boolean field")
expect(fields[1].text).to include("String field")
@@ -93,6 +93,8 @@ RSpec.describe "Show project custom fields on project overview page", :js do
expect(fields[4].text).to include("Date field")
expect(fields[5].text).to include("Link field")
expect(fields[6].text).to include("Text field")
+ expect(fields[7].text).to include("Calculated field using int")
+ expect(fields[8].text).to include("Calculated field using int and float")
end
overview_page.within_custom_field_section_container(section_for_select_fields) do
@@ -124,7 +126,7 @@ RSpec.describe "Show project custom fields on project overview page", :js do
overview_page.within_custom_field_section_container(section_for_input_fields) do
fields = page.all(".op-project-custom-field-container")
- expect(fields.size).to eq(7)
+ expect(fields.size).to eq(9)
expect(fields[0].text).to include("Boolean field")
expect(fields[1].text).to include("Integer field")
@@ -132,7 +134,9 @@ RSpec.describe "Show project custom fields on project overview page", :js do
expect(fields[3].text).to include("Date field")
expect(fields[4].text).to include("Link field")
expect(fields[5].text).to include("Text field")
- expect(fields[6].text).to include("String field")
+ expect(fields[6].text).to include("Calculated field using int")
+ expect(fields[7].text).to include("Calculated field using int and float")
+ expect(fields[8].text).to include("String field")
end
end
end
@@ -571,6 +575,72 @@ RSpec.describe "Show project custom fields on project overview page", :js do
end
end
+ describe "with calculated value CFs" do
+ describe "with value set by user" do
+ it "shows the correct value for the project custom field if given" do
+ overview_page.visit_page
+
+ overview_page.within_project_attributes_sidebar do
+ overview_page.within_custom_field_container(calculated_from_int_project_custom_field) do
+ expect(page).to have_text "Calculated field using int"
+ expect(page).to have_text "234"
+ end
+
+ overview_page.within_custom_field_container(calculated_from_int_and_float_project_custom_field) do
+ expect(page).to have_text "Calculated field using int and float"
+ expect(page).to have_text "15,185.088"
+ end
+ end
+ end
+ end
+
+ describe "with value unset by user" do
+ before do
+ calculated_from_int_project_custom_field.custom_values.where(customized: project).first.update!(value: "")
+ calculated_from_int_and_float_project_custom_field.custom_values.where(customized: project).first.update!(value: "")
+ end
+
+ it "shows the correct value for the project custom field if given" do
+ overview_page.visit_page
+
+ overview_page.within_project_attributes_sidebar do
+ overview_page.within_custom_field_container(calculated_from_int_project_custom_field) do
+ expect(page).to have_text "Calculated field using int"
+ expect(page).to have_text I18n.t("placeholders.default")
+ end
+
+ overview_page.within_custom_field_container(calculated_from_int_and_float_project_custom_field) do
+ expect(page).to have_text "Calculated field using int and float"
+ expect(page).to have_text I18n.t("placeholders.default")
+ end
+ end
+ end
+ end
+
+ describe "with no value set by user" do
+ before do
+ calculated_from_int_project_custom_field.custom_values.where(customized: project).destroy_all
+ calculated_from_int_and_float_project_custom_field.custom_values.where(customized: project).destroy_all
+ end
+
+ it "shows an N/A text for the project custom field if no value given" do
+ overview_page.visit_page
+
+ overview_page.within_project_attributes_sidebar do
+ overview_page.within_custom_field_container(calculated_from_int_project_custom_field) do
+ expect(page).to have_text "Calculated field using int"
+ expect(page).to have_text I18n.t("placeholders.default")
+ end
+
+ overview_page.within_custom_field_container(calculated_from_int_and_float_project_custom_field) do
+ expect(page).to have_text "Calculated field using int and float"
+ expect(page).to have_text I18n.t("placeholders.default")
+ end
+ end
+ end
+ end
+ end
+
describe "with list CF" do
describe "with value set by user" do
it "shows the correct value for the project custom field if given" do