cirandas.net

ref: master

plugins/solr/test/unit/profile_test.rb


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
require_relative '../test_helper'

class ProfileTest < ActiveSupport::TestCase
  def setup
    @environment = Environment.default
    @environment.enable_plugin(SolrPlugin)
  end

  attr_accessor :environment

  should 'reindex articles after saving' do
    profile = create(Person, :name => 'something', :user_id => fast_create(User).id)
    art = profile.articles.build(:name => 'something')
    Profile.expects(:solr_batch_add).with(includes(art))
    profile.save!
  end

  should 'act as faceted' do
    st = fast_create(State, :acronym => 'XZ')
    city = fast_create(City, :name => 'Tabajara', :parent_id => st.id)
    cat = fast_create(Category)
    prof = fast_create(Person, :region_id => city.id)
    prof.add_category(cat, true)

    facet = Profile.facet_by_id(:solr_f_region)
    assert_equal [[prof.region.id.to_s, 'Tabajara, XZ', 1]], facet[:proc].call(facet, [[prof.send(:solr_f_region), 1]])

    assert_equal "solr_category_filter:#{cat.id}", Person.solr_facet_category_query.call(cat)
  end

  should 'act as searchable' do
    TestSolr.enable
    st = create(State, :name => 'California', :acronym => 'CA', :environment_id => Environment.default.id)
    city = create(City, :name => 'Inglewood', :parent_id => st.id, :environment_id => Environment.default.id)
    p = create(Person, :name => "Hiro", :address => 'U-Stor-It', :nickname => 'Protagonist',
               :user_id => fast_create(User).id, :region_id => city.id)
    cat = create(Category, :name => "Science Fiction", :acronym => "sf", :abbreviation => "sci-fi")
    p.add_category cat

    # fields
    assert_includes Profile.find_by_contents('Hiro')[:results].docs, p
    assert_includes Profile.find_by_contents('Protagonist')[:results].docs, p
    # filters
    assert_includes Profile.find_by_contents('Hiro', {}, { :filter_queries => ["solr_is_public:true"]})[:results].docs, p
    assert_not_includes Profile.find_by_contents('Hiro', {}, { :filter_queries => ["solr_is_public:false"]})[:results].docs, p
    assert_includes Profile.find_by_contents('Hiro', {}, { :filter_queries => ["environment_id:\"#{Environment.default.id}\""]})[:results].docs, p
    # includes
    assert_includes Profile.find_by_contents("Inglewood")[:results].docs, p
    assert_includes Profile.find_by_contents("California")[:results].docs, p
    assert_includes Profile.find_by_contents("Science")[:results].docs, p
    # not includes
    assert_not_includes Profile.find_by_contents('Stor')[:results].docs, p
  end

  should 'boost name matches' do
    TestSolr.enable
    in_addr = create(Person, :name => 'something', :address => 'bananas in the address!', :user_id => fast_create(User).id)
    in_name = create(Person, :name => 'bananas in the name!', :user_id => fast_create(User).id)
    assert_equal [in_name], Person.find_by_contents('bananas')[:results].docs
  end

  should 'be able to add extra data for index' do
    klass = Class.new(Profile)
    klass.any_instance.expects(:random_method)
    klass.solr_extra_data_for_index :random_method

    klass.new.solr_extra_data_for_index
  end

  should 'be able to add a block as extra data for index' do
    klass = Class.new(Profile)
    result = Object.new
    klass.solr_extra_data_for_index do |obj|
      result
    end

    assert_includes klass.new.solr_extra_data_for_index, result
  end

  should 'actually index by results of solr_extra_data_for_index' do
    TestSolr.enable
    class ExtraDataForIndex < Profile
      solr_extra_data_for_index do |obj|
        'sample indexed text'
      end
    end
    profile = ExtraDataForIndex.create!(:name => 'testprofile', :identifier => 'testprofile')

    assert_includes ExtraDataForIndex.find_by_contents('sample')[:results], profile
  end

  should 'find_by_contents' do
    TestSolr.enable
    p = create(Profile, :name => 'wanted')

    assert Profile.find_by_contents('wanted')[:results].include?(p)
    refute  Profile.find_by_contents('not_wanted')[:results].include?(p)
  end

  # This problem should be solved; talk to BrĂ¡ulio if it fails
  should 'be able to find profiles by their names' do
    TestSolr.enable
    small = create(Profile, :name => 'A small profile for testing')
    big = create(Profile, :name => 'A big profile for testing')

    assert Profile.find_by_contents('small')[:results].include?(small)
    assert Profile.find_by_contents('big')[:results].include?(big)

    both = Profile.find_by_contents('profile testing')[:results]
    assert both.include?(small)
    assert both.include?(big)
  end

  should 'search with latitude and longitude' do
    TestSolr.enable
    e = fast_create(Enterprise, {:lat => 45, :lng => 45}, :search => true)

    assert_includes Enterprise.find_by_contents('', {}, {:radius => 2, :latitude => 45, :longitude => 45})[:results].docs, e
  end

  should 'index profile identifier for searching' do
    TestSolr.enable
    Profile.destroy_all
    p = create(Profile, :identifier => 'lalala')
    assert_includes Profile.find_by_contents('lalala')[:results], p
  end

  should 'index profile name for searching' do
    TestSolr.enable
    p = create(Profile, :name => 'Interesting Profile')
    assert_includes Profile.find_by_contents('interesting')[:results], p
  end

  should 'index comments title together with article' do
    TestSolr.enable
    owner = create_user('testuser').person
    art = fast_create(TextArticle, :profile_id => owner.id, :name => 'ytest')
    c1 = Comment.create(:title => 'a nice comment', :body => 'anything', :author => owner, :source => art ); c1.save!

    assert_includes Article.find_by_contents('nice')[:results], art
  end

  should 'index by schema name when database is postgresql' do
    TestSolr.enable
    uses_postgresql 'schema_one'
    p1 = Profile.create!(:name => 'some thing', :identifier => 'some-thing')
    assert_equal [p1], Profile.find_by_contents('thing')[:results].docs
    uses_postgresql 'schema_two'
    p2 = Profile.create!(:name => 'another thing', :identifier => 'another-thing')
    assert_not_includes Profile.find_by_contents('thing')[:results], p1
    assert_includes Profile.find_by_contents('thing')[:results], p2
    uses_postgresql 'schema_one'
    assert_includes Profile.find_by_contents('thing')[:results], p1
    assert_not_includes Profile.find_by_contents('thing')[:results], p2
    uses_sqlite
  end

  should 'not index by schema name when database is not postgresql' do
    TestSolr.enable
    uses_sqlite
    p1 = Profile.create!(:name => 'some thing', :identifier => 'some-thing')
    assert_equal [p1], Profile.find_by_contents('thing')[:results].docs
    p2 = Profile.create!(:name => 'another thing', :identifier => 'another-thing')
    assert_includes Profile.find_by_contents('thing')[:results], p1
    assert_includes Profile.find_by_contents('thing')[:results], p2
  end
end