|
5 | 5 | let(:item1) { create(:item, organization: organization, name: "My Item 1") } |
6 | 6 | let(:item2) { create(:item, organization: organization, name: "My Item 2") } |
7 | 7 | let(:item3) { create(:item, organization: organization, name: "My Item 3") } |
| 8 | + let(:two_days_ago) { 2.days.ago } # Store exact timestamp to be able to assert later |
8 | 9 | before(:each) do |
9 | 10 | TestInventory.create_inventory(storage_location.organization, { |
10 | 11 | storage_location.id => { |
|
18 | 19 | }) |
19 | 20 | end |
20 | 21 |
|
21 | | - around(:each) do |ex| |
22 | | - freeze_time do |
23 | | - ex.run |
24 | | - end |
25 | | - end |
26 | | - |
27 | 22 | describe "increases" do |
28 | 23 | let(:itemizable) do |
29 | 24 | line_items = [ |
|
39 | 34 |
|
40 | 35 | let(:attributes) do |
41 | 36 | { |
42 | | - issued_at: 2.days.ago, |
| 37 | + issued_at: two_days_ago, |
43 | 38 | line_items_attributes: {"0": {item_id: item1.id, quantity: 2}, "1": {item_id: item2.id, quantity: 2}} |
44 | 39 | } |
45 | 40 | end |
46 | 41 |
|
47 | | - subject do |
| 42 | + def call_update_service |
48 | 43 | described_class.call(itemizable: itemizable, |
49 | 44 | params: attributes, |
50 | 45 | event_class: DonationEvent) |
|
53 | 48 | it "should update quantity in same storage location" do |
54 | 49 | expect(storage_location.size).to eq(20) |
55 | 50 | expect(new_storage_location.size).to eq(20) |
56 | | - subject |
| 51 | + call_update_service |
57 | 52 | expect(itemizable.reload.line_items.count).to eq(2) |
58 | 53 | expect(itemizable.line_items.sum(&:quantity)).to eq(4) |
59 | 54 | expect(storage_location.size).to eq(14) |
60 | 55 | expect(new_storage_location.size).to eq(20) |
61 | | - expect(itemizable.issued_at).to eq(2.days.ago) |
| 56 | + expect(itemizable.issued_at.to_fs(:db)).to eq(two_days_ago.to_fs(:db)) # Use to_fs to ignore nanosecond differences |
62 | 57 | expect(UpdateExistingEvent.count).to eq(1) |
63 | 58 | end |
64 | 59 |
|
65 | 60 | it "fails with a validation message for donation events with invalid issued_at" do |
66 | 61 | attributes[:issued_at] = "" |
67 | 62 |
|
68 | | - expect { subject }.to raise_error do |e| |
| 63 | + expect { call_update_service }.to raise_error do |e| |
69 | 64 | expect(e).to be_a(ActiveRecord::RecordInvalid) |
70 | 65 | expect(e.message).to eq("Validation failed: Issue date can't be blank") |
71 | 66 | end |
|
75 | 70 | context "when there is no intervening audit" do |
76 | 71 | it "should update quantity in different locations" do |
77 | 72 | attributes[:storage_location_id] = new_storage_location.id |
78 | | - subject |
| 73 | + call_update_service |
79 | 74 | expect(itemizable.reload.line_items.count).to eq(2) |
80 | 75 | expect(itemizable.line_items.sum(&:quantity)).to eq(4) |
81 | 76 | expect(storage_location.size).to eq(10) |
|
89 | 84 | "If you need to change the storage location, please delete this donation and create a new donation with the new storage location." |
90 | 85 | create(:audit, :with_items, item: itemizable.items.first, organization: organization, storage_location: storage_location, status: "finalized") |
91 | 86 | attributes[:storage_location_id] = new_storage_location.id |
92 | | - expect { subject }.to raise_error(msg) |
| 87 | + expect { call_update_service }.to raise_error(msg) |
93 | 88 | end |
94 | 89 | end |
95 | 90 | end |
96 | 91 |
|
97 | 92 | it "should raise an error if any item is inactive" do |
98 | 93 | item1.update!(active: false) |
99 | 94 | msg = "Update failed: The following items are currently inactive: My Item 1. Please reactivate them before continuing." |
100 | | - expect { subject }.to raise_error(msg) |
| 95 | + expect { call_update_service }.to raise_error(msg) |
101 | 96 | end |
102 | 97 | end |
103 | 98 |
|
|
116 | 111 |
|
117 | 112 | let(:attributes) do |
118 | 113 | { |
119 | | - issued_at: 2.days.ago, |
| 114 | + issued_at: two_days_ago, |
120 | 115 | line_items_attributes: {"0": {item_id: item1.id, quantity: 2}, "1": {item_id: item2.id, quantity: 2}} |
121 | 116 | } |
122 | 117 | end |
123 | 118 |
|
124 | | - subject do |
| 119 | + def call_update_service |
125 | 120 | described_class.call(itemizable: itemizable, params: attributes, event_class: DistributionEvent) |
126 | 121 | end |
127 | 122 |
|
128 | 123 | it "should update quantity in same storage location" do |
129 | 124 | expect(storage_location.size).to eq(20) |
130 | 125 | expect(new_storage_location.size).to eq(20) |
131 | | - subject |
| 126 | + call_update_service |
132 | 127 | expect(itemizable.reload.line_items.count).to eq(2) |
133 | 128 | expect(itemizable.line_items.sum(&:quantity)).to eq(4) |
134 | 129 | expect(storage_location.size).to eq(26) |
135 | 130 | expect(new_storage_location.size).to eq(20) |
136 | | - expect(itemizable.issued_at).to eq(2.days.ago) |
| 131 | + expect(itemizable.issued_at.to_fs(:db)).to eq(two_days_ago.to_fs(:db)) # Use to_fs to ignore nanosecond differences |
137 | 132 | end |
138 | 133 |
|
139 | 134 | it "fails with a validation message for distribution events with invalid issued_at" do |
140 | 135 | attributes[:issued_at] = "" |
141 | 136 |
|
142 | | - expect { subject }.to raise_error do |e| |
| 137 | + expect { call_update_service }.to raise_error do |e| |
143 | 138 | expect(e).to be_a(ActiveRecord::RecordInvalid) |
144 | 139 | expect(e.message).to eq("Validation failed: Distribution date and time can't be blank") |
145 | 140 | end |
|
149 | 144 | context "when there is no intervening audit" do |
150 | 145 | it "should update quantity in different locations" do |
151 | 146 | attributes[:storage_location_id] = new_storage_location.id |
152 | | - subject |
| 147 | + call_update_service |
153 | 148 | expect(itemizable.reload.line_items.count).to eq(2) |
154 | 149 | expect(itemizable.line_items.sum(&:quantity)).to eq(4) |
155 | 150 | expect(storage_location.size).to eq(30) |
|
163 | 158 | "If you need to change the storage location, please reclaim this distribution and create a new distribution from the new storage location." |
164 | 159 | create(:audit, :with_items, item: itemizable.items.first, organization: organization, storage_location: storage_location, status: "finalized") |
165 | 160 | attributes[:storage_location_id] = new_storage_location.id |
166 | | - expect { subject }.to raise_error(msg) |
| 161 | + expect { call_update_service }.to raise_error(msg) |
167 | 162 | end |
168 | 163 | end |
169 | 164 | end |
170 | 165 |
|
171 | 166 | it "should raise an error if any item is inactive" do |
172 | 167 | item1.update!(active: false) |
173 | 168 | msg = "Update failed: The following items are currently inactive: My Item 1. Please reactivate them before continuing." |
174 | | - expect { subject }.to raise_error(msg) |
| 169 | + expect { call_update_service }.to raise_error(msg) |
175 | 170 | end |
176 | 171 | end |
177 | 172 |
|
|
188 | 183 | line_items: line_items, |
189 | 184 | issued_at: 1.day.ago) |
190 | 185 | end |
| 186 | + |
191 | 187 | let(:attributes) do |
192 | 188 | { |
193 | | - issued_at: 2.days.ago, |
| 189 | + issued_at: two_days_ago, |
194 | 190 | line_items_attributes: {"0": {item_id: item1.id, quantity: 5}, "1": {item_id: item3.id, quantity: 50}} |
195 | 191 | } |
196 | 192 | end |
| 193 | + |
197 | 194 | it "should send an itemizable event if it already exists" do |
198 | 195 | DonationEvent.publish(itemizable) |
199 | 196 | expect(DonationEvent.count).to eq(1) |
|
216 | 213 | expect(View::Inventory.total_inventory(organization.id)).to eq(75) # 40 - 5 (item1) - 10 (item2) + 50 (item3) |
217 | 214 | end |
218 | 215 | end |
| 216 | + |
219 | 217 | 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 |
227 | 218 | let(:itemizable) do |
228 | 219 | line_items = [ |
229 | 220 | create(:line_item, item_id: item1.id, quantity: 5), |
|
235 | 226 | line_items: line_items, |
236 | 227 | issued_at: 1.day.ago) |
237 | 228 | end |
| 229 | + |
238 | 230 | let(:attributes) do |
239 | 231 | { |
240 | | - issued_at: 2.days.ago, |
| 232 | + issued_at: two_days_ago, |
241 | 233 | line_items_attributes: {"0": {item_id: item1.id, quantity: 2}, "1": {item_id: item3.id, quantity: 6}} |
242 | 234 | } |
243 | 235 | end |
| 236 | + |
| 237 | + before(:each) do |
| 238 | + TestInventory.create_inventory(storage_location.organization, { |
| 239 | + storage_location.id => { |
| 240 | + item3.id => 10 |
| 241 | + } |
| 242 | + }) |
| 243 | + end |
| 244 | + |
244 | 245 | it "should send an itemizable event if it already exists" do |
245 | 246 | DistributionEvent.publish(itemizable) |
246 | 247 | expect(DistributionEvent.count).to eq(1) |
|
264 | 265 | end |
265 | 266 |
|
266 | 267 | it "should raise an error if there is an intervening snapshot" do |
267 | | - itemizable.save! |
268 | | - travel(-1.week) |
269 | | - SnapshotEvent.publish(organization) |
270 | | - travel 1.week |
| 268 | + travel(-1.week) do |
| 269 | + SnapshotEvent.publish(organization) |
| 270 | + end |
271 | 271 | itemizable.update!(created_at: 2.weeks.ago) |
272 | 272 | expect do |
273 | 273 | described_class.call(itemizable: itemizable, params: attributes, event_class: DistributionEvent) |
|
276 | 276 |
|
277 | 277 | it "should not raise an error if no inventory was changed" do |
278 | 278 | no_change_attrs = { |
279 | | - issued_at: 2.days.ago, |
| 279 | + issued_at: two_days_ago, |
280 | 280 | line_items_attributes: { |
281 | 281 | "0": {item_id: item1.id, quantity: 10}, |
282 | 282 | "1": {item_id: item2.id, quantity: 10} |
283 | 283 | } |
284 | 284 | } |
285 | | - itemizable.save! |
286 | | - travel(-1.week) |
287 | | - SnapshotEvent.publish(organization) |
288 | | - travel 1.week |
| 285 | + travel(-1.week) do |
| 286 | + SnapshotEvent.publish(organization) |
| 287 | + end |
289 | 288 | itemizable.update!(created_at: 2.weeks.ago) |
290 | 289 | expect do |
291 | 290 | described_class.call(itemizable: itemizable, params: no_change_attrs, event_class: DistributionEvent) |
|
0 commit comments