|
1 | 1 | RSpec.describe LowInventoryQuery do |
2 | | - subject { LowInventoryQuery.call(organization).map { |r| r.to_h.symbolize_keys } } |
3 | | - |
4 | 2 | let(:organization) { create :organization } |
5 | 3 | let(:storage_location) { create :storage_location, organization: organization } |
6 | 4 |
|
7 | 5 | let(:minimum_quantity) { 0 } |
8 | 6 | let(:recommended_quantity) { 0 } |
9 | | - let(:inventory_item_quantity) { 100 } |
| 7 | + let(:current_quantity) { 100 } |
10 | 8 |
|
11 | 9 | let(:item) do |
12 | 10 | create :item, |
|
15 | 13 | on_hand_recommended_quantity: recommended_quantity |
16 | 14 | end |
17 | 15 |
|
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 |
27 | 19 |
|
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 } |
30 | 23 |
|
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 |
32 | 28 | end |
33 | 29 |
|
34 | 30 | context "when minimum_quantity is 0 and recommended_quantity is nil and item quantity is 0" do |
35 | | - let(:item) { create :item, organization: organization } |
36 | 31 | let(:minimum_quantity) { 0 } |
37 | | - let(:inventory_item_quantity) { 0 } |
| 32 | + let(:current_quantity) { 0 } |
38 | 33 |
|
39 | | - it { is_expected.to eq [] } |
| 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 |
| 37 | + end |
40 | 38 | end |
41 | 39 |
|
42 | 40 | context "when inventory quantity is over minimum quantity" do |
43 | 41 | let(:minimum_quantity) { 50 } |
| 42 | + let(:current_quantity) { 100 } |
44 | 43 |
|
45 | | - 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 |
46 | 48 | end |
47 | 49 |
|
48 | 50 | context "when minimum_quantity is equal to quantity" do |
49 | 51 | let(:minimum_quantity) { 100 } |
| 52 | + let(:current_quantity) { 100 } |
50 | 53 |
|
51 | | - 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 |
52 | 58 | end |
53 | 59 |
|
54 | 60 | context "when inventory quantity drops below minimum quantity" do |
55 | 61 | let(:minimum_quantity) { 200 } |
| 62 | + let(:current_quantity) { 100 } |
56 | 63 |
|
57 | | - it { |
58 | | - 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({ |
59 | 67 | id: item.id, |
60 | 68 | name: item.name, |
61 | 69 | on_hand_minimum_quantity: 200, |
62 | 70 | on_hand_recommended_quantity: 0, |
63 | 71 | total_quantity: 100 |
64 | 72 | }) |
65 | | - } |
| 73 | + end |
66 | 74 | end |
67 | 75 |
|
68 | 76 | context "when inventory quantity equals recommended quantity" do |
| 77 | + let(:minimum_quantity) { 50 } |
69 | 78 | let(:recommended_quantity) { 100 } |
| 79 | + let(:current_quantity) { 100 } |
70 | 80 |
|
71 | | - 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 |
72 | 85 | end |
73 | 86 |
|
74 | 87 | context "when inventory quantity drops below recommended quantity" do |
| 88 | + let(:minimum_quantity) { 50 } |
75 | 89 | let(:recommended_quantity) { 200 } |
| 90 | + let(:current_quantity) { 75 } |
76 | 91 |
|
77 | | - it { |
78 | | - 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({ |
79 | 95 | id: item.id, |
80 | 96 | name: item.name, |
81 | | - on_hand_minimum_quantity: 0, |
| 97 | + on_hand_minimum_quantity: 50, |
82 | 98 | on_hand_recommended_quantity: 200, |
83 | | - total_quantity: 100 |
| 99 | + total_quantity: 75 |
84 | 100 | }) |
85 | | - } |
| 101 | + end |
86 | 102 | end |
87 | 103 |
|
88 | 104 | context "when items are in multiple storage locations" do |
89 | | - let(:recommended_quantity) { 300 } |
| 105 | + let(:minimum_quantity) { 50 } |
| 106 | + let(:recommended_quantity) { 55 } |
| 107 | + let(:current_quantity) { 40 } |
90 | 108 | let(:secondary_storage_location) { create :storage_location, organization: organization } |
91 | | - let!(:secondary_purchase) { |
92 | | - create :purchase, |
93 | | - :with_items, |
94 | | - organization: organization, |
95 | | - storage_location: secondary_storage_location, |
96 | | - item: item, |
97 | | - item_quantity: inventory_item_quantity, |
98 | | - issued_at: Time.current |
99 | | - } |
100 | | - |
101 | | - it { |
102 | | - expect(subject.count).to eq 1 |
103 | | - } |
104 | | - |
105 | | - it { |
106 | | - 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({ |
107 | 120 | id: item.id, |
108 | 121 | name: item.name, |
109 | | - on_hand_minimum_quantity: 0, |
110 | | - on_hand_recommended_quantity: 300, |
111 | | - total_quantity: 200 |
| 122 | + on_hand_minimum_quantity: 50, |
| 123 | + on_hand_recommended_quantity: 55, |
| 124 | + total_quantity: 42 |
112 | 125 | }) |
113 | | - } |
| 126 | + end |
114 | 127 | end |
115 | 128 | end |
0 commit comments