Skip to content

Commit

Permalink
REFACTOR replace kit :with_item with KitCreateService, hard code rspec
Browse files Browse the repository at this point in the history
* Replace :with_item trait with calls to KitCreateService to make it easy
  to change line_item itemizable behavior later
* Remove unnecessary :with_item traits
* Hard code all rspecs matching against kit values, finishes rubyforgood#4217 for kits
  • Loading branch information
jimmyli97 committed Aug 15, 2024
1 parent 23bc899 commit bfc744b
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 38 deletions.
23 changes: 15 additions & 8 deletions spec/events/inventory_aggregate_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -381,11 +381,14 @@
end

it "should process a kit allocation event" do
kit = FactoryBot.create(:kit, :with_item, organization: organization)
params = FactoryBot.attributes_for(:kit)
params[:line_items_attributes] = [
{item_id: item1.id, quantity: 10},
{item_id: item2.id, quantity: 3}
]

kit = KitCreateService.new(organization_id: organization.id, kit_params: params).call.kit

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
Expand Down Expand Up @@ -416,7 +419,14 @@
end

it "should process a kit deallocation event" do
kit = FactoryBot.create(:kit, :with_item, organization: organization)
params = FactoryBot.attributes_for(:kit)
params[:line_items_attributes] = [
{item_id: item1.id, quantity: 20},
{item_id: item2.id, quantity: 5}
]

kit = KitCreateService.new(organization_id: organization.id, kit_params: params).call.kit

TestInventory.create_inventory(organization,
{
storage_location1.id => {
Expand All @@ -432,9 +442,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
Expand Down
14 changes: 7 additions & 7 deletions spec/factories/kits.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@
#
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 KitCreateService with:
# kit_params = attributes_for(:kit)
# kit_params[:line_items_attributes] = [{ item_id: _id_, quantity: _quantity_, ...}]
# KitCreateService.new(organization_id: organization.id, kit_params: kit_params).call.kit
end
end
20 changes: 13 additions & 7 deletions spec/models/item_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,12 @@
end

context "in a kit" do
let(:kit) { create(:kit, organization: organization) }
before do
create(:line_item, itemizable: kit, item: item)
params = FactoryBot.attributes_for(:kit)
params[:line_items_attributes] = [
{item_id: item.id, quantity: 1}
]
KitCreateService.new(organization_id: organization.id, kit_params: params).call
end

it "should return false" do
Expand Down Expand Up @@ -271,10 +274,12 @@
end

context "in a kit" do
let(:kit) { create(:kit, organization: organization) }

before do
create(:line_item, itemizable: kit, item: item)
params = FactoryBot.attributes_for(:kit)
params[:line_items_attributes] = [
{item_id: item.id, quantity: 1}
]
KitCreateService.new(organization_id: organization.id, kit_params: params).call
end

it "should return false" do
Expand Down Expand Up @@ -424,8 +429,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

Expand Down
19 changes: 13 additions & 6 deletions spec/models/kit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -131,7 +134,11 @@
end

specify 'deactivate and reactivate' do
kit = create(:kit, :with_item)
params = FactoryBot.attributes_for(:kit)
params[:line_items_attributes] = [
{item_id: create(:item).id, quantity: 1}
]
kit = KitCreateService.new(organization_id: organization.id, kit_params: params).call.kit
expect(kit.active).to eq(true)
expect(kit.item.active).to eq(true)
kit.deactivate
Expand Down
14 changes: 12 additions & 2 deletions spec/requests/kit_requests_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
let(:user) { create(:user, organization: organization) }
let(:organization_admin) { create(:organization_admin, organization: organization) }

let!(:kit) { create(:kit, :with_item, organization: organization) }
let!(:kit) {
params = FactoryBot.attributes_for(:kit)
params[:line_items_attributes] = [
{item_id: create(:item).id, quantity: 1}
]
KitCreateService.new(organization_id: organization.id, kit_params: params).call.kit
}

describe "while signed in" do
before do
Expand All @@ -13,7 +19,11 @@
describe "GET #index" do
before do
# this shouldn't be shown
create(:kit, :with_item, active: false, name: "DOOBIE KIT", organization: organization)
params = FactoryBot.attributes_for(:kit, active: false, name: "DOOBIE KIT")
params[:line_items_attributes] = [
{item_id: create(:item).id, quantity: 1}
]
KitCreateService.new(organization_id: organization.id, kit_params: params).call
end

it "should include deactivate" do
Expand Down
20 changes: 14 additions & 6 deletions spec/services/reports/acquisition_report_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,24 @@
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)
params = FactoryBot.attributes_for(:kit)
params[:line_items_attributes] = [
{item_id: disposable_kit_item.id, quantity: 5}
]
disposable_kit = KitCreateService.new(organization_id: organization.id, kit_params: params).call.kit

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])
params = FactoryBot.attributes_for(:kit)
params[:line_items_attributes] = [
{item_id: another_disposable_kit_item.id, quantity: 5}
]
another_disposable_kit = KitCreateService.new(organization_id: organization.id, kit_params: params).call.kit

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")
Expand All @@ -42,6 +49,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,
Expand Down Expand Up @@ -155,9 +163,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",
Expand Down
4 changes: 2 additions & 2 deletions spec/services/reports/children_served_report_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit bfc744b

Please sign in to comment.