Skip to content

Commit

Permalink
tests for converting affiliations. #324
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Aug 3, 2019
1 parent a8399fa commit 69db099
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 6 deletions.
21 changes: 15 additions & 6 deletions app/models/doi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ def self.convert_affiliation_by_id(options={})
Doi.where(id: id..(id + 499)).find_each do |doi|
should_update = false
creators = Array.wrap(doi.creators).map do |c|
if !c.is_a?(Hash)
if !(c.is_a?(Hash))
logger.error "[MySQL] creators for DOI #{doi.doi} should be a hash."
elsif c["affiliation"].nil?
c["affiliation"] = []
Expand All @@ -597,7 +597,7 @@ def self.convert_affiliation_by_id(options={})
c["affiliation"] = Array.wrap(c["affiliation"])
should_update = true
elsif c["affiliation"].is_a?(Array)
c["affiliation"].map do |a|
c["affiliation"] = c["affiliation"].map do |a|
if a.nil?
should_update = true

Expand All @@ -615,7 +615,7 @@ def self.convert_affiliation_by_id(options={})
c
end
contributors = Array.wrap(doi.contributors).map do |c|
if !c.is_a?(Hash)
if !(c.is_a?(Hash))
logger.error "[MySQL] creators for DOI #{doi.doi} should be a hash."
elsif c["affiliation"].nil?
c["affiliation"] = []
Expand All @@ -627,7 +627,7 @@ def self.convert_affiliation_by_id(options={})
c["affiliation"] = Array.wrap(c["affiliation"])
should_update = true
elsif c["affiliation"].is_a?(Array)
c["affiliation"].map do |a|
c["affiliation"] = c["affiliation"].map do |a|
if a.nil?
should_update = true

Expand All @@ -646,16 +646,25 @@ def self.convert_affiliation_by_id(options={})
end

if should_update
Doi.auditing_enabled = false
doi.update_columns(creators: creators, contributors: contributors)
Doi.auditing_enabled = true

count += 1
end

unless (Array.wrap(doi.creators).all? { |c| c.is_a?(Hash) && c["affiliation"].is_a?(Array) && c["affiliation"].all? { |a| a.is_a?(Hash) } } && Array.wrap(doi.contributors).all? { |c| c.is_a?(Hash) && c["affiliation"].is_a?(Array) && c["affiliation"].all? { |a| a.is_a?(Hash) } })
logger.error "[MySQL] Error converting affiliations for doi #{doi.doi}: creators #{doi.creators.inspect} contributors #{doi.contributors.inspect}."
fail TypeError, "Affiliation for doi #{doi.doi} is of wrong type" if Rails.env.test?
end
end

logger.info "[MySQL] Converted affiliations for #{count} DOIs with IDs #{id} - #{(id + 499)}." if count > 0

count
rescue Faraday::ConnectionFailed, ActiveRecord::LockWaitTimeout => error
logger.info "[MySQL] Error #{error.message} converting affiliations for DOIs with IDs #{id} - #{(id + 499)}."
rescue TypeError, ActiveRecord::ActiveRecordError, ActiveRecord::LockWaitTimeout => error
logger.error "[MySQL] Error converting affiliations for DOIs with IDs #{id} - #{(id + 499)}."
count
end

def doi=(value)
Expand Down
96 changes: 96 additions & 0 deletions spec/models/doi_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,102 @@
end
end

describe "convert_affiliations" do
let(:doi) { create(:doi)}

context "affiliation nil" do
let(:creators) { [{
"name": "Ausmees, K.",
"nameType": "Personal",
"givenName": "K.",
"familyName": "Ausmees",
"affiliation": nil
}] }
let(:doi) { create(:doi, creators: creators, contributors: [])}

it "convert" do
expect(Doi.convert_affiliation_by_id(id: doi.id)).to eq(1)
end
end

context "affiliation empty array" do
let(:creators) { [{
"name": "Ausmees, K.",
"nameType": "Personal",
"givenName": "K.",
"familyName": "Ausmees",
"affiliation": []
}] }
let(:doi) { create(:doi, creators: creators, contributors: [])}

it "convert" do
expect(Doi.convert_affiliation_by_id(id: doi.id)).to eq(0)
end
end

context "affiliation array of hashes" do
let(:creators) { [{
"name": "Ausmees, K.",
"nameType": "Personal",
"givenName": "K.",
"familyName": "Ausmees",
"affiliation": [{ "name": "Department of Microbiology; Tartu University; Tartu Estonia" }]
}] }
let(:doi) { create(:doi, creators: creators, contributors: [])}

it "convert" do
expect(Doi.convert_affiliation_by_id(id: doi.id)).to eq(0)
end
end

context "affiliation hash" do
let(:creators) { [{
"name": "Ausmees, K.",
"nameType": "Personal",
"givenName": "K.",
"familyName": "Ausmees",
"affiliation": { "name": "Department of Microbiology; Tartu University; Tartu Estonia" }
}] }
let(:doi) { create(:doi, creators: creators, contributors: [])}

it "convert" do
expect(Doi.convert_affiliation_by_id(id: doi.id)).to eq(1)
end
end

context "affiliation array of strings" do
let(:creators) { [{
"name": "Ausmees, K.",
"nameType": "Personal",
"givenName": "K.",
"familyName": "Ausmees",
"affiliation": ["Andrology Centre; Tartu University Hospital; Tartu Estonia", "Department of Surgery; Tartu University; Tartu Estonia"]
}] }
let(:doi) { create(:doi, creators: creators, contributors: [])}

it "convert" do
expect(Doi.convert_affiliation_by_id(id: doi.id)).to eq(1)
end
end

context "affiliation string" do
let(:creators) { [{
"name": "Ausmees, K.",
"nameType": "Personal",
"givenName": "K.",
"familyName": "Ausmees",
"affiliation": "Andrology Centre; Tartu University Hospital; Tartu Estonia"
}] }
let(:doi) { create(:doi, creators: creators, contributors: [])}

it "convert" do
expect(Doi.convert_affiliation_by_id(id: doi.id)).to eq(1)
end
end
end



describe "migrates landing page" do
let(:provider) { create(:provider, symbol: "ADMIN") }
let(:client) { create(:client, provider: provider) }
Expand Down

0 comments on commit 69db099

Please sign in to comment.