Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix stock availability bug fixes #162 #163

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/models/spree/line_item_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def quantity_with_part_line_items(quantity)
end

def quantity_without_part_line_items(quantity)
product.assemblies_parts.each_with_object({}) do |ap, hash|
variant.parts_variants.each_with_object({}) do |ap, hash|
hash[ap.part] = ap.count * quantity
end
end
Expand Down
101 changes: 83 additions & 18 deletions spec/models/spree/line_item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,100 @@ module Spree
context "bundle parts stock" do
let(:parts) { (1..2).map { create(:variant) } }

before { product.master.parts << parts }
context 'assembly without variants' do
before { product.master.parts << parts }

context "one of them not in stock" do
before do
part = product.parts.first
part.stock_items.update_all backorderable: false
context "one of them not in stock" do
before do
part = product.parts.first
part.stock_items.update_all backorderable: false

expect(part).not_to be_in_stock
end

expect(part).not_to be_in_stock
it "doesn't save line item quantity" do
expect { order.contents.add(variant, 10) }.to(
raise_error ActiveRecord::RecordInvalid
)
end
end

it "doesn't save line item quantity" do
expect { order.contents.add(variant, 10) }.to(
raise_error ActiveRecord::RecordInvalid
)
context "in stock" do
before do
parts.each do |part|
part.stock_items.first.set_count_on_hand(10)
end
expect(parts[0]).to be_in_stock
expect(parts[1]).to be_in_stock
end

it "saves line item quantity" do
line_item = order.contents.add(variant, 10)
expect(line_item).to be_valid
end
end
end

context "in stock" do
context 'multi-variant assembly' do
let(:assembly1) { variant }
let(:assembly2) { create(:variant) }

let(:parts2) { (1..2).map { create(:variant) } }

before do
parts.each do |part|
part.stock_items.first.set_count_on_hand(10)
product.variants << assembly1
product.variants << assembly2

assembly1.parts << parts
assembly2.parts << parts2
end

context "one part of desired assembly variant not in stock" do
before do
part = product.parts.first
part.stock_items.update_all backorderable: false

expect(part).not_to be_in_stock
end

it "doesn't save line item quantity" do
expect { order.contents.add(variant, 10) }.to(
raise_error ActiveRecord::RecordInvalid
)
end
expect(parts[0]).to be_in_stock
expect(parts[1]).to be_in_stock
end

it "saves line item quantity" do
line_item = order.contents.add(variant, 10)
expect(line_item).to be_valid
context "in stock" do
before do
[parts, parts2].each do |parts_list|
parts_list.each do |part|
part.stock_items.first.set_count_on_hand(10)
end

expect(parts_list[0]).to be_in_stock
expect(parts_list[1]).to be_in_stock
end
end

it "saves line item quantity" do
line_item = order.contents.add(variant, 10)
expect(line_item).to be_valid
end

context "one part of other assembly variant not in stock" do
before do
part = parts2.first
part.stock_items.update_all backorderable: false
part.stock_items.first.set_count_on_hand(0)

expect(part).not_to be_in_stock
end

it "saves line item quantity" do
line_item = order.contents.add(variant, 10)
expect(line_item).to be_valid
end
end
end
end
end
Expand Down