Skip to content

Commit a5dc184

Browse files
authored
#4217 kit factory refactor (#4585)
* Update bundler version * REFACTOR clean up unused line item trait introduced in #4163 * REFACTOR replace kit :with_item with KitCreateService, hard code rspec * 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 #4217 for kits * FIX typo in params in donation and purchase controllers * Another typo in docs * DOCS add comment clarifying itemizable works with line items * REFACTOR rename Item:kits scope to :housing_a_kit, add rspecs * Clearer name * Add rspecs to test :housing_a_kit and :loose scopes * REFACTOR DRY up kit base_item, seed_base_items, prevent kit base_item deletion * Move seed_base_items code into one static function * Move kit base item creation code into one static function * Add code to prevent calling destroy on kit base item and corresponding rspec * Added comments - not sure about whether other base item request specs are useful or what the purpose of destroy is in the controller if it can't be called * Add rspec for item.is_in_kit? * Fix lint * rename findorcreatebaseitem to add ! * REFACTOR remove unused code childrenservedreportservice * All SQL code is duplicated in AcquisitionReportService * RSpec is close enough to what is in AcquisitionReportService Spec that it can be removed without merging in (only difference is Diapers - Adult Briefs category is not created, but that shouldn't matter because the SQL looks for %diaper% so this category isn't testing anything different) * Fix bin/setup so it's working with seed_base_item move * Revert "REFACTOR clean up unused line item trait introduced in #4163" This reverts commit ceec002. * Revert "FIX typo in params in donation and purchase controllers" This reverts commit 7cabfa6. * Revert "DOCS add comment clarifying itemizable works with line items" This reverts commit 1109170. * Revert "REFACTOR rename Item:kits scope to :housing_a_kit, add rspecs" This reverts commit b9b8755. * Revert "REFACTOR DRY up kit base_item, seed_base_items, prevent kit base_item deletion" This reverts commit 8c18e42. * Revert "Add rspec for item.is_in_kit?" This reverts commit e1e2c31. * Revert "REFACTOR remove unused code childrenservedreportservice" This reverts commit 141d959. * Revert "Fix bin/setup so it's working with seed_base_item move" This reverts commit 080721e. * REFACTOR move kit creation in specs into helper method * fix failing rspecs
1 parent 7450be0 commit a5dc184

File tree

9 files changed

+63
-40
lines changed

9 files changed

+63
-40
lines changed

spec/events/inventory_aggregate_spec.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -381,11 +381,11 @@
381381
end
382382

383383
it "should process a kit allocation event" do
384-
kit = FactoryBot.create(:kit, :with_item, organization: organization)
384+
kit = create_kit(organization: organization, line_items_attributes: [
385+
{item_id: item1.id, quantity: 10},
386+
{item_id: item2.id, quantity: 3}
387+
])
385388

386-
kit.line_items = []
387-
kit.line_items << build(:line_item, quantity: 10, item: item1, itemizable: kit)
388-
kit.line_items << build(:line_item, quantity: 3, item: item2, itemizable: kit)
389389
KitAllocateEvent.publish(kit, storage_location1.id, 2)
390390

391391
# 30 - (10*2) = 10, 10 - (3*2) = 4
@@ -416,7 +416,11 @@
416416
end
417417

418418
it "should process a kit deallocation event" do
419-
kit = FactoryBot.create(:kit, :with_item, organization: organization)
419+
kit = create_kit(organization: organization, line_items_attributes: [
420+
{item_id: item1.id, quantity: 20},
421+
{item_id: item2.id, quantity: 5}
422+
])
423+
420424
TestInventory.create_inventory(organization,
421425
{
422426
storage_location1.id => {

spec/factories/kits.rb

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,17 @@
1313
#
1414
FactoryBot.define do
1515
factory :kit do
16-
sequence(:name) { |n| "Test Kit #{n}" }
16+
sequence(:name) { |n| "Default Kit Name #{n} - Don't Match" }
1717
organization
1818

1919
after(:build) do |instance, _|
2020
if instance.line_items.blank?
21-
instance.line_items << create(:line_item, item: create(:item, organization: instance.organization), itemizable: instance)
21+
instance.line_items << build(:line_item, item: create(:item, organization: instance.organization), itemizable: nil)
2222
end
2323
end
2424

25-
trait :with_item do
26-
after(:create) do |instance, _|
27-
create(:item, kit: instance, organization: instance.organization)
28-
end
29-
end
25+
# See #3652, changes to this factory are in progress
26+
# For now, to create corresponding item and line item and persist to database call create_kit
27+
# from spec/support/kit_helper.rb
3028
end
3129
end

spec/models/item_spec.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,10 @@
249249
end
250250

251251
context "in a kit" do
252-
let(:kit) { create(:kit, organization: organization) }
253252
before do
254-
create(:line_item, itemizable: kit, item: item)
253+
create_kit(organization: organization, line_items_attributes: [
254+
{item_id: item.id, quantity: 1}
255+
])
255256
end
256257

257258
it "should return false" do
@@ -284,10 +285,10 @@
284285
end
285286

286287
context "in a kit" do
287-
let(:kit) { create(:kit, organization: organization) }
288-
289288
before do
290-
create(:line_item, itemizable: kit, item: item)
289+
create_kit(organization: organization, line_items_attributes: [
290+
{item_id: item.id, quantity: 1}
291+
])
291292
end
292293

293294
it "should return false" do
@@ -460,8 +461,9 @@
460461
let(:kit) { create(:kit, name: "my kit") }
461462

462463
it "updates kit name" do
463-
item.update(name: "my new name")
464-
expect(item.name).to eq kit.name
464+
name = "my new name"
465+
item.update(name: name)
466+
expect(kit.name).to eq name
465467
end
466468
end
467469

spec/models/kit_spec.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,13 @@
5858
end
5959

6060
it "->alphabetized retrieves items in alphabetical order" do
61-
kit_c = create(:kit, name: "KitC")
62-
kit_b = create(:kit, name: "KitB")
63-
kit_a = create(:kit, name: "KitA")
64-
alphabetized_list = [kit_a.name, kit_b.name, kit_c.name]
61+
a_name = "KitA"
62+
b_name = "KitB"
63+
c_name = "KitC"
64+
create(:kit, name: c_name)
65+
create(:kit, name: b_name)
66+
create(:kit, name: a_name)
67+
alphabetized_list = [a_name, b_name, c_name]
6568

6669
expect(Kit.alphabetized.count).to eq(3)
6770
expect(Kit.alphabetized.map(&:name)).to eq(alphabetized_list)
@@ -107,7 +110,7 @@
107110
end
108111

109112
describe '#can_deactivate?' do
110-
let(:kit) { create(:kit, :with_item, organization: organization) }
113+
let(:kit) { create_kit(organization: organization) }
111114

112115
context 'with inventory' do
113116
it 'should return false' do
@@ -132,7 +135,7 @@
132135
end
133136

134137
specify 'deactivate and reactivate' do
135-
kit = create(:kit, :with_item)
138+
kit = create_kit(organization: organization)
136139
expect(kit.active).to eq(true)
137140
expect(kit.item.active).to eq(true)
138141
kit.deactivate

spec/requests/kit_requests_spec.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
let(:user) { create(:user, organization: organization) }
44
let(:organization_admin) { create(:organization_admin, organization: organization) }
55

6-
let!(:kit) { create(:kit, :with_item, organization: organization) }
6+
let!(:kit) {
7+
create_kit(organization: organization)
8+
}
79

810
describe "while signed in" do
911
before do
@@ -13,7 +15,7 @@
1315
describe "GET #index" do
1416
before do
1517
# this shouldn't be shown
16-
create(:kit, :with_item, active: false, name: "DOOBIE KIT", organization: organization)
18+
create_kit(organization: organization, active: false, name: "DOOBIE KIT")
1719
end
1820

1921
it "should include deactivate" do

spec/services/reports/children_served_report_service_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
non_disposable_item = organization.items.where.not(id: organization.items.disposable).first
2828

2929
# Kits
30-
kit = create(:kit, :with_item, organization: organization)
30+
kit = create(:kit, organization: organization)
3131

3232
create(:base_item, name: "Toddler Disposable Diaper", partner_key: "toddler diapers", category: "disposable diaper")
3333
create(:base_item, name: "Infant Disposable Diaper", partner_key: "infant diapers", category: "infant disposable diaper")
@@ -71,7 +71,7 @@
7171
non_disposable_item = organization.items.where.not(id: organization.items.disposable).first
7272

7373
# Kits
74-
kit = create(:kit, :with_item, organization: organization)
74+
kit = create(:kit, organization: organization)
7575

7676
create(:base_item, name: "Toddler Disposable Diaper", partner_key: "toddler diapers", category: "disposable diaper")
7777
create(:base_item, name: "Infant Disposable Diaper", partner_key: "infant diapers", category: "infant disposable diaper")

spec/services/reports/diaper_report_service_spec.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,20 @@
1515
disposable_kit_item = create(:item, name: "Adult Disposable Diapers", partner_key: "adult diapers")
1616
another_disposable_kit_item = create(:item, name: "Infant Disposable Diapers", partner_key: "infant diapers")
1717

18-
disposable_line_item = create(:line_item, item: disposable_kit_item, quantity: 5)
19-
another_disposable_line_item = create(:line_item, item: another_disposable_kit_item, quantity: 5)
18+
disposable_kit = create_kit(organization: organization, line_items_attributes: [
19+
{item_id: disposable_kit_item.id, quantity: 5}
20+
])
2021

21-
disposable_kit = create(:kit, :with_item, organization: organization, line_items: [disposable_line_item])
22-
another_disposable_kit = create(:kit, :with_item, organization: organization, line_items: [another_disposable_line_item])
22+
another_disposable_kit = create_kit(organization: organization, line_items_attributes: [
23+
{item_id: another_disposable_kit_item.id, quantity: 5}
24+
])
2325

2426
disposable_kit_item_distribution = create(:distribution, organization: organization, issued_at: within_time)
2527
another_disposable_kit_item_distribution = create(:distribution, organization: organization, issued_at: within_time)
2628

2729
create(:line_item, :distribution, quantity: 10, item: disposable_kit.item, itemizable: disposable_kit_item_distribution)
2830
create(:line_item, :distribution, quantity: 10, item: another_disposable_kit.item, itemizable: another_disposable_kit_item_distribution)
31+
# Total disposable items distributed so far: 5*10 + 5*10 = 100
2932

3033
# create disposable and non disposable items
3134
create(:base_item, name: "3T Diaper", partner_key: "toddler diapers", category: "disposable diaper")
@@ -42,6 +45,7 @@
4245
create_list(:line_item, 5, :distribution, quantity: 20, item: disposable_item, itemizable: dist)
4346
create_list(:line_item, 5, :distribution, quantity: 30, item: non_disposable_item, itemizable: dist)
4447
end
48+
# Total disposable items distributed: (100) + 2*5*20 = 300
4549

4650
# Donations outside drives
4751
non_drive_donations = create_list(:donation, 2,
@@ -155,9 +159,9 @@
155159

156160
it "should return the proper results on #report" do
157161
expect(subject.report).to eq({
158-
entries: {"Disposable diapers distributed" => "320",
162+
entries: {"Disposable diapers distributed" => "300",
159163
"Cloth diapers distributed" => "300",
160-
"Average monthly disposable diapers distributed" => "27",
164+
"Average monthly disposable diapers distributed" => "25",
161165
"Total product drives" => 2,
162166
"Disposable diapers collected from drives" => "600",
163167
"Cloth diapers collected from drives" => "900",

spec/services/reports/period_supply_report_service_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@
3434
# Spec will ensure that only the required data is included.
3535

3636
# Kits
37-
period_supplies_kit = create(:kit, :with_item, organization: organization)
38-
another_period_supply_kit = create(:kit, :with_item, organization: organization)
39-
donated_period_supply_kit = create(:kit, :with_item, organization: organization)
40-
purchased_period_supply_kit = create(:kit, :with_item, organization: organization)
41-
pad_and_tampon_kit = create(:kit, :with_item, organization: organization)
37+
period_supplies_kit = create_kit(organization: organization)
38+
another_period_supply_kit = create_kit(organization: organization)
39+
donated_period_supply_kit = create_kit(organization: organization)
40+
purchased_period_supply_kit = create_kit(organization: organization)
41+
pad_and_tampon_kit = create_kit(organization: organization)
4242

4343
create(:base_item, name: "Adult Pads", partner_key: "adult pads", category: "Menstrual Supplies")
4444
create(:base_item, name: "Adult Tampons", partner_key: "adult tampons", category: "Menstrual Supplies")

spec/support/kit_helper.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def create_kit(name: nil, active: true, organization: create(:organization), line_items_attributes: nil)
2+
params = FactoryBot.attributes_for(:kit, active: active)
3+
params[:name] = name if name
4+
5+
params[:line_items_attributes] = (line_items_attributes || [
6+
{item_id: create(:item, organization: organization).id, quantity: 1}
7+
])
8+
9+
KitCreateService.new(organization_id: organization.id, kit_params: params).call.kit
10+
end

0 commit comments

Comments
 (0)