mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-25 20:46:48 +00:00
57 lines
1.9 KiB
Ruby
57 lines
1.9 KiB
Ruby
require 'spec_helper'
|
|
|
|
module Spree
|
|
module Stock
|
|
describe AvailabilityValidator do
|
|
let(:validator) { AvailabilityValidator.new({}) }
|
|
|
|
context "line item without existing inventory units" do
|
|
let(:order) { create(:order_with_line_items) }
|
|
let(:line_item) { order.line_items.first }
|
|
|
|
context "available quantity when variant.on_hand > line_item.quantity" do
|
|
it "suceeds" do
|
|
line_item.quantity = line_item.variant.on_hand - 1
|
|
validator.validate(line_item)
|
|
expect(line_item).to be_valid
|
|
end
|
|
end
|
|
|
|
context "unavailable quantity when variant.on_hand < line_item.quantity" do
|
|
it "fails" do
|
|
line_item.quantity = line_item.variant.on_hand + 1
|
|
validator.validate(line_item)
|
|
expect(line_item).not_to be_valid
|
|
end
|
|
|
|
it "succeeds with line_item skip_stock_check" do
|
|
line_item.skip_stock_check = true
|
|
line_item.quantity = line_item.variant.on_hand + 1
|
|
validator.validate(line_item)
|
|
expect(line_item).to be_valid
|
|
end
|
|
end
|
|
end
|
|
|
|
describe "line item with existing inventory units" do
|
|
let(:order) { create(:completed_order_with_totals) }
|
|
let(:line_item) { order.line_items.first }
|
|
|
|
context "when adding all variant.on_hand quantity to existing line_item quantity" do
|
|
it "succeeds because it excludes existing inventory units from the validation" do
|
|
line_item.quantity += line_item.variant.on_hand
|
|
validator.validate(line_item)
|
|
expect(line_item).to be_valid
|
|
end
|
|
|
|
it "fails if one more item is added" do
|
|
line_item.quantity += line_item.variant.on_hand + 1
|
|
validator.validate(line_item)
|
|
expect(line_item).not_to be_valid
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|