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
[#64348] Fixup specs for Calculated Values
Tobias Dillmann9338922f56f0
diff --git a/app/models/custom_field/calculated_value.rb b/app/models/custom_field/calculated_value.rbindex 26fedbe8c36..eed19b772b7 100644--- a/app/models/custom_field/calculated_value.rb+++ b/app/models/custom_field/calculated_value.rb@@ -134,7 +134,7 @@ module CustomField::CalculatedValue# Returns a list of custom field IDs used in the formula.# For a formula like `2 + {{cf_12}} + {{cf_4}}` it returns `[12, 4]`.def cf_ids_used_in_formula(formula_str)- formula_str.scan(/\b\{\{cf_(\d+)}}\b/).flatten.compact.map(&:to_i)+ formula_str.scan(/\{\{cf_(\d+)}}/).flatten.compact.map(&:to_i)enddef formula_str_without_patternsdiff --git a/spec/features/admin/custom_fields/projects/calculated_value_spec.rb b/spec/features/admin/custom_fields/projects/calculated_value_spec.rbindex bbf28fd1a91..e98cc6d91e9 100644--- a/spec/features/admin/custom_fields/projects/calculated_value_spec.rb+++ b/spec/features/admin/custom_fields/projects/calculated_value_spec.rb@@ -77,7 +77,7 @@ RSpec.describe "Edit project custom field calculated value", :js, with_flag: { cfill_in("custom_field_name", with: "Updated name", fill_options: { clear: :backspace })select(section_for_select_fields.name, from: "custom_field_custom_field_section_id")- fill_in("custom_field_formula", with: "1 + 1", fill_options: { clear: :backspace })+ find_field(id: "custom_field_formula", type: :hidden).set("1 + 1")click_on "Save"@@ -114,7 +114,7 @@ RSpec.describe "Edit project custom field calculated value", :js, with_flag: { cit "prevents saving a calculated value with an empty formula" dooriginal_formula = calculated_value.formula_string- fill_in("custom_field_formula", with: "")+ find_field(id: "custom_field_formula", type: :hidden).set("")click_on "Save"expect(page).to have_text("Formula can't be blank")@@ -133,7 +133,7 @@ RSpec.describe "Edit project custom field calculated value", :js, with_flag: { cvisit new_admin_settings_project_custom_field_path(field_format: "calculated_value",custom_field_section_id: section_for_input_fields.id)fill_in("custom_field_name", with: "New calculated value")- fill_in("custom_field_formula", with: "1 + 1")+ find_field(id: "custom_field_formula", type: :hidden).set("1 + 1")click_on "Save"expect(page).to have_text("Format is not set to one of the allowed values.")diff --git a/spec/models/custom_field/calculated_value_spec.rb b/spec/models/custom_field/calculated_value_spec.rbindex 859dd9aed38..d32dca547b9 100644--- a/spec/models/custom_field/calculated_value_spec.rb+++ b/spec/models/custom_field/calculated_value_spec.rb@@ -34,11 +34,16 @@ RSpec.describe CustomField::CalculatedValue, with_flag: { calculated_value_projesubject(:custom_field) { create(:calculated_value_project_custom_field, formula: "1 + 1") }describe "#formula=" do+ let!(:int) { create(:project_custom_field, :integer, default_value: 2, is_for_all: true) }+ let!(:float) { create(:project_custom_field, :float, default_value: 3.5, is_for_all: true) }++ current_user { create(:admin) }+it "splits formula and referenced custom fields on persist if given a string" do- formula = "1 * cf_7 + cf_42"+ formula = "1 * {{cf_#{int.id}}} + {{cf_#{float.id}}}"subject.formula = formula- expect(subject.formula).to eq({ "formula" => formula, "referenced_custom_fields" => [7, 42] })+ expect(subject.formula).to eq({ "formula" => formula, "referenced_custom_fields" => [int.id, float.id] })endit "omits referenced custom fields if none are given" do@@ -56,7 +61,7 @@ RSpec.describe CustomField::CalculatedValue, with_flag: { calculated_value_projeendit "returns the formula as a string" do- formula = "1 * cf_7 + cf_42"+ formula = "1 * {{cf_7}} + {{cf_42}}"subject.formula = formulaexpect(subject.formula_string).to eq(formula)@@ -118,11 +123,5 @@ RSpec.describe CustomField::CalculatedValue, with_flag: { calculated_value_projeit_behaves_like "invalid formula", "is invalid."end-- context "with a formula that causes a division by zero error" do- let(:formula) { "1 / 0" }-- it_behaves_like "invalid formula", "is invalid."- endendend