mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-08 22:56:06 +00:00
Improve variant stock specs' performance
This commit is contained in:
@@ -52,7 +52,7 @@ describe VariantStock do
|
||||
end
|
||||
|
||||
context 'when the variant has no stock item' do
|
||||
let(:variant) { build(:variant) }
|
||||
let(:variant) { build_stubbed(:variant) }
|
||||
|
||||
it 'raises' do
|
||||
expect { variant.on_hand = 3 }
|
||||
@@ -78,20 +78,25 @@ describe VariantStock do
|
||||
end
|
||||
|
||||
context 'when the stock items is not backorderable' do
|
||||
before do
|
||||
variant.stock_items.first.update_attribute(
|
||||
:backorderable, false
|
||||
)
|
||||
end
|
||||
|
||||
it 'returns false' do
|
||||
variant = build_stubbed(
|
||||
:variant,
|
||||
stock_locations: [build_stubbed(:stock_location)]
|
||||
)
|
||||
expect(variant.on_demand).to be_falsy
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the variant has not been saved yet' do
|
||||
let(:variant) { build(:variant) }
|
||||
let(:variant) do
|
||||
build_stubbed(
|
||||
:variant,
|
||||
stock_locations: [
|
||||
build_stubbed(:stock_location, backorderable_default: false)
|
||||
]
|
||||
)
|
||||
end
|
||||
|
||||
it 'has no stock items' do
|
||||
expect(variant.stock_items.count).to eq 0
|
||||
@@ -127,7 +132,7 @@ describe VariantStock do
|
||||
end
|
||||
|
||||
context 'when the variant has no stock item' do
|
||||
let(:variant) { build(:variant) }
|
||||
let(:variant) { build_stubbed(:variant) }
|
||||
|
||||
it 'raises' do
|
||||
expect { variant.on_demand = 3 }.to raise_error(StandardError)
|
||||
@@ -137,11 +142,23 @@ describe VariantStock do
|
||||
|
||||
describe '#can_supply?' do
|
||||
context 'when variant on_demand' do
|
||||
let(:variant) { create(:variant, on_demand: true) }
|
||||
let(:variant) do
|
||||
build_stubbed(
|
||||
:variant,
|
||||
on_demand: true,
|
||||
stock_locations: [build_stubbed(:stock_location)]
|
||||
)
|
||||
end
|
||||
let(:stock_item) { Spree::StockItem.new(backorderable: true) }
|
||||
|
||||
before do
|
||||
allow(variant).to receive(:stock_items).and_return([stock_item])
|
||||
end
|
||||
|
||||
it "returns true for zero" do
|
||||
expect(variant.can_supply?(0)).to eq(true)
|
||||
end
|
||||
|
||||
it "returns true for large quantity" do
|
||||
expect(variant.can_supply?(100_000)).to eq(true)
|
||||
end
|
||||
@@ -149,9 +166,18 @@ describe VariantStock do
|
||||
|
||||
context 'when variant not on_demand' do
|
||||
context 'when variant in stock' do
|
||||
let(:variant) do
|
||||
build_stubbed(
|
||||
:variant,
|
||||
on_demand: false,
|
||||
stock_locations: [build_stubbed(:stock_location)]
|
||||
)
|
||||
end
|
||||
|
||||
it "returns true for zero" do
|
||||
expect(variant.can_supply?(0)).to eq(true)
|
||||
end
|
||||
|
||||
it "returns true for number equal to stock level" do
|
||||
expect(variant.can_supply?(variant.total_on_hand)).to eq(true)
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user