|
1 | | -RSpec.describe Exports::ExportAdjustmentsCSVService, :wip do |
| 1 | +RSpec.describe Exports::ExportAdjustmentsCSVService do |
2 | 2 | # Create organization after items to ensure proper associations |
3 | 3 | let!(:item1) { create(:item, name: "item1") } |
4 | 4 | let!(:item2) { create(:item, name: "item2") } |
|
19 | 19 | [item1, item2, item3, item4, item5].map(&:name).sort |
20 | 20 | end |
21 | 21 |
|
22 | | - let(:expected_headers) do |
23 | | - [ |
24 | | - "Created date", "Storage Area", |
25 | | - "Comment", "# of changes" |
26 | | - ] + sorted_item_names |
27 | | - end |
28 | | - |
29 | 22 | let(:storage_location) { create(:storage_location, organization: organization) } |
30 | 23 | let(:user) { create(:user, organization: organization) } |
31 | 24 |
|
| 25 | + around do |example| |
| 26 | + travel_to Time.zone.local(2024, 12, 25) |
| 27 | + example.run |
| 28 | + travel_back |
| 29 | + end |
| 30 | + |
32 | 31 | describe "#generate_csv_data" do |
33 | | - subject { described_class.new(adjustments: adjustments, organization: organization).generate_csv_data } |
| 32 | + subject { described_class.generate_csv(adjustments, organization) } |
34 | 33 |
|
35 | 34 | context "with multiple adjustments and items" do |
36 | 35 | let(:adjustments) do |
|
67 | 66 | ] |
68 | 67 | end |
69 | 68 |
|
70 | | - it "should have the expected headers" do |
71 | | - expect(subject[0]).to eq(expected_headers) |
72 | | - end |
73 | | - |
74 | | - it "should include the adjustment data in the rows" do |
75 | | - # Check 1st adjustment |
76 | | - expect(subject[1]).to include( |
77 | | - adjustments[0].created_at.strftime("%F"), |
78 | | - storage_location.name, |
79 | | - "adjustment 1", |
80 | | - 2 # Number of items changed |
81 | | - ) |
82 | | - |
83 | | - # Check 2nd adjustment |
84 | | - expect(subject[2]).to include( |
85 | | - adjustments[1].created_at.strftime("%F"), |
86 | | - storage_location.name, |
87 | | - "adjustment 2", |
88 | | - 1 # Number of items changed |
89 | | - ) |
90 | | - |
91 | | - # Check 3rd adjustment |
92 | | - expect(subject[3]).to include( |
93 | | - adjustments[2].created_at.strftime("%F"), |
94 | | - storage_location.name, |
95 | | - "adjustment 3", |
96 | | - 1 # Number of items changed |
97 | | - ) |
98 | | - end |
99 | | - |
100 | | - it "should include the correct item quantities" do |
101 | | - # Get indexes of item quantity columns |
102 | | - item1_idx = expected_headers.index(item1.name) |
103 | | - item2_idx = expected_headers.index(item2.name) |
104 | | - item3_idx = expected_headers.index(item3.name) |
105 | | - item4_idx = expected_headers.index(item4.name) |
106 | | - item5_idx = expected_headers.index(item5.name) |
107 | | - |
108 | | - # Check 1st adjustment |
109 | | - expect(subject[1][item1_idx]).to eq(10) |
110 | | - expect(subject[1][item2_idx]).to eq(-5) |
111 | | - expect(subject[1][item3_idx]).to eq(0) |
112 | | - expect(subject[1][item4_idx]).to eq(0) |
113 | | - expect(subject[1][item5_idx]).to eq(0) |
| 69 | + it "should include the correct adjustment data" do |
| 70 | + csv = <<~CSV |
| 71 | + Created date,Storage Area,Comment,# of changes,item1,item2,item3,item4,item5 |
| 72 | + 2024-12-25,Smithsonian Conservation Center,adjustment 1,2,10,-5,0,0,0 |
| 73 | + 2024-12-25,Smithsonian Conservation Center,adjustment 2,1,0,0,3,0,0 |
| 74 | + 2024-12-25,Smithsonian Conservation Center,adjustment 3,1,7,0,0,0,0 |
| 75 | + CSV |
114 | 76 |
|
115 | | - # Check 2nd adjustment |
116 | | - expect(subject[2][item1_idx]).to eq(0) |
117 | | - expect(subject[2][item2_idx]).to eq(0) |
118 | | - expect(subject[2][item3_idx]).to eq(3) |
119 | | - expect(subject[2][item4_idx]).to eq(0) |
120 | | - expect(subject[2][item5_idx]).to eq(0) |
121 | | - |
122 | | - # Check 3rd adjustment |
123 | | - expect(subject[3][item1_idx]).to eq(7) |
124 | | - expect(subject[3][item2_idx]).to eq(0) |
125 | | - expect(subject[3][item3_idx]).to eq(0) |
126 | | - expect(subject[3][item4_idx]).to eq(0) |
127 | | - expect(subject[3][item5_idx]).to eq(0) |
128 | | - end |
129 | | - |
130 | | - it "should correctly sum up the number of changes" do |
131 | | - idx = expected_headers.index("# of changes") |
132 | | - expect(subject[1][idx]).to eq(2) |
133 | | - expect(subject[2][idx]).to eq(1) |
134 | | - expect(subject[3][idx]).to eq(1) |
| 77 | + expect(subject).to eq(csv) |
135 | 78 | end |
136 | 79 | end |
137 | 80 |
|
138 | 81 | context "when there are no adjustments" do |
139 | 82 | let(:adjustments) { [] } |
140 | 83 |
|
141 | 84 | it "returns only headers row" do |
142 | | - expect(subject.size).to eq(1) |
143 | | - expect(subject[0]).to eq(expected_headers) |
144 | | - end |
145 | | - end |
146 | | - end |
| 85 | + csv = <<~CSV |
| 86 | + Created date,Storage Area,Comment,# of changes,item1,item2,item3,item4,item5 |
| 87 | + CSV |
147 | 88 |
|
148 | | - describe "#generate_csv" do |
149 | | - subject { described_class.new(adjustments: adjustments, organization: organization).generate_csv } |
150 | | - |
151 | | - let(:adjustments) do |
152 | | - [ |
153 | | - create(:adjustment, |
154 | | - storage_location: storage_location, |
155 | | - organization: organization, |
156 | | - line_items_attributes: [ |
157 | | - {item_id: item1.id, quantity: 111}, |
158 | | - {item_id: item2.id, quantity: -7} |
159 | | - ]) |
160 | | - ] |
161 | | - end |
162 | | - |
163 | | - it "generates valid CSV data" do |
164 | | - expect(subject).to be_a(String) |
165 | | - parsed_csv = CSV.parse(subject, headers: true) |
166 | | - expect(parsed_csv.headers).to include("Created date", |
167 | | - "Storage Area", |
168 | | - "Comment", |
169 | | - "# of changes", |
170 | | - item1.name, |
171 | | - item2.name, |
172 | | - item3.name, |
173 | | - item4.name) |
174 | | - |
175 | | - expect(parsed_csv.first["# of changes"]).to eq("2") |
176 | | - expect(parsed_csv.first[item1.name]).to eq("111") |
177 | | - expect(parsed_csv.first[item2.name]).to eq("-7") |
| 89 | + expect(subject).to eq(csv) |
| 90 | + end |
178 | 91 | end |
179 | 92 | end |
180 | 93 | end |
0 commit comments