<commit_before>require 'spec_helper'

describe Pmc do

  context "class methods" do
    subject { Pmc }

    it "should provide a date range" do
      # array of hashes for the 10 last months, excluding the current month
      start_date = 10.months.ago.to_date
      end_date = 1.month.ago.to_date
      response = subject.date_range(month: start_date.month, year: start_date.year)
      response.count.should == 10
      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/pmc"
      stub = stub_request(:get, url).to_return(:body => File.read(fixture_path + 'pmc_report.json'), :status => 200, :headers => { "Content-Type" => "application/json" })
      response = CSV.parse(subject.to_csv)
      response.count.should == 25
      response.first.should eq(["doi", "html", "pdf", "total"])
      response.last.should eq(["10.1371/journal.ppat.1000446", "9", "6", "15"])
    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/pmc_html_views"
      stub = stub_request(:get, url).to_return(:body => File.read(fixture_path + 'pmc_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 == 25
      response.first.should eq(["doi"] + dates)
      response.last.should eq(["10.1371/journal.ppat.1000446", "5", "4"])
    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/pmc_pdf_views"
      stub = stub_request(:get, url).to_return(:body => File.read(fixture_path + 'pmc_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 == 25
      response.first.should eq(["doi"] + dates)
      response[2].should eq(["10.1371/journal.pbio.0030137", "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/pmc_combined_views"
      stub = stub_request(:get, url).to_return(:body => File.read(fixture_path + 'pmc_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 == 25
      response.first.should eq(["doi"] + dates)
      response[3].should eq(["10.1371/journal.pbio.0040015", "9", "10"])
    end
  end

  let(:pmc) { FactoryGirl.create(:pmc) }

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

  it "should report that there are no events if article was published on the same day" do
    article = FactoryGirl.build(:article, :published_on => Time.zone.today)
    pmc.get_data(article).should eq({ :events => [], :event_count => nil })
  end

  context "save PMC data" do
    let(:month) { 1.month.ago.month }
    let(:year) { 1.month.ago.year }
    let(:journal) { "plosbiol" }

    it "should fetch and save PMC data" do
      stub = stub_request(:get, pmc.get_feed_url(month, year, journal)).to_return(:headers => { "Content-Type" => "application/xml" }, :body => File.read(fixture_path + 'pmc.xml'), :status => 200)
      pmc.get_feed(month, year).should be_empty
      file = "#{Rails.root}/data/pmcstat_#{journal}_#{month}_#{year}.xml"
      File.exist?(file).should be_true
      stub.should have_been_requested
      Alert.count.should == 0
    end
  end<commit_msg>ALM-401 make monthly report test independent of current date<commit_after>require 'spec_helper'

describe Pmc do

  context "class methods" do
    subject { Pmc }

    it "should provide a date range" do
      # array of hashes for the 10 last months, excluding the current month
      start_date = 10.months.ago.to_date
      end_date = 1.month.ago.to_date
      response = subject.date_range(month: start_date.month, year: start_date.year)
      response.count.should == 10
      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/pmc"
      stub = stub_request(:get, url).to_return(:body => File.read(fixture_path + 'pmc_report.json'), :status => 200, :headers => { "Content-Type" => "application/json" })
      response = CSV.parse(subject.to_csv)
      response.count.should == 25
      response.first.should eq(["doi", "html", "pdf", "total"])
      response.last.should eq(["10.1371/journal.ppat.1000446", "9", "6", "15"])
    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]}" }
      row = ["10.1371/journal.ppat.1000446", "5", "4"]
      row.fill("0", 3..(dates.length))
      url = "#{CONFIG[:couchdb_url]}_design/reports/_view/pmc_html_views"
      stub = stub_request(:get, url).to_return(:body => File.read(fixture_path + 'pmc_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 == 25
      response.first.should eq(["doi"] + dates)
      response.last.should eq(row)
    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]}" }
      row = ["10.1371/journal.pbio.0030137", "0", "0"]
      row.fill("0", 3..(dates.length))
      url = "#{CONFIG[:couchdb_url]}_design/reports/_view/pmc_pdf_views"
      stub = stub_request(:get, url).to_return(:body => File.read(fixture_path + 'pmc_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 == 25
      response.first.should eq(["doi"] + dates)
      response[2].should eq(row)
    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]}" }
      row = ["10.1371/journal.pbio.0040015", "9", "10"]
      row.fill("0", 3..(dates.length))
      url = "#{CONFIG[:couchdb_url]}_design/reports/_view/pmc_combined_views"
      stub = stub_request(:get, url).to_return(:body => File.read(fixture_path + 'pmc_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 == 25
      response.first.should eq(["doi"] + dates)
      response[3].should eq(row)
    end
  end

  let(:pmc) { FactoryGirl.create(:pmc) }

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

  it "should report that there are no events if article was published on the same day" do
    article = FactoryGirl.build(:article, :published_on => Time.zone.today)
    pmc.get_data(article).should eq({ :events => [], :event_count => nil })
  end

  context "save PMC data" do
    let(:month) { 1.month.ago.month }
    let(:year) { 1.month.ago.year }
    let(:journal) { "plosbiol" }

    it "should fetch and save PMC data" do
      stub = stub_request(:get, pmc.get_feed_url(month, year, journal)).to_return(:headers => { "Content-Type" => "application/xml" }, :body => File.read(fixture_path + 'pmc.xml'), :status => 200)
      pmc.get_feed(month, year).should be_empty
      file = "#{Rails.root}/data/pmcstat_#{journal}_#{month}_#{year}.xml"
      File.exist?(file).should be_true
      stub.should have_been_requested
      Alert.count.should == 0
    end
  end
{'fraction_non_alphanumeric': 0.16306062654947037, 'fraction_numerical': 0.03189091728645481, 'mean_word_length': 3.6056045666839647, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '3359553', 'n_tokens_mistral': 3230, 'n_tokens_neox': 2966, 'n_words': 669}