mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
update specs; add specs to validate weight calculator preference is kg or lb
This commit is contained in:
@@ -3,7 +3,6 @@ require 'spree/localized_number'
|
||||
module Calculator
|
||||
class Weight < Spree::Calculator
|
||||
extend Spree::LocalizedNumber
|
||||
|
||||
preference :unit_from_list, :string, default: "kg"
|
||||
preference :per_unit, :decimal, default: 0.0
|
||||
|
||||
@@ -13,6 +12,14 @@ module Calculator
|
||||
I18n.t('spree.weight')
|
||||
end
|
||||
|
||||
def set_preference(name, value)
|
||||
if name == :unit_from_list && !["kg", "lb"].include?(value)
|
||||
self.calculable.errors.add(:preferred_unit_from_list, "must be kg or lb")
|
||||
else
|
||||
send self.class.preference_setter_method(name), value
|
||||
end
|
||||
end
|
||||
|
||||
def compute(object)
|
||||
line_items = line_items_for object
|
||||
(total_weight(line_items) * preferred_per_unit).round(2)
|
||||
@@ -76,9 +83,9 @@ module Calculator
|
||||
def convert_weight(value)
|
||||
return 0 unless value
|
||||
|
||||
if preferences[:unit] == "kg"
|
||||
if preferences[:unit_from_list] == "kg"
|
||||
value / 1000
|
||||
elsif preferences[:unit] == "lb"
|
||||
elsif preferences[:unit_from_list] == "lb"
|
||||
value / 453.6
|
||||
else
|
||||
raise "Unknown unit preference"
|
||||
|
||||
@@ -15,7 +15,7 @@ describe Calculator::Weight do
|
||||
order = double(:order, line_items: [line_item1, line_item2, line_item3])
|
||||
|
||||
subject.set_preference(:per_unit, 5)
|
||||
subject.set_preference(:unit, "kg")
|
||||
subject.set_preference(:unit_from_list, "kg")
|
||||
expect(subject.compute(order)).to eq(350) # (10 * 1 + 20 * 3) * 5
|
||||
end
|
||||
|
||||
@@ -25,7 +25,7 @@ describe Calculator::Weight do
|
||||
|
||||
before {
|
||||
subject.set_preference(:per_unit, 5)
|
||||
subject.set_preference(:unit, "kg")
|
||||
subject.set_preference(:unit_from_list, "kg")
|
||||
}
|
||||
|
||||
it "computes shipping cost for a line item" do
|
||||
@@ -60,7 +60,7 @@ describe Calculator::Weight do
|
||||
object_with_order = double(:object_with_order, order: order)
|
||||
|
||||
subject.set_preference(:per_unit, 5)
|
||||
subject.set_preference(:unit, "kg")
|
||||
subject.set_preference(:unit_from_list, "kg")
|
||||
expect(subject.compute(object_with_order)).to eq(250) # (10 * 1 + 20 * 2) * 5
|
||||
end
|
||||
|
||||
@@ -68,7 +68,7 @@ describe Calculator::Weight do
|
||||
let!(:product) { create(:product, product_attributes) }
|
||||
let!(:variant) { create(:variant, variant_attributes.merge(product: product)) }
|
||||
|
||||
let(:calculator) { described_class.new(preferred_per_unit: 6, preferred_unit: "kg") }
|
||||
let(:calculator) { described_class.new(preferred_per_unit: 6, preferred_unit_from_list: "kg") }
|
||||
let(:line_item) do
|
||||
create(:line_item, variant: variant, quantity: 2).tap do |object|
|
||||
object.send(:calculate_final_weight_volume)
|
||||
@@ -165,7 +165,7 @@ describe Calculator::Weight do
|
||||
|
||||
before {
|
||||
subject.set_preference(:per_unit, 5)
|
||||
subject.set_preference(:unit, "kg")
|
||||
subject.set_preference(:unit_from_list, "kg")
|
||||
}
|
||||
|
||||
context "when unit_value is zero variant.weight is present" do
|
||||
@@ -208,4 +208,18 @@ describe Calculator::Weight do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it "allows a preferred_unit of 'kg' and 'lb'" do
|
||||
subject.calculable = build(:shipping_method)
|
||||
subject.set_preference(:per_unit, 5)
|
||||
subject.set_preference(:unit_from_list, "kg")
|
||||
expect(subject.calculable.errors.count).to eq(0)
|
||||
end
|
||||
|
||||
it "does not allow a preferred_unit of anything but 'kg' or 'lb'" do
|
||||
subject.calculable = build(:shipping_method)
|
||||
subject.set_preference(:per_unit, 5)
|
||||
subject.set_preference(:unit_from_list, "kb")
|
||||
expect(subject.calculable.errors.count).to eq(1)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user