cirandas.net

ref: master

plugins/solr/test/unit/acts_as_searchable_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
require_relative '../test_helper'
require_relative '/../../lib/acts_as_searchable'

class ActsAsSearchableTest < ActiveSupport::TestCase

  def setup
		@test_model = Class.new ActiveRecord::Base
	end

  def silent
		# http://mentalized.net/journal/2010/04/02/suppress_warnings_from_ruby/
		original_verbosity = $VERBOSE
    $VERBOSE = nil
    result = yield
    $VERBOSE = original_verbosity
    return result
  end

	should 'be enabled by default' do
		assert ActsAsSearchable::ClassMethods::ACTS_AS_SEARCHABLE_ENABLED, true
  end

  should 'not be searchable when disabled' do
		# suppress warning about already initialized constant
		silent { ActsAsSearchable::ClassMethods::ACTS_AS_SEARCHABLE_ENABLED = false }

    @test_model.expects(:acts_as_solr).never
		@test_model.acts_as_searchable
  end

  should 'correctly pass options to search engine' do
    options = {:fields => [{:name => :text}]}
		@test_model.expects(:acts_as_solr).with(options)
		@test_model.acts_as_searchable options
  end

	should 'always include schema name as a field' do
		@test_model.expects(:acts_as_solr).with(has_entry(:fields, [{:field1 => :text}, {:schema_name => :string}]))
		@test_model.acts_as_searchable :fields => [{:field1 => :text}]
		# ...even with no fields
		@test_model = Class.new ActiveRecord::Base
		@test_model.expects(:acts_as_solr).with(has_entry(:additional_fields, [{:schema_name => :string}]))
		@test_model.acts_as_searchable
  end

end