Skip to content

Commit

Permalink
Factor out @rows and use @parsed_csv instead
Browse files Browse the repository at this point in the history
- Update fixture - Ruby 2.5.8 can't handle a second row that's blank
  • Loading branch information
maxkadel committed Oct 1, 2021
1 parent 1913fc2 commit 5e05c8f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
23 changes: 11 additions & 12 deletions app/uploaders/zizia/csv_manifest_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def initialize(manifest_uploader)

def validate
parse_csv
return unless @rows
return unless @parsed_csv

missing_headers
duplicate_headers
Expand All @@ -39,8 +39,10 @@ def validate

# One record per row
def record_count
return nil unless @rows
@rows.size - 1 # Don't include the header row
return nil unless @parsed_csv.size

row_with_values = @parsed_csv.map { |row| row.to_hash.values.all?(&:nil?) }
row_with_values.count(false)
end

def delimiter
Expand All @@ -67,7 +69,7 @@ def valid_headers
def parse_csv
# Note: The 'table' method automatically turns the headers into symbols with underscores
@parsed_csv = CSV.table(csv_file.path)
@rows = CSV.read(csv_file.path).reject { |x| x.empty? || x.all?(nil) }
# @rows = CSV.read(csv_file.path).reject { |x| x.empty? || x.all?(nil) }
rescue
@errors << 'We are unable to read this CSV file.'
end
Expand Down Expand Up @@ -191,18 +193,15 @@ def valid_object_types

# Make sure this column contains only valid values
def validate_values(header_name, valid_values_method, case_insensitive = false)
column_number = headers.find_index(header_name)
return unless column_number

@rows.each_with_index do |row, i|
next if i.zero? # Skip the header row
next unless row[column_number]
values = row[column_number].split(delimiter)
@parsed_csv.each_with_index do |row, index|
next if row.to_hash.values.all?(&:nil?) # Skip blank rows
next unless row[header_name]
values = row[header_name].split(delimiter)
valid_values = method(valid_values_method).call
invalid_values = invalid_values(values, valid_values, case_insensitive)

invalid_values.each do |value|
@errors << "Invalid #{header_name.to_s.titleize} in row #{i + 1}: #{value}"
@errors << "Invalid #{header_name.to_s.titleize} in row #{index + 2}: #{value}"
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
identifier,license,deduplication_key,visibility,location,keyword,rights statement,creator,title,files

abc/123,https://creativecommons.org/licenses/by/4.0/,abc/123,PUBlic,http://www.geonames.org/5667009/montana.html|~|http://www.geonames.org/6252001/united-states.html,Clothing stores $z California $z Los Angeles|~|Interior design $z California $z Los Angeles,http://rightsstatements.org/vocab/InC/1.0/,"Connell, Will, $d 1898-1961","Interior view of The Bachelors haberdashery designed by Julius Ralph Davidson, Los Angeles, circa 1929",dog.jpg

,,,,,,,,,
Expand Down
8 changes: 8 additions & 0 deletions spec/uploaders/zizia/csv_manfest_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
expect(validator.errors).to eq(['Missing required metadata in row 3: "Creator" field cannot be blank'])
expect(validator.warnings).to eq(['The field name "type" is not supported. This field will be ignored, and the metadata for this field will not be imported.'])
end

it "counts the records in the sheet" do
expect(validator.record_count).to eq 2
end
end

context "with a csv with the wrong object type" do
Expand Down Expand Up @@ -133,6 +137,10 @@
sheet_headers = [:identifier, :license, :deduplication_key, :visibility, :location, :keyword, :rights_statement, :creator, :title, :files]
expect(validator.headers). to match_array(sheet_headers)
end

it "counts the records in the sheet" do
expect(validator.record_count).to eq 2
end
end

context "with files for a work on a separate row" do
Expand Down

0 comments on commit 5e05c8f

Please sign in to comment.