Skip to content

Commit

Permalink
prepared article_coverage spec 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 952da9e commit 89df9e7
Show file tree
Hide file tree
Showing 4 changed files with 210 additions and 12 deletions.

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.

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

19 changes: 7 additions & 12 deletions spec/models/article_coverage_spec.rb
@@ -1,6 +1,6 @@
require 'rails_helper'

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

let(:work) { FactoryGirl.build(:work, :doi => "10.1371/journal.pone.0047712", published_on: "2013-11-01") }
Expand All @@ -18,32 +18,27 @@
context "get_data from the Article Coverage API" do
it "should report if work doesn't exist in Article Coverage source" do
work = FactoryGirl.build(:work, :doi => "10.1371/journal.pone.0008776")
stub = stub_request(:get, subject.get_query_url(work)).to_return(:body => {"error" => "Article not found"}.to_json, :status => 404)
expect(subject.get_data(work)).to eq(error: "Article not found", status: 404)
expect(stub).to have_been_requested
end

it "should report if there are no events and event_count returned by the Article Coverage API" do
work = FactoryGirl.build(:work, :doi => "10.1371/journal.pone.0008775")
body = File.read(fixture_path + 'article_coverage_curated_nil.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(subject.get_data(work)).to eq(error: "Article not found", status: 404)
end

it "should report if there are events and event_count returned by the Article Coverage API" do
body = File.read(fixture_path + 'article_coverage.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["doi"]).to eq(work.doi)
expect(response["referrals"].length).to eq(8)
referral = response["referrals"].first
expect(referral["title"]).to eq("Everything You Know About Your Personal Hygiene Is Wrong ")
end

it "should catch timeout errors with the Article Coverage API" do
stub = stub_request(:get, subject.get_query_url(work)).to_return(:status => [408])
response = subject.get_data(work, options = { :source_id => subject.id })
expect(response).to eq(error: "the server responded with status 408 for http://example.org?doi=#{work.doi_escaped}", :status=>408)
expect(response).to eq(error: "the server responded with status 408 for http://mediacuration.plos.org/api/v1?doi=#{work.doi_escaped}&state=all", :status=>408)
expect(stub).to have_been_requested
expect(Alert.count).to eq(1)
alert = Alert.first
Expand Down

0 comments on commit 89df9e7

Please sign in to comment.