Skip to content

Commit

Permalink
rubocop fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
peetucket committed Mar 11, 2022
1 parent c4068d8 commit 89d250b
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 17 deletions.
4 changes: 4 additions & 0 deletions spec/lib/agent/author_name_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,16 +181,20 @@
expect(fn_query).not_to include(be_empty)
expect(fn_query.size).to eq(fn_query.uniq.size)
end

it 'includes name with first_name' do
expect(fn_query).to include "#{all_names.last_name},#{all_names.first_name}"
end

it 'does not include name with first_initial' do
expect(fn_query).not_to include "#{all_names.last_name},#{all_names.first_initial}"
end

it 'does not include name with middle_name' do
expect(fn_query).not_to include "#{all_names.last_name},#{all_names.first_name},#{all_names.middle_name}"
expect(fn_query).to all(exclude(",#{all_names.middle_name}"))
end

it 'does not include name with middle_initial' do
expect(fn_query).not_to include "#{all_names.last_name},#{all_names.first_name},#{all_names.middle_initial}"
expect(fn_query).to all(exclude(",#{all_names.middle_initial}"))
Expand Down
83 changes: 70 additions & 13 deletions spec/lib/web_of_science/query_name_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
let(:blank_author) { create :author, :blank_first_name }
let(:names) { query_name.send(:names) }

let(:alternate_identity) { create :author_identity } # this creates the associated author as well
let(:alternate_author_identity) { alternate_identity.author }

# avoid caching Savon client across examples (affects VCR)
before { allow(WebOfScience).to receive(:client).and_return(WebOfScience::Client.new(Settings.WOS.AUTH_CODE)) }

Expand Down Expand Up @@ -75,9 +78,9 @@
end

describe '#names' do
#=> "\"Altman,Russ\" or \"Altman,R\" or \"Altman,Russ,Biagio\" or \"Altman,Russ,B\" or \"Altman,R,B\""
#=> "\"Altman,Russ\" or \"Altman,R\" or \"Altman,Russ,Biagio\" or \"Altman,Russ,B\" or \"Altman,RB\" or "\"Altman,R,B\""
it 'includes all of the name variations, including the preferred last name' do
expect(names.size).to eq 5
expect(names.size).to eq 6
expect(names).to include('Altman,Russ')
end
end
Expand Down Expand Up @@ -131,21 +134,75 @@
end
end

context 'for a single alternate identity with invalid data' do
context 'for a single alternate identity' do
let(:alt_last_name) { alternate_identity.last_name }
let(:alt_first_name) { alternate_identity.first_name }
let(:alt_middle_name) { alternate_identity.middle_name }

describe '#names' do
let(:author_one_identity) { create :author }
let(:bad_alternate_identity) { create :author_identity }
context 'with invalid data and ambiguous first name' do
it 'ignores the bad alternate identity data and first initial variants' do
alternate_identity.update(first_name: '.', institution: 'Example')
expect(alternate_author_identity.unique_first_initial?).to be false # because of a non-Stanford alternate identity
expect(alternate_author_identity.author_identities.first.first_name).to eq '.' # bad first name
# we do not get the name variant with the period for a first name (i.e. the alternate identity)
# nor do we get first initial variants because of the ambiguous first initial
# (we would have more if we allowed the bad name variant and the ambiguous first initial)
expect(described_class.new(alternate_author_identity).send(:names)).to match_array %w[Edler,Alice
Edler,Alice,Jim
Edler,Alice,J]
end
end

context 'with invalid data and non-ambiguous first name' do
it 'ignores the bad alternate identity data but includes first initial variants' do
alternate_identity.update(first_name: '.', institution: 'Stanford')
expect(alternate_author_identity.unique_first_initial?).to be true # because alternate identity is Stanford and unique
expect(alternate_author_identity.author_identities.first.first_name).to eq '.' # bad first name
# we do not get the name variant with the period for a first name (i.e. no alternate identity)
expect(described_class.new(alternate_author_identity).send(:names)).to match_array %w[Edler,Alice
Edler,A
Edler,Alice,Jim
Edler,Alice,J
Edler,AJ
Edler,A,J]
end
end

before do
bad_alternate_identity.update(first_name: '.')
author_one_identity.author_identities << bad_alternate_identity
context 'with valid data and ambiguous first name' do
it 'ignores the first initial variants' do
alternate_identity.update(first_name: 'Sam', institution: 'Example')
expect(alternate_author_identity.unique_first_initial?).to be false # because of a non-Stanford alternate identity
# we do not get first initial variants because of the ambiguous first initial
# but we do get the other variants with the alternate identity
# (we would have more if we allowed the bad name variant and the ambiguous first initial)
expect(described_class.new(alternate_author_identity).send(:names)).to match_array ['Edler,Alice',
'Edler,Alice,Jim',
'Edler,Alice,J',
"#{alt_last_name},#{alt_first_name}",
"#{alt_last_name},#{alt_first_name},#{alt_middle_name}",
"#{alt_last_name},#{alt_first_name},#{alt_middle_name[0]}"]
end
end

it 'ignores the bad alternate identity data' do
expect(author_one_identity.author_identities.first.first_name).to eq '.' # bad first name
# we get three name variants out (we would have more if we allowed the bad name variant)
expect(described_class.new(author_one_identity).send(:names)).to eq %w[Edler,Alice Edler,Alice,Jim
Edler,Alice,J]
context 'with valid data and non-ambiguous first name' do
it 'includes all name variants' do
alternate_identity.update(first_name: 'Alice2', institution: 'Stanford')
expect(alternate_author_identity.unique_first_initial?).to be true # because alternate identity is Stanford and unique
# we get all variants with first initials and also the alternate identity
expect(described_class.new(alternate_author_identity).send(:names)).to match_array ['Edler,Alice',
'Edler,A',
'Edler,Alice,Jim',
'Edler,Alice,J',
'Edler,AJ',
'Edler,A,J',
"#{alt_last_name},#{alt_first_name}",
"#{alt_last_name},#{alt_first_name[0]}",
"#{alt_last_name},#{alt_first_name},#{alt_middle_name}",
"#{alt_last_name},#{alt_first_name},#{alt_middle_name[0]}",
"#{alt_last_name},#{alt_first_name[0]}#{alt_middle_name[0]}",
"#{alt_last_name},#{alt_first_name[0]},#{alt_middle_name[0]}"]
end
end
end
end
Expand Down
11 changes: 7 additions & 4 deletions spec/models/author_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,25 +70,28 @@
it 'confirms unique first initial within stanford with no alternate identities' do
odd_name = create :odd_name
expect(odd_name.author_identities.size).to eq(0) # has no alternate identities
expect(odd_name.unique_first_initial?).to eq(true) # and no other odd names likes this at stanford, so ok to search with first initial
expect(odd_name.unique_first_initial?).to be(true) # and no other odd names likes this at stanford, so ok to search with first initial
end

it 'confirms unique first initial within stanford with stanford only alternate identities' do
subject.update_from_cap_authorship_profile_hash(auth_hash)
expect(subject.author_identities.size).to eq(2) # has alternate identities
expect(subject.unique_first_initial?).to eq(true) # ok, because all identities are stanford or no institution, and no other first name ambiguity
expect(subject.unique_first_initial?).to be(true) # ok, because all identities are stanford or no institution, and no other first name ambiguity
end

it 'confirms ambiguous first initial within stanford with no alternate identities' do
create :author_duped_last_name
expect(subject.author_identities.size).to eq(0) # no alternate identities
expect(subject.unique_first_initial?).to eq(false) # not unique, because we now have another stanford author with same last name and first initial
expect(subject.unique_first_initial?).to be(false) # not unique, because we now have another stanford author with same last name and first initial
end

it 'confirms ambiguous first initial even when non ambiguous within Stanford due to a non-Stanford alternate identity existing' do
author_with_alternate_identities = create :author_with_alternate_identities
expect(author_with_alternate_identities.author_identities.size).to eq(1) # alternate identities for primary author
expect(author_with_alternate_identities.author_identities.first.institution).not_to be blank? # alternate institution is not empty
expect(author_with_alternate_identities.author_identities.first.institution.include?('Stanford')).to be false # alternate institution is not Stanford
# not unique, because even though there are no other stanford authors with similar names, they have a non-Stanford alternate identity
expect(author_with_alternate_identities.unique_first_initial?).to eq(false)
expect(author_with_alternate_identities.unique_first_initial?).to be(false)
end
end

Expand Down

0 comments on commit 89d250b

Please sign in to comment.