Skip to content

Commit

Permalink
Merge pull request #416 from psu-stewardship/413-pub-query-api-issue
Browse files Browse the repository at this point in the history
.uniq the query array when querying api on Activity Insight ID and DOI
  • Loading branch information
ajkiessl authored Dec 13, 2021
2 parents 870fb4b + 34a27da commit eac7c4f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 12 deletions.
4 changes: 2 additions & 2 deletions app/models/publication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ def self.filter_by_activity_insight_id(query, activity_insight_id)
.where(publication_imports: {
source: 'Activity Insight',
source_identifier: activity_insight_id
})
}).uniq
end

def self.filter_by_doi(query, doi)
Expand All @@ -599,7 +599,7 @@ def self.filter_by_doi(query, doi)
doi = url_prefix + doi
end

query.where(doi: doi)
query.where(doi: doi).uniq
end

private
Expand Down
59 changes: 49 additions & 10 deletions spec/requests/api/v1/publications_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def query_pubs
let!(:inaccessible_pub) { create(:publication, visible: true) }
let(:params) { '' }
let!(:token) { create :api_token, token: 'token123', total_requests: 0, last_used_at: nil }
let(:org) { create :organization }
let(:user) { create :user }
let!(:org) { create :organization }
let!(:user) { create :user }

before do
publications.each { |p| create :authorship, publication: p, user: user }
Expand Down Expand Up @@ -59,21 +59,42 @@ def query_pubs

describe 'params:' do
describe 'activity_insight_id' do
let(:ai_pub) { create(:publication, visible: true, imports: [pub_import]) }
let(:pub_import) { create(:publication_import, source: 'Activity Insight', source_identifier: '123') }
let!(:ai_pub) { create(:publication, visible: true, imports: [pub_import]) }
let!(:pub_import) { create(:publication_import, source: 'Activity Insight', source_identifier: '123') }

before do
create :authorship, user: user, publication: ai_pub
query_pubs
unless RSpec.current_example.metadata[:skip_before]
query_pubs
end
end

context 'with a valid Activity Insight ID' do
let(:params) { '?activity_insight_id=123' }

it 'returns a publication matching the specified Activity Insight ID' do
expect(json_response[:data].size).to eq(1)
expect(json_response[:data].first[:attributes][:activity_insight_ids].size).to eq(1)
expect(json_response[:data].first[:attributes][:activity_insight_ids].first).to eq('123')
context 'when one record is returned from the query' do
it 'returns a publication matching the specified Activity Insight ID' do
expect(json_response[:data].size).to eq(1)
expect(json_response[:data].first[:attributes][:activity_insight_ids].size).to eq(1)
expect(json_response[:data].first[:attributes][:activity_insight_ids].first).to eq('123')
end
end

context 'when the user of the found record is in multiple orgs (query returns multiple of same record)' do
let!(:org2) { create :organization }

before do
create :organization_api_permission, organization: org2, api_token: token
user.organizations << org2
user.save
query_pubs
end

it 'returns a unique list of publications matching the specified Activity Insight ID', skip_before: true do
expect(json_response[:data].size).to eq(1)
expect(json_response[:data].first[:attributes][:activity_insight_ids].size).to eq(1)
expect(json_response[:data].first[:attributes][:activity_insight_ids].first).to eq('123')
end
end
end

Expand All @@ -91,7 +112,9 @@ def query_pubs

before do
create :authorship, user: user, publication: doi_pub
query_pubs
unless RSpec.current_example.metadata[:skip_before]
query_pubs
end
end

context 'with a full DOI URL' do
Expand All @@ -101,6 +124,22 @@ def query_pubs
expect(json_response[:data].size).to eq(1)
expect(json_response[:data].first[:attributes][:doi]).to eq('https://doi.org/10.26207/46a7-9981')
end

context 'when the user of the found record is in multiple orgs (query returns multiple of same record)' do
let!(:org2) { create :organization }

before do
create :organization_api_permission, organization: org2, api_token: token
user.organizations << org2
user.save
query_pubs
end

it 'returns a unique list of publications matching the specified DOI', skip_before: true do
expect(json_response[:data].size).to eq(1)
expect(json_response[:data].first[:attributes][:doi]).to eq('https://doi.org/10.26207/46a7-9981')
end
end
end

context 'with a DOI starting with the doi: prefix' do
Expand Down

0 comments on commit eac7c4f

Please sign in to comment.