|
4 | 4 |
|
5 | 5 | RSpec.describe EmissionsExportSerializer do |
6 | 6 | let(:scenario) { Scenario.default } |
7 | | - let(:serializer) { EmissionsExportSerializer.new(scenario) } |
| 7 | + let(:serializer) { described_class.new(scenario) } |
8 | 8 |
|
9 | 9 | describe '#as_csv' do |
10 | 10 | it 'returns a CSV string' do |
|
30 | 30 | expect(csv.length).to be >= 1 |
31 | 31 |
|
32 | 32 | # If there are data rows, validate structure |
33 | | - if csv.length > 1 |
34 | | - expect(csv[1].length).to eq(7) |
35 | | - end |
| 33 | + expect(csv[1].length).to eq(7) if csv.length > 1 |
36 | 34 | end |
37 | 35 |
|
38 | 36 | it 'formats node data correctly when rows exist' do |
39 | 37 | csv = CSV.parse(serializer.as_csv) |
40 | | - data_rows = csv[1..-1] # Skip header |
| 38 | + data_rows = csv[1..] # Skip header |
41 | 39 |
|
42 | 40 | # Each row (if any) should have 7 columns |
43 | 41 | data_rows.each do |row| |
|
49 | 47 | it 'exports CO2 capture as negative value' do |
50 | 48 | csv = CSV.parse(serializer.as_csv) |
51 | 49 | # Find any row with capture value |
52 | | - rows_with_capture = csv[1..-1].select { |row| row[2].to_f < 0 } |
| 50 | + rows_with_capture = csv[1..].select { |row| row[2].to_f < 0 } |
53 | 51 |
|
54 | 52 | # If there are CCS nodes, they should have negative capture |
55 | | - if rows_with_capture.any? |
56 | | - expect(rows_with_capture.first[2].to_f).to be < 0 |
57 | | - end |
| 53 | + expect(rows_with_capture.first[2].to_f).to be < 0 if rows_with_capture.any? |
58 | 54 | end |
59 | 55 |
|
60 | 56 | it 'validates Total GHG = Production - Capture + Other GHG' do |
61 | 57 | csv = CSV.parse(serializer.as_csv) |
62 | | - data_rows = csv[1..-1] |
| 58 | + data_rows = csv[1..] |
63 | 59 |
|
64 | 60 | data_rows.each do |row| |
65 | 61 | production = row[1].to_f |
66 | | - capture = row[2].to_f # Already negative in export |
| 62 | + capture = row[2].to_f # Already negative in export |
67 | 63 | other_ghg = row[3].to_f |
68 | 64 | total_ghg = row[4].to_f |
69 | 65 |
|
|
111 | 107 | expect(serializer.send(:format_value, 1.23456789)).to eq('1.23456789') |
112 | 108 | end |
113 | 109 |
|
114 | | - |
115 | 110 | it 'handles integer-like floats correctly' do |
116 | 111 | expect(serializer.send(:format_value, 5.0)).to eq('5.0') |
117 | 112 | end |
|
126 | 121 | end |
127 | 122 | end |
128 | 123 |
|
129 | | - describe '#has_emissions?' do |
| 124 | + describe '#emissions?' do |
130 | 125 | let(:node_with_fossil) do |
131 | 126 | double('Node', query: double('Query', |
132 | 127 | direct_co2_emission_of_fossil_gross: 10.0, |
133 | | - direct_co2_emission_of_bio_gross: 0.0 |
134 | | - )) |
| 128 | + direct_co2_emission_of_bio_gross: 0.0)) |
135 | 129 | end |
136 | 130 |
|
137 | 131 | let(:node_with_bio) do |
138 | 132 | double('Node', query: double('Query', |
139 | 133 | direct_co2_emission_of_fossil_gross: 0.0, |
140 | | - direct_co2_emission_of_bio_gross: 5.0 |
141 | | - )) |
| 134 | + direct_co2_emission_of_bio_gross: 5.0)) |
142 | 135 | end |
143 | 136 |
|
144 | 137 | let(:node_with_both) do |
145 | 138 | double('Node', query: double('Query', |
146 | 139 | direct_co2_emission_of_fossil_gross: 10.0, |
147 | | - direct_co2_emission_of_bio_gross: 5.0 |
148 | | - )) |
| 140 | + direct_co2_emission_of_bio_gross: 5.0)) |
149 | 141 | end |
150 | 142 |
|
151 | 143 | let(:node_with_neither) do |
152 | 144 | double('Node', query: double('Query', |
153 | 145 | direct_co2_emission_of_fossil_gross: 0.0, |
154 | | - direct_co2_emission_of_bio_gross: 0.0 |
155 | | - )) |
| 146 | + direct_co2_emission_of_bio_gross: 0.0)) |
156 | 147 | end |
157 | 148 |
|
158 | 149 | let(:node_with_error) do |
|
162 | 153 | end |
163 | 154 |
|
164 | 155 | it 'returns true for nodes with fossil emissions' do |
165 | | - expect(serializer.send(:has_emissions?, node_with_fossil)).to be true |
| 156 | + expect(serializer.send(:emissions?, node_with_fossil)).to be(true) |
166 | 157 | end |
167 | 158 |
|
168 | 159 | it 'returns true for nodes with bio emissions' do |
169 | | - expect(serializer.send(:has_emissions?, node_with_bio)).to be true |
| 160 | + expect(serializer.send(:emissions?, node_with_bio)).to be(true) |
170 | 161 | end |
171 | 162 |
|
172 | 163 | it 'returns true for nodes with both emission types' do |
173 | | - expect(serializer.send(:has_emissions?, node_with_both)).to be true |
| 164 | + expect(serializer.send(:emissions?, node_with_both)).to be(true) |
174 | 165 | end |
175 | 166 |
|
176 | 167 | it 'returns false for nodes with no emissions' do |
177 | | - expect(serializer.send(:has_emissions?, node_with_neither)).to be false |
| 168 | + expect(serializer.send(:emissions?, node_with_neither)).to be(false) |
178 | 169 | end |
179 | 170 |
|
180 | 171 | it 'returns false when query raises an error' do |
181 | | - expect(serializer.send(:has_emissions?, node_with_error)).to be false |
| 172 | + expect(serializer.send(:emissions?, node_with_error)).to be(false) |
182 | 173 | end |
183 | 174 | end |
184 | 175 |
|
|
198 | 189 | end |
199 | 190 |
|
200 | 191 | let(:node_returning_nan) do |
201 | | - double('Node', key: :nan_node, query: double('Query', direct_co2_emission_of_fossil: Float::NAN)) |
| 192 | + double('Node', key: :nan_node, |
| 193 | + query: double('Query', direct_co2_emission_of_fossil: Float::NAN)) |
202 | 194 | end |
203 | 195 |
|
204 | 196 | let(:node_returning_infinity) do |
205 | | - double('Node', key: :inf_node, query: double('Query', direct_co2_emission_of_fossil: Float::INFINITY)) |
| 197 | + double('Node', key: :inf_node, |
| 198 | + query: double('Query', direct_co2_emission_of_fossil: Float::INFINITY)) |
206 | 199 | end |
207 | 200 |
|
208 | 201 | it 'returns the query result when successful' do |
|
218 | 211 | end |
219 | 212 |
|
220 | 213 | it 'returns nil when query returns NaN' do |
221 | | - expect(serializer.send(:safe_query, node_returning_nan, :direct_co2_emission_of_fossil)).to be_nil |
| 214 | + expect(serializer.send(:safe_query, node_returning_nan, |
| 215 | + :direct_co2_emission_of_fossil)).to be_nil |
222 | 216 | end |
223 | 217 |
|
224 | 218 | it 'returns nil when query returns Infinity' do |
225 | 219 | expect(serializer.send(:safe_query, node_returning_infinity, :direct_co2_emission_of_fossil)).to be_nil |
226 | 220 | end |
227 | 221 |
|
228 | 222 | it 'returns nil when query returns nil' do |
229 | | - nil_node = double('Node', key: :nil_node, query: double('Query', direct_co2_emission_of_fossil: nil)) |
| 223 | + nil_node = double('Node', key: :nil_node, |
| 224 | + query: double('Query', direct_co2_emission_of_fossil: nil)) |
230 | 225 | expect(serializer.send(:safe_query, nil_node, :direct_co2_emission_of_fossil)).to be_nil |
231 | 226 | end |
232 | 227 | end |
|
0 commit comments