spec/lib/post_spec.rb in post_clerk-0.4 vs spec/lib/post_spec.rb in post_clerk-0.5

- old
+ new

@@ -1,115 +1,114 @@ +require "spec_helper" + RSpec.describe OfficeClerk::Post do - subject { described_class.new } + describe 'using the default weight-price table: [1 2 5 10 20] => [2 5 10 15 18]' do + context '.handling fee' do + it 'gives handling fee plus min price with defaults and 0 items' do + basket = create :basket + result = price_for_basket(basket , :handling_fee => 10) + expect(result).to eq(12.0) + end - context 'returns description' do - %w(en fi).each do |locale| - it "in supported language: #{locale}" do - I18n.with_locale(locale.to_sym) do - expect(described_class.description).to eq I18n.t(:postal_service) - end + it 'gives handling fee plus min price with defaults and 1 items' do + basket = create :basket_with_item + result = price_for_basket(basket , :handling_fee => 5) + expect(result).to eq(7.0) end - end - end - describe 'using the default weight-price table: [1 2 5 10 20] => [6 9 12 15 18]' do - context '.compute(package)' do - it 'gives 15.0 when total price is 100 and weight is 10kg' do - create_our_package(weight: 10.0, price: 100.0, quantity: 1) - result = subject.compute(@basket) + it 'gives no handling fee just min price with defaults and 1 items' do + basket = create :basket_with_item + result = price_for_basket(basket ) + expect(result).to eq(2.0) + end + end + context "free shipping " do + it "gives 0 for more than 100 cost, default setting " do + basket = basket_with({:weight => 10.0} , { price: 110.0} ) + result = price_for_basket(basket) + expect(result).to eq(0.0) + end + it "gives non 0 for less than 100 cost, default setting " do + basket = basket_with({:weight => 10.0} , { price: 90.0} ) + result = price_for_basket(basket) expect(result).to eq(15.0) end - - it 'gives 25.0 when total price is 40 and weight is 10kg' do - create_our_package(weight: 10.0, price: 40.0, quantity: 1) - result = subject.compute(@basket) - expect(result).to eq(25.0) + it 'gives 0 when total price is more than the MAX, for one item (different max)' do + basket = basket_with({:weight => 15.0} , {:price => 350} ) + result = price_for_basket(basket , :max_price => 300.0) + expect(result).to eq(0.0) end + end + context '.price_for(basket)' do + it 'gives next price for 1.5 kg item => 5' do + basket = basket_with({:weight => 1.5} ) + result = price_for_basket(basket) + expect(result).to eq(5.0) + end - it 'gives 6 when total price is 60 and weight is less than 1kg' do - create_our_package(weight: 0.5, price: 60.0, quantity: 1) - result = subject.compute(@basket) - expect(result).to eq(6.0) + it 'gives next price for 2.5 kg item => 10' do + basket = basket_with({:weight => 2.5} ) + result = price_for_basket(basket) + expect(result).to eq(10.0) end - it 'gives 16 when total price is 40 and weight is less than 1kg' do - create_our_package(weight: 0.5, price: 40.0, quantity: 1) - result = subject.compute(@basket) - expect(result).to eq(16.0) + it 'gives 15.0 when total price is 100 and weight is 10kg' do + basket = basket_with({:weight => 10.0} ) + result = price_for_basket(basket) + expect(result).to eq(15.0) end - it 'gives 30 when total price is 200 and weight is 25kg (split into two)' do - create_our_package(weight: 25.0, price: 200.0, quantity: 1) - subject.preferred_max_price = 250 - result = subject.compute(@basket) - expect(result).to eq(30.0) + it 'gives 15.0 when total price is 40 and weight is 10kg' do + basket = basket_with({:weight => 10.0} , { price: 40.0} ) + result = price_for_basket(basket) + expect(result).to eq(15.0) end - it 'gives 12 when total price is 100, there are three items and their weight is unknown' do - order = create(:order) - variant = create(:base_variant, weight: nil) - [30.0, 40.0, 30.0].each { |price| create_line_item(order, variant, price: price) } - order.line_items.reload - package = create(:shipment, order: order) - result = subject.compute(package) - expect(result).to eq(12.0) + it 'gives 6 when total price is 60 and weight is less than 1kg' do + basket = basket_with({:weight => 0.5} , { price: 60.0} ) + result = price_for_basket(basket) + expect(result).to eq(2.0) end - it 'gives 0 when total price is more than the MAX, for any number of items' do - create_our_package(weight: 25.0, price: 350.0, quantity: 1) - subject.preferred_max_price = 300 - result = subject.compute(@basket) - expect(result).to eq(0.0) + it 'gives 30 when total price is 200 and weight is 25kg (split into two)' do + basket = basket_with({:weight => 25.0} , {:price => 200.0} ) + result = price_for_basket(basket , :max_price => 250) + expect(result).to eq(28.0) end end end - describe 'when preferred max weight, length and width are 18 kg, 120 cm and 60 cm' do + describe 'when preferred max is 20 kg' do context '.available?(package)' do - it 'is false when item weighs more than 18kg' do - create_our_package(weight: 20, height: 70, width: 30, depth: 30) - expect(subject.available?(@basket)).to be(false) + it 'is false when item weighs more than 20kg' do + basket = basket_with(:weight => 25 ) + expect(OfficeClerk::Post.new({}).available?(basket)).to be(false) end - - it 'is false when item is longer than 120cm' do - create_our_package(weight: 10, height: 130, width: 30, depth: 30) - expect(subject.available?(@basket)).to be(false) - end - - it 'is false when item is wider than 60cm' do - create_our_package(weight: 10, height: 80, width: 70, depth: 30) - expect(subject.available?(@basket)).to be(false) - end end + end + + def basket_with product = {}, item = {} + basket = create :basket_with_item + basket.items.first.product.update_attributes!(product) + basket.items.first.update_attributes!(item) + basket.cache_total #TODO after save hook does not work to automatically update the totals + basket + end + def price_for_basket(basket , args = {}) + OfficeClerk::Post.new(args).price_for(basket) + end - context '.item_oversized?(variant)' do - it 'is true if the longest side is more than 120cm' do - create_our_package(weight: 10, height: 130, width: 40, depth: 30) - expect(subject.item_oversized?(@variant)).to be(true) + context 'returns description' do + %w(en fi).each do |locale| + it "in supported language: #{locale}" do + I18n.with_locale(locale.to_sym) do + OfficeClerk::ShippingMethod.all.each do |name , method| + expect(method.description).not_to be_blank + expect(method.description).not_to include("missing") + end + end end - - it 'is true if the second longest side is more than 60cm' do - create_our_package(weight: 10, height: 80, width: 70, depth: 30) - expect(subject.item_oversized?(@variant)).to be(true) - end end end - def create_our_package(args = {}) - @variant = create :base_variant, args.except!(:quantity) - order = create :order - create_line_item(order, @variant, args) - order.line_items.reload - @basket = create :shipment, order: order - end - - def create_line_item(order, variant, args = {}) - create( - :line_item, - price: args[:price] || BigDecimal.new('10.00'), - quantity: args[:quantity] || 1, - order: order, - variant: variant - ) - end end