Skip to content

Commit 3003f2c

Browse files
committed
Formatting fixes
1 parent 3ad40f4 commit 3003f2c

8 files changed

Lines changed: 44 additions & 49 deletions

File tree

app/controllers/api/v3/export_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ def energy_flow
2828
#
2929
# Returns a CSV file containing the energetic inputs and outputs of every node in the present graph.
3030
def energy_flow_present
31-
send_csv(NodeFlowSerializer.new(@scenario.gql.present.graph, 'MJ'), 'energy_flow_present.%d.csv')
31+
send_csv(NodeFlowSerializer.new(@scenario.gql.present.graph, 'MJ'),
32+
'energy_flow_present.%d.csv')
3233
end
3334

3435
# GET /api/v3/scenarios/:id/molecule_flow

app/models/qernel/node_api/direct_bio_emissions.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def direct_edge_bio_carbon_content(edge)
145145
return composition if composition
146146
end
147147

148-
nil # TODO: consider this case
148+
0.0 # TODO: consider this case
149149
end
150150
end
151151
end

app/models/qernel/node_api/direct_emissions.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ def direct_edge_carbon_content(edge)
135135
return composition if composition
136136
end
137137

138-
nil # TODO: consider this case
138+
0.0
139139
end
140140
end
141141
end

app/serializers/emissions_export_serializer.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
# Presents comprehensive emission data including gross, captured, and net emissions
24
# for both fossil and biogenic CO2, plus other GHG emissions.
35
class EmissionsExportSerializer
@@ -20,7 +22,7 @@ def as_csv(*)
2022

2123
nodes.each do |node|
2224
row = node_row(node)
23-
csv << row if row # Only include valid rows (skip nil)
25+
csv << row if row # Only include valid rows (skip nil)
2426
end
2527
end
2628
end
@@ -42,19 +44,17 @@ def header_row
4244
def nodes
4345
# Include all energy nodes that have emissions or are final demand nodes
4446
@graph.nodes.select do |node|
45-
has_emissions?(node) || node.groups.include?(:final_demand)
47+
emissions?(node) || node.groups.include?(:final_demand)
4648
end.sort_by(&:key)
4749
end
4850

49-
def has_emissions?(node)
51+
def emissions?(node)
5052
node.query.direct_co2_emission_of_fossil_gross.positive? ||
5153
node.query.direct_co2_emission_of_bio_gross.positive?
5254
rescue StandardError => e
5355
# Nodes without DirectEmissions module (e.g., molecule graph nodes) will raise NoMethodError
5456
# This is expected and we silently exclude them
55-
Rails.logger.debug(
56-
"Emissions check failed for #{node.key}: #{e.class.name} - #{e.message}"
57-
)
57+
Rails.logger.debug { "Emissions check failed for #{node.key}: #{e.class.name} - #{e.message}" }
5858
false
5959
end
6060

@@ -78,7 +78,7 @@ def node_row(node)
7878
co2_production = to_kton(fossil_gross)
7979
co2_capture = to_kton(fossil_captured + bio_captured)
8080

81-
# Column 3: Other GHG emissions (CH4, N2O, etc. - currently 0, future: AREA attributes)
81+
# TODO: Column 3: Other GHG emissions (CH4, N2O, etc. - currently 0, future: AREA attributes)
8282
other_ghg = 0.0
8383

8484
# Column 4: Total GHG emissions = production - capture + other
@@ -88,9 +88,8 @@ def node_row(node)
8888
bio_net = safe_query(node, :direct_co2_emission_of_bio)
8989
biogenic = bio_net.nil? ? nil : to_kton(bio_net)
9090

91-
# Column 6: CO2 emissions end-use allocation (primary emissions)
92-
primary_fossil = safe_query(node, :primary_co2_emission_of_fossil)
93-
end_use_allocation = primary_fossil.nil? ? nil : to_kton(primary_fossil)
91+
# TODO: Column 6: CO2 emissions end-use allocation (PLACEHOLDER)
92+
end_use_allocation = 0.0
9493

9594
[
9695
node.key,
@@ -147,6 +146,7 @@ def format_value(value)
147146
end
148147

149148
return '0' if value.abs < 0.000001 # 1 kg
149+
150150
value.to_s
151151
end
152152
end

spec/controllers/api/v3/exports_controller_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
require 'spec_helper'
44

55
describe Api::V3::ExportController do
6-
let(:scenario) { FactoryBot.create(:scenario) }
6+
let(:scenario) { create(:scenario) }
77
let(:user) { create(:user) }
88
let(:headers) { access_token_header(user, :write) }
99

spec/models/qernel/edge_api_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
require 'spec_helper'
44

55
RSpec.describe Qernel::NodeApi::Base do
6-
let(:supplier) { FactoryBot.build(:node, key: :supplier) }
7-
let(:consumer) { FactoryBot.build(:node, key: :consumer) }
6+
let(:supplier) { build(:node, key: :supplier) }
7+
let(:consumer) { build(:node, key: :consumer) }
88
let(:carrier) { Qernel::Carrier.new(key: :network_gas) }
99
let!(:edge) { Qernel::Edge.new('', consumer, supplier, carrier, :share) }
1010

spec/models/qernel/node_api/direct_emissions_spec.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@
113113
end
114114
end
115115

116-
117116
context 'with multi-level supply chain' do
118117
# Create a graph:
119118
# [Coal Producer] -> [Coal Plant] -> [Electricity Grid] -> [Data Center] -> [Terminus]
@@ -322,7 +321,6 @@
322321
end
323322
end
324323

325-
326324
context 'with zero total input' do
327325
let(:builder) do
328326
TestGraphBuilder.new.tap do |builder|
@@ -359,7 +357,8 @@
359357
builder.add(:green_gas_producer, groups: [:primary_energy_demand], demand: 30)
360358

361359
# Network gas distributor receives from two sources
362-
builder.connect(:natural_gas_producer, :network_gas_distributor, :natural_gas, type: :share)
360+
builder.connect(:natural_gas_producer, :network_gas_distributor, :natural_gas,
361+
type: :share)
363362
builder.connect(:green_gas_producer, :network_gas_distributor, :green_gas, type: :share)
364363

365364
# Distributor supplies burner with mixed network gas (nil co2 value - uses recursion)

spec/serializers/emissions_export_serializer_spec.rb

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
RSpec.describe EmissionsExportSerializer do
66
let(:scenario) { Scenario.default }
7-
let(:serializer) { EmissionsExportSerializer.new(scenario) }
7+
let(:serializer) { described_class.new(scenario) }
88

99
describe '#as_csv' do
1010
it 'returns a CSV string' do
@@ -30,14 +30,12 @@
3030
expect(csv.length).to be >= 1
3131

3232
# 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
3634
end
3735

3836
it 'formats node data correctly when rows exist' do
3937
csv = CSV.parse(serializer.as_csv)
40-
data_rows = csv[1..-1] # Skip header
38+
data_rows = csv[1..] # Skip header
4139

4240
# Each row (if any) should have 7 columns
4341
data_rows.each do |row|
@@ -49,21 +47,19 @@
4947
it 'exports CO2 capture as negative value' do
5048
csv = CSV.parse(serializer.as_csv)
5149
# 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 }
5351

5452
# 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?
5854
end
5955

6056
it 'validates Total GHG = Production - Capture + Other GHG' do
6157
csv = CSV.parse(serializer.as_csv)
62-
data_rows = csv[1..-1]
58+
data_rows = csv[1..]
6359

6460
data_rows.each do |row|
6561
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
6763
other_ghg = row[3].to_f
6864
total_ghg = row[4].to_f
6965

@@ -111,7 +107,6 @@
111107
expect(serializer.send(:format_value, 1.23456789)).to eq('1.23456789')
112108
end
113109

114-
115110
it 'handles integer-like floats correctly' do
116111
expect(serializer.send(:format_value, 5.0)).to eq('5.0')
117112
end
@@ -126,33 +121,29 @@
126121
end
127122
end
128123

129-
describe '#has_emissions?' do
124+
describe '#emissions?' do
130125
let(:node_with_fossil) do
131126
double('Node', query: double('Query',
132127
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))
135129
end
136130

137131
let(:node_with_bio) do
138132
double('Node', query: double('Query',
139133
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))
142135
end
143136

144137
let(:node_with_both) do
145138
double('Node', query: double('Query',
146139
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))
149141
end
150142

151143
let(:node_with_neither) do
152144
double('Node', query: double('Query',
153145
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))
156147
end
157148

158149
let(:node_with_error) do
@@ -162,23 +153,23 @@
162153
end
163154

164155
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)
166157
end
167158

168159
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)
170161
end
171162

172163
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)
174165
end
175166

176167
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)
178169
end
179170

180171
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)
182173
end
183174
end
184175

@@ -198,11 +189,13 @@
198189
end
199190

200191
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))
202194
end
203195

204196
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))
206199
end
207200

208201
it 'returns the query result when successful' do
@@ -218,15 +211,17 @@
218211
end
219212

220213
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
222216
end
223217

224218
it 'returns nil when query returns Infinity' do
225219
expect(serializer.send(:safe_query, node_returning_infinity, :direct_co2_emission_of_fossil)).to be_nil
226220
end
227221

228222
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))
230225
expect(serializer.send(:safe_query, nil_node, :direct_co2_emission_of_fossil)).to be_nil
231226
end
232227
end

0 commit comments

Comments
 (0)