diff --git a/app/models/base_item.rb b/app/models/base_item.rb index d910a46f25..2623528887 100644 --- a/app/models/base_item.rb +++ b/app/models/base_item.rb @@ -29,4 +29,3 @@ def to_h { partner_key: partner_key, name: name } end end - diff --git a/app/services/kit_create_service.rb b/app/services/kit_create_service.rb index 80e976d2ee..b797af25fa 100644 --- a/app/services/kit_create_service.rb +++ b/app/services/kit_create_service.rb @@ -20,7 +20,7 @@ def call # kit item created. kit_base_item = fetch_or_create_kit_base_item - # Create the Item. + # Create the item item_creation = ItemCreateService.new( organization_id: organization.id, item_params: { @@ -100,4 +100,3 @@ def partner_key_for(name) "kit_#{name.underscore.gsub(/\s+/, '_')}" end end - diff --git a/spec/events/inventory_aggregate_spec.rb b/spec/events/inventory_aggregate_spec.rb index 4c65ad97c9..27dadc349b 100644 --- a/spec/events/inventory_aggregate_spec.rb +++ b/spec/events/inventory_aggregate_spec.rb @@ -381,11 +381,11 @@ end it "should process a kit allocation event" do - kit = FactoryBot.create(:kit, :with_item, organization: organization) + kit = create_kit(organization: organization, line_items_attributes: [ + {item_id: item1.id, quantity: 10}, + {item_id: item2.id, quantity: 3} + ]) - kit.line_items = [] - kit.line_items << build(:line_item, quantity: 10, item: item1, itemizable: kit) - kit.line_items << build(:line_item, quantity: 3, item: item2, itemizable: kit) KitAllocateEvent.publish(kit, storage_location1.id, 2) # 30 - (10*2) = 10, 10 - (3*2) = 4 @@ -416,7 +416,11 @@ end it "should process a kit deallocation event" do - kit = FactoryBot.create(:kit, :with_item, organization: organization) + kit = create_kit(organization: organization, line_items_attributes: [ + {item_id: item1.id, quantity: 20}, + {item_id: item2.id, quantity: 5} + ]) + TestInventory.create_inventory(organization, { storage_location1.id => { @@ -432,9 +436,6 @@ }) inventory = InventoryAggregate.inventory_for(organization.id) # reload - kit.line_items = [] - kit.line_items << build(:line_item, quantity: 20, item: item1, itemizable: kit) - kit.line_items << build(:line_item, quantity: 5, item: item2, itemizable: kit) KitDeallocateEvent.publish(kit, storage_location1, 2) # 30 + (20*2) = 70, 10 + (5*2) = 20 diff --git a/spec/factories/kits.rb b/spec/factories/kits.rb index ef81343014..28a4a3585d 100644 --- a/spec/factories/kits.rb +++ b/spec/factories/kits.rb @@ -13,19 +13,17 @@ # FactoryBot.define do factory :kit do - sequence(:name) { |n| "Test Kit #{n}" } + sequence(:name) { |n| "Default Kit Name #{n} - Don't Match" } organization after(:build) do |instance, _| if instance.line_items.blank? - instance.line_items << create(:line_item, item: create(:item, organization: instance.organization), itemizable: instance) + instance.line_items << build(:line_item, item: create(:item, organization: instance.organization), itemizable: nil) end end - trait :with_item do - after(:create) do |instance, _| - create(:item, kit: instance, organization: instance.organization) - end - end + # See #3652, changes to this factory are in progress + # For now, to create corresponding item and line item and persist to database call create_kit + # from spec/support/kit_helper.rb end end diff --git a/spec/models/item_spec.rb b/spec/models/item_spec.rb index c2ea38cf07..109ff4f191 100644 --- a/spec/models/item_spec.rb +++ b/spec/models/item_spec.rb @@ -236,9 +236,10 @@ end context "in a kit" do - let(:kit) { create(:kit, organization: organization) } before do - create(:line_item, itemizable: kit, item: item) + create_kit(organization: organization, line_items_attributes: [ + {item_id: item.id, quantity: 1} + ]) end it "should return false" do @@ -271,10 +272,10 @@ end context "in a kit" do - let(:kit) { create(:kit, organization: organization) } - before do - create(:line_item, itemizable: kit, item: item) + create_kit(organization: organization, line_items_attributes: [ + {item_id: item.id, quantity: 1} + ]) end it "should return false" do @@ -424,8 +425,9 @@ let(:kit) { create(:kit, name: "my kit") } it "updates kit name" do - item.update(name: "my new name") - expect(item.name).to eq kit.name + name = "my new name" + item.update(name: name) + expect(kit.name).to eq name end end diff --git a/spec/models/kit_spec.rb b/spec/models/kit_spec.rb index 989fb83ce2..45b9440b7e 100644 --- a/spec/models/kit_spec.rb +++ b/spec/models/kit_spec.rb @@ -58,10 +58,13 @@ end it "->alphabetized retrieves items in alphabetical order" do - kit_c = create(:kit, name: "KitC") - kit_b = create(:kit, name: "KitB") - kit_a = create(:kit, name: "KitA") - alphabetized_list = [kit_a.name, kit_b.name, kit_c.name] + a_name = "KitA" + b_name = "KitB" + c_name = "KitC" + create(:kit, name: c_name) + create(:kit, name: b_name) + create(:kit, name: a_name) + alphabetized_list = [a_name, b_name, c_name] expect(Kit.alphabetized.count).to eq(3) expect(Kit.alphabetized.map(&:name)).to eq(alphabetized_list) @@ -107,7 +110,7 @@ end describe '#can_deactivate?' do - let(:kit) { create(:kit, :with_item, organization: organization) } + let(:kit) { create(:kit, organization: organization) } context 'with inventory' do it 'should return false' do @@ -131,7 +134,7 @@ end specify 'deactivate and reactivate' do - kit = create(:kit, :with_item) + kit = create_kit(organization: organization) expect(kit.active).to eq(true) expect(kit.item.active).to eq(true) kit.deactivate diff --git a/spec/requests/kit_requests_spec.rb b/spec/requests/kit_requests_spec.rb index 35284743bb..1ac17d5935 100644 --- a/spec/requests/kit_requests_spec.rb +++ b/spec/requests/kit_requests_spec.rb @@ -3,7 +3,9 @@ let(:user) { create(:user, organization: organization) } let(:organization_admin) { create(:organization_admin, organization: organization) } - let!(:kit) { create(:kit, :with_item, organization: organization) } + let!(:kit) { + create_kit(organization: organization) + } describe "while signed in" do before do @@ -13,7 +15,7 @@ describe "GET #index" do before do # this shouldn't be shown - create(:kit, :with_item, active: false, name: "DOOBIE KIT", organization: organization) + create_kit(organization: organization, active: false, name: "DOOBIE KIT") end it "should include deactivate" do diff --git a/spec/services/reports/acquisition_report_service_spec.rb b/spec/services/reports/acquisition_report_service_spec.rb index 8ea62471d3..5f3ba09cf3 100644 --- a/spec/services/reports/acquisition_report_service_spec.rb +++ b/spec/services/reports/acquisition_report_service_spec.rb @@ -15,17 +15,20 @@ disposable_kit_item = create(:item, name: "Adult Disposable Diapers", partner_key: "adult diapers") another_disposable_kit_item = create(:item, name: "Infant Disposable Diapers", partner_key: "infant diapers") - disposable_line_item = create(:line_item, item: disposable_kit_item, quantity: 5) - another_disposable_line_item = create(:line_item, item: another_disposable_kit_item, quantity: 5) + disposable_kit = create_kit(organization: organization, line_items_attributes: [ + {item_id: disposable_kit_item.id, quantity: 5} + ]) - disposable_kit = create(:kit, :with_item, organization: organization, line_items: [disposable_line_item]) - another_disposable_kit = create(:kit, :with_item, organization: organization, line_items: [another_disposable_line_item]) + another_disposable_kit = create_kit(organization: organization, line_items_attributes: [ + {item_id: another_disposable_kit_item.id, quantity: 5} + ]) disposable_kit_item_distribution = create(:distribution, organization: organization, issued_at: within_time) another_disposable_kit_item_distribution = create(:distribution, organization: organization, issued_at: within_time) create(:line_item, :distribution, quantity: 10, item: disposable_kit.item, itemizable: disposable_kit_item_distribution) create(:line_item, :distribution, quantity: 10, item: another_disposable_kit.item, itemizable: another_disposable_kit_item_distribution) + # Total disposable items distributed so far: 5*10 + 5*10 = 100 # create disposable and non disposable items create(:base_item, name: "3T Diaper", partner_key: "toddler diapers", category: "disposable diaper") @@ -42,6 +45,7 @@ create_list(:line_item, 5, :distribution, quantity: 20, item: disposable_item, itemizable: dist) create_list(:line_item, 5, :distribution, quantity: 30, item: non_disposable_item, itemizable: dist) end + # Total disposable items distributed: (100) + 2*5*20 = 300 # Donations outside drives non_drive_donations = create_list(:donation, 2, @@ -155,9 +159,9 @@ it 'should return the proper results on #report' do expect(subject.report).to eq({ - entries: { "Disposable diapers distributed" => "320", + entries: { "Disposable diapers distributed" => "300", "Cloth diapers distributed" => "300", - "Average monthly disposable diapers distributed" => "27", + "Average monthly disposable diapers distributed" => "25", "Total product drives" => 2, "Disposable diapers collected from drives" => "600", "Cloth diapers collected from drives" => "900", diff --git a/spec/services/reports/children_served_report_service_spec.rb b/spec/services/reports/children_served_report_service_spec.rb index e660760693..e54268cb65 100644 --- a/spec/services/reports/children_served_report_service_spec.rb +++ b/spec/services/reports/children_served_report_service_spec.rb @@ -27,7 +27,7 @@ non_disposable_item = organization.items.where.not(id: organization.items.disposable).first # Kits - kit = create(:kit, :with_item, organization: organization) + kit = create(:kit, organization: organization) create(:base_item, name: "Toddler Disposable Diaper", partner_key: "toddler diapers", category: "disposable diaper") create(:base_item, name: "Infant Disposable Diaper", partner_key: "infant diapers", category: "infant disposable diaper") @@ -71,7 +71,7 @@ non_disposable_item = organization.items.where.not(id: organization.items.disposable).first # Kits - kit = create(:kit, :with_item, organization: organization) + kit = create(:kit, organization: organization) create(:base_item, name: "Toddler Disposable Diaper", partner_key: "toddler diapers", category: "disposable diaper") create(:base_item, name: "Infant Disposable Diaper", partner_key: "infant diapers", category: "infant disposable diaper") diff --git a/spec/support/kit_helper.rb b/spec/support/kit_helper.rb new file mode 100644 index 0000000000..958ab7b230 --- /dev/null +++ b/spec/support/kit_helper.rb @@ -0,0 +1,10 @@ +def create_kit(name: nil, active: true, organization: create(:organization), line_items_attributes: nil) + params = FactoryBot.attributes_for(:kit, active: active) + params[:name] = name if name + + params[:line_items_attributes] = (line_items_attributes || [ + {item_id: create(:item, organization: organization).id, quantity: 1} + ]) + + KitCreateService.new(organization_id: organization.id, kit_params: params).call.kit +end