Skip to content

Commit

Permalink
refactor: remove seeded item reliance (events) (#4337)
Browse files Browse the repository at this point in the history
* refactor: remove seeded item reliance

* refactor: clean up tests

Use itemizable and improve donation/kit creation
  • Loading branch information
elasticspoon authored May 12, 2024
1 parent 7326a15 commit 0803246
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 49 deletions.
95 changes: 48 additions & 47 deletions spec/events/inventory_aggregate_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
RSpec.describe InventoryAggregate do
let(:organization) { FactoryBot.create(:organization) }
RSpec.describe InventoryAggregate, skip_seed: true do
let(:organization) { FactoryBot.create(:organization, :with_items) }
let(:storage_location1) { FactoryBot.create(:storage_location, organization: organization) }
let(:storage_location2) { FactoryBot.create(:storage_location, organization: organization) }
let(:item1) { FactoryBot.create(:item, organization: organization) }
Expand All @@ -25,8 +25,8 @@

it "should process a donation event" do
donation = FactoryBot.create(:donation, organization: organization, storage_location: storage_location1)
donation.line_items << build(:line_item, quantity: 50, item: item1)
donation.line_items << build(:line_item, quantity: 30, item: item2)
donation.line_items << build(:line_item, quantity: 50, item: item1, itemizable: donation)
donation.line_items << build(:line_item, quantity: 30, item: item2, itemizable: donation)
DonationEvent.publish(donation)

# 30 + 50 = 80, 10 + 30 = 40
Expand Down Expand Up @@ -56,8 +56,8 @@

it "should process a distribution event" do
dist = FactoryBot.create(:distribution, organization: organization, storage_location: storage_location1)
dist.line_items << build(:line_item, quantity: 20, item: item1)
dist.line_items << build(:line_item, quantity: 5, item: item2)
dist.line_items << build(:line_item, quantity: 20, item: item1, itemizable: dist)
dist.line_items << build(:line_item, quantity: 5, item: item2, itemizable: dist)
DistributionEvent.publish(dist)

# 30 - 20 = 10, 10 - 5 = 5
Expand Down Expand Up @@ -88,8 +88,8 @@
it "should process a donation event after a new storage location is created" do
new_loc = FactoryBot.create(:storage_location, organization: organization)
donation = FactoryBot.create(:donation, organization: organization, storage_location: new_loc)
donation.line_items << build(:line_item, quantity: 20, item: item1)
donation.line_items << build(:line_item, quantity: 5, item: item2)
donation.line_items << build(:line_item, quantity: 20, item: item1, itemizable: donation)
donation.line_items << build(:line_item, quantity: 5, item: item2, itemizable: donation)
DonationEvent.publish(donation)

# 30 - 20 = 10, 10 - 5 = 5
Expand Down Expand Up @@ -126,8 +126,8 @@

it "should process a donation destroyed event" do
donation = FactoryBot.create(:donation, organization: organization, storage_location: storage_location1)
donation.line_items << build(:line_item, quantity: 20, item: item1)
donation.line_items << build(:line_item, quantity: 5, item: item2)
donation.line_items << build(:line_item, quantity: 20, item: item1, itemizable: donation)
donation.line_items << build(:line_item, quantity: 5, item: item2, itemizable: donation)
DonationEvent.publish(donation)
DonationDestroyEvent.publish(donation)

Expand Down Expand Up @@ -158,8 +158,8 @@

it "should process a purchase destroyed event" do
purchase = FactoryBot.create(:purchase, organization: organization, storage_location: storage_location1)
purchase.line_items << build(:line_item, quantity: 20, item: item1)
purchase.line_items << build(:line_item, quantity: 5, item: item2)
purchase.line_items << build(:line_item, quantity: 20, item: item1, itemizable: purchase)
purchase.line_items << build(:line_item, quantity: 5, item: item2, itemizable: purchase)
PurchaseEvent.publish(purchase)
PurchaseDestroyEvent.publish(purchase)

Expand Down Expand Up @@ -191,8 +191,8 @@

it "should process an adjustment event" do
adjustment = FactoryBot.create(:adjustment, organization: organization, storage_location: storage_location1)
adjustment.line_items << build(:line_item, quantity: 20, item: item1)
adjustment.line_items << build(:line_item, quantity: -5, item: item2)
adjustment.line_items << build(:line_item, quantity: 20, item: item1, itemizable: adjustment)
adjustment.line_items << build(:line_item, quantity: -5, item: item2, itemizable: adjustment)
AdjustmentEvent.publish(adjustment)

# 30 + 20 = 50, 10 - 5 = 5
Expand Down Expand Up @@ -222,8 +222,8 @@

it "should process a purchase event" do
purchase = FactoryBot.create(:purchase, organization: organization, storage_location: storage_location1)
purchase.line_items << build(:line_item, quantity: 50, item: item1)
purchase.line_items << build(:line_item, quantity: 30, item: item2)
purchase.line_items << build(:line_item, quantity: 50, item: item1, itemizable: purchase)
purchase.line_items << build(:line_item, quantity: 30, item: item2, itemizable: purchase)
PurchaseEvent.publish(purchase)

# 30 + 50 = 80, 10 + 30 = 40
Expand Down Expand Up @@ -253,8 +253,8 @@

it "should process a distribution destroyed event" do
dist = FactoryBot.create(:distribution, organization: organization, storage_location: storage_location1)
dist.line_items << build(:line_item, quantity: 20, item: item1)
dist.line_items << build(:line_item, quantity: 10, item: item2)
dist.line_items << build(:line_item, quantity: 20, item: item1, itemizable: dist)
dist.line_items << build(:line_item, quantity: 10, item: item2, itemizable: dist)
DistributionEvent.publish(dist)
DistributionDestroyEvent.publish(dist)

Expand Down Expand Up @@ -286,8 +286,8 @@

it "should process a transfer event" do
transfer = FactoryBot.create(:transfer, organization: organization, from: storage_location1, to: storage_location2)
transfer.line_items << build(:line_item, quantity: 20, item: item1)
transfer.line_items << build(:line_item, quantity: 5, item: item2)
transfer.line_items << build(:line_item, quantity: 20, item: item1, itemizable: transfer)
transfer.line_items << build(:line_item, quantity: 5, item: item2, itemizable: transfer)
TransferEvent.publish(transfer)

# 30 - 20 = 10, 10 - 5 = 5
Expand Down Expand Up @@ -319,8 +319,8 @@

it "should process a transfer destroy event" do
transfer = FactoryBot.create(:transfer, organization: organization, from: storage_location2, to: storage_location1)
transfer.line_items << build(:line_item, quantity: 5, item: item2)
transfer.line_items << build(:line_item, quantity: 3, item: item3)
transfer.line_items << build(:line_item, quantity: 5, item: item2, itemizable: transfer)
transfer.line_items << build(:line_item, quantity: 3, item: item3, itemizable: transfer)
TransferEvent.publish(transfer)
TransferDestroyEvent.publish(transfer)

Expand Down Expand Up @@ -352,8 +352,8 @@

it "should process an audit event" do
audit = FactoryBot.create(:audit, organization: organization, storage_location: storage_location1)
audit.line_items << build(:line_item, quantity: 20, item: item1)
audit.line_items << build(:line_item, quantity: 10, item: item3)
audit.line_items << build(:line_item, quantity: 20, item: item1, itemizable: audit)
audit.line_items << build(:line_item, quantity: 10, item: item3, itemizable: audit)
AuditEvent.publish(audit)

described_class.handle(AuditEvent.last, inventory)
Expand Down Expand Up @@ -382,9 +382,10 @@

it "should process a kit allocation event" do
kit = FactoryBot.create(:kit, :with_item, organization: organization)

kit.line_items = []
kit.line_items << build(:line_item, quantity: 10, item: item1)
kit.line_items << build(:line_item, quantity: 3, item: item2)
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 @@ -432,8 +433,8 @@
inventory = InventoryAggregate.inventory_for(organization.id) # reload

kit.line_items = []
kit.line_items << build(:line_item, quantity: 20, item: item1)
kit.line_items << build(:line_item, quantity: 5, item: item2)
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 Expand Up @@ -499,29 +500,29 @@
it "should process multiple events" do
item4 = FactoryBot.create(:item, organization: organization)
donation = FactoryBot.create(:donation, organization: organization, storage_location: storage_location1)
donation.line_items << build(:line_item, quantity: 50, item: item1)
donation.line_items << build(:line_item, quantity: 30, item: item2)
donation.line_items << build(:line_item, quantity: 50, item: item1, itemizable: donation)
donation.line_items << build(:line_item, quantity: 30, item: item2, itemizable: donation)
DonationEvent.publish(donation)

donation2 = FactoryBot.create(:donation, organization: organization, storage_location: storage_location1)
donation2.line_items << build(:line_item, quantity: 30, item: item1)
donation2.line_items << build(:line_item, quantity: 30, item: item1, itemizable: donation)
DonationEvent.publish(donation2)

donation3 = FactoryBot.create(:donation, organization: organization, storage_location: storage_location2)
donation3.line_items << build(:line_item, quantity: 50, item: item2)
donation3.line_items << build(:line_item, quantity: 50, item: item2, itemizable: donation)
DonationEvent.publish(donation3)

# correction event
donation3.line_items = [build(:line_item, quantity: 40, item: item2)]
donation3.line_items << build(:line_item, quantity: 50, item: item4)
donation3.line_items = [build(:line_item, quantity: 40, item: item2, itemizable: donation)]
donation3.line_items << build(:line_item, quantity: 50, item: item4, itemizable: donation)
DonationEvent.publish(donation3)

dist = FactoryBot.create(:distribution, organization: organization, storage_location: storage_location1)
dist.line_items << build(:line_item, quantity: 10, item: item1)
dist.line_items << build(:line_item, quantity: 10, item: item1, itemizable: dist)
DistributionEvent.publish(dist)

dist2 = FactoryBot.create(:distribution, organization: organization, storage_location: storage_location2)
dist2.line_items << build(:line_item, quantity: 15, item: item2)
dist2.line_items << build(:line_item, quantity: 15, item: item2, itemizable: dist)
DistributionEvent.publish(dist2)

result = InventoryAggregate.inventory_for(organization.id)
Expand All @@ -548,15 +549,15 @@

it "should handle changing storage location" do
donation = FactoryBot.create(:donation, organization: organization, storage_location: storage_location1)
donation.line_items << build(:line_item, quantity: 50, item: item2)
donation.line_items << build(:line_item, quantity: 50, item: item2, itemizable: donation)
DonationEvent.publish(donation)

donation2 = FactoryBot.create(:donation, organization: organization, storage_location: storage_location1)
donation2.line_items << build(:line_item, quantity: 50, item: item1)
donation2.line_items << build(:line_item, quantity: 50, item: item1, itemizable: donation)
DonationEvent.publish(donation2)

donation2.update!(storage_location_id: storage_location2.id)
donation2.line_items = [build(:line_item, quantity: 30, item: item1)]
donation2.line_items = [build(:line_item, quantity: 30, item: item1, itemizable: donation)]
DonationEvent.publish(donation2)

result = InventoryAggregate.inventory_for(organization.id)
Expand All @@ -582,15 +583,15 @@

it "should handle intervening audits" do
donation = FactoryBot.create(:donation, organization: organization, storage_location: storage_location1)
donation.line_items << build(:line_item, quantity: 30, item: item1)
donation.line_items << build(:line_item, quantity: 30, item: item1, itemizable: donation)
DonationEvent.publish(donation)

dist = FactoryBot.create(:distribution, organization: organization, storage_location: storage_location1)
dist.line_items << build(:line_item, quantity: 10, item: item1)
dist.line_items << build(:line_item, quantity: 10, item: item1, itemizable: dist)
DistributionEvent.publish(dist)

audit = FactoryBot.create(:audit, organization: organization, storage_location: storage_location1)
audit.line_items << build(:line_item, quantity: 50, item: item1)
audit.line_items << build(:line_item, quantity: 50, item: item1, itemizable: audit)
AuditEvent.publish(audit)

dist.line_items[0].quantity = 40 # this should be a NOW event and remove another 30
Expand All @@ -612,11 +613,11 @@

it "should handle timing correctly" do
donation = FactoryBot.create(:donation, organization: organization, storage_location: storage_location1)
donation.line_items << build(:line_item, quantity: 30, item: item1)
donation.line_items << build(:line_item, quantity: 30, item: item1, itemizable: donation)
DonationEvent.publish(donation)

dist = FactoryBot.create(:distribution, organization: organization, storage_location: storage_location1)
dist.line_items << build(:line_item, quantity: 10, item: item1)
dist.line_items << build(:line_item, quantity: 10, item: item1, itemizable: dist)
DistributionEvent.publish(dist)

# correction event
Expand Down Expand Up @@ -644,8 +645,8 @@
it "should ignore unusable snapshots" do
freeze_time do
donation = FactoryBot.create(:donation, organization: organization, storage_location: storage_location1)
donation.line_items << build(:line_item, quantity: 50, item: item1)
donation.line_items << build(:line_item, quantity: 30, item: item2)
donation.line_items << build(:line_item, quantity: 50, item: item1, itemizable: donation)
donation.line_items << build(:line_item, quantity: 30, item: item2, itemizable: donation)
DonationEvent.publish(donation)

travel 1.minute
Expand All @@ -668,7 +669,7 @@

travel 1.minute
# correction event - should ruin the snapshot since it's updating a previous event
donation.line_items = [build(:line_item, quantity: 40, item: item1)]
donation.line_items = [build(:line_item, quantity: 40, item: item1, itemizable: donation)]
event = DonationEvent.publish(donation)
event.update!(event_time: donation.created_at)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/distributions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
.first
&.db_item
item = evaluator.item || event_item
instance.line_items << build(:line_item, quantity: evaluator.item_quantity, item: item)
instance.line_items << build(:line_item, quantity: evaluator.item_quantity, item: item, itemizable: instance)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/kits.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

after(:build) do |instance, _|
if instance.line_items.blank?
instance.line_items << create(:line_item, item: create(:item, organization: instance.organization))
instance.line_items << create(:line_item, item: create(:item, organization: instance.organization), itemizable: instance)
end
end

Expand Down

0 comments on commit 0803246

Please sign in to comment.