<commit_before>require 'spec_helper'

describe Counter do
  let(:counter) { FactoryGirl.create(:counter) }

  it "should report that there are no events if the doi is missing" do
    article_without_doi = FactoryGirl.build(:article, :doi => "")
    counter.get_data(article_without_doi).should eq({ :events => [], :event_count => nil })
  end

  context "use the Counter API" do
    it "should report if there are no events and event_count returned by the Counter API" do
      article = FactoryGirl.build(:article, :doi => "10.1371/journal.pone.0044294")
      body = File.read(fixture_path + 'counter_nil.xml')
      stub = stub_request(:get, counter.get_query_url(article)).to_return(:body => body, :status => 404)
      counter.get_data(article).should eq({ :events => [], :event_count => 0, :events_url => counter.get_query_url(article), :event_metrics => { :pdf=>0, :html=>0, :shares=>nil, :groups=>nil, :comments=>nil, :likes=>nil, :citations=>nil, :total=>0 }, :attachment => nil })
      stub.should have_been_requested
    end

    it "should report if there are events and event_count returned by the Counter API" do
      article = FactoryGirl.build(:article, :doi => "10.1371/journal.pone.0008776")
      body = File.read(fixture_path + 'counter.xml')
      stub = stub_request(:get, counter.get_query_url(article)).to_return(:body => body, :status => 200)
      response = counter.get_data(article)
      response[:events].length.should eq(37)
      response[:events_url].should eq(counter.get_query_url(article))
      response[:event_count].should eq(3387)
      response[:attachment][:data].should eq(body)
      response[:event_metrics].should eq({ :pdf=>447, :html=>2919, :shares=>nil, :groups=>nil, :comments=>nil, :likes=>nil, :citations=>nil, :total=>3387 })
      stub.should have_been_requested
    end

    it "should catch errors with the Counter API" do
      article = FactoryGirl.build(:article, :doi => "10.1371/journal.pone.0000001")
      stub = stub_request(:get, counter.get_query_url(article)).to_return(:status => [408])
      counter.get_data(article, options = { :source_id => counter.id }).should be_nil
      stub.should have_been_requested
      Alert.count.should == 1
      alert = Alert.first
      alert.class_name.should eq("Net::HTTPRequestTimeOut")
      alert.status.should == 408
      alert.source_id.should == counter.id
    end
  end
end<commit_msg>ALM-401 completed test for counter source<commit_after>require 'spec_helper'

describe Counter do

  context "class methods" do
    subject { Counter }

    it "should provide a date range" do
      # array of hashes for the 10 last months, including the current month
      start_date = 10.months.ago.to_date
      end_date = Date.today
      response = subject.date_range(month: start_date.month, year: start_date.year)
      response.count.should == 11
      response.last.should eq({ month: end_date.month, year: end_date.year })
    end

    it "should format the CouchDB report as csv" do
      url = "#{CONFIG[:couchdb_url]}_design/reports/_view/counter"
      stub = stub_request(:get, url).to_return(:body => File.read(fixture_path + 'counter_report.json'), :status => 200, :headers => { "Content-Type" => "application/json" })
      response = CSV.parse(subject.to_csv)
      response.count.should == 27
      response.first.should eq(["doi", "html", "pdf", "total"])
      response.last.should eq(["10.1371/journal.ppat.1000446", "7489", "1147", "8676"])
    end

    it "should format the CouchDB HTML report as csv" do
      start_date = Date.new(2013,11,1)
      dates = subject.date_range(month: start_date.month, year: start_date.year).map { |date| "#{date[:year]}-#{date[:month]}" }
      url = "#{CONFIG[:couchdb_url]}_design/reports/_view/counter_html_views"
      stub = stub_request(:get, url).to_return(:body => File.read(fixture_path + 'counter_html_report.json'), :status => 200, :headers => { "Content-Type" => "application/json" })
      response = CSV.parse(subject.to_csv(format: "html", month: 11, year: 2013))
      response.count.should == 27
      response.first.should eq(["doi"] + dates)
      response.last.should eq(["10.1371/journal.ppat.1000446", "112", "95", "45"])
    end

    it "should format the CouchDB PDF report as csv" do
      start_date = Date.new(2013,11,1)
      dates = subject.date_range(month: start_date.month, year: start_date.year).map { |date| "#{date[:year]}-#{date[:month]}" }
      url = "#{CONFIG[:couchdb_url]}_design/reports/_view/counter_pdf_views"
      stub = stub_request(:get, url).to_return(:body => File.read(fixture_path + 'counter_pdf_report.json'), :status => 200, :headers => { "Content-Type" => "application/json" })
      response = CSV.parse(subject.to_csv(format: "pdf", month: 11, year: 2013))
      response.count.should == 27
      response.first.should eq(["doi"] + dates)
      response[2].should eq(["10.1371/journal.pbio.0020413", "0", "0", "1"])
    end

    it "should format the CouchDB XML report as csv" do
      start_date = Date.new(2013,11,1)
      dates = subject.date_range(month: start_date.month, year: start_date.year).map { |date| "#{date[:year]}-#{date[:month]}" }
      url = "#{CONFIG[:couchdb_url]}_design/reports/_view/counter_xml_views"
      stub = stub_request(:get, url).to_return(:body => File.read(fixture_path + 'counter_xml_report.json'), :status => 200, :headers => { "Content-Type" => "application/json" })
      response = CSV.parse(subject.to_csv(format: "xml", month: 11, year: 2013))
      response.count.should == 27
      response.first.should eq(["doi"] + dates)
      response[2].should eq(["10.1371/journal.pbio.0020413", "0", "0", "0"])
    end

    it "should format the CouchDB combined report as csv" do
      start_date = Date.new(2013,11,1)
      dates = subject.date_range(month: start_date.month, year: start_date.year).map { |date| "#{date[:year]}-#{date[:month]}" }
      url = "#{CONFIG[:couchdb_url]}_design/reports/_view/counter_combined_views"
      stub = stub_request(:get, url).to_return(:body => File.read(fixture_path + 'counter_combined_report.json'), :status => 200, :headers => { "Content-Type" => "application/json" })
      response = CSV.parse(subject.to_csv(format: "combined", month: 11, year: 2013))
      response.count.should == 27
      response.first.should eq(["doi"] + dates)
      response[3].should eq(["10.1371/journal.pbio.0030137", "165", "149", "61"])
    end
  end

  context "use the PMC API" do
    let(:counter) { FactoryGirl.create(:counter) }

    it "should report that there are no events if the doi is missing" do
      article_without_doi = FactoryGirl.build(:article, :doi => "")
      counter.get_data(article_without_doi).should eq({ :events => [], :event_count => nil })
    end

    context "use the Counter API" do
      it "should report if there are no events and event_count returned by the Counter API" do
        article = FactoryGirl.build(:article, :doi => "10.1371/journal.pone.0044294")
        body = File.read(fixture_path + 'counter_nil.xml')
        stub = stub_request(:get, counter.get_query_url(article)).to_return(:body => body, :status => 404)
        counter.get_data(article).should eq({ :events => [], :event_count => 0, :events_url => counter.get_query_url(article), :event_metrics => { :pdf=>0, :html=>0, :shares=>nil, :groups=>nil, :comments=>nil, :likes=>nil, :citations=>nil, :total=>0 }, :attachment => nil })
        stub.should have_been_requested
      end

      it "should report if there are events and event_count returned by the Counter API" do
        article = FactoryGirl.build(:article, :doi => "10.1371/journal.pone.0008776")
        body = File.read(fixture_path + 'counter.xml')
        stub = stub_request(:get, counter.get_query_url(article)).to_return(:body => body, :status => 200)
        response = counter.get_data(article)
        response[:events].length.should eq(37)
        response[:events_url].should eq(counter.get_query_url(article))
        response[:event_count].should eq(3387)
        response[:attachment][:data].should eq(body)
        response[:event_metrics].should eq({ :pdf=>447, :html=>2919, :shares=>nil, :groups=>nil, :comments=>nil, :likes=>nil, :citations=>nil, :total=>3387 })
        stub.should have_been_requested
      end

      it "should catch errors with the Counter API" do
        article = FactoryGirl.build(:article, :doi => "10.1371/journal.pone.0000001")
        stub = stub_request(:get, counter.get_query_url(article)).to_return(:status => [408])
        counter.get_data(article, options = { :source_id => counter.id }).should be_nil
        stub.should have_been_requested
        Alert.count.should == 1
        alert = Alert.first
        alert.class_name.should eq("Net::HTTPRequestTimeOut")
        alert.status.should == 408
        alert.source_id.should == counter.id
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.15286695955840937, 'fraction_numerical': 0.03751267320040554, 'mean_word_length': 3.8119241192411923, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 7}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7219941', 'n_tokens_mistral': 3100, 'n_tokens_neox': 2792, 'n_words': 637}