<commit_before># coding: utf-8
require 'rails_helper'
require 'feedlr_helper'

describe Entry do
  it "is created by feeldr entry" do
    feed_id = 'feed/http://test.com/rss'
    entry   = FeedlrHelper::entry(feed_id)
    feed    = Feed.first_or_create(id: feed_id)
    e       = Entry.first_or_create_by_feedlr(entry, feed)
    expect(e).not_to be_nil()
    expect(e.published).not_to be_nil()
    expect(e.crawled).not_to be_nil()
  end

  describe "::set_count_of_enclosures" do
    let (:feed) { FactoryGirl.create(:feed) }
    before do
      Entry::set_count_of_enclosures(feed.entries)
    end
    it {
      expect(feed.entries[0].count_of            ).not_to be_nil
      expect(feed.entries[0].count_of[:tracks]   ).to eq(TRACK_PER_ENTRY)
      expect(feed.entries[0].count_of[:albums]   ).to eq(ALBUM_PER_ENTRY)
      expect(feed.entries[0].count_of[:playlists]).to eq(PLAYLIST_PER_ENTRY)
    }
  end

  describe "::latest_items" do
    feed_num = 5
    before(:each) do
      DatabaseCleaner.start
      feeds = (0..feed_num-1).map { |i| FactoryGirl.create(:feed) }
      feeds.each do |f|
        f.entries.each do |e|
          e.published = 2.days.ago
          e.save!
        end
      end
    end
    after(:each) do
      DatabaseCleaner.clean
    end
    it "showes first N entries since specified time" do
      top_of_last_3_days = Entry.latest_items(entries_per_feed: 1, since: 3.days.ago)
      expect(top_of_last_3_days.count).to eq(feed_num)

      top_of_last_1_days = Entry.latest_items(entries_per_feed: 1, since: 1.days.ago)
      expect(top_of_last_1_days.count).to eq(0)

      top3_of_last_3_days = Entry.latest_items(entries_per_feed: 3, since: 3.days.ago)
      expect(top3_of_last_3_days.count).to eq(3 * feed_num)
      expect(top3_of_last_3_days[0].feed_id).not_to eq(top3_of_last_3_days[1].feed_id)
      expect(top3_of_last_3_days[0].feed_id).not_to eq(top3_of_last_3_days[2].feed_id)
      expect(top3_of_last_3_days[0].feed_id).to eq(top3_of_last_3_days[5].feed_id)
    end
  end

  describe "::popular_items_within_period" do
    before(:each) do
      DatabaseCleaner.start
      user     = FactoryGirl.create(:member)
      feed     = FactoryGirl.create(:feed)
      old_feed = FactoryGirl.create(:feed)
      old_user = FactoryGirl.create(:member)
      (0...ITEM_NUM).to_a.each { |i|
        n = i + 1
        SavedEntry.create! user:       user,
                           entry:      feed.entries[n],
                           created_at: n.days.ago,
                           updated_at: n.days.ago
        SavedEntry.create! user:       old_user,
                           entry:      old_feed.entries[n],
                           created_at: n.months.ago,
                           updated_at: n.months.ago
      }
    end
    after(:each) do
      DatabaseCleaner.clean
    end

    it "showes popular entries within a certain time period" do
      all = Entry.popular_items_within_period(period: 5.years.ago..Time.now)<commit_msg>Refacotoring: remove unnecessary DatabaseCleaner call<commit_after># coding: utf-8
require 'rails_helper'
require 'feedlr_helper'

describe Entry do
  it "is created by feeldr entry" do
    feed_id = 'feed/http://test.com/rss'
    entry   = FeedlrHelper::entry(feed_id)
    feed    = Feed.first_or_create(id: feed_id)
    e       = Entry.first_or_create_by_feedlr(entry, feed)
    expect(e).not_to be_nil()
    expect(e.published).not_to be_nil()
    expect(e.crawled).not_to be_nil()
  end

  describe "::set_count_of_enclosures" do
    let (:feed) { FactoryGirl.create(:feed) }
    before do
      Entry::set_count_of_enclosures(feed.entries)
    end
    it {
      expect(feed.entries[0].count_of            ).not_to be_nil
      expect(feed.entries[0].count_of[:tracks]   ).to eq(TRACK_PER_ENTRY)
      expect(feed.entries[0].count_of[:albums]   ).to eq(ALBUM_PER_ENTRY)
      expect(feed.entries[0].count_of[:playlists]).to eq(PLAYLIST_PER_ENTRY)
    }
  end

  describe "::latest_items" do
    feed_num = 5
    before(:each) do
      feeds = (0..feed_num-1).map { |i| FactoryGirl.create(:feed) }
      feeds.each do |f|
        f.entries.each do |e|
          e.published = 2.days.ago
          e.save!
        end
      end
    end
    it "showes first N entries since specified time" do
      top_of_last_3_days = Entry.latest_items(entries_per_feed: 1, since: 3.days.ago)
      expect(top_of_last_3_days.count).to eq(feed_num)

      top_of_last_1_days = Entry.latest_items(entries_per_feed: 1, since: 1.days.ago)
      expect(top_of_last_1_days.count).to eq(0)

      top3_of_last_3_days = Entry.latest_items(entries_per_feed: 3, since: 3.days.ago)
      expect(top3_of_last_3_days.count).to eq(3 * feed_num)
      expect(top3_of_last_3_days[0].feed_id).not_to eq(top3_of_last_3_days[1].feed_id)
      expect(top3_of_last_3_days[0].feed_id).not_to eq(top3_of_last_3_days[2].feed_id)
      expect(top3_of_last_3_days[0].feed_id).to eq(top3_of_last_3_days[5].feed_id)
    end
  end

  describe "::popular_items_within_period" do
    before(:each) do
      user     = FactoryGirl.create(:member)
      feed     = FactoryGirl.create(:feed)
      old_feed = FactoryGirl.create(:feed)
      old_user = FactoryGirl.create(:member)
      (0...ITEM_NUM).to_a.each { |i|
        n = i + 1
        SavedEntry.create! user:       user,
                           entry:      feed.entries[n],
                           created_at: n.days.ago,
                           updated_at: n.days.ago
        SavedEntry.create! user:       old_user,
                           entry:      old_feed.entries[n],
                           created_at: n.months.ago,
                           updated_at: n.months.ago
      }
    end

    it "showes popular entries within a certain time period" do
      all = Entry.popular_items_within_period(period: 5.years.ago..Time.now)
{'fraction_non_alphanumeric': 0.11021872863978127, 'fraction_numerical': 0.01572112098427888, 'mean_word_length': 2.4985056784219966, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '1847360', 'n_tokens_mistral': 2137, 'n_tokens_neox': 2065, 'n_words': 351}