From 6d48fd0ae498349c53550050384fb5bcde48c0d9 Mon Sep 17 00:00:00 2001 From: Stefanni Brasil Date: Mon, 27 Jul 2026 15:34:32 -0600 Subject: [PATCH 1/6] Add unit test for sync_request_units! --- spec/models/item_spec.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/spec/models/item_spec.rb b/spec/models/item_spec.rb index a6471def4b..59c0a2edd8 100644 --- a/spec/models/item_spec.rb +++ b/spec/models/item_spec.rb @@ -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 From eb52f1d13473adb1849975e439202cb38bb98eab Mon Sep 17 00:00:00 2001 From: Stefanni Brasil Date: Mon, 27 Jul 2026 15:34:54 -0600 Subject: [PATCH 2/6] Add coverage for ItemCreateService when :enable_packs is enabled --- spec/services/item_create_service_spec.rb | 98 +++++++++++++++++------ 1 file changed, 72 insertions(+), 26 deletions(-) diff --git a/spec/services/item_create_service_spec.rb b/spec/services/item_create_service_spec.rb index 8c8d3eb555..67986891d9 100644 --- a/spec/services/item_create_service_spec.rb +++ b/spec/services/item_create_service_spec.rb @@ -26,46 +26,92 @@ 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 disabled' do + 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) + expect(fake_organization_item).to have_received(:save!) + 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 + + 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 - # Assert that the service object calls the expected method. - expect(fake_organization_item).to have_received(:save!) + 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 + + 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) + end + end end end - 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 + 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([]) - 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) + 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 - context 'because the item create raised an error' do - let(:fake_error) { StandardError.new('random-error') } + 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 + + 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([]) - before do - allow(fake_organization_item).to receive(:save!).and_raise(fake_error) + 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 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) + 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 + + 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([]) + + 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 From efce228eab68b17ec89b0ad5f5534308cb72ecce Mon Sep 17 00:00:00 2001 From: Stefanni Brasil Date: Tue, 28 Jul 2026 15:02:49 -0600 Subject: [PATCH 3/6] Add coverage for ItemRequest and RequestsTotalItemsServices with enable_packs enabled and disabled --- spec/models/partners/item_request_spec.rb | 108 +++++++++++++++ .../requests_total_items_service_spec.rb | 128 +++++++++++++++--- 2 files changed, 219 insertions(+), 17 deletions(-) diff --git a/spec/models/partners/item_request_spec.rb b/spec/models/partners/item_request_spec.rb index 9d63cde2e4..b5912a33c6 100644 --- a/spec/models/partners/item_request_spec.rb +++ b/spec/models/partners/item_request_spec.rb @@ -48,6 +48,114 @@ 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 + + context 'when enable_packs is disabled' do + context 'when there is a request unit' do + it 'returns only the quantity' do + 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') + end + end + + context 'when there is no request unit' do + it 'returns only the quantity' do + 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 + + context 'when enable_packs is disabled' do + context 'when there is a request unit' do + it 'returns only the item_name' do + 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') + end + end + + context 'when there is no request unit' do + it 'returns only the item_name' do + 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 diff --git a/spec/services/requests_total_items_service_spec.rb b/spec/services/requests_total_items_service_spec.rb index 2ad28ba24a..1ed026bfc7 100644 --- a/spec/services/requests_total_items_service_spec.rb +++ b/spec/services/requests_total_items_service_spec.rb @@ -21,16 +21,56 @@ Request.where(id: local_requests.map(&:id)) end - it 'return items with correct quantities calculated' do - expect(subject.first.last).to eq(80) + context 'when enable_packs is enabled' do + context 'when not all items have request units' do + it 'return items with correct quantities calculated, by individual items' do + Flipper.enable(:enable_packs) + + expect(subject).to eq({"item_name_0" => 20, "item_name_0 - bundles" => 60, "item_name_1" => 20, "item_name_1 - bundles" => 60, "item_name_2" => 20, "item_name_2 - bundles" => 60}) + end + end + + context 'when the items have request units' do + it 'return items with correct quantities calculated, grouped by packs' do + Flipper.enable(:enable_packs) + requests = [ + create(:request, :with_item_requests, request_items: item_ids.map { |k| { "item_id" => k, "quantity" => 20, "request_unit" => "bundle"} }), + create(:request, :with_item_requests, request_items: item_ids.map { |k| { "item_id" => k, "quantity" => 10, "request_unit" => "bundle" } }), + create(:request, :with_item_requests, request_items: item_ids.map { |k| { "item_id" => k, "quantity" => 50, "request_unit" => "bundle" } }) + ] + Request.where(id: requests.map(&:id)) + + result = RequestsTotalItemsService.new(requests: requests).calculate + + expect(result).to eq({"item_name_0 - bundles" => 80, "item_name_1 - bundles" => 80, "item_name_2 - bundles" => 80}) + end + end end - it 'return the names of items correctly' do - expect(subject.keys).to eq([ - "item_name_0", - "item_name_1", - "item_name_2" - ]) + context 'when enable_packs is disabled' do + context 'when not all items have request units' do + it 'return items with correct quantities calculated, grouped by packs' do + Flipper.disable(:enable_packs) + + expect(subject).to eq({"item_name_0" => 80, "item_name_1" => 80, "item_name_2" => 80}) + end + end + + context 'when the items have request units' do + it 'return items with correct quantities calculated, grouped by packs' do + Flipper.disable(:enable_packs) + requests = [ + create(:request, :with_item_requests, request_items: item_ids.map { |k| { "item_id" => k, "quantity" => 20, "request_unit" => "bundle"} }), + create(:request, :with_item_requests, request_items: item_ids.map { |k| { "item_id" => k, "quantity" => 10, "request_unit" => "bundle" } }), + create(:request, :with_item_requests, request_items: item_ids.map { |k| { "item_id" => k, "quantity" => 50, "request_unit" => "bundle" } }) + ] + Request.where(id: requests.map(&:id)) + + result = RequestsTotalItemsService.new(requests: requests).calculate + + expect(result).to eq({"item_name_0" => 80, "item_name_1" => 80, "item_name_2" => 80}) + end + end end context 'when custom request units are specified and enabled' do @@ -75,18 +115,72 @@ end context 'when request item belongs to deleted item' do - let(:item) { create(:item, :with_unit, name: "Diaper", organization:, unit: "pack") } - let!(:requests) do - request = create(:request, :with_item_requests, request_items: [{"item_id" => item.id, "quantity" => 10, "request_unit" => "pack"}]) - Request.where(id: request.id) - end + context 'when enable_packs is enabled' do + context 'when the request unit is present' do + it 'returns item with correct quantity calculated' do + item = create(:item, :with_unit, name: "Diaper", organization:, unit: "pack") + request = create( + :request, + :with_item_requests, + request_items: [{"item_id" => item.id, "quantity" => 10, "request_unit" => "pack"}] + ) + item.destroy - before do - item.destroy + Flipper.enable(:enable_packs) + + expect(RequestsTotalItemsService.new(requests: [request]).calculate).to eq({"Diaper - packs" => 10}) + end + end + + context 'when the request unit is not present' do + it 'returns item with correct quantity calculated, without the request unit' do + item = create(:item, :with_unit, name: "Diaper", organization:, unit: "pack") + request = create( + :request, + :with_item_requests, + request_items: [{"item_id" => item.id, "quantity" => 10}] + ) + item.destroy + + Flipper.enable(:enable_packs) + + expect(RequestsTotalItemsService.new(requests: [request]).calculate).to eq({"Diaper" => 10}) + end + end end - it 'returns item with correct quantity calculated' do - expect(subject).to eq({"Diaper" => 10}) + context 'when enable_packs is disabled' do + context 'when the request unit is present' do + it 'returns item with correct quantity calculated, without the request unit' do + item = create(:item, :with_unit, name: "Diaper", organization:, unit: "pack") + request = create( + :request, + :with_item_requests, + request_items: [{"item_id" => item.id, "quantity" => 10, "request_unit" => "pack"}] + ) + item.destroy + + Flipper.disable(:enable_packs) + + expect(RequestsTotalItemsService.new(requests: [request]).calculate).to eq({"Diaper" => 10}) + end + end + + context 'when the request unit is not present' do + it 'returns item with correct quantity calculated, without the request unit' do + item = create(:item, :with_unit, name: "Diaper", organization:, unit: "pack") + request = create( + :request, + :with_item_requests, + request_items: [{"item_id" => item.id, "quantity" => 10}] + ) + item.destroy + + Flipper.disable(:enable_packs) + + expect(RequestsTotalItemsService.new(requests: [request]).calculate).to eq({"Diaper" => 10}) + end + end end end end From 906abf208fa6cdd6207c1f8d34c4842b8b83c19e Mon Sep 17 00:00:00 2001 From: Stefanni Brasil Date: Tue, 28 Jul 2026 15:15:57 -0600 Subject: [PATCH 4/6] Add coverage for DistributionPdf when enable_packs is enabled and disabled --- spec/pdfs/distribution_pdf_spec.rb | 201 ++++++++++++++++++++++------- 1 file changed, 154 insertions(+), 47 deletions(-) diff --git a/spec/pdfs/distribution_pdf_spec.rb b/spec/pdfs/distribution_pdf_spec.rb index 24dba9f6e2..800b1f8c3e 100644 --- a/spec/pdfs/distribution_pdf_spec.rb +++ b/spec/pdfs/distribution_pdf_spec.rb @@ -48,7 +48,85 @@ 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" 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 + + context "when enable_packs is disabled" do + it "hides value and package columns when true on organization, and does not include request unit info" do + Flipper.disable(: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", ""], + ["", "", ""], + ["Total Items Received", 200, 150] + ]) + end + + it "hides value columns when true on organization, and does not include request unit info" do + Flipper.disable(: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", "", 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) @@ -57,13 +135,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) @@ -72,64 +152,91 @@ ["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 "when enable_packs is disabled" do + it "hides value and package columns when true on organization, and does not include request unit info" do + Flipper.disable(:enable_packs) - 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) + 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, and does not include request unit info" do + Flipper.disable(: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", "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"] + ["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 end + + context "regardless of request data" do + describe "#hide_columns" do + 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 + + context "when enable_packs is disabled" do + it "hides package column when true on organization, and does not include request unit info" do + Flipper.disable(: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", "", "$4.00", nil], + ["", "", "", "", ""], + ["Total Items Received", 200, 150, "", "$250.00"] + ]) + end + end + end + end end describe "address pdf output" do From f722576c2ef184f9aa3be3ef87fceb1f4d711914 Mon Sep 17 00:00:00 2001 From: Stefanni Brasil Date: Tue, 28 Jul 2026 15:22:25 -0600 Subject: [PATCH 5/6] Explicitely disable enable_packs for tests --- spec/models/partners/item_request_spec.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/spec/models/partners/item_request_spec.rb b/spec/models/partners/item_request_spec.rb index b5912a33c6..12a7178098 100644 --- a/spec/models/partners/item_request_spec.rb +++ b/spec/models/partners/item_request_spec.rb @@ -81,6 +81,8 @@ context 'when enable_packs is disabled' do context 'when there is a request unit' do it 'returns only the quantity' do + Flipper.disable(:enable_packs) + item = create(:item, organization: organization) create(:item_unit, item:, name: 'flat') request = create(:request, organization: organization) @@ -92,6 +94,8 @@ context 'when there is no request unit' do it 'returns only the quantity' do + Flipper.disable(:enable_packs) + item = create(:item, organization: organization) create(:item_unit, item:, name: 'flat') request = create(:request, organization: organization) @@ -135,6 +139,8 @@ context 'when enable_packs is disabled' do context 'when there is a request unit' do it 'returns only the item_name' do + Flipper.disable(:enable_packs) + item = create(:item, organization: organization, name: 'Item name') create(:item_unit, item:, name: 'flat') request = create(:request, organization: organization) @@ -146,6 +152,8 @@ context 'when there is no request unit' do it 'returns only the item_name' do + Flipper.disable(:enable_packs) + item = create(:item, organization: organization, name: 'Item name') create(:item_unit, item:, name: 'flat') request = create(:request, organization: organization) From 62903446152b28c855ecb552407cf3897045d138 Mon Sep 17 00:00:00 2001 From: Stefanni Brasil Date: Tue, 28 Jul 2026 15:30:42 -0600 Subject: [PATCH 6/6] Better test setup organization --- spec/pdfs/distribution_pdf_spec.rb | 2 +- spec/services/item_create_service_spec.rb | 4 ++++ .../requests_total_items_service_spec.rb | 18 ++++++++++-------- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/spec/pdfs/distribution_pdf_spec.rb b/spec/pdfs/distribution_pdf_spec.rb index 800b1f8c3e..2cc6ea7e98 100644 --- a/spec/pdfs/distribution_pdf_spec.rb +++ b/spec/pdfs/distribution_pdf_spec.rb @@ -49,7 +49,7 @@ context "with request data" do describe "#hide_columns" do context "when enable_packs is enabled" do - it "hides value and package columns when true on organization" 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) diff --git a/spec/services/item_create_service_spec.rb b/spec/services/item_create_service_spec.rb index 67986891d9..4221b73281 100644 --- a/spec/services/item_create_service_spec.rb +++ b/spec/services/item_create_service_spec.rb @@ -29,6 +29,8 @@ context 'when enable_packs is disabled' do context 'when there are no issues' do it 'should return a result object with success? returning true and the item' do + Flipper.disable(:enable_packs) + expect(subject).to be_a_kind_of(Result) expect(subject.success?).to eq(true) expect(subject.value).to eq(fake_organization_item) @@ -43,6 +45,8 @@ end it 'should return a result object with an ActiveRecord::RecordNotFound error' do + Flipper.disable(:enable_packs) + expect(subject).to be_a_kind_of(Result) expect(subject.success?).to eq(false) expect(subject.error).to be_a_kind_of(ActiveRecord::RecordNotFound) diff --git a/spec/services/requests_total_items_service_spec.rb b/spec/services/requests_total_items_service_spec.rb index 1ed026bfc7..d3c0ab9908 100644 --- a/spec/services/requests_total_items_service_spec.rb +++ b/spec/services/requests_total_items_service_spec.rb @@ -33,6 +33,7 @@ context 'when the items have request units' do it 'return items with correct quantities calculated, grouped by packs' do Flipper.enable(:enable_packs) + requests = [ create(:request, :with_item_requests, request_items: item_ids.map { |k| { "item_id" => k, "quantity" => 20, "request_unit" => "bundle"} }), create(:request, :with_item_requests, request_items: item_ids.map { |k| { "item_id" => k, "quantity" => 10, "request_unit" => "bundle" } }), @@ -59,6 +60,7 @@ context 'when the items have request units' do it 'return items with correct quantities calculated, grouped by packs' do Flipper.disable(:enable_packs) + requests = [ create(:request, :with_item_requests, request_items: item_ids.map { |k| { "item_id" => k, "quantity" => 20, "request_unit" => "bundle"} }), create(:request, :with_item_requests, request_items: item_ids.map { |k| { "item_id" => k, "quantity" => 10, "request_unit" => "bundle" } }), @@ -118,6 +120,8 @@ context 'when enable_packs is enabled' do context 'when the request unit is present' do it 'returns item with correct quantity calculated' do + Flipper.enable(:enable_packs) + item = create(:item, :with_unit, name: "Diaper", organization:, unit: "pack") request = create( :request, @@ -126,14 +130,14 @@ ) item.destroy - Flipper.enable(:enable_packs) - expect(RequestsTotalItemsService.new(requests: [request]).calculate).to eq({"Diaper - packs" => 10}) end end context 'when the request unit is not present' do it 'returns item with correct quantity calculated, without the request unit' do + Flipper.enable(:enable_packs) + item = create(:item, :with_unit, name: "Diaper", organization:, unit: "pack") request = create( :request, @@ -142,8 +146,6 @@ ) item.destroy - Flipper.enable(:enable_packs) - expect(RequestsTotalItemsService.new(requests: [request]).calculate).to eq({"Diaper" => 10}) end end @@ -152,6 +154,8 @@ context 'when enable_packs is disabled' do context 'when the request unit is present' do it 'returns item with correct quantity calculated, without the request unit' do + Flipper.disable(:enable_packs) + item = create(:item, :with_unit, name: "Diaper", organization:, unit: "pack") request = create( :request, @@ -160,14 +164,14 @@ ) item.destroy - Flipper.disable(:enable_packs) - expect(RequestsTotalItemsService.new(requests: [request]).calculate).to eq({"Diaper" => 10}) end end context 'when the request unit is not present' do it 'returns item with correct quantity calculated, without the request unit' do + Flipper.disable(:enable_packs) + item = create(:item, :with_unit, name: "Diaper", organization:, unit: "pack") request = create( :request, @@ -176,8 +180,6 @@ ) item.destroy - Flipper.disable(:enable_packs) - expect(RequestsTotalItemsService.new(requests: [request]).calculate).to eq({"Diaper" => 10}) end end