Permalink
Browse files

Added handling of case when variant weight is zero or negative value

  • Loading branch information...
1 parent ae76665 commit 1e25e2b9ab25bb3198fc4b9b616447cc07f544d9 @romul romul committed Oct 9, 2012
Showing with 15 additions and 1 deletion.
  1. +3 −1 app/models/spree/calculator/active_shipping/base.rb
  2. +12 −0 spec/models/weight_limits_spec.rb
@@ -154,8 +154,10 @@ def convert_order_to_weights_array(order)
max_weight = max_weight_for_country(order.ship_address.country)
weights = order.line_items.map do |line_item|
- item_weight = line_item.variant.weight.present? ? line_item.variant.weight : default_weight
+ item_weight = line_item.variant.weight.to_f
+ item_weight = default_weight if item_weight <= 0
item_weight *= multiplier
+
quantity = line_item.quantity
if max_weight <= 0
item_weight * quantity
@@ -12,9 +12,12 @@ module ActiveShipping
let(:line_item_2) { mock_model(Spree::LineItem, :variant_id => 2, :quantity => 4, :variant => mock_model(Spree::Variant, :weight => 5.25)) }
let(:line_item_3) { mock_model(Spree::LineItem, :variant_id => 3, :quantity => 1, :variant => mock_model(Spree::Variant, :weight => 29.0)) }
let(:line_item_4) { mock_model(Spree::LineItem, :variant_id => 4, :quantity => 1, :variant => mock_model(Spree::Variant, :weight => 100.0)) }
+ let(:line_item_5) { mock_model(Spree::LineItem, :variant_id => 5, :quantity => 1, :variant => mock_model(Spree::Variant, :weight => 0.0)) }
+ let(:line_item_6) { mock_model(Spree::LineItem, :variant_id => 5, :quantity => 1, :variant => mock_model(Spree::Variant, :weight => -1.0)) }
let(:order) { mock_model Spree::Order, :number => "R12345", :ship_address => address, :line_items => [ line_item_1, line_item_2, line_item_3 ] }
let(:us_order) { mock_model Spree::Order, :number => "R12347", :ship_address => us_address, :line_items => [ line_item_1, line_item_2, line_item_3 ] }
let(:too_heavy_order) { mock_model Spree::Order, :number => "R12349", :ship_address => us_address, :line_items => [ line_item_3, line_item_4 ] }
+ let(:order_with_invalid_weights) { mock_model Spree::Order, :number => "R12350", :ship_address => us_address, :line_items => [ line_item_5, line_item_6 ] }
let(:international_calculator) { Spree::Calculator::Usps::PriorityMailInternational.new }
@@ -24,6 +27,7 @@ module ActiveShipping
Rails.cache.clear
Spree::ActiveShipping::Config.set(:units => "imperial")
Spree::ActiveShipping::Config.set(:unit_multiplier => 16)
+ Spree::ActiveShipping::Config.set(:default_weight => 1)
end
describe "compute" do
@@ -68,5 +72,13 @@ module ActiveShipping
domestic_calculator.send(:max_weight_for_country, country).should == 70.0*Spree::ActiveShipping::Config[:unit_multiplier]
end
end
+
+ describe "validation of line item weight" do
+ it "should avoid zero weight or negative weight" do
+ weights = domestic_calculator.send :convert_order_to_weights_array, order_with_invalid_weights
+ default_weight = Spree::ActiveShipping::Config[:default_weight] * Spree::ActiveShipping::Config[:unit_multiplier]
+ weights.should == [default_weight, default_weight]
+ end
+ end
end
end

0 comments on commit 1e25e2b

Please sign in to comment.