|
| 1 | +require "rails_helper" |
| 2 | + |
| 3 | +RSpec.describe "Integrating with the simple coordinator" do |
| 4 | + let(:order) { create(:order_with_line_items, line_items_count: 2) } |
| 5 | + |
| 6 | + subject { Spree::Stock::SimpleCoordinator.new(order) } |
| 7 | + |
| 8 | + it 'builds shipments' do |
| 9 | + expect(subject.shipments.size).to eq(1) |
| 10 | + end |
| 11 | + |
| 12 | + it "builds a shipment for all active stock locations" do |
| 13 | + expect(subject.shipments.count).to eq Spree::StockLocation.count |
| 14 | + end |
| 15 | + |
| 16 | + context "missing stock items in active stock location" do |
| 17 | + let!(:another_location) { create(:stock_location, propagate_all_variants: false) } |
| 18 | + |
| 19 | + it "builds shipments only for valid active stock locations" do |
| 20 | + expect(subject.shipments.count).to eq(Spree::StockLocation.count - 1) |
| 21 | + end |
| 22 | + end |
| 23 | + |
| 24 | + it "does not unintentionally add shipments to the order" do |
| 25 | + subject.shipments |
| 26 | + expect { |
| 27 | + order.recalculate |
| 28 | + }.not_to change { |
| 29 | + order.shipments.count |
| 30 | + } |
| 31 | + end |
| 32 | + |
| 33 | + context "when there is one unit that has stock in a stock location that a non-tracked unit has no stock item in" do |
| 34 | + let!(:stock_location_1) { create(:stock_location, propagate_all_variants: false, active: true) } |
| 35 | + let!(:stock_location_2) { create(:stock_location, propagate_all_variants: false, active: true) } |
| 36 | + |
| 37 | + let!(:variant_1) do |
| 38 | + create(:variant, track_inventory: true).tap do |variant| |
| 39 | + variant.stock_items.destroy_all |
| 40 | + stock_item = variant.stock_items.create!(stock_location: stock_location_1) |
| 41 | + stock_item.set_count_on_hand(10) |
| 42 | + end |
| 43 | + end |
| 44 | + let!(:variant_2) do |
| 45 | + create(:variant, track_inventory: false).tap do |variant| |
| 46 | + variant.stock_items.destroy_all |
| 47 | + stock_item = variant.stock_items.create!(stock_location: stock_location_2) |
| 48 | + stock_item.set_count_on_hand(0) |
| 49 | + end |
| 50 | + end |
| 51 | + |
| 52 | + let!(:order) { create(:order, line_items: [create(:line_item, variant: variant_1), create(:line_item, variant: variant_2)]) } |
| 53 | + |
| 54 | + it "splits the inventory units to stock locations that they have stock items for" do |
| 55 | + shipments = subject.shipments |
| 56 | + |
| 57 | + expect(shipments.size).to eq 2 |
| 58 | + |
| 59 | + location_1_shipment = shipments.detect { |shipment| shipment.stock_location == stock_location_1 } |
| 60 | + location_2_shipment = shipments.detect { |shipment| shipment.stock_location == stock_location_2 } |
| 61 | + |
| 62 | + expect(location_1_shipment).to be_present |
| 63 | + expect(location_2_shipment).to be_present |
| 64 | + |
| 65 | + expect(location_1_shipment.inventory_units.map(&:variant)).to eq [variant_1] |
| 66 | + expect(location_2_shipment.inventory_units.map(&:variant)).to eq [variant_2] |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + context "with no backordering" do |
| 71 | + let!(:stock_location_1) { create(:stock_location, propagate_all_variants: false, active: true) } |
| 72 | + |
| 73 | + let!(:variant) { create(:variant, track_inventory: true) } |
| 74 | + |
| 75 | + before do |
| 76 | + stock_item_one = variant.stock_items.create!(stock_location: stock_location_1, backorderable: false) |
| 77 | + stock_item_one.set_count_on_hand(location_1_inventory) |
| 78 | + end |
| 79 | + |
| 80 | + let!(:order) { create(:order) } |
| 81 | + let!(:line_item) { create(:line_item, order: order, variant: variant, quantity: 5) } |
| 82 | + before { order.reload } |
| 83 | + let(:shipments) { subject.shipments } |
| 84 | + |
| 85 | + shared_examples "a fulfillable package" do |
| 86 | + it "packages correctly" do |
| 87 | + expect(shipments).not_to be_empty |
| 88 | + inventory_units = shipments.flat_map(&:inventory_units) |
| 89 | + expect(inventory_units.size).to eq(5) |
| 90 | + expect(inventory_units.uniq.size).to eq(5) |
| 91 | + end |
| 92 | + end |
| 93 | + |
| 94 | + shared_examples "an unfulfillable package" do |
| 95 | + it "raises exception" do |
| 96 | + expect{ shipments }.to raise_error(Spree::Order::InsufficientStock) |
| 97 | + end |
| 98 | + |
| 99 | + it 'raises exception and includes unfulfillable items' do |
| 100 | + begin |
| 101 | + expect(shipments).not_to be_empty |
| 102 | + rescue Spree::Order::InsufficientStock => e |
| 103 | + expect(e.items.keys.map(&:id)).to contain_exactly(variant.id) |
| 104 | + end |
| 105 | + end |
| 106 | + end |
| 107 | + |
| 108 | + context 'with no stock locations' do |
| 109 | + let(:location_1_inventory) { 0 } |
| 110 | + before { variant.stock_items.destroy_all } |
| 111 | + it_behaves_like "an unfulfillable package" |
| 112 | + end |
| 113 | + |
| 114 | + context 'with a single stock location' do |
| 115 | + context "with no inventory" do |
| 116 | + let(:location_1_inventory) { 0 } |
| 117 | + it_behaves_like "an unfulfillable package" |
| 118 | + end |
| 119 | + |
| 120 | + context "with insufficient inventory" do |
| 121 | + let(:location_1_inventory) { 1 } |
| 122 | + it_behaves_like "an unfulfillable package" |
| 123 | + end |
| 124 | + |
| 125 | + context "with sufficient inventory" do |
| 126 | + let(:location_1_inventory) { 5 } |
| 127 | + it_behaves_like "a fulfillable package" |
| 128 | + end |
| 129 | + end |
| 130 | + |
| 131 | + context 'with two stock locations' do |
| 132 | + let!(:stock_location_2) { create(:stock_location, propagate_all_variants: false, active: true) } |
| 133 | + before do |
| 134 | + stock_item_two = variant.stock_items.create!(stock_location: stock_location_2, backorderable: false) |
| 135 | + stock_item_two.set_count_on_hand(location_2_inventory) |
| 136 | + end |
| 137 | + |
| 138 | + context "with no inventory" do |
| 139 | + let(:location_1_inventory) { 0 } |
| 140 | + let(:location_2_inventory) { 0 } |
| 141 | + it_behaves_like "an unfulfillable package" |
| 142 | + end |
| 143 | + |
| 144 | + context "with some but insufficient inventory in each location" do |
| 145 | + let(:location_1_inventory) { 1 } |
| 146 | + let(:location_2_inventory) { 1 } |
| 147 | + it_behaves_like "an unfulfillable package" |
| 148 | + end |
| 149 | + |
| 150 | + context "has sufficient inventory in the first location" do |
| 151 | + let(:location_1_inventory) { 5 } |
| 152 | + let(:location_2_inventory) { 0 } |
| 153 | + it_behaves_like "a fulfillable package" |
| 154 | + end |
| 155 | + |
| 156 | + context "has sufficient inventory in the second location" do |
| 157 | + let(:location_1_inventory) { 0 } |
| 158 | + let(:location_2_inventory) { 5 } |
| 159 | + it_behaves_like "a fulfillable package" |
| 160 | + end |
| 161 | + |
| 162 | + context "with sufficient inventory only across both locations" do |
| 163 | + let(:location_1_inventory) { 2 } |
| 164 | + let(:location_2_inventory) { 3 } |
| 165 | + it_behaves_like "a fulfillable package" |
| 166 | + end |
| 167 | + |
| 168 | + context "has sufficient inventory in the second location and some in the first" do |
| 169 | + let(:location_1_inventory) { 2 } |
| 170 | + let(:location_2_inventory) { 5 } |
| 171 | + it_behaves_like "a fulfillable package" |
| 172 | + end |
| 173 | + |
| 174 | + context "has sufficient inventory in the first location and some in the second" do |
| 175 | + let(:location_1_inventory) { 5 } |
| 176 | + let(:location_2_inventory) { 2 } |
| 177 | + it_behaves_like "a fulfillable package" |
| 178 | + end |
| 179 | + |
| 180 | + context "with sufficient inventory in both locations" do |
| 181 | + let(:location_1_inventory) { 5 } |
| 182 | + let(:location_2_inventory) { 5 } |
| 183 | + it_behaves_like "a fulfillable package" |
| 184 | + end |
| 185 | + end |
| 186 | + |
| 187 | + context 'with three stock locations' do |
| 188 | + let!(:stock_location_2) { create(:stock_location, propagate_all_variants: false, active: true) } |
| 189 | + let!(:stock_location_3) { create(:stock_location, propagate_all_variants: false, active: true) } |
| 190 | + before do |
| 191 | + stock_item_two = variant.stock_items.create!(stock_location: stock_location_2, backorderable: false) |
| 192 | + stock_item_two.set_count_on_hand(location_2_inventory) |
| 193 | + |
| 194 | + stock_item_three = variant.stock_items.create!(stock_location: stock_location_3, backorderable: false) |
| 195 | + stock_item_three.set_count_on_hand(location_3_inventory) |
| 196 | + end |
| 197 | + |
| 198 | + # Regression test for https://github.com/solidusio/solidus/issues/2122 |
| 199 | + context "with sufficient inventory in first two locations" do |
| 200 | + let(:location_1_inventory) { 3 } |
| 201 | + let(:location_2_inventory) { 3 } |
| 202 | + let(:location_3_inventory) { 3 } |
| 203 | + |
| 204 | + it_behaves_like "a fulfillable package" |
| 205 | + |
| 206 | + it "creates only two packages" do |
| 207 | + expect(shipments.count).to eq(2) |
| 208 | + end |
| 209 | + end |
| 210 | + |
| 211 | + context "with sufficient inventory only across all three locations" do |
| 212 | + let(:location_1_inventory) { 2 } |
| 213 | + let(:location_2_inventory) { 2 } |
| 214 | + let(:location_3_inventory) { 2 } |
| 215 | + |
| 216 | + it_behaves_like "a fulfillable package" |
| 217 | + |
| 218 | + it "creates three packages" do |
| 219 | + expect(shipments.count).to eq(3) |
| 220 | + end |
| 221 | + end |
| 222 | + |
| 223 | + context "with sufficient inventory only across all three locations" do |
| 224 | + let(:location_1_inventory) { 2 } |
| 225 | + let(:location_2_inventory) { 2 } |
| 226 | + let(:location_3_inventory) { 2 } |
| 227 | + |
| 228 | + it_behaves_like "a fulfillable package" |
| 229 | + |
| 230 | + it "creates three packages" do |
| 231 | + expect(shipments.count).to eq(3) |
| 232 | + end |
| 233 | + end |
| 234 | + end |
| 235 | + end |
| 236 | +end |
0 commit comments