Skip to content

Commit 0bb13f0

Browse files
committed
Fix flaky ItemizableUpdateService. Logic was order dependent and have removed the global Timecop freeze to be able to order Event entries.
1 parent 95cae51 commit 0bb13f0

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

spec/services/itemizable_update_service_spec.rb

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
let(:item1) { create(:item, organization: organization, name: "My Item 1") }
66
let(:item2) { create(:item, organization: organization, name: "My Item 2") }
77
let(:item3) { create(:item, organization: organization, name: "My Item 3") }
8+
89
before(:each) do
910
TestInventory.create_inventory(storage_location.organization, {
1011
storage_location.id => {
@@ -16,6 +17,9 @@
1617
item2.id => 10
1718
}
1819
})
20+
# Make sure any other events that come after have their own event_time
21+
# to avoid possible flaky tests due to event order
22+
travel 1.second
1923
end
2024

2125
around(:each) do |ex|
@@ -44,7 +48,7 @@
4448
}
4549
end
4650

47-
subject do
51+
def call_update_service
4852
described_class.call(itemizable: itemizable,
4953
params: attributes,
5054
event_class: DonationEvent)
@@ -53,7 +57,7 @@
5357
it "should update quantity in same storage location" do
5458
expect(storage_location.size).to eq(20)
5559
expect(new_storage_location.size).to eq(20)
56-
subject
60+
call_update_service
5761
expect(itemizable.reload.line_items.count).to eq(2)
5862
expect(itemizable.line_items.sum(&:quantity)).to eq(4)
5963
expect(storage_location.size).to eq(14)
@@ -65,7 +69,7 @@
6569
it "fails with a validation message for donation events with invalid issued_at" do
6670
attributes[:issued_at] = ""
6771

68-
expect { subject }.to raise_error do |e|
72+
expect { call_update_service }.to raise_error do |e|
6973
expect(e).to be_a(ActiveRecord::RecordInvalid)
7074
expect(e.message).to eq("Validation failed: Issue date can't be blank")
7175
end
@@ -75,7 +79,7 @@
7579
context "when there is no intervening audit" do
7680
it "should update quantity in different locations" do
7781
attributes[:storage_location_id] = new_storage_location.id
78-
subject
82+
call_update_service
7983
expect(itemizable.reload.line_items.count).to eq(2)
8084
expect(itemizable.line_items.sum(&:quantity)).to eq(4)
8185
expect(storage_location.size).to eq(10)
@@ -89,15 +93,15 @@
8993
"If you need to change the storage location, please delete this donation and create a new donation with the new storage location."
9094
create(:audit, :with_items, item: itemizable.items.first, organization: organization, storage_location: storage_location, status: "finalized")
9195
attributes[:storage_location_id] = new_storage_location.id
92-
expect { subject }.to raise_error(msg)
96+
expect { call_update_service }.to raise_error(msg)
9397
end
9498
end
9599
end
96100

97101
it "should raise an error if any item is inactive" do
98102
item1.update!(active: false)
99103
msg = "Update failed: The following items are currently inactive: My Item 1. Please reactivate them before continuing."
100-
expect { subject }.to raise_error(msg)
104+
expect { call_update_service }.to raise_error(msg)
101105
end
102106
end
103107

@@ -121,14 +125,14 @@
121125
}
122126
end
123127

124-
subject do
128+
def call_update_service
125129
described_class.call(itemizable: itemizable, params: attributes, event_class: DistributionEvent)
126130
end
127131

128132
it "should update quantity in same storage location" do
129133
expect(storage_location.size).to eq(20)
130134
expect(new_storage_location.size).to eq(20)
131-
subject
135+
call_update_service
132136
expect(itemizable.reload.line_items.count).to eq(2)
133137
expect(itemizable.line_items.sum(&:quantity)).to eq(4)
134138
expect(storage_location.size).to eq(26)
@@ -139,7 +143,7 @@
139143
it "fails with a validation message for distribution events with invalid issued_at" do
140144
attributes[:issued_at] = ""
141145

142-
expect { subject }.to raise_error do |e|
146+
expect { call_update_service }.to raise_error do |e|
143147
expect(e).to be_a(ActiveRecord::RecordInvalid)
144148
expect(e.message).to eq("Validation failed: Distribution date and time can't be blank")
145149
end
@@ -149,7 +153,7 @@
149153
context "when there is no intervening audit" do
150154
it "should update quantity in different locations" do
151155
attributes[:storage_location_id] = new_storage_location.id
152-
subject
156+
call_update_service
153157
expect(itemizable.reload.line_items.count).to eq(2)
154158
expect(itemizable.line_items.sum(&:quantity)).to eq(4)
155159
expect(storage_location.size).to eq(30)
@@ -163,15 +167,15 @@
163167
"If you need to change the storage location, please reclaim this distribution and create a new distribution from the new storage location."
164168
create(:audit, :with_items, item: itemizable.items.first, organization: organization, storage_location: storage_location, status: "finalized")
165169
attributes[:storage_location_id] = new_storage_location.id
166-
expect { subject }.to raise_error(msg)
170+
expect { call_update_service }.to raise_error(msg)
167171
end
168172
end
169173
end
170174

171175
it "should raise an error if any item is inactive" do
172176
item1.update!(active: false)
173177
msg = "Update failed: The following items are currently inactive: My Item 1. Please reactivate them before continuing."
174-
expect { subject }.to raise_error(msg)
178+
expect { call_update_service }.to raise_error(msg)
175179
end
176180
end
177181

@@ -194,6 +198,7 @@
194198
line_items_attributes: {"0": {item_id: item1.id, quantity: 5}, "1": {item_id: item3.id, quantity: 50}}
195199
}
196200
end
201+
197202
it "should send an itemizable event if it already exists" do
198203
DonationEvent.publish(itemizable)
199204
expect(DonationEvent.count).to eq(1)
@@ -216,14 +221,8 @@
216221
expect(View::Inventory.total_inventory(organization.id)).to eq(75) # 40 - 5 (item1) - 10 (item2) + 50 (item3)
217222
end
218223
end
224+
219225
describe "with distributions" do
220-
before(:each) do
221-
TestInventory.create_inventory(storage_location.organization, {
222-
storage_location.id => {
223-
item3.id => 10
224-
}
225-
})
226-
end
227226
let(:itemizable) do
228227
line_items = [
229228
create(:line_item, item_id: item1.id, quantity: 5),
@@ -241,6 +240,15 @@
241240
line_items_attributes: {"0": {item_id: item1.id, quantity: 2}, "1": {item_id: item3.id, quantity: 6}}
242241
}
243242
end
243+
244+
before(:each) do
245+
TestInventory.create_inventory(storage_location.organization, {
246+
storage_location.id => {
247+
item3.id => 10
248+
}
249+
})
250+
end
251+
244252
it "should send an itemizable event if it already exists" do
245253
DistributionEvent.publish(itemizable)
246254
expect(DistributionEvent.count).to eq(1)

0 commit comments

Comments
 (0)