Skip to content

Commit

Permalink
add specs to check new full-export headings for institutions
Browse files Browse the repository at this point in the history
  • Loading branch information
thomaskrenzke committed Feb 20, 2025
1 parent 0a03311 commit 33da5fe
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/common/exporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def write_versioned_row(csv, csv_headers)
raise(MissingAttributeError, "#{klass.name} is not versioned") unless klass.has_attribute?('version_id')

klass.includes(:version)
.limit(4).each do |record|
.find_each(batch_size: Settings.active_record.batch_size.find_each) do |record|
csv << csv_headers.keys.map { |k| record.respond_to?(k) == false ? nil : format(k, record.public_send(k)) }
end
end
Expand Down
27 changes: 27 additions & 0 deletions spec/controllers/dashboards_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,33 @@ def load_table(klass)
it 'redirects to index on error' do
expect(get(:export_version, params: { format: :xml, number: 1 })).to redirect_to(action: :index)
end

context 'when requesting a full export' do
before do
# build some real institutions to populate the CSV file, but skip the slow geocoding step
allow(InstitutionBuilder::Factory).to receive(:geocode_using_csv_file)
InstitutionBuilder.run(User.first)
end

it 'generates a CSV with the appropriate column headers' do
get(:export_version, params: { format: :csv, number: 1, export_all: true })
csv_data = CSV.parse(response.body, headers: true)

# just pick a reasonable sample of column headers
%w[
version
weams_priority_enrollment
weams_physical_city
scorecard_hbcu
sec_103_solely_requires_coe
computed_complaints_refund_by_ope_id_do_not_sum
sec_109_closure109
sva_student_veteran
].each do |header_name|
expect(csv_data.headers).to include(header_name)
end
end
end
end

describe 'GET api_fetch' do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,20 @@
# Pull the default CSV options to be used
default_options = Common::Shared.file_type_defaults(described_class.name)

def check_attributes_from_records(rows, header_row)
def check_attributes_from_records(rows, header_row, use_full_export_name = false)
described_class.find_each.with_index do |record, i|
attributes = {}

rows[i].each.with_index do |value, j|
header = Common::Shared.convert_csv_header(header_row[j])
attributes[mapping[header][:column]] = value
attributes[mapping[header][:column]] = version if mapping[header][:column].eql? :version

column_info = use_full_export_name ? mapping.values.find{|m| m[:full_export_name] == header} : mapping[header]
attributes[column_info[:column]] = value
attributes[column_info[:column]] = version if column_info[:column].eql? :version
end

csv_record = described_class.new(attributes)
csv_record.derive_dependent_columns if csv_record.respond_to?(:derive_dependent_columns)

ignored_attributes = %w[id version created_at updated_at csv_row version_id institution_search]
csv_test_attributes = csv_record.attributes.except(*ignored_attributes)
test_attributes = record.attributes.except(*ignored_attributes)
Expand Down Expand Up @@ -54,7 +55,7 @@ def check_attributes_from_records(rows, header_row)
rows = described_class.export_by_version(true).split("\n")
header_row = rows.shift.split(default_options[:col_sep]).map(&:downcase)
rows = CSV.parse(rows.join("\n"), col_sep: default_options[:col_sep])
check_attributes_from_records(rows, header_row)
check_attributes_from_records(rows, header_row, true)
expect(header_row.size).to eq 149
end
end
Expand Down

0 comments on commit 33da5fe

Please sign in to comment.