Skip to content

Instantly share code, notes, and snippets.

@cored
Created July 24, 2018 17:45
Show Gist options
  • Save cored/26064dc18cec7ae50e37c7d17aafa5b8 to your computer and use it in GitHub Desktop.
Save cored/26064dc18cec7ae50e37c7d17aafa5b8 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
require "rails_helper"
describe Bedpost::Orders::ReadyToGroup, type: :consumer do
subject(:process_ready_to_group) { described_class.new.process(payload) }
let(:shipments_message) do
{
shipments: fixtures_for_shipments(shipments),
}.to_json
end
let(:payload) do
build_message(shipments_message)
end
#products
let(:mattress_product) { create(:mattress_product, sku: "CA001-01-GRY-4") }
let(:foundation_product) { create(:foundation_product) }
let(:split_adjustable_base) { create(:adjustable_bedframe_product) }
let(:pillow_product) { create(:pillow_product, sku: 'PILLOWSTD') }
let(:king_pillow_product) { create(:pillow_product, sku: 'PILLOWKNG') }
let(:uk_pillow_product) { create(:pillow_product, sku: 'PILLOWSTANDARDUK') }
let(:uk_king_pillow_product) { create(:pillow_product, sku: 'PILLOWSUPERKINGUK') }
let(:dach_pillow_product) { create(:pillow_product, sku: 'KISSEN40X80') }
let(:dach_king_pillow_product) { create(:pillow_product, sku: 'KISSEN80X80') }
let(:flat_sheet) { create :sheet_product, height: 1.25, sku: "FLAT" }
let(:fitted_sheet) { create :sheet_product, height: 1.75, sku: "FITTED" }
let(:pillow_case) { create :sheet_product, height: 0.5, sku: "CASE" }
let(:duvet) { create :sheet_product, height: 2.75, sku: "DUVET" }
#shipping preferences (as sent in payload by Solidus)
let(:local_courier) { 'Courier' }
let(:parcel) { 'Parcel' }
let(:in_store) { 'in_store_pickup' }
let(:store_delivery) { 'store_delivery' }
let(:nwgd) { 'national_courier' }
describe "#process" do
let(:shipments) { [] }
it 'measure processing time' do
allow(Casper::Statsd).to receive(:time)
process_ready_to_group
expect(Casper::Statsd).to have_received(:time)
.with("casper.bedpost.groupments.duration")
end
context "when this is a new order" do
context "when no shipments are present in the payload" do
let(:shipments_message) { {}.to_json }
it "does not stop execution" do
expect { process_ready_to_group }.not_to raise_error
end
end
context 'when the order contains only local courier shipments' do
let(:shipping_preference) { local_courier }
context 'when there are multiple mattresses' do
let(:shipment_1) { ShipmentFactory.new('H1234', shipping_preference, mattress_product) }
let(:shipment_2) { ShipmentFactory.new('H4321', shipping_preference, mattress_product) }
let(:shipments) { [shipment_1, shipment_2] }
it 'creates a single shipment' do
expect { process_ready_to_group }.to change(Shipment, :count).from(0).to(1)
end
it 'combines the mattresses into the single shipment' do
process_ready_to_group
shipment = Shipment.last
expect(shipments.map(&:number)).to include(shipment.number)
expect(shipment.products).to match_array(shipments.map(&:products))
end
it 'calls the group complete publisher with persisted shipments and non-persisted numbers' do
allow(Groupments::GroupingCompletePublisher).to receive(:call)
process_ready_to_group
shipment = Shipment.last
expect(Groupments::GroupingCompletePublisher)
.to have_received(:call)
.with([shipment], deleted: [shipment_2.number])
end
end
end
context "when the order contains only parcel shipments" do
let(:shipping_preference) { parcel }
context "when there are multiple mattresses" do
let(:shipment_1) { ShipmentFactory.new('H1234', shipping_preference, mattress_product) }
let(:shipment_2) { ShipmentFactory.new('H4321', shipping_preference, mattress_product) }
let(:shipments) { [shipment_1, shipment_2] }
it "creates individual shipments" do
expect { process_ready_to_group }.to change(Shipment, :count).from(0).to(2)
end
end
context "when there are multiple queen adjustable bases and mattress" do
let(:shipment_1) { ShipmentFactory.new('H4341', shipping_preference, split_adjustable_base) }
let(:shipment_2) { ShipmentFactory.new('H4351', shipping_preference, split_adjustable_base) }
let(:shipment_3) { ShipmentFactory.new('H4361', shipping_preference, split_adjustable_base) }
let(:shipment_4) { ShipmentFactory.new('H1299', shipping_preference, mattress_product) }
let(:shipments) { [shipment_1, shipment_2, shipment_3, shipment_4] }
it "creates individual shipments" do
expect { process_ready_to_group }.to change(Shipment, :count).from(0).to(4)
end
it "puts the correct items in the shipments" do
process_ready_to_group
actual_skus = Shipment.all.map { |s| s.products.map(&:sku) }
expected_skus = [
[mattress_product.sku],
[split_adjustable_base.sku, split_adjustable_base.sku],
[split_adjustable_base.sku, split_adjustable_base.sku],
[split_adjustable_base.sku, split_adjustable_base.sku],
]
expect(actual_skus).to match_array(expected_skus)
end
it "separates the proper shipment items by requested variation sku" do
process_ready_to_group
aggregate_failures do
shipment = Shipment.find_by(number: shipment_1.number)
expect(shipment.shipment_items.map(&:requested_variation_sku)).to eq([nil])
[shipment_2, shipment_3, shipment_4].map(&:number).each do |number|
shipment = Shipment.find_by(number: number)
expect(shipment.shipment_items.map(&:requested_variation_sku))
.to match_array(split_adjustable_base.split_variation_skus)
end
end
end
it 'publishes the shipments back to Solidus' do
allow(Groupments::GroupingCompletePublisher).to receive(:call)
process_ready_to_group
expect(Groupments::GroupingCompletePublisher)
.to have_received(:call)
.with(
a_collection_containing_exactly(*Shipment.where(number: shipments.map(&:number)).to_a),
deleted: []
)
end
end
context "when the order contains pillows" do
context "when it is a US order" do
context "when there are 3 standard pillows" do
let(:shipment_1) { ShipmentFactory.new('H1234', shipping_preference, pillow_product) }
let(:shipment_2) { ShipmentFactory.new('H4325', shipping_preference, pillow_product) }
let(:shipment_3) { ShipmentFactory.new('H4326', shipping_preference, pillow_product) }
let(:shipments) { [shipment_1, shipment_2, shipment_3] }
it "puts 2 pillows in a STDPILBOX3 and 1 in STDPILBOX1" do
expect { process_ready_to_group }.to change(Shipment, :count).from(0).to(2)
expect(Shipment.last(2).map(&:box_sku)).to match_array(['STDPILBOX3', 'STDPILBOX1'])
end
end
context "when there are 3 king pillows" do
let(:shipment_1) { ShipmentFactory.new('H1234', shipping_preference, king_pillow_product) }
let(:shipment_2) { ShipmentFactory.new('H4325', shipping_preference, king_pillow_product) }
let(:shipment_3) { ShipmentFactory.new('H4326', shipping_preference, king_pillow_product) }
let(:shipments) { [shipment_1, shipment_2, shipment_3] }
it " puts 2 pillows in a KNGPILBOX4 and 1 in KNGPILBOX2" do
expect { process_ready_to_group }.to change(Shipment, :count).from(0).to(2)
expect(Shipment.last(2).map(&:box_sku)).to match_array(['KNGPILBOX2', 'KNGPILBOX4'])
end
end
end
context "when it is a DACH order" do
context "when there are 3 standard pillows" do
let(:shipment_1) { ShipmentFactory.new('H1234', shipping_preference, dach_pillow_product) }
let(:shipment_2) { ShipmentFactory.new('H4325', shipping_preference, dach_pillow_product) }
let(:shipment_3) { ShipmentFactory.new('H4326', shipping_preference, dach_pillow_product) }
let(:shipments) { [shipment_1, shipment_2, shipment_3] }
it "puts 2 pillows in a 8040PLBOXDB and 1 in 8040PLBOXSGL" do
expect { process_ready_to_group }.to change(Shipment, :count).from(0).to(2)
expect(Shipment.last(2).map(&:box_sku)).to match_array(['8040PLBOXDB', '8040PLBOXSGL'])
end
end
context "when there are 3 king pillows" do
let(:shipment_1) {
ShipmentFactory.new('H1234', shipping_preference, dach_king_pillow_product)
}
let(:shipment_2) {
ShipmentFactory.new('H4325', shipping_preference, dach_king_pillow_product)
}
let(:shipment_3) {
ShipmentFactory.new('H4326', shipping_preference, dach_king_pillow_product)
}
let(:shipments) { [shipment_1, shipment_2, shipment_3] }
it " puts 2 pillows in a 8080PLBOXDB and 1 in 8080PLBOXSGL" do
expect { process_ready_to_group }.to change(Shipment, :count).from(0).to(2)
expect(Shipment.last(2).map(&:box_sku)).to match_array(['8080PLBOXDB', '8080PLBOXSGL'])
end
end
end
context "when it is a UK order" do
context "when there are 3 standard pillows" do
let(:shipment_1) { ShipmentFactory.new('H1234', shipping_preference, uk_pillow_product) }
let(:shipment_2) { ShipmentFactory.new('H4325', shipping_preference, uk_pillow_product) }
let(:shipment_3) { ShipmentFactory.new('H4326', shipping_preference, uk_pillow_product) }
let(:shipments) { [shipment_1, shipment_2, shipment_3] }
it "puts 2 pillows in a STDPILBOX2 and 1 in STDPILBOX1" do
expect { process_ready_to_group }.to change(Shipment, :count).from(0).to(2)
expect(Shipment.last(2).map(&:box_sku)).to match_array(['STDPILBOX2', 'STDPILBOX1'])
end
end
context "when there are 3 king pillows" do
let(:shipment_1) { ShipmentFactory.new('H1234', shipping_preference, uk_king_pillow_product) }
let(:shipment_2) { ShipmentFactory.new('H4325', shipping_preference, uk_king_pillow_product) }
let(:shipment_3) { ShipmentFactory.new('H4326', shipping_preference, uk_king_pillow_product) }
let(:shipments) { [shipment_1, shipment_2, shipment_3] }
it " puts 2 pillows in a KINGPILBOX2 and 1 in KINGPILBOX1" do
expect { process_ready_to_group }.to change(Shipment, :count).from(0).to(2)
expect(Shipment.last(2).map(&:box_sku)).to match_array(['KINGPILBOX2', 'KINGPILBOX1'])
end
end
end
end
context "when there are sheets" do
context "when there's one queen flat sheet" do
let(:shipment_1) { ShipmentFactory.new('H1234', shipping_preference, flat_sheet) }
let(:shipments) { [shipment_1] }
it "creates a box with the smallest (2.5) dimension" do
process_ready_to_group
shipment = Shipment.last
expect(shipment.box_sku).to eql "2.50box2"
end
end
context "when theres's one queen flat and fitted sheet" do
context "when no items have a messaged_ships_by" do
let(:shipment_1) do
ShipmentFactory.new('H1234', shipping_preference, flat_sheet)
end
let(:shipment_2) do
ShipmentFactory.new('H1235', shipping_preference, fitted_sheet)
end
let(:shipments) { [shipment_1, shipment_2] }
it "creates one box with the medium (3.75) dimension" do
expect { process_ready_to_group }.to change(Shipment, :count).from(0).to(1)
shipment = Shipment.last
expect(shipment.box_sku).to eql "3.75box3"
end
end
context "when one of the items have a messaged_ships_by" do
let(:shipment_1) do
ShipmentFactory.new('H1234', shipping_preference, flat_sheet, Date.today + 7.days)
end
let(:shipment_2) do
ShipmentFactory.new('H1235', shipping_preference, fitted_sheet)
end
let(:shipments) { [shipment_1, shipment_2] }
it "creates two separate shipments" do
expect { process_ready_to_group }.to change(Shipment, :count).from(0).to(2)
end
end
context "when both items have (two different) messaged_ships_by" do
let(:shipment_1) do
ShipmentFactory.new('H1234', shipping_preference, flat_sheet, Date.today + 7.days)
end
let(:shipment_2) do
ShipmentFactory.new('H1235', shipping_preference, fitted_sheet, Date.today + 14.days)
end
let(:shipments) { [shipment_1, shipment_2] }
it "creates two separate shipments" do
expect { process_ready_to_group }.to change(Shipment, :count).from(0).to(2)
end
end
context "when both items have the same messaged_ships_by" do
let(:shipment_1) do
ShipmentFactory.new('H1234', shipping_preference, flat_sheet, Date.today + 14.days)
end
let(:shipment_2) do
ShipmentFactory.new('H1235', shipping_preference, fitted_sheet, Date.today + 14.days)
end
let(:shipments) { [shipment_1, shipment_2] }
it "creates one shipment" do
expect { process_ready_to_group }.to change(Shipment, :count).from(0).to(1)
end
end
end
context "when there's 2 queen flats, 2 fitted sheets, 4 pillow cases, 2 duvet covers" do
let(:shipments) do
2.times.flat_map do |n|
[flat_sheet, fitted_sheet, duvet].each_with_index.map do |product, idx|
ShipmentFactory.new("H123#{idx}#{n}", shipping_preference, product)
end
end + 4.times.map do |n|
ShipmentFactory.new("H12#{n}4", shipping_preference, pillow_case)
end
end
it "creates three shipments" do
expect {
process_ready_to_group
}.to change(Shipment, :count).from(0).to(3)
end
it "assign shipments to the right box sku" do
process_ready_to_group
expect(
Shipment.all.flat_map(&:shipment_items).map do |si|
[si.product.sku, si.parent_shipment.box_sku]
end
).to match_array([
["CASE", "3.75box3"],
["CASE", "3.75box3"],
["CASE", "5.50box4"],
["CASE", "5.50box4"],
["DUVET", "5.50box4"],
["DUVET", "5.50box4"],
["FITTED", "5.50box4"],
["FITTED", "5.50box4"],
["FLAT", "3.75box3"],
["FLAT", "3.75box3"],
])
end
end
context "when there's 10 queen flat sheets" do
let(:shipments) do
1.upto(10).map do |idx|
ShipmentFactory.new("H1#{idx}24", shipping_preference, flat_sheet)
end
end
it "creates 3 shipments" do
expect {
process_ready_to_group
}.to change(Shipment, :count).from(0).to(3)
end
it "assigns the shipments to the right box sku" do
process_ready_to_group
expect(
Shipment.all.flat_map(&:shipment_items).map do |si|
[si.product.sku, si.parent_shipment.box_sku]
end
).to match_array([
["FLAT", "3.75box3"],
["FLAT", "3.75box3"],
["FLAT", "5.50box4"],
["FLAT", "5.50box4"],
["FLAT", "5.50box4"],
["FLAT", "5.50box4"],
["FLAT", "5.50box4"],
["FLAT", "5.50box4"],
["FLAT", "5.50box4"],
["FLAT", "5.50box4"],
])
end
end
end
end
context "when the order contains sheets in both courier and parcel shipments" do
let(:shipments) do
courier = 1.upto(2).map do |idx|
ShipmentFactory.new("H1#{idx}24", parcel, flat_sheet)
end
parcel = 1.upto(2).map do |idx|
ShipmentFactory.new("H1#{idx}25", local_courier, flat_sheet)
end
courier + parcel
end
it "creates 1 grouped parcel shipment and 1 grouped courier shipment" do
expect {
process_ready_to_group
}.to change(Shipment, :count).from(0).to(2)
expect(Shipment.where(shipping_preference: local_courier).count).to eq 1
expect(Shipment.where(shipping_preference: parcel).count).to eq 1
courier_shipment = Shipment.where(shipping_preference: local_courier).first
expect(
courier_shipment.shipment_items.map(&:product)
).to eq [flat_sheet, flat_sheet]
parcel_shipment = Shipment.where(shipping_preference: parcel).first
expect(
parcel_shipment.shipment_items.map(&:product)
).to eq [flat_sheet, flat_sheet]
end
end
context "when the order contains only in store shipments" do
let(:shipping_preference) { in_store }
context "when there are multiple mattresses" do
let(:shipment_1) { ShipmentFactory.new('H1234', shipping_preference, mattress_product) }
let(:shipment_2) { ShipmentFactory.new('H4321', shipping_preference, mattress_product) }
let(:shipments) { [shipment_1, shipment_2] }
it "creates individual shipments without grouping" do
expect { process_ready_to_group }.to change(Shipment, :count).from(0).to(2)
expect(
Shipment.find_by(number: 'H1234').shipment_items.map(&:product)
).to match_array([mattress_product])
expect(
Shipment.find_by(number: 'H4321').shipment_items.map(&:product)
).to match_array([mattress_product])
end
end
end
context "when the order contains only store delivery shipments" do
let(:shipping_preference) { store_delivery }
context "when there are multiple mattresses" do
let(:shipment_1) { ShipmentFactory.new('H1234', shipping_preference, mattress_product) }
let(:shipment_2) { ShipmentFactory.new('H4321', shipping_preference, mattress_product) }
let(:shipments) { [shipment_1, shipment_2] }
it "creates individual shipments without grouping" do
expect { process_ready_to_group }.to change(Shipment, :count).from(0).to(2)
expect(
Shipment.find_by(number: 'H1234').shipment_items.map(&:product)
).to match_array([mattress_product])
expect(
Shipment.find_by(number: 'H4321').shipment_items.map(&:product)
).to match_array([mattress_product])
end
end
end
context 'when the order has a shipping preference of NWGD' do
let(:shipping_preference) { nwgd }
let!(:xpo_albany_option) { create(:xpo_albany_option) }
let(:mattress_1) { ShipmentFactory.new('H1234', shipping_preference, mattress_product) }
let(:mattress_2) { ShipmentFactory.new('H1235', shipping_preference, mattress_product) }
let(:mattress_3) { ShipmentFactory.new('H1236', shipping_preference, mattress_product) }
let(:foundation_1) { ShipmentFactory.new('H1237', shipping_preference, foundation_product) }
let(:foundation_2) { ShipmentFactory.new('H1238', shipping_preference, foundation_product) }
let(:shipments) { [mattress_1, mattress_2, mattress_3, foundation_1, foundation_2] }
let(:mattresses) { [mattress_1, mattress_2, mattress_3] }
let(:handle_shipment) { nil }
before do
allow(Inventory::WarehouseAssigner).to receive(:assign_shipment) { handle_shipment }
allow(Inventory::Reservations).to receive(:reserve_shipment)
allow(Xpo::OrderWorker).to receive(:perform_in)
end
context 'when all the items are forward stocked' do
let!(:xpo_warehouse) do
create(:xpo_albany_warehouse,
:stocked,
products: [mattress_product, foundation_product],
count_per_product: 3)
end
it 'creates one shipment' do
expect { process_ready_to_group }.to change(Shipment, :count).from(0).to(1)
end
end
context 'when only some items are forward stocked' do
let!(:ups_ground) { create(:ups_ground) }
context 'when only the mattresses are forward stocked' do
let!(:xpo_warehouse) do
create(:xpo_albany_warehouse,
:stocked,
products: [mattress_product],
count_per_product: 3)
end
it 'puts all the mattresses in one shipment and leaves foundations in original shipments' do
expect { process_ready_to_group }.to change(Shipment, :count).from(0).to(3)
end
end
context 'when some of the mattresses are forward stocked' do
let(:shipments) { mattresses }
let!(:xpo_warehouse) do
create(:xpo_albany_warehouse,
:stocked,
products: [mattress_product],
count_per_product: 2)
end
it 'groups the two forward stocked mattresses in one shipment
and the third in a separate shipment' do
expect { process_ready_to_group }.to change(Shipment, :count).from(0).to(2)
end
end
end
context 'when no items are forward stocked' do
let!(:ups_ground) { create(:ups_ground) }
let(:shipments) { mattresses }
let!(:xpo_warehouse) { create(:xpo_albany_warehouse) }
it 'returns all original shipments ungrouped' do
expect { process_ready_to_group }.to change(Shipment, :count).from(0).to(3)
end
end
end
end
context "when this is an existing order" do
context "when we switch from courier to parcel" do
let(:initial_shipping_preference) { local_courier }
let(:shipping_preference) { parcel }
context "when shipping method changes from courier to parcel on an existing grouped shipment" do
let(:shipment_1) { ShipmentFactory.new('H1234', shipping_preference, [mattress_product]*3) }
let(:shipment_2) { ShipmentFactory.new('H5678', shipping_preference, []) }
let(:shipment_3) { ShipmentFactory.new('H9012', shipping_preference, []) }
let(:shipments) { [shipment_1,shipment_2,shipment_3] }
it "creates individual shipments" do
expect { process_ready_to_group }.to change(Shipment, :count).from(0).to(3)
expect(Shipment.find_by(number: shipment_1.number).shipment_items.count).to eq(1)
expect(Shipment.find_by(number: shipment_1.number).shipping_preference).to eq(:parcel)
expect(Shipment.find_by(number: shipment_2.number).shipment_items.count).to eq(1)
expect(Shipment.find_by(number: shipment_2.number).shipping_preference).to eq(:parcel)
expect(Shipment.find_by(number: shipment_3.number).shipment_items.count).to eq(1)
expect(Shipment.find_by(number: shipment_3.number).shipping_preference).to eq(:parcel)
end
it 'calls the group complete publisher with persisted shipments no deleted numbers' do
allow(Groupments::GroupingCompletePublisher).to receive(:call)
process_ready_to_group
expect(Groupments::GroupingCompletePublisher)
.to have_received(:call)
.with(
a_collection_containing_exactly(*Shipment.where(number: shipments.map(&:number)).to_a),
deleted: []
)
end
context "when not enough empty shipments were sent" do
let(:shipments) { [shipment_1,shipment_2] }
it "raises a NotEnoughShipments exception" do
expect { process_ready_to_group }.to(
raise_exception(Bedpost::Orders::ReadyToGroup::NotEnoughShipments)
)
end
it "notify slack with the discripency" do
allow(Notifiers::Groupments).to receive(:notify)
.with("Not enough shipments - shipment count: 2, item count: 3")
begin
process_ready_to_group
rescue described_class::NotEnoughShipments
expect(Notifiers::Groupments).to have_received(:notify)
.with("Not enough shipments - shipment count: 2, item count: 3")
end
end
end
end
end
context "when we switch from parcel to courier" do
let(:initial_shipping_preference) { parcel }
let(:shipping_preference) { local_courier }
context "when shipping method changes from parcel to courier on an existing grouped shipment" do
let(:shipment_1) { ShipmentFactory.new('H1234', shipping_preference, [mattress_product]) }
let(:shipment_2) { ShipmentFactory.new('H5678', shipping_preference, [mattress_product]) }
let(:shipment_3) { ShipmentFactory.new('H9012', shipping_preference, [mattress_product]) }
let(:shipments) { [shipment_1,shipment_2,shipment_3] }
it "groups the shipments together" do
expect { process_ready_to_group }.to change(Shipment, :count).from(0).to(1)
expect(Shipment.find_by(number: shipment_1.number).shipment_items.count).to eq(3)
expect(Shipment.find_by(number: shipment_1.number).shipping_preference).to eq(:courier)
expect(Shipment.find_by(number: shipment_2.number)).to be_nil
expect(Shipment.find_by(number: shipment_3.number)).to be_nil
end
it 'calls the group complete publisher with persisted shipments and deleted numbers' do
allow(Groupments::GroupingCompletePublisher).to receive(:call)
process_ready_to_group
expect(Groupments::GroupingCompletePublisher)
.to have_received(:call)
.with(
[Shipment.find_by(number: shipment_1.number)],
deleted: [shipment_2.number,shipment_3.number]
)
end
end
end
context "when we switch from parcel to nwgd" do
let(:initial_shipping_preference) { parcel }
let(:shipping_preference) { nwgd }
let(:handle_shipment) { nil }
let!(:xpo_albany_option) { create(:xpo_albany_option) }
let!(:xpo_warehouse) do
create(:xpo_albany_warehouse,
:stocked,
products: [mattress_product],
count_per_product: 3)
end
before do
allow(Inventory::WarehouseAssigner).to receive(:assign_shipment) { handle_shipment }
allow(Inventory::Reservations).to receive(:reserve_shipment)
allow(Xpo::OrderWorker).to receive(:perform_in)
end
context "when shipping method changes from parcel to nwgd on an existing grouped shipment" do
let(:shipment_1) { ShipmentFactory.new('H1234', shipping_preference, [mattress_product]) }
let(:shipment_2) { ShipmentFactory.new('H5678', shipping_preference, [mattress_product]) }
let(:shipment_3) { ShipmentFactory.new('H9012', shipping_preference, [mattress_product]) }
let(:shipments) { [shipment_1, shipment_2, shipment_3] }
context "when the grouped shipment is fully forward stocked" do
it "groups them into one shipment" do
expect { process_ready_to_group }.to change(Shipment, :count).from(0).to(1)
end
it 'calls the group complete publisher with persisted shipments and deleted numbers' do
allow(Groupments::GroupingCompletePublisher).to receive(:call)
process_ready_to_group
expect(Groupments::GroupingCompletePublisher)
.to have_received(:call)
.with(
[Shipment.find_by(number: shipment_1.number)],
deleted: [shipment_2.number, shipment_3.number]
)
end
end
context "when some of the grouped shipments are forward stocked" do
let!(:xpo_warehouse) do
create(:xpo_albany_warehouse,
:stocked,
products: [mattress_product],
count_per_product: 2)
end
let!(:ups_ground) { create(:ups_ground) }
it "groups the forward stocked shipments and leaves the remaining ones in original shipments" do
expect { process_ready_to_group }.to change(Shipment, :count).from(0).to(2)
end
it 'calls the group complete publisher with persisted shipments and deleted numbers' do
allow(Groupments::GroupingCompletePublisher).to receive(:call)
process_ready_to_group
expect(Groupments::GroupingCompletePublisher)
.to have_received(:call)
.with(
[Shipment.find_by(number: shipment_2.number),
Shipment.find_by(number: shipment_1.number),],
deleted: [shipment_3.number]
)
end
end
context "when none of the grouped shipments are forward stocked" do
let!(:xpo_warehouse) { create(:xpo_albany_warehouse) }
let!(:ups_ground) { create(:ups_ground) }
it "creates individual shipments" do
expect { process_ready_to_group }.to change(Shipment, :count).from(0).to(3)
expect(Shipment.find_by(number: shipment_1.number).shipment_items.count).to eq(1)
expect(Shipment.find_by(number: shipment_1.number).shipping_preference).to eq(:national_courier)
expect(Shipment.find_by(number: shipment_2.number).shipment_items.count).to eq(1)
expect(Shipment.find_by(number: shipment_2.number).shipping_preference).to eq(:national_courier)
expect(Shipment.find_by(number: shipment_3.number).shipment_items.count).to eq(1)
expect(Shipment.find_by(number: shipment_3.number).shipping_preference).to eq(:national_courier)
end
it 'calls the group complete publisher with persisted shipments and no deleted numbers' do
allow(Groupments::GroupingCompletePublisher).to receive(:call)
process_ready_to_group
expect(Groupments::GroupingCompletePublisher)
.to have_received(:call)
.with(
a_collection_containing_exactly(*Shipment.where(number: shipments.map(&:number)).to_a),
deleted: []
)
end
end
end
end
context "when we switch from nwgd to parcel" do
let(:initial_shipping_preference) { nwgd }
let(:shipping_preference) { parcel }
context "when shipping method changes from nwgd to parcel on an existing grouped shipment" do
let(:shipment_1) { ShipmentFactory.new('H1234', shipping_preference, [mattress_product]*3) }
let(:shipment_2) { ShipmentFactory.new('H5678', shipping_preference, []) }
let(:shipment_3) { ShipmentFactory.new('H9012', shipping_preference, []) }
let(:shipments) { [shipment_1,shipment_2,shipment_3] }
it "creates individual shipments" do
expect { process_ready_to_group }.to change(Shipment, :count).from(0).to(3)
expect(Shipment.find_by(number: shipment_1.number).shipment_items.count).to eq(1)
expect(Shipment.find_by(number: shipment_1.number).shipping_preference).to eq(:parcel)
expect(Shipment.find_by(number: shipment_2.number).shipment_items.count).to eq(1)
expect(Shipment.find_by(number: shipment_2.number).shipping_preference).to eq(:parcel)
expect(Shipment.find_by(number: shipment_3.number).shipment_items.count).to eq(1)
expect(Shipment.find_by(number: shipment_3.number).shipping_preference).to eq(:parcel)
end
it 'calls the group complete publisher with persisted shipments and no deleted numbers' do
allow(Groupments::GroupingCompletePublisher).to receive(:call)
process_ready_to_group
expect(Groupments::GroupingCompletePublisher)
.to have_received(:call)
.with(
a_collection_containing_exactly(*Shipment.where(number: shipments.map(&:number)).to_a),
deleted: []
)
end
end
end
end
end
private
def fixtures_for_shipments(shipments)
shipments.map { |shipment| fixture_for_shipment(shipment) }
end
def fixture_for_shipment(shipment)
load_json_fixture('groupments/shipment.json').merge(
serialized_shipment_factory(shipment)
)
end
def serialized_shipment_factory(shipment)
{
"id" => shipment.number,
"number" => shipment.number,
"status" => "ready",
"shipping_preference" => shipment.shipping_preference,
"inventory_units" => Array(shipment.products).map{|p| { "product_id" => p.sku, "quantity" => 1 } },
"messaged_ships_by" => shipment.messaged_ships_by,
}
end
ShipmentFactory = Struct.new(:number, :shipping_preference, :products, :messaged_ships_by)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment