<commit_before>require 'rails_helper'

describe Organisation, :type => :model do

  before do
    FactoryGirl.factories.clear
    FactoryGirl.find_definitions

    @category1 = FactoryGirl.create(:category, :charity_commission_id => 207)
    @category2 = FactoryGirl.create(:category, :charity_commission_id => 305)
    @category3 = FactoryGirl.create(:category, :charity_commission_id => 108)
    @category4 = FactoryGirl.create(:category, :charity_commission_id => 302)
    @category5 = FactoryGirl.create(:category, :charity_commission_id => 306)
    @org1 = FactoryGirl.build(:organisation, :email => "", :name => 'Harrow Bereavement Counselling', :description => 'Bereavement Counselling', :address => '64 pinner road', :postcode => 'HA1 3TE', :donation_info => 'www.harrow-bereavment.co.uk/donate')
    @org1.save!
    @org2 = FactoryGirl.build(:organisation, :name => 'Indian Elders Association',:email => "",
                              :description => 'Care for the elderly', :address => '64 pinner road', :postcode => 'HA1 3RE', :donation_info => 'www.indian-elders.co.uk/donate')
    @org2.categories << @category1
    @org2.categories << @category2
    @org2.save!
    @org3 = FactoryGirl.build(:organisation, :email => "", :name => 'Age UK Elderly', :description => 'Care for older people', :address => '64 pinner road', :postcode => 'HA1 3RE', :donation_info => 'www.age-uk.co.uk/donate')
    @org3.categories << @category1
    @org3.save!
  end

  describe '#not_updated_recently?' do
    let(:org){FactoryGirl.create(:organisation, updated_at: Time.now)}

    it{expect(org.not_updated_recently?).to be false}

    context "updated too long ago" do
      let(:org){FactoryGirl.create(:organisation, updated_at: 365.day.ago)}
      it{expect(org.not_updated_recently?).to be true}
    end

    context "when updated recently" do
      let(:org){FactoryGirl.create(:organisation, updated_at: 364.day.ago)}
      it{expect(org.not_updated_recently?).to be false}
    end
  end

  describe "#not_updated_recently_or_has_no_owner?" do
    let(:subject){FactoryGirl.create(:organisation, :name => "Org with no owner", :updated_at => 364.day.ago)}
    context 'has no owner but updated recently' do
      it{expect(subject.not_updated_recently_or_has_no_owner?).to be true}
    end
    context 'has owner but old update' do
      let(:subject){FactoryGirl.create(:organisation_with_owner, :updated_at => 366.day.ago)}
      it{expect(subject.not_updated_recently_or_has_no_owner?).to be true}
    end
    context 'has no owner and old update' do 
      let(:subject){FactoryGirl.create(:organisation, :updated_at => 366.day.ago)}
      it{expect(subject.not_updated_recently_or_has_no_owner?).to be true}
    end
    context 'has owner and recent update' do
      let(:subject){FactoryGirl.create(:organisation_with_owner, :updated_at => 364.day.ago)}
      it{expect(subject.not_updated_recently_or_has_no_owner?).to be false}
    end
  end

  describe "#gmaps4rails_marker_attrs" do
    context 'no user' do
      it 'returns small icon when no associated user' do
        expect(@org1.gmaps4rails_marker_attrs).to eq(["https://maps.gstatic.com/intl/en_ALL/mapfiles/markers2/measle.png", {"data-id"=>@org1.id, :class=>"measle"}])
      end
    end

    context 'has user' do
      before(:each) do
        usr = FactoryGirl.create(:user, :email => "<EMAIL>")
        usr.confirm!
        @org1.users << [usr]
        @org1.save!
      end
      after(:each) do
        allow(Time).to receive(:now).and_call_original
      end
      it 'returns large icon when there is an associated user' do
        expect(@org1.gmaps4rails_marker_attrs).to eq( ["http://mt.googleapis.com/vt/icon/name=icons/spotlight/spotlight-poi.png", {"data-id"=>@org1.id,:class=>"marker"}])
      end

      [365, 366, 500].each do |days|
        it "returns small icon when update is #{days} days old" do
          future_time = Time.at(Time.now + days.day)
          allow(Time).to receive(:now){future_time}
          expect(@org1.gmaps4rails_marker_attrs).to eq(["https://maps.gstatic.com/intl/en_ALL/mapfiles/markers2/measle.png", {"data-id"=>@org1.id, :class=>"measle"}])
        end
      end
      [ 2, 100, 200, 364].each do |days|
        it "returns large icon when update is only #{days} days old" do
          future_time = Time.at(Time.now + days.day)
          allow(Time).to receive(:now){future_time}
          expect(@org1.gmaps4rails_marker_attrs).to eq( ["http://mt.googleapis.com/vt/icon/name=icons/spotlight/spotlight-poi.png", {"data-id"=>@org1.id, :class=>"marker"} ])
        end
      end
    end
  end
  context 'scopes for orphan orgs' do
    before(:each) do
      @user = FactoryGirl.create(:user, :email => "<EMAIL>")
      @user.confirm!
    end

    it 'should allow us to grab orgs with emails' do
      expect(Organisation.not_null_email).to eq []
      @org1.email = "<EMAIL>"
      @org1.save
      expect(Organisation.not_null_email).to eq [@org1]
    end
<commit_msg>Change org spec to use computed fields<commit_after>require 'rails_helper'

describe Organisation, :type => :model do

  before do
    FactoryGirl.factories.clear
    FactoryGirl.find_definitions

    @category1 = FactoryGirl.create(:category, :charity_commission_id => 207)
    @category2 = FactoryGirl.create(:category, :charity_commission_id => 305)
    @category3 = FactoryGirl.create(:category, :charity_commission_id => 108)
    @category4 = FactoryGirl.create(:category, :charity_commission_id => 302)
    @category5 = FactoryGirl.create(:category, :charity_commission_id => 306)
    @org1 = FactoryGirl.build(:organisation, :email => "", :name => 'Harrow Bereavement Counselling', :description => 'Bereavement Counselling', :address => '64 pinner road', :postcode => 'HA1 3TE', :donation_info => 'www.harrow-bereavment.co.uk/donate')
    @org1.save!
    @org2 = FactoryGirl.build(:organisation, :name => 'Indian Elders Association',:email => "",
                              :description => 'Care for the elderly', :address => '64 pinner road', :postcode => 'HA1 3RE', :donation_info => 'www.indian-elders.co.uk/donate')
    @org2.categories << @category1
    @org2.categories << @category2
    @org2.save!
    @org3 = FactoryGirl.build(:organisation, :email => "", :name => 'Age UK Elderly', :description => 'Care for older people', :address => '64 pinner road', :postcode => 'HA1 3RE', :donation_info => 'www.age-uk.co.uk/donate')
    @org3.categories << @category1
    @org3.save!
  end

  describe '#not_updated_recently?' do
    let(:org){FactoryGirl.create(:organisation, updated_at: Time.now)}

    it{expect(org.not_updated_recently?).to be false}

    context "updated too long ago" do
      let(:org){FactoryGirl.create(:organisation, updated_at: 365.day.ago)}
      it{expect(org.not_updated_recently?).to be true}
    end

    context "when updated recently" do
      let(:org){FactoryGirl.create(:organisation, updated_at: 364.day.ago)}
      it{expect(org.not_updated_recently?).to be false}
    end
  end

  describe "#not_updated_recently_or_has_no_owner?" do
    let(:subject){FactoryGirl.create(:organisation, :name => "Org with no owner", :updated_at => 364.day.ago)}
    context 'has no owner but updated recently' do
      it{expect(subject.not_updated_recently_or_has_no_owner?).to be true}
    end
    context 'has owner but old update' do
      let(:subject){FactoryGirl.create(:organisation_with_owner, :updated_at => 366.day.ago)}
      it{expect(subject.not_updated_recently_or_has_no_owner?).to be true}
    end
    context 'has no owner and old update' do 
      let(:subject){FactoryGirl.create(:organisation, :updated_at => 366.day.ago)}
      it{expect(subject.not_updated_recently_or_has_no_owner?).to be true}
    end
    context 'has owner and recent update' do
      let(:subject){FactoryGirl.create(:organisation_with_owner, :updated_at => 364.day.ago)}
      it{expect(subject.not_updated_recently_or_has_no_owner?).to be false}
    end
  end

  describe "#gmaps4rails_marker_attrs" do

    def build_org_with_computed_fields_and_updated_at org, updated_at = nil
      org.update_attributes(updated_at: updated_at) unless updated_at.nil?
      Queries::Organisations.xyz(Organisation.where(id: org.id)).first
    end
    context 'no user' do
      it 'returns small icon when no associated user' do
        expect(build_org_with_computed_fields_and_updated_at(@org1).gmaps4rails_marker_attrs).to eq(["https://maps.gstatic.com/intl/en_ALL/mapfiles/markers2/measle.png",
          {"data-id"=>@org1.id, :class=>"measle"}])
      end
    end

    context 'has user' do
      before(:each) do
        usr = FactoryGirl.create(:user, :email => "<EMAIL>")
        usr.confirm!
        @org1.users << [usr]
        @org1.save!
      end
      it 'returns large icon when there is an associated user' do
        expect(build_org_with_computed_fields_and_updated_at(@org1).gmaps4rails_marker_attrs).to eq( ["http://mt.googleapis.com/vt/icon/name=icons/spotlight/spotlight-poi.png",
          {"data-id"=>@org1.id,:class=>"marker"}])
      end

      [365, 366, 500].each do |days|
        it "returns small icon when update is #{days} days old" do
          past_time = Time.at(Time.now - days.day)
          expect(build_org_with_computed_fields_and_updated_at(@org1, past_time).gmaps4rails_marker_attrs).to eq(["https://maps.gstatic.com/intl/en_ALL/mapfiles/markers2/measle.png",
            {"data-id"=>@org1.id, :class=>"measle"}])
        end
      end
      [ 2, 100, 200, 364].each do |days|
        it "returns large icon when update is only #{days} days old" do
          past_time = Time.at(Time.now - days.day)
          expect(build_org_with_computed_fields_and_updated_at(@org1, past_time).gmaps4rails_marker_attrs).to eq( ["http://mt.googleapis.com/vt/icon/name=icons/spotlight/spotlight-poi.png",
            {"data-id"=>@org1.id, :class=>"marker"} ])
        end
      end
    end
  end
  context 'scopes for orphan orgs' do
    before(:each) do
      @user = FactoryGirl.create(:user, :email => "<EMAIL>")
      @user.confirm!
    end

    it 'should allow us to grab orgs with emails' do
      expect(Organisation.not_null_email).to eq []
      @org1.email = "<EMAIL>"
      @org1.save
      expect(Organisation.not_null_email).to eq [@org1]
    end

{'fraction_non_alphanumeric': 0.13741866563076624, 'fraction_numerical': 0.0196173642808585, 'mean_word_length': 3.6096687555953446, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 87, 'https://': 4, 'lorem ipsum': 0, 'www.': 6, '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': '6938142', 'n_tokens_mistral': 3614, 'n_tokens_neox': 3362, 'n_words': 717}