<commit_before>require 'spec_helper'

describe FacebookDataCacher do
  context '.perform' do
    let(:user) { FactoryGirl.create :user, oauth_token: '<PASSWORD>' }

    before do
      stub_http_request(:post, /graph.facebook.com/).to_return body: [
          { code: 200,
            headers: [{ name: 'Content-Type',
                        value: 'text/javascript; charset=UTF-8' }],
            body: "{}" },
          { code: 200,
            headers: [{ name: 'Content-Type',
                        value: 'text/javascript; charset=UTF-8' }],
            body: "{}" } ].to_json
    end

    it "caches user data when it is nil" do
      expect(FacebookDataCacher.perform user.id).to be true
    end

    it "caches user data when it is expired" do
      expect(FacebookDataCacher.perform user.id).to be true
      expect(FacebookDataCacher.perform user.id).to be nil
      travel_to APP_CONFIG.facebook.cache_expiry_time.from_now do
        expect(FacebookDataCacher.perform user.id).to be true
        expect(FacebookDataCacher.perform user.id).to be nil
      end
    end

    it "doesn't cache user data when it is still valid" do
      user_with_fresh_data = FactoryGirl.create :user, oauth_token: '<PASSWORD>', facebook_data_cached_at: Time.now
      expect(FacebookDataCacher.perform user_with_fresh_data.id).to be nil
    end

    it "doesn't fail when response is wrong" do
      stub_http_request(:post, /graph.facebook.com/).to_return status: 500
      expect(FacebookDataCacher.perform user.id).to be nil
    end
  end
end<commit_msg>fixed facebook cacher specs<commit_after>require 'spec_helper'

describe FacebookDataCacher do
  context '.perform' do
    let(:user) { FactoryGirl.create :user, oauth_token: '<PASSWORD>' }

    before do
      stub_http_request(:post, /graph.facebook.com/).to_return body: [
          { code: 200,
            headers: [{ name: 'Content-Type',
                        value: 'text/javascript; charset=UTF-8' }],
            body: "{}" },
          { code: 200,
            headers: [{ name: 'Content-Type',
                        value: 'text/javascript; charset=UTF-8' }],
            body: "{}" } ].to_json
    end

    it "caches user data when they are nil" do
      expect(FacebookDataCacher.perform user.id).to be true
    end

    it "caches user data when they are expired" do
      expect(FacebookDataCacher.perform user.id).to be true
      expect(FacebookDataCacher.perform user.id).to be nil
      travel_to (APP_CONFIG.facebook.cache_expiry_time.from_now + 1.second) do
        expect(FacebookDataCacher.perform user.id).to be true
        expect(FacebookDataCacher.perform user.id).to be nil
      end
    end

    it "doesn't cache user data when they are still valid" do
      user_with_fresh_data = FactoryGirl.create :user, oauth_token: '<PASSWORD>', facebook_data_cached_at: Time.now
      expect(FacebookDataCacher.perform user_with_fresh_data.id).to be nil
    end

    it "doesn't fail when response is wrong" do
      stub_http_request(:post, /graph.facebook.com/).to_return status: 500
      expect(FacebookDataCacher.perform user.id).to be nil
    end
  end
end
{'fraction_non_alphanumeric': 0.10254777070063695, 'fraction_numerical': 0.00732484076433121, 'mean_word_length': 2.6396292004634994, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1776325', 'n_tokens_mistral': 1013, 'n_tokens_neox': 936, 'n_words': 251}