|
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 | + |
8 | 9 | before(:each) do |
9 | 10 | TestInventory.create_inventory(storage_location.organization, { |
10 | 11 | storage_location.id => { |
|
16 | 17 | item2.id => 10 |
17 | 18 | } |
18 | 19 | }) |
| 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 |
19 | 23 | end |
20 | 24 |
|
21 | 25 | around(:each) do |ex| |
|
44 | 48 | } |
45 | 49 | end |
46 | 50 |
|
47 | | - subject do |
| 51 | + def call_update_service |
48 | 52 | described_class.call(itemizable: itemizable, |
49 | 53 | params: attributes, |
50 | 54 | event_class: DonationEvent) |
|
53 | 57 | it "should update quantity in same storage location" do |
54 | 58 | expect(storage_location.size).to eq(20) |
55 | 59 | expect(new_storage_location.size).to eq(20) |
56 | | - subject |
| 60 | + call_update_service |
57 | 61 | expect(itemizable.reload.line_items.count).to eq(2) |
58 | 62 | expect(itemizable.line_items.sum(&:quantity)).to eq(4) |
59 | 63 | expect(storage_location.size).to eq(14) |
|
65 | 69 | it "fails with a validation message for donation events with invalid issued_at" do |
66 | 70 | attributes[:issued_at] = "" |
67 | 71 |
|
68 | | - expect { subject }.to raise_error do |e| |
| 72 | + expect { call_update_service }.to raise_error do |e| |
69 | 73 | expect(e).to be_a(ActiveRecord::RecordInvalid) |
70 | 74 | expect(e.message).to eq("Validation failed: Issue date can't be blank") |
71 | 75 | end |
|
75 | 79 | context "when there is no intervening audit" do |
76 | 80 | it "should update quantity in different locations" do |
77 | 81 | attributes[:storage_location_id] = new_storage_location.id |
78 | | - subject |
| 82 | + call_update_service |
79 | 83 | expect(itemizable.reload.line_items.count).to eq(2) |
80 | 84 | expect(itemizable.line_items.sum(&:quantity)).to eq(4) |
81 | 85 | expect(storage_location.size).to eq(10) |
|
89 | 93 | "If you need to change the storage location, please delete this donation and create a new donation with the new storage location." |
90 | 94 | create(:audit, :with_items, item: itemizable.items.first, organization: organization, storage_location: storage_location, status: "finalized") |
91 | 95 | attributes[:storage_location_id] = new_storage_location.id |
92 | | - expect { subject }.to raise_error(msg) |
| 96 | + expect { call_update_service }.to raise_error(msg) |
93 | 97 | end |
94 | 98 | end |
95 | 99 | end |
96 | 100 |
|
97 | 101 | it "should raise an error if any item is inactive" do |
98 | 102 | item1.update!(active: false) |
99 | 103 | 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) |
101 | 105 | end |
102 | 106 | end |
103 | 107 |
|
|
121 | 125 | } |
122 | 126 | end |
123 | 127 |
|
124 | | - subject do |
| 128 | + def call_update_service |
125 | 129 | described_class.call(itemizable: itemizable, params: attributes, event_class: DistributionEvent) |
126 | 130 | end |
127 | 131 |
|
128 | 132 | it "should update quantity in same storage location" do |
129 | 133 | expect(storage_location.size).to eq(20) |
130 | 134 | expect(new_storage_location.size).to eq(20) |
131 | | - subject |
| 135 | + call_update_service |
132 | 136 | expect(itemizable.reload.line_items.count).to eq(2) |
133 | 137 | expect(itemizable.line_items.sum(&:quantity)).to eq(4) |
134 | 138 | expect(storage_location.size).to eq(26) |
|
139 | 143 | it "fails with a validation message for distribution events with invalid issued_at" do |
140 | 144 | attributes[:issued_at] = "" |
141 | 145 |
|
142 | | - expect { subject }.to raise_error do |e| |
| 146 | + expect { call_update_service }.to raise_error do |e| |
143 | 147 | expect(e).to be_a(ActiveRecord::RecordInvalid) |
144 | 148 | expect(e.message).to eq("Validation failed: Distribution date and time can't be blank") |
145 | 149 | end |
|
149 | 153 | context "when there is no intervening audit" do |
150 | 154 | it "should update quantity in different locations" do |
151 | 155 | attributes[:storage_location_id] = new_storage_location.id |
152 | | - subject |
| 156 | + call_update_service |
153 | 157 | expect(itemizable.reload.line_items.count).to eq(2) |
154 | 158 | expect(itemizable.line_items.sum(&:quantity)).to eq(4) |
155 | 159 | expect(storage_location.size).to eq(30) |
|
163 | 167 | "If you need to change the storage location, please reclaim this distribution and create a new distribution from the new storage location." |
164 | 168 | create(:audit, :with_items, item: itemizable.items.first, organization: organization, storage_location: storage_location, status: "finalized") |
165 | 169 | attributes[:storage_location_id] = new_storage_location.id |
166 | | - expect { subject }.to raise_error(msg) |
| 170 | + expect { call_update_service }.to raise_error(msg) |
167 | 171 | end |
168 | 172 | end |
169 | 173 | end |
170 | 174 |
|
171 | 175 | it "should raise an error if any item is inactive" do |
172 | 176 | item1.update!(active: false) |
173 | 177 | 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) |
175 | 179 | end |
176 | 180 | end |
177 | 181 |
|
|
194 | 198 | line_items_attributes: {"0": {item_id: item1.id, quantity: 5}, "1": {item_id: item3.id, quantity: 50}} |
195 | 199 | } |
196 | 200 | end |
| 201 | + |
197 | 202 | it "should send an itemizable event if it already exists" do |
198 | 203 | DonationEvent.publish(itemizable) |
199 | 204 | expect(DonationEvent.count).to eq(1) |
|
234 | 239 | expect(DonationEvent.count).to eq(2) |
235 | 240 | end |
236 | 241 | end |
| 242 | + |
237 | 243 | 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 |
245 | 244 | let(:itemizable) do |
246 | 245 | line_items = [ |
247 | 246 | create(:line_item, item_id: item1.id, quantity: 5), |
|
259 | 258 | line_items_attributes: {"0": {item_id: item1.id, quantity: 2}, "1": {item_id: item3.id, quantity: 6}} |
260 | 259 | } |
261 | 260 | 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 | + |
262 | 270 | it "should send an itemizable event if it already exists" do |
263 | 271 | DistributionEvent.publish(itemizable) |
264 | 272 | expect(DistributionEvent.count).to eq(1) |
|
0 commit comments