Skip to content

Commit

Permalink
Raise exception if dividing by 0
Browse files Browse the repository at this point in the history
While this should never happen it is possible to call this method
and subsequently try to save a NaN value if the line_item has
already been fully cancelled.
  • Loading branch information
ericsaupe committed Aug 22, 2019
1 parent 631b205 commit f0732cc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
5 changes: 4 additions & 1 deletion core/app/models/spree/unit_cancel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ def compute_amount(line_item)
private

def weighted_line_item_amount(line_item)
line_item.total_before_tax / quantity_of_line_item(line_item)
quantity_of_line_item = quantity_of_line_item(line_item)
raise ZeroDivisionError, "Line Item does not have any inventory units available to cancel" if quantity_of_line_item.zero?

line_item.total_before_tax / quantity_of_line_item
end

def quantity_of_line_item(line_item)
Expand Down
5 changes: 5 additions & 0 deletions core/spec/models/spree/unit_cancel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@
it "divides the line item total by the uncanceled units size" do
expect(subject).to eq(-10.0)
end

it "raises an error if dividing by 0" do
inventory_unit.cancel!
expect { subject }.to raise_error ZeroDivisionError, "Line Item does not have any inventory units available to cancel"
end
end

context "it is called with a line item that doesnt belong to the inventory unit" do
Expand Down

0 comments on commit f0732cc

Please sign in to comment.