Skip to content

Commit 89753cf

Browse files
committed
Refactor low inventory spec
1 parent 644e2e5 commit 89753cf

File tree

1 file changed

+62
-54
lines changed

1 file changed

+62
-54
lines changed
Lines changed: 62 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
RSpec.describe LowInventoryQuery do
2-
subject { LowInventoryQuery.call(organization).map { |r| r.to_h.symbolize_keys } }
3-
42
let(:organization) { create :organization }
53
let(:storage_location) { create :storage_location, organization: organization }
64

75
let(:minimum_quantity) { 0 }
86
let(:recommended_quantity) { 0 }
9-
let(:inventory_item_quantity) { 100 }
7+
let(:current_quantity) { 100 }
108

119
let(:item) do
1210
create :item,
@@ -15,106 +13,116 @@
1513
on_hand_recommended_quantity: recommended_quantity
1614
end
1715

18-
let!(:purchase) {
19-
create :purchase,
20-
:with_items,
21-
organization: organization,
22-
storage_location: storage_location,
23-
item: item,
24-
item_quantity: inventory_item_quantity,
25-
issued_at: Time.current
26-
}
16+
before :each do
17+
TestInventory.create_inventory(organization, {storage_location.id => {item.id => current_quantity}})
18+
end
2719

28-
context "when minimum_quantity and recommended_quantity is nil" do
29-
let(:item) { create :item, organization: organization }
20+
context "when minimum_quantity and recommended_quantity are zero" do
21+
let(:minimum_quantity) { 0 }
22+
let(:recommended_quantity) { 0 }
3023

31-
it { is_expected.to eq [] }
24+
it "should return an empty array" do
25+
result = LowInventoryQuery.call(organization).map { |r| r.to_h.symbolize_keys }
26+
expect(result).to be_empty
27+
end
3228
end
3329

3430
context "when minimum_quantity is 0 and recommended_quantity is nil and item quantity is 0" do
35-
let(:item) { create :item, organization: organization }
3631
let(:minimum_quantity) { 0 }
32+
let(:current_quantity) { 0 }
3733

38-
# Use TestInventory to set up inventory directly, instead of creating
39-
# a purchase with 0 quantity items (which our validation now rejects).
40-
before do
41-
TestInventory.create_inventory(organization, {storage_location.id => {item.id => 0}})
34+
it "should return an empty array" do
35+
result = LowInventoryQuery.call(organization).map { |r| r.to_h.symbolize_keys }
36+
expect(result).to be_empty
4237
end
43-
44-
it { is_expected.to eq [] }
4538
end
4639

4740
context "when inventory quantity is over minimum quantity" do
4841
let(:minimum_quantity) { 50 }
42+
let(:current_quantity) { 100 }
4943

50-
it { is_expected.to eq [] }
44+
it "should return an empty array" do
45+
result = LowInventoryQuery.call(organization).map { |r| r.to_h.symbolize_keys }
46+
expect(result).to be_empty
47+
end
5148
end
5249

5350
context "when minimum_quantity is equal to quantity" do
5451
let(:minimum_quantity) { 100 }
52+
let(:current_quantity) { 100 }
5553

56-
it { is_expected.to eq [] }
54+
it "should return an empty array" do
55+
result = LowInventoryQuery.call(organization).map { |r| r.to_h.symbolize_keys }
56+
expect(result).to be_empty
57+
end
5758
end
5859

5960
context "when inventory quantity drops below minimum quantity" do
6061
let(:minimum_quantity) { 200 }
62+
let(:current_quantity) { 100 }
6163

62-
it {
63-
is_expected.to include({
64+
it "should include the item in the low inventory list" do
65+
result = LowInventoryQuery.call(organization).map { |r| r.to_h.symbolize_keys }
66+
expect(result).to include({
6467
id: item.id,
6568
name: item.name,
6669
on_hand_minimum_quantity: 200,
6770
on_hand_recommended_quantity: 0,
6871
total_quantity: 100
6972
})
70-
}
73+
end
7174
end
7275

7376
context "when inventory quantity equals recommended quantity" do
77+
let(:minimum_quantity) { 50 }
7478
let(:recommended_quantity) { 100 }
79+
let(:current_quantity) { 100 }
7580

76-
it { is_expected.to eq [] }
81+
it "should return an empty array" do
82+
result = LowInventoryQuery.call(organization).map { |r| r.to_h.symbolize_keys }
83+
expect(result).to be_empty
84+
end
7785
end
7886

7987
context "when inventory quantity drops below recommended quantity" do
88+
let(:minimum_quantity) { 50 }
8089
let(:recommended_quantity) { 200 }
90+
let(:current_quantity) { 75 }
8191

82-
it {
83-
is_expected.to include({
92+
it "should include the item in the low inventory list" do
93+
result = LowInventoryQuery.call(organization).map { |r| r.to_h.symbolize_keys }
94+
expect(result).to include({
8495
id: item.id,
8596
name: item.name,
86-
on_hand_minimum_quantity: 0,
97+
on_hand_minimum_quantity: 50,
8798
on_hand_recommended_quantity: 200,
88-
total_quantity: 100
99+
total_quantity: 75
89100
})
90-
}
101+
end
91102
end
92103

93104
context "when items are in multiple storage locations" do
94-
let(:recommended_quantity) { 300 }
105+
let(:minimum_quantity) { 50 }
106+
let(:recommended_quantity) { 55 }
107+
let(:current_quantity) { 40 }
95108
let(:secondary_storage_location) { create :storage_location, organization: organization }
96-
let!(:secondary_purchase) {
97-
create :purchase,
98-
:with_items,
99-
organization: organization,
100-
storage_location: secondary_storage_location,
101-
item: item,
102-
item_quantity: inventory_item_quantity,
103-
issued_at: Time.current
104-
}
105-
106-
it {
107-
expect(subject.count).to eq 1
108-
}
109-
110-
it {
111-
is_expected.to include({
109+
110+
it "should have no low inventory items when global total is above minimum" do
111+
TestInventory.create_inventory(organization, {secondary_storage_location.id => {item.id => 17}})
112+
result = LowInventoryQuery.call(organization).map { |r| r.to_h.symbolize_keys }
113+
expect(result).to be_empty
114+
end
115+
116+
it "should have no low inventory items when global total is below minimum" do
117+
TestInventory.create_inventory(organization, {secondary_storage_location.id => {item.id => 2}})
118+
result = LowInventoryQuery.call(organization).map { |r| r.to_h.symbolize_keys }
119+
expect(result).to include({
112120
id: item.id,
113121
name: item.name,
114-
on_hand_minimum_quantity: 0,
115-
on_hand_recommended_quantity: 300,
116-
total_quantity: 200
122+
on_hand_minimum_quantity: 50,
123+
on_hand_recommended_quantity: 55,
124+
total_quantity: 42
117125
})
118-
}
126+
end
119127
end
120128
end

0 commit comments

Comments
 (0)