Skip to content

Commit

Permalink
Make sure we write requested columns even if none of them have any data
Browse files Browse the repository at this point in the history
  • Loading branch information
dmolesUC committed Apr 4, 2023
1 parent 39e70e2 commit 4e1452d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/berkeley_library/holdings/xlsx_writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def initialize(ss, rlf: true, uc: true, hathi_trust: true)
@rlf = rlf
@uc = uc
@hathi_trust = hathi_trust

ensure_columns!
end

def <<(result)
Expand All @@ -34,6 +36,15 @@ def <<(result)

private

def ensure_columns!
if rlf
nrlf_col_index
srlf_col_index
end
uc_col_index if uc
ht_col_index if hathi_trust
end

def row_index_for(oclc_number)
row_index = row_index_by_oclc_number[oclc_number]
return row_index if row_index
Expand Down
50 changes: 50 additions & 0 deletions spec/berkeley_library/holdings/xlsx_writer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,56 @@ module Holdings
end
end

it 'writes requested columns even if empty' do
result = HoldingsResult.new(oclc_number, ht_record_url: record_url)
writer = XLSXWriter.new(ss)
writer << result

expected = {
'OCLC Number' => oclc_number.to_i,
'NRLF' => nil,
'SRLF' => nil,
'Other UC' => nil,
'Hathi Trust' => record_url,
# check that we preserve existing values
'MMSID' => ss.value_at(r_index, c_index_mmsid)
}

expected.each do |col_header, v_expected|
c_index = ss.find_column_index_by_header!(col_header)
v_actual = ss.value_at(r_index, c_index)
expect(v_actual).to eq(v_expected)
end
end

it 'accepts results that only have non-requested values' do
result = HoldingsResult.new(
oclc_number,
wc_symbols: %w[CLU CUY ZAP ZAS]
)

writer = XLSXWriter.new(ss, rlf: false, uc: false)
writer << result

expected = {
'OCLC Number' => oclc_number.to_i,
'Hathi Trust' => nil,
# check that we preserve existing values
'MMSID' => ss.value_at(r_index, c_index_mmsid)
}

expected.each do |col_header, v_expected|
c_index = ss.find_column_index_by_header!(col_header)
v_actual = ss.value_at(r_index, c_index)
expect(v_actual).to eq(v_expected)
end

['NRLF', 'SRLF', 'Other UC'].each do |header|
c_index = ss.find_column_index_by_header(header)
expect(c_index).to be_nil
end
end

it 'can write a result without a HathiTrust record URL' do
writer = XLSXWriter.new(ss)
writer << result
Expand Down

0 comments on commit 4e1452d

Please sign in to comment.