From b52c99ac266860867730d7d4ee357825be34f5ad Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Thu, 17 Jan 2019 15:08:47 +0000 Subject: [PATCH 1/3] Delete on_hand test in product spec, this is already covered in product_stock_spec --- spec/models/spree/product_spec.rb | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/spec/models/spree/product_spec.rb b/spec/models/spree/product_spec.rb index 973e2782ad..6f04f97a0c 100644 --- a/spec/models/spree/product_spec.rb +++ b/spec/models/spree/product_spec.rb @@ -711,26 +711,6 @@ module Spree e.variants(true).should be_empty end end - - describe '#on_hand' do - let(:product) { create(:product) } - - context 'when the product has variants' do - before { create(:variant, product: product) } - - it 'returns the sum of the on_hand of its variants' do - expect(product.on_hand).to eq(Float::INFINITY) - end - end - - context 'when the product has no variants' do - before { product.variants.destroy_all } - - it 'returns the on_hand of the master' do - expect(product.on_hand).to eq(product.master.on_hand) - end - end - end end describe "product import" do From 1d60732581df4e7e377660f009ba8e15ee015cc6 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Thu, 17 Jan 2019 16:43:02 +0000 Subject: [PATCH 2/3] Improve readability of payment_method_spec --- spec/models/spree/payment_method_spec.rb | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/spec/models/spree/payment_method_spec.rb b/spec/models/spree/payment_method_spec.rb index 28707876d3..8b62281c26 100644 --- a/spec/models/spree/payment_method_spec.rb +++ b/spec/models/spree/payment_method_spec.rb @@ -28,16 +28,20 @@ module Spree end it "computes the amount of fees" do - pickup = create(:payment_method) order = create(:order) - expect(pickup.compute_amount(order)).to eq 0 - transaction = create(:payment_method, calculator: Calculator::FlatRate.new(preferred_amount: 10)) - expect(transaction.compute_amount(order)).to eq 10 - transaction = create(:payment_method, calculator: Calculator::FlatPercentItemTotal.new(preferred_flat_percent: 10)) - expect(transaction.compute_amount(order)).to eq 0 + + free_payment_method = create(:payment_method) # flat rate calculator with preferred_amount of 0 + expect(free_payment_method.compute_amount(order)).to eq 0 + + flat_rate_payment_method = create(:payment_method, calculator: Calculator::FlatRate.new(preferred_amount: 10)) + expect(flat_rate_payment_method.compute_amount(order)).to eq 10 + + flat_percent_payment_method = create(:payment_method, calculator: Calculator::FlatPercentItemTotal.new(preferred_flat_percent: 10)) + expect(flat_percent_payment_method.compute_amount(order)).to eq 0 + product = create(:product) order.add_variant(product.master) - expect(transaction.compute_amount(order)).to eq 2.0 + expect(flat_percent_payment_method.compute_amount(order)).to eq 2.0 end end end From f896e78e6f85d6f25755a444a4f4fed203ac8132 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Thu, 17 Jan 2019 14:34:15 +0000 Subject: [PATCH 3/3] Set StockLocation.backorderable_default to false in DefaultStockLocation and in StockLocation factory. This makes the default value of variant.on_demand false in all environments and in tests. Additionally, adapt VariantStock.on_demand test and product factory to this change (setting on_hand value in product factory so it's not out of stock by default). --- app/services/default_stock_location.rb | 2 +- spec/factories.rb | 22 ++++++++++++++++------ spec/models/concerns/variant_stock_spec.rb | 2 +- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/app/services/default_stock_location.rb b/app/services/default_stock_location.rb index 9269f17743..dc4499198b 100644 --- a/app/services/default_stock_location.rb +++ b/app/services/default_stock_location.rb @@ -6,7 +6,7 @@ class DefaultStockLocation def self.create! country = Spree::Country.find_by_iso(ENV['DEFAULT_COUNTRY_CODE']) state = country.states.first - Spree::StockLocation.create!(name: NAME, country_id: country.id, state_id: state.id) + Spree::StockLocation.create!(name: NAME, country_id: country.id, state_id: state.id, backorderable_default: false) end def self.destroy_all diff --git a/spec/factories.rb b/spec/factories.rb index 742f943ed7..43b9136437 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -551,19 +551,26 @@ FactoryBot.define do on_hand { 5 } end after(:create) do |product, evaluator| - product.variants.first.tap do |variant| - variant.on_demand = evaluator.on_demand - variant.count_on_hand = evaluator.on_hand - variant.save - end + product.master.on_demand = evaluator.on_demand + product.master.on_hand = evaluator.on_hand + product.variants.first.on_demand = evaluator.on_demand + product.variants.first.on_hand = evaluator.on_hand end end end - FactoryBot.modify do factory :product do + transient do + on_hand { 5 } + end + primary_taxon { Spree::Taxon.first || FactoryBot.create(:taxon) } + + after(:create) do |product, evaluator| + product.master.on_hand = evaluator.on_hand + product.variants.first.on_hand = evaluator.on_hand + end end factory :base_product do @@ -661,6 +668,9 @@ FactoryBot.modify do # Ensures the name attribute is not assigned after instantiating the default location transient { name 'default' } + + # sets the default value for variant.on_demand + backorderable_default false end factory :shipment, class: Spree::Shipment do diff --git a/spec/models/concerns/variant_stock_spec.rb b/spec/models/concerns/variant_stock_spec.rb index 2943b7575b..7dc3080b45 100644 --- a/spec/models/concerns/variant_stock_spec.rb +++ b/spec/models/concerns/variant_stock_spec.rb @@ -135,7 +135,7 @@ describe VariantStock do let(:variant) { build(:variant) } it 'returns stock location default' do - expect(variant.on_demand).to be_truthy + expect(variant.on_demand).to be_falsy end end end