Skip to content

Commit 71491f9

Browse files
adds tests to support csv export for audits
1 parent f945f13 commit 71491f9

File tree

3 files changed

+97
-6
lines changed

3 files changed

+97
-6
lines changed

spec/models/audit_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,32 @@
127127
expect(Audit.finalized_since?(xfer3, storage_location4)).to be false # no audits at location
128128
expect(Audit.finalized_since?(xfer3, storage_location5)).to be false # since status isn't finalized
129129
end
130+
131+
describe ".generate_csv_from_inventory" do
132+
let!(:audit) { create(:audit, :with_items, organization: organization) }
133+
let(:sl) { audit.storage_location }
134+
let(:inventory) { View::Inventory.new(organization.id) }
135+
136+
it "generates a CSV" do
137+
csv_data = described_class.generate_csv_from_inventory([audit], inventory)
138+
139+
expect(csv_data).to be_a(String)
140+
141+
table = CSV.parse(csv_data)
142+
header = table.first
143+
row = table.second
144+
145+
expect(header).to eq(described_class.csv_export_headers)
146+
147+
expect(row[0]).to eq(audit.updated_at.strftime("%B %d %Y"))
148+
expect(row[1]).to eq(audit.status)
149+
expect(row[2]).to eq(sl.name)
150+
expect(row[3]).to eq(sl.address)
151+
expect(row[4]).to eq(sl.square_footage.to_s)
152+
expect(row[5]).to eq(sl.warehouse_type.to_s)
153+
expect(row[6]).to eq("0")
154+
end
155+
end
130156
end
131157

132158
describe "versioning" do

spec/requests/audits_requests_spec.rb

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
RSpec.describe "Audits", type: :request do
22
let(:organization) { create(:organization) }
33
let(:organization_admin) { create(:organization_admin, organization: organization) }
4-
4+
let(:storage_location) { create(:storage_location, organization: organization) }
55
let(:valid_attributes) do
66
{
77
organization_id: organization.id,
8-
storage_location_id: create(:storage_location, organization: organization).id,
8+
storage_location_id: storage_location.id,
99
user_id: create(:organization_admin, organization: organization).id
1010
}
1111
end
@@ -30,10 +30,70 @@
3030
end
3131

3232
describe "GET #index" do
33-
it "is successful" do
34-
Audit.create! valid_attributes
35-
get audits_path
36-
expect(response).to be_successful
33+
context "html" do
34+
it "is successful" do
35+
Audit.create! valid_attributes
36+
get audits_path
37+
expect(response).to be_successful
38+
end
39+
end
40+
41+
context "csv" do
42+
let(:response_format) { 'csv' }
43+
let!(:audits) do
44+
[
45+
create(:audit, organization: organization, storage_location: storage_location_with_duplicate_item),
46+
create(:audit, organization: organization, storage_location: storage_location_with_items)
47+
]
48+
end
49+
let(:storage_location_with_duplicate_item) {
50+
create(:storage_location,
51+
name: "Storage Location with Duplicate Items",
52+
address: "1500 Remount Road, Front Royal, VA 22630",
53+
warehouse_type: StorageLocation::WAREHOUSE_TYPES.first,
54+
square_footage: 100)
55+
}
56+
let(:storage_location_with_items) {
57+
create(:storage_location,
58+
name: "Storage Location with Items",
59+
address: "123 Donation Site Way",
60+
warehouse_type: StorageLocation::WAREHOUSE_TYPES.first,
61+
square_footage: 100)
62+
}
63+
let(:item1) { create(:item, name: 'A') }
64+
let(:item2) { create(:item, name: 'B') }
65+
let(:item3) { create(:item, name: 'C') }
66+
67+
before do
68+
TestInventory.create_inventory(storage_location_with_items.organization, {
69+
storage_location_with_items.id => {
70+
item1.id => 1,
71+
item2.id => 1,
72+
item3.id => 1
73+
},
74+
storage_location_with_duplicate_item.id => { item3.id => 1 }
75+
})
76+
end
77+
78+
it "succeeds" do
79+
get audits_path(format: response_format)
80+
expect(response).to be_successful
81+
end
82+
83+
it "includes headers followed by alphabetized item names" do
84+
get audits_path(format: response_format)
85+
expect(response.body.split("\n")[0]).to eq([Audit.csv_export_headers, item1.name, item2.name, item3.name].join(','))
86+
end
87+
88+
it "Generates csv with Storage Location fields, alphabetized item names, item quantities lined up in their columns, and zeroes for no inventory" do
89+
get audits_path(format: response_format)
90+
csv = <<~CSV
91+
Audit Date,Audit Status,Name,Address,Square Footage,Warehouse Type,Total Inventory,A,B,C
92+
August 21 2025,in_progress,Storage Location with Duplicate Items,"1500 Remount Road, Front Royal, VA 22630",100,Residential space used,1,0,0,1
93+
August 21 2025,in_progress,Storage Location with Items,123 Donation Site Way,100,Residential space used,3,1,1,1
94+
CSV
95+
expect(response.body).to eq(csv)
96+
end
3797
end
3898
end
3999

spec/system/audit_system_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@
135135
context "when viewing the audits index" do
136136
subject { audits_path }
137137

138+
it "should have the 'Export Audit' button" do
139+
visit subject
140+
expect(page).to have_content(" Export Audit")
141+
end
142+
138143
it "should be able to filter the #index by storage location" do
139144
storage_location2 = create(:storage_location, name: "there", organization: organization)
140145
create(:audit, organization: organization, storage_location: storage_location)

0 commit comments

Comments
 (0)