From faf379fd47aaf8ce906b86c687ed81dc2012da6c Mon Sep 17 00:00:00 2001 From: Naomi Dushay Date: Thu, 3 Aug 2023 13:18:56 -0700 Subject: [PATCH] no form source data unless there's a form value --- app/services/description_import.rb | 35 ++-- spec/services/description_import_spec.rb | 195 +++++++++++++++++++++++ 2 files changed, 219 insertions(+), 11 deletions(-) diff --git a/app/services/description_import.rb b/app/services/description_import.rb index c3c5e7a43..5981de399 100644 --- a/app/services/description_import.rb +++ b/app/services/description_import.rb @@ -26,6 +26,7 @@ def import compacted_params = compact_params(params) remove_contributors_if_role_without_name(compacted_params) + remove_form_if_source_without_value(compacted_params) remove_nested_contributors_if_role_without_name(compacted_params) Success(Cocina::Models::Description.new(compacted_params)) @@ -95,23 +96,35 @@ def compact_params(params) end end - def remove_nested_contributors_if_role_without_name(compacted_params_hash) - [:event, :relatedResource].each do |parent_property| - next if compacted_params_hash[parent_property].blank? + def remove_contributors_if_role_without_name(compacted_params_hash) + return unless compacted_params_hash && compacted_params_hash[:contributor] - compacted_params_hash[parent_property].each do |parent_object| - remove_contributors_if_role_without_name(parent_object) - end + compacted_params_hash[:contributor].each do |contributor| + next unless contributor && contributor[:name].nil? && contributor[:role].present? + + compacted_params_hash[:contributor].delete(contributor) end end - def remove_contributors_if_role_without_name(compacted_params_hash) - return unless compacted_params_hash && compacted_params_hash.compact[:contributor] + def remove_form_if_source_without_value(compacted_params_hash) + return unless compacted_params_hash && compacted_params_hash[:form] - compacted_params_hash.compact[:contributor].each do |contributor| - next unless contributor && contributor[:name].nil? && contributor[:role].present? + compacted_params_hash[:form].each do |form| + next unless form && form[:value].nil? && (form[:source].present? || form[:type].present?) - compacted_params_hash[:contributor].delete(contributor) + compacted_params_hash[:form].delete(form) + end + end + + def remove_nested_contributors_if_role_without_name(compacted_params_hash) + # event can have contributors, geographic can have form + [:relatedResource, :event, :geographic].each do |parent_property| + next if compacted_params_hash[parent_property].blank? + + compacted_params_hash[parent_property].each do |parent_object| + remove_contributors_if_role_without_name(parent_object) + remove_form_if_source_without_value(parent_object) + end end end end diff --git a/spec/services/description_import_spec.rb b/spec/services/description_import_spec.rb index ebacafe19..6072653f4 100644 --- a/spec/services/description_import_spec.rb +++ b/spec/services/description_import_spec.rb @@ -490,4 +490,199 @@ end end end + + context "with form property" do + let(:title) { "my great form" } + let(:csv) do + CSV.parse(csv_data, headers: true) + end + let(:expected) { Cocina::Models::Description.new(expected_hash) } + + context "when at top level" do + let(:expected_hash) do + { + title: [{value: title}], + purl: "https://purl/jr825qh8124" + }.tap do |h| + h[:form] = [expected_form] if expected_form + end + end + + [nil, "prints"].each do |form_value| + context "when type, no source, value: '#{form_value}'" do + let(:csv_data) do + <<~CSV + druid,source_id,purl,title1.value,form1.value,form1.type,form1.source.code,form1.source.value,form1.source.uri + jr825qh8124,form:values,https://purl/jr825qh8124,#{title},#{form_value},genre,,, + CSV + end + let(:expected_form) do + if form_value + { + value: form_value, + type: "genre" + } + end + end + + it "has the expected value" do + expect(updated.value!.to_h).to eq expected.to_h + end + end + + context "when form source as code, no form type, form value: '#{form_value}'" do + let(:csv_data) do + <<~CSV + druid,source_id,purl,title1.value,form1.value,form1.type,form1.source.code,form1.source.value,form1.source.uri + jr825qh8124,form:values,https://purl/jr825qh8124,#{title},#{form_value},,lcgft,, + CSV + end + let(:expected_form) do + if form_value + { + value: form_value, + source: {code: "lcgft"} + } + end + end + + it "has the expected value" do + expect(updated.value!.to_h).to eq expected.to_h + end + end + + context "when form source as value, no form type, form value: '#{form_value}'" do + let(:csv_data) do + <<~CSV + druid,source_id,purl,title1.value,form1.value,form1.type,form1.source.code,form1.source.value,form1.source.uri + jr825qh8124,form:values,https://purl/jr825qh8124,#{title},#{form_value},,,source-value, + CSV + end + let(:expected_form) do + if form_value + { + value: form_value, + source: {value: "source-value"} + } + end + end + + it "has the expected value" do + expect(updated.value!.to_h).to eq expected.to_h + end + end + + context "when form source as URI, no form type, form value: '#{form_value}'" do + let(:csv_data) do + <<~CSV + druid,source_id,purl,title1.value,form1.value,form1.type,form1.source.code,form1.source.value,form1.source.uri + jr825qh8124,form:values,https://purl/jr825qh8124,#{title},#{form_value},,,,http://id.loc.gov/authorities/genreForms/ + CSV + end + let(:expected_form) do + if form_value + { + value: form_value, + source: {uri: "http://id.loc.gov/authorities/genreForms/"} + } + end + end + + it "has the expected value" do + expect(updated.value!.to_h).to eq expected.to_h + end + end + + context "when form source as code and URI, no form type, form value: '#{form_value}'" do + let(:csv_data) do + <<~CSV + druid,source_id,purl,title1.value,form1.value,form1.type,form1.source.code,form1.source.value,form1.source.uri + jr825qh8124,form:values,https://purl/jr825qh8124,#{title},#{form_value},,lcgft,,http://id.loc.gov/authorities/genreForms/ + CSV + end + let(:expected_form) do + if form_value + { + value: form_value, + source: { + code: "lcgft", + uri: "http://id.loc.gov/authorities/genreForms/" + } + } + end + end + + it "has the expected value" do + expect(updated.value!.to_h).to eq expected.to_h + end + end + + context "when form source and type empty, form value: '#{form_value}'" do + let(:csv_data) do + <<~CSV + druid,source_id,purl,title1.value,form1.value,form1.type,form1.source.code,form1.source.value,form1.source.uri + jr825qh8124,form:values,https://purl/jr825qh8124,#{title},#{form_value},,,, + CSV + end + let(:expected_form) do + { value: form_value } if form_value + end + + it "has the expected value" do + expect(updated.value!.to_h).to eq expected.to_h + end + end + end + end + + context "when at nested level" do + let(:expected_hash) do + { + title: [{value: title}], + purl: "https://purl/jr825qh8124" + }.tap do |h| + form_value = + if expected_form + [expected_form] + else + [] + end + h[:relatedResource] = [ + form: form_value, + title: [], + contributor: [], + event: [], + language: [], + note: [], + identifier: [], + subject: [] + ] + end + end + let(:expected) { Cocina::Models::Description.new(expected_hash) } + + [nil, "prints"].each do |form_value| + context "when form has type, no source, value: '#{form_value}'" do + let(:csv_data) do + <<~CSV + druid,source_id,purl,title1.value,relatedResource1.form1.value,relatedResource1.form1.type,relatedResource1.form1.source.code,relatedResource1.form1.source.value,relatedResource1.form1.source.uri + jr825qh8124,form:values,https://purl/jr825qh8124,#{title},#{form_value},genre,,, + CSV + end + let(:expected_form) do + if form_value + { + value: form_value, + type: "genre" + } + end + end + + it "has the expected value" do + expect(updated.value!.to_h).to eq expected.to_h + end + end + end + end + end end