Update weight calculator and add specs

This commit is contained in:
Kristina Lim
2019-07-23 23:19:37 +08:00
parent 0d6ba90ea1
commit e1fce8304d
2 changed files with 105 additions and 4 deletions

View File

@@ -25,13 +25,21 @@ module Calculator
end
def line_item_weight(line_item)
return 0 if line_item.variant.product.andand.variant_unit != 'weight'
if line_item.final_weight_volume.present?
# Divided by 1000 because grams is the base weight unit and the calculator price is per_kg
line_item.final_weight_volume / 1000
if line_item.variant.product.andand.variant_unit == 'weight'
# Divided by 1000 because grams is the base weight unit and the calculator price is per_kg
line_item.final_weight_volume / 1000.0
else
final_units = (1.0 * line_item.final_weight_volume / line_item.variant.unit_value).round(3)
weight_per_variant(line_item) * final_units
end
else
(line_item.variant.andand.weight || 0) * line_item.quantity
weight_per_variant(line_item) * line_item.quantity
end
end
def weight_per_variant(line_item)
line_item.variant.andand.weight || 0
end
end
end

View File

@@ -55,4 +55,97 @@ describe Calculator::Weight do
subject.set_preference(:per_kg, 10)
expect(subject.compute(object_with_order)).to eq((10 * 1 + 5 * 2) * 10)
end
context "when line item final_weight_volume is set" do
let!(:product) { create(:product, product_attributes) }
let!(:variant) { create(:variant, variant_attributes.merge(product: product)) }
let(:calculator) { described_class.new(preferred_per_kg: 6) }
let(:line_item) do
build(:line_item, variant: variant, quantity: 2).tap do |object|
object.send(:calculate_final_weight_volume)
end
end
context "when the product uses weight unit" do
context "when the product is in g (3g)" do
let!(:product_attributes) { { variant_unit: "weight", variant_unit_scale: 1.0 } }
let!(:variant_attributes) { { unit_value: 300.0, weight: 0.30 } }
it "is correct" do
expect(line_item.final_weight_volume).to eq(600) # 600g
line_item.final_weight_volume = 700 # 700g
expect(calculator.compute(line_item)).to eq(4.2)
end
end
context "when the product is in kg (3kg)" do
let!(:product_attributes) { { variant_unit: "weight", variant_unit_scale: 1_000.0 } }
let!(:variant_attributes) { { unit_value: 3_000.0, weight: 3.0 } }
it "is correct" do
expect(line_item.final_weight_volume).to eq(6_000) # 6kg
line_item.final_weight_volume = 7_000 # 7kg
expect(calculator.compute(line_item)).to eq(42)
end
end
context "when the product is in T (3T)" do
let!(:product_attributes) { { variant_unit: "weight", variant_unit_scale: 1_000_000.0 } }
let!(:variant_attributes) { { unit_value: 3_000_000.0, weight: 3_000.0 } }
it "is correct" do
expect(line_item.final_weight_volume).to eq(6_000_000) # 6T
line_item.final_weight_volume = 7_000_000 # 7T
expect(calculator.compute(line_item)).to eq(42_000)
end
end
end
context "when the product uses volume unit" do
context "when the product is in mL (300mL)" do
let!(:product_attributes) { { variant_unit: "volume", variant_unit_scale: 0.001 } }
let!(:variant_attributes) { { unit_value: 0.3, weight: 0.25 } }
it "is correct" do
expect(line_item.final_weight_volume).to eq(0.6) # 600mL
line_item.final_weight_volume = 0.7 # 700mL
expect(calculator.compute(line_item)).to eq(3.50)
end
end
context "when the product is in L (3L)" do
let!(:product_attributes) { { variant_unit: "volume", variant_unit_scale: 1 } }
let!(:variant_attributes) { { unit_value: 3.0, weight: 2.5 } }
it "is correct" do
expect(line_item.final_weight_volume).to eq(6) # 6L
line_item.final_weight_volume = 7 # 7L
expect(calculator.compute(line_item)).to eq(35.00)
end
end
context "when the product is in kL (3kL)" do
let!(:product_attributes) { { variant_unit: "volume", variant_unit_scale: 1_000 } }
let!(:variant_attributes) { { unit_value: 3_000.0, weight: 2_500.0 } }
it "is correct" do
expect(line_item.final_weight_volume).to eq(6_000) # 6kL
line_item.final_weight_volume = 7_000 # 7kL
expect(calculator.compute(line_item)).to eq(34_995)
end
end
end
context "when the product uses item unit" do
let!(:product_attributes) { { variant_unit: "items", variant_unit_scale: nil, variant_unit: "pc", display_as: "pc" } }
let!(:variant_attributes) { { unit_value: 3.0, weight: 2.5, display_as: "pc" } }
it "is correct" do
expect(line_item.final_weight_volume).to eq(6) # 6 pcs
line_item.final_weight_volume = 7 # 7 pcs
expect(calculator.compute(line_item)).to eq(35.0)
end
end
end
end