Skip to content

Commit ef48193

Browse files
authored
Fix flaky ItemizableUpdateService by making sure that the setup event is first one second earlier than other events that come later as time is frozen. (#5479)
1 parent fe7c4c7 commit ef48193

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)
@@ -234,14 +239,8 @@
234239
expect(DonationEvent.count).to eq(2)
235240
end
236241
end
242+
237243
describe "with distributions" do
238-
before(:each) do
239-
TestInventory.create_inventory(storage_location.organization, {
240-
storage_location.id => {
241-
item3.id => 10
242-
}
243-
})
244-
end
245244
let(:itemizable) do
246245
line_items = [
247246
create(:line_item, item_id: item1.id, quantity: 5),
@@ -259,6 +258,15 @@
259258
line_items_attributes: {"0": {item_id: item1.id, quantity: 2}, "1": {item_id: item3.id, quantity: 6}}
260259
}
261260
end
261+
262+
before(:each) do
263+
TestInventory.create_inventory(storage_location.organization, {
264+
storage_location.id => {
265+
item3.id => 10
266+
}
267+
})
268+
end
269+
262270
it "should send an itemizable event if it already exists" do
263271
DistributionEvent.publish(itemizable)
264272
expect(DistributionEvent.count).to eq(1)

0 commit comments

Comments
 (0)