Skip to content

Commit

Permalink
no form source data unless there's a form value
Browse files Browse the repository at this point in the history
  • Loading branch information
ndushay committed Aug 3, 2023
1 parent ea12a9d commit faf379f
Show file tree
Hide file tree
Showing 2 changed files with 219 additions and 11 deletions.
35 changes: 24 additions & 11 deletions app/services/description_import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
195 changes: 195 additions & 0 deletions spec/services/description_import_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit faf379f

Please sign in to comment.