Handle all line items without unit_value in weight calculation

This commit is contained in:
Maikel Linke
2020-03-26 17:13:29 +11:00
parent 24c8f38111
commit 4bcd665379
2 changed files with 28 additions and 4 deletions

View File

@@ -54,7 +54,7 @@ module Calculator
# Customer ends up getting 350mL (line_item.final_weight_volume) of wine
# that represent 2.8 (quantity_implied_in_final_weight_volume) glasses of wine
def quantity_implied_in_final_weight_volume(line_item)
return line_item.quantity if line_item.variant.unit_value.zero?
return line_item.quantity if line_item.variant.unit_value.to_f.zero?
(1.0 * line_item.final_weight_volume / line_item.variant.unit_value).round(3)
end

View File

@@ -152,7 +152,7 @@ describe Calculator::Weight do
end
end
context "when variant_unit is 'items' and unit_value is zero" do
context "when variant_unit is 'items'" do
let(:product) {
create(:product, variant_unit: 'items', variant_unit_scale: nil, variant_unit_name: "bunch")
}
@@ -160,7 +160,7 @@ describe Calculator::Weight do
before { subject.set_preference(:per_kg, 5) }
context "when variant.weight is present" do
context "when unit_value is zero variant.weight is present" do
let(:variant) { create(:variant, product: product, unit_value: 0, weight: 10.0) }
it "uses the variant weight" do
@@ -168,12 +168,36 @@ describe Calculator::Weight do
end
end
context "when variant.weight is nil" do
context "when unit_value is zero variant.weight is nil" do
let(:variant) { create(:variant, product: product, unit_value: 0, weight: nil) }
it "uses zero weight" do
expect(subject.compute(line_item)).to eq 0
end
end
context "when unit_value is nil and variant.weight is present" do
let(:variant) {
create(:variant, product: product, unit_description: "bunches", unit_value: nil, weight: 10.0)
}
it "uses the variant weight" do
line_item.final_weight_volume = 1
expect(subject.compute(line_item)).to eq 50.0
end
end
context "when unit_value is nil and variant.weight is nil" do
let(:variant) {
create(:variant, product: product, unit_description: "bunches", unit_value: nil, weight: nil)
}
it "uses zero weight" do
line_item.final_weight_volume = 1
expect(subject.compute(line_item)).to eq 0
end
end
end
end