Skip to content

Commit

Permalink
Show an error if it's trying to create a non-existent object type
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkadel committed Sep 7, 2021
1 parent 74e6ec1 commit f481ea1
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 18 deletions.
11 changes: 10 additions & 1 deletion app/uploaders/zizia/csv_manifest_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def validate
invalid_license
invalid_resource_type
invalid_rights_statement
invalid_object_type
end

# One record per row
Expand Down Expand Up @@ -141,6 +142,10 @@ def invalid_rights_statement
validate_values('rights statement', :valid_rights_statements)
end

def invalid_object_type
validate_values('object type', :valid_object_types)
end

def valid_licenses
@valid_license_ids ||= Hyrax::LicenseService.new.authority.all.select { |license| license[:active] }.map { |license| license[:id] }
end
Expand All @@ -153,6 +158,10 @@ def valid_rights_statements
@valid_rights_statement_ids ||= Qa::Authorities::Local.subauthority_for('rights_statements').all.select { |term| term[:active] }.map { |term| term[:id] }
end

def valid_object_types
@valid_object_types ||= ['c', 'w']
end

# Make sure this column contains only valid values
def validate_values(header_name, valid_values_method)
column_number = @transformed_headers.find_index(header_name)
Expand All @@ -164,7 +173,7 @@ def validate_values(header_name, valid_values_method)

values = row[column_number].split(delimiter)
valid_values = method(valid_values_method).call
invalid_values = values.select { |value| !valid_values.include?(value) }
invalid_values = values.select { |value| !valid_values.include?(value.downcase) }

invalid_values.each do |value|
@errors << "Invalid #{header_name.titleize} in row #{i + 1}: #{value}"
Expand Down
6 changes: 4 additions & 2 deletions lib/zizia/hyrax/hyrax_record_importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,14 @@ def import(record:)
def import_type(record)
raise 'No curation_concern found for import' unless
defined?(Hyrax) && Hyrax&.config&.curation_concerns&.any?
if record.object_type
if record.object_type.present?
case record.object_type.first&.downcase
when "c"
Collection
else
when "w"
Hyrax.config.curation_concerns.first
else
Rails.logger.error "[zizia] Unrecognized object_type: #{record.object_type.first&.downcase}"
end
else
Hyrax.config.curation_concerns.first
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object type,identifier,type,license,deduplication_key,visibility,location,keyword,rights statement,creator,title,files
i,abc/123,work,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
C,,,,7,Public,,,,,Test collection,
81 changes: 66 additions & 15 deletions spec/dummy/spec/system/import_from_csv_with_errors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,85 @@
require 'rails_helper'
include Warden::Test::Helpers

RSpec.describe 'Importing records from a CSV file with fatal errors', type: :system, js: true do
RSpec.describe 'Importing records from a CSV file with fatal errors', :clean, type: :system, js: true do
let(:bad_csv_file) { File.join(fixture_path, 'csv_import', 'csv_files_with_problems', 'missing_headers.csv') }

let(:collection) { FactoryBot.build(:collection) }
let(:test_strategy) { Flipflop::FeatureSet.current.test! }

context 'logged in as an admin user' do
let(:admin_user) { FactoryBot.create(:admin) }
before do
collection.save!
login_as admin_user
end

it 'aborts the import' do
visit '/csv_imports/new'
# Fill in and submit the form
attach_file('csv_import[manifest]', bad_csv_file, make_visible: true)
select collection.title.first, from: "csv_import[fedora_collection_id]"
click_on 'Preview Import'
context "with the old ui" do

# We expect to see errors for this CSV file.
expect(page).to have_content 'Missing required column: "Title".'
before do
test_strategy.switch!(:new_zizia_ui, false)
collection.save!
end

# Because there are fatal errors, the 'Start Import' button should not be available.
expect(page).not_to have_button('Start Import')
expect(page).to have_content 'You will need to correct the errors'
it 'aborts the import' do
visit '/csv_imports/new'
# Fill in and submit the form
attach_file('csv_import[manifest]', bad_csv_file, make_visible: true)
select collection.title.first, from: "csv_import[fedora_collection_id]"
click_on 'Preview Import'

# There is a link so the user can start over with a new CSV file.
click_on 'Try Again'
# We expect to see errors for this CSV file.
expect(page).to have_content 'Missing required column: "Title".'

# Because there are fatal errors, the 'Start Import' button should not be available.
expect(page).not_to have_button('Start Import')
expect(page).to have_content 'You will need to correct the errors'

# There is a link so the user can start over with a new CSV file.
click_on 'Try Again'
end
end

context "with the new ui" do
before do
test_strategy.switch!(:new_zizia_ui, true)
end

it 'aborts the import' do
visit '/csv_imports/new'
# Fill in and submit the form
attach_file('csv_import[manifest]', bad_csv_file, make_visible: true)
click_on 'Preview Import'

# We expect to see errors for this CSV file.
expect(page).to have_content 'Missing required column: "Title".'

# Because there are fatal errors, the 'Start Import' button should not be available.
expect(page).not_to have_button('Start Import')
expect(page).to have_content 'You will need to correct the errors'

# There is a link so the user can start over with a new CSV file.
click_on 'Try Again'
end
context "with a csv with an unrecognized object_type" do
let(:csv_file) { File.join(fixture_path, 'csv_import', 'csv_files_with_problems', 'wrong_object_type.csv') }

it 'aborts the import' do
visit '/csv_imports/new'
# Fill in and submit the form
attach_file('csv_import[manifest]', csv_file, make_visible: true)
click_on 'Preview Import'

# We expect to see errors for this CSV file.
expect(page).to have_content 'Invalid Object Type in row 2: i'

# Because there are fatal errors, the 'Start Import' button should not be available.
expect(page).not_to have_button('Start Import')
expect(page).to have_content 'You will need to correct the errors'

# There is a link so the user can start over with a new CSV file.
click_on 'Try Again'
end
end
end
end
end

0 comments on commit f481ea1

Please sign in to comment.