Skip to content

Commit

Permalink
prepared plos fulltext source for vcr. #212
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Jan 1, 2015
1 parent 5c8d6e1 commit 4b87655
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 21 deletions.
2 changes: 1 addition & 1 deletion app/models/sources/plos_fulltext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def get_events_url(work)
def get_query_string(work)
return nil unless work.doi.present? || work.canonical_url.present?

[work.doi, work.canonical_url].compact.map { |i| "everything:\"#{i}\"" }.join("+OR+")
[work.doi, work.canonical_url].compact.map { |i| "everything:%22#{i}%22" }.join("+OR+")
end

def parse_data(result, work, options={})
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 12 additions & 15 deletions spec/models/plos_fulltext_spec.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
require 'rails_helper'

describe PlosFulltext, :type => :model do
describe PlosFulltext, type: :model, vcr: true do
subject { FactoryGirl.create(:plos_fulltext) }

let(:work) { FactoryGirl.build(:work, doi: nil, canonical_url: "https://github.com/rougier/ten-rules") }

context "lookup canonical URL" do
it "should look up canonical URL if there is no work url" do
work = FactoryGirl.create(:work, :doi => "10.1371/journal.pone.0043007", :canonical_url => nil)
lookup_stub = stub_request(:get, work.doi_as_url).to_return(:status => 404)
#lookup_stub = stub_request(:get, work.doi_as_url).to_return(:status => 404)
response = subject.get_data(work)
expect(lookup_stub).to have_been_requested
#expect(lookup_stub).to have_been_requested
end

it "should not look up canonical URL if there is work url" do
lookup_stub = stub_request(:get, work.canonical_url).to_return(:status => 200, :headers => { 'Location' => work.canonical_url })
stub = stub_request(:get, subject.get_query_url(work)).to_return(:body => File.read(fixture_path + 'plos_fulltext.json'))
#lookup_stub = stub_request(:get, work.canonical_url).to_return(:status => 200, :headers => { 'Location' => work.canonical_url })
#stub = stub_request(:get, subject.get_query_url(work)).to_return(:body => File.read(fixture_path + 'plos_fulltext.json'))
response = subject.get_data(work)
expect(lookup_stub).not_to have_been_requested
expect(stub).to have_been_requested
#expect(lookup_stub).not_to have_been_requested
#expect(stub).to have_been_requested
end
end

Expand All @@ -29,19 +29,16 @@
end

it "should report if there are no events returned by the PLOS Search API" do
body = File.read(fixture_path + 'plos_fulltext_nil.json')
stub = stub_request(:get, subject.get_query_url(work)).to_return(:body => body)
work = FactoryGirl.build(:work, doi: nil, canonical_url: "https://github.com/pymor/pymor")
response = subject.get_data(work)
expect(response).to eq(JSON.parse(body))
expect(stub).to have_been_requested
expect(response["response"]["numFound"]).to eq(0)
end

it "should report if there are events and event_count returned by the PLOS Search API" do
body = File.read(fixture_path + 'plos_fulltext.json')
stub = stub_request(:get, subject.get_query_url(work)).to_return(:body => body)
response = subject.get_data(work)
expect(response).to eq(JSON.parse(body))
expect(stub).to have_been_requested
expect(response["response"]["numFound"]).to eq(1)
doc = response["response"]["docs"].first
expect(doc["id"]).to eq("10.1371/journal.pcbi.1003833")
end

it "should catch errors with the PLOS Search API" do
Expand Down

0 comments on commit 4b87655

Please sign in to comment.