Skip to content
16 changes: 16 additions & 0 deletions spec/models/item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -493,4 +493,20 @@
end
end
end

describe "#sync_request_units!" do
it "returns the created request units names for the given unit_ids" do
item = create(:item, organization:)
unit_1 = create(:unit, organization:, name: 'Unit 1')
unit_2 = create(:unit, organization:, name: 'Unit 2')
_unit_3 = create(:unit, organization:, name: 'Not included')
create(:item_unit, item:, name: unit_1.name)
create(:item_unit, item:, name: unit_2.name)
unit_ids = [unit_1.id, unit_2.id]

result = item.sync_request_units!(unit_ids)

expect(result).to contain_exactly('Unit 1', 'Unit 2')
end
end
end
60 changes: 60 additions & 0 deletions spec/models/partners/item_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,66 @@
describe "versioning" do
it { is_expected.to be_versioned }
end

describe '#quantity_with_units' do
context 'when enable_packs is enabled' do
context 'when there is a request unit' do
it 'returns the quantity with the request unit' do
Flipper.enable(:enable_packs)

item = create(:item, organization: organization)
create(:item_unit, item:, name: 'flat')
request = create(:request, organization: organization)
item_request = create(:item_request, request:, item: item, request_unit: 'flat', name: "Item 1", quantity: 10)

expect(item_request.quantity_with_units).to eq('10 flats')
end
end

context 'when there is no request unit' do
it 'returns only the quantity' do
Flipper.enable(:enable_packs)

item = create(:item, organization: organization)
create(:item_unit, item:, name: 'flat')
request = create(:request, organization: organization)
item_request = create(:item_request, request:, item: item, name: 'Item 1', quantity: 10)

expect(item_request.quantity_with_units).to eq('10')
end
end
end
end

describe '#name_with_unit' do
context 'when enable_packs is enabled' do
context 'when there is a request unit' do
it 'returns the item name with the request unit' do
Flipper.enable(:enable_packs)

item = create(:item, organization: organization, name: 'Item name')
create(:item_unit, item:, name: 'flat')
request = create(:request, organization: organization)
item_request = create(:item_request, request:, item: item, request_unit: 'flat', name: "Item 1", quantity: 10)

expect(item_request.name_with_unit).to eq('Item name - flats')
end
end

context 'when there is no request unit' do
it 'returns only the item name' do
Flipper.enable(:enable_packs)

item = create(:item, organization: organization, name: 'Item name')
create(:item_unit, item:, name: 'flat')
request = create(:request, organization: organization)
item_request = create(:item_request, request:, item: item, name: 'Item 1', quantity: 10)

expect(item_request.name_with_unit).to eq('Item name')
end
end
end
end
end


114 changes: 65 additions & 49 deletions spec/pdfs/distribution_pdf_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,49 @@

context "with request data" do
describe "#hide_columns" do
it "hides value and package columns when true on organization" do
context "when enable_packs is enabled" do
it "hides value and package columns when true on organization, and includes the request unit info" do
Flipper.enable(:enable_packs)

pdf = described_class.new(org_hiding_packages_and_values, distribution)
data = pdf.request_data
pdf.hide_columns(data)
expect(data).to eq([
["Items Received", "Requested", "Received"],
["Item 1", "", 50],
["Item 2", "30", 100],
["Item 3", "50", ""],
["Item 4", "120 packs", ""],
["", "", ""],
["Total Items Received", 200, 150]
])
end

it "hides value columns when true on organization" do
Flipper.enable(:enable_packs)

pdf = described_class.new(org_hiding_values, distribution)
data = pdf.request_data
pdf.hide_columns(data)
expect(data).to eq([
["Items Received", "Requested", "Received", "Packages"],
["Item 1", "", 50, "1"],
["Item 2", "30", 100, nil],
["Item 3", "50", "", nil],
["Item 4", "120 packs", "", nil],
["", "", ""],
["Total Items Received", 200, 150, ""]
])
end
end
end
end

context "with non request data" do
context "when enable_packs is enabled" do
it "hides value and package columns when true on organization, and includes request unit info" do
Flipper.enable(:enable_packs)

pdf = described_class.new(org_hiding_packages_and_values, distribution)
data = pdf.request_data
pdf.hide_columns(data)
Expand All @@ -57,13 +99,15 @@
["Item 1", "", 50],
["Item 2", "30", 100],
["Item 3", "50", ""],
["Item 4", "120", ""],
["Item 4", "120 packs", ""],
["", "", ""],
["Total Items Received", 200, 150]
])
end

it "hides value columns when true on organization" do
it "hides value columns when true on organization, and includes request unit info" do
Flipper.enable(:enable_packs)

pdf = described_class.new(org_hiding_values, distribution)
data = pdf.request_data
pdf.hide_columns(data)
Expand All @@ -72,61 +116,33 @@
["Item 1", "", 50, "1"],
["Item 2", "30", 100, nil],
["Item 3", "50", "", nil],
["Item 4", "120", "", nil],
["Item 4", "120 packs", "", nil],
["", "", ""],
["Total Items Received", 200, 150, ""]
])
end
end
end

context "with non request data" do
it "hides value and package columns when true on organization" do
pdf = described_class.new(org_hiding_packages_and_values, distribution)
data = pdf.request_data
pdf.hide_columns(data)
expect(data).to eq([
["Items Received", "Requested", "Received"],
["Item 1", "", 50],
["Item 2", "30", 100],
["Item 3", "50", ""],
["Item 4", "120", ""],
["", "", ""],
["Total Items Received", 200, 150]
])
end

it "hides value columns when true on organization" do
pdf = described_class.new(org_hiding_values, distribution)
data = pdf.request_data
pdf.hide_columns(data)
expect(data).to eq([
["Items Received", "Requested", "Received", "Packages"],
["Item 1", "", 50, "1"],
["Item 2", "30", 100, nil],
["Item 3", "50", "", nil],
["Item 4", "120", "", nil],
["", "", ""],
["Total Items Received", 200, 150, ""]
])
end
end

context "regardless of request data" do
describe "#hide_columns" do
it "hides package column when true on organization" do
pdf = described_class.new(org_hiding_packages, distribution)
data = pdf.request_data
pdf.hide_columns(data)
expect(data).to eq([
["Items Received", "Requested", "Received", "Value/item", "In-Kind Value Received"],
["Item 1", "", 50, "$1.00", "$50.00"],
["Item 2", "30", 100, "$2.00", "$200.00"],
["Item 3", "50", "", "$3.00", nil],
["Item 4", "120", "", "$4.00", nil],
["", "", "", "", ""],
["Total Items Received", 200, 150, "", "$250.00"]
])
context "when enable_packs is enabled" do
it "hides package column when true on organization, and includes request unit info" do
Flipper.enable(:enable_packs)

pdf = described_class.new(org_hiding_packages, distribution)
data = pdf.request_data
pdf.hide_columns(data)
expect(data).to eq([
["Items Received", "Requested", "Received", "Value/item", "In-Kind Value Received"],
["Item 1", "", 50, "$1.00", "$50.00"],
["Item 2", "30", 100, "$2.00", "$200.00"],
["Item 3", "50", "", "$3.00", nil],
["Item 4", "120 packs", "", "$4.00", nil],
["", "", "", "", ""],
["Total Items Received", 200, 150, "", "$250.00"]
])
end
end
end
end
Expand Down
69 changes: 38 additions & 31 deletions spec/services/item_create_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,46 +26,53 @@
allow(organization).to receive(:storage_locations).and_return(fake_organization_storage_locations)
end

context 'when there are no issues' do
it 'should return a result object with success? returning true and the item' do
expect(subject).to be_a_kind_of(Result)
expect(subject.success?).to eq(true)
expect(subject.value).to eq(fake_organization_item)
context 'when enable_packs is enabled' do
context 'when there are no issues' do
it 'should return a result object with success? returning true and the item' do
Flipper.enable(:enable_packs)
allow(fake_organization_item).to receive(:sync_request_units!).with([])

expect(subject).to be_a_kind_of(Result)
expect(subject.success?).to eq(true)
expect(subject.value).to eq(fake_organization_item)
expect(fake_organization_item).to have_received(:save!)
expect(fake_organization_item).to have_received(:sync_request_units!)
end
end

it 'should execute the expected methods' do
# Invoke the subject aka call the service object call method
subject
context 'when an issue occurs in transaction' do
context 'because the organization_id does not match any Organization' do
before do
allow(Organization).to receive(:find).with(organization_id).and_raise(ActiveRecord::RecordNotFound)
end

# Assert that the service object calls the expected method.
expect(fake_organization_item).to have_received(:save!)
end
end
it 'should return a result object with an ActiveRecord::RecordNotFound error' do
Flipper.enable(:enable_packs)
allow(fake_organization_item).to receive(:sync_request_units!).with([])

context 'when an issue occurs in transaction' do
context 'because the organization_id does not match any Organization' do
before do
allow(Organization).to receive(:find).with(organization_id).and_raise(ActiveRecord::RecordNotFound)
expect(subject).to be_a_kind_of(Result)
expect(subject.success?).to eq(false)
expect(subject.error).to be_a_kind_of(ActiveRecord::RecordNotFound)
expect(fake_organization_item).not_to have_received(:sync_request_units!)
end
end

it 'should return a result object with an ActiveRecord::RecordNotFound error' do
expect(subject).to be_a_kind_of(Result)
expect(subject.success?).to eq(false)
expect(subject.error).to be_a_kind_of(ActiveRecord::RecordNotFound)
end
end
context 'because the item create raised an error' do
let(:fake_error) { StandardError.new('random-error') }

context 'because the item create raised an error' do
let(:fake_error) { StandardError.new('random-error') }
before do
allow(fake_organization_item).to receive(:save!).and_raise(fake_error)
end

before do
allow(fake_organization_item).to receive(:save!).and_raise(fake_error)
end
it 'should return a result object with the raised error' do
Flipper.enable(:enable_packs)
allow(fake_organization_item).to receive(:sync_request_units!).with([])

it 'should return a result object with the raised error' do
expect(subject).to be_a_kind_of(Result)
expect(subject.success?).to eq(false)
expect(subject.error).to eq(fake_error)
expect(subject).to be_a_kind_of(Result)
expect(subject.success?).to eq(false)
expect(subject.error).to eq(fake_error)
expect(fake_organization_item).not_to have_received(:sync_request_units!)
end
end
end
end
Expand Down
Loading
Loading