Skip to content

Commit

Permalink
Do not swallow error if work is not a number
Browse files Browse the repository at this point in the history
  • Loading branch information
cbliard committed Apr 19, 2024
1 parent 47c725b commit 476e5d6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
10 changes: 8 additions & 2 deletions app/services/work_packages/set_attributes_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,14 @@ def update_done_ratio
end

def round_progress_values
work_package.estimated_hours = work_package.estimated_hours&.round(2)
work_package.remaining_hours = work_package.remaining_hours&.round(2)
rounded = work_package.estimated_hours&.round(2)
if rounded != work_package.estimated_hours
work_package.estimated_hours = rounded
end
rounded = work_package.remaining_hours&.round(2)
if rounded != work_package.remaining_hours
work_package.remaining_hours = rounded
end
end

def update_remaining_hours_from_percent_complete
Expand Down
13 changes: 13 additions & 0 deletions spec/services/work_packages/set_attributes_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,19 @@
"and remaining work is updated and rounded to 2 decimals"
end

context "when work is set to a string" do
let(:call_attributes) { { estimated_hours: "I am a string" } }
let(:expected_attributes) { { estimated_hours: 0.0, remaining_hours: 0.0 } }

it "keeps the original string value in the _before_type_cast method " \
"so that validation can detect it is invalid" do
allow(work_package).to receive(:save)
instance.call(call_attributes)

expect(work_package.estimated_hours_before_type_cast).to eq("I am a string")
end
end

context "when work and remaining work are set" do
let(:call_attributes) { { estimated_hours: 10.0, remaining_hours: 0 } }
let(:expected_attributes) { call_attributes.merge(done_ratio: 100) }
Expand Down

0 comments on commit 476e5d6

Please sign in to comment.