cirandas.net

ref: master

plugins/solr/test/test_helper.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
# Solr start/stop
if not $test_helper_loaded
  ENV["RAILS_ENV"] = "test"
	abort unless system 'rake -s solr:start'
  at_exit { system 'rake -s solr:stop' }
  $test_helper_loaded = true
end

require 'test_helper'

class ActsAsSolr::Post
  class << self
    alias_method :execute_orig, :execute
  end
end
module ActsAsSolr::ParserMethods
  alias_method :parse_results_orig, :parse_results
end

class TestSolr

  def self.enable
    ActsAsSolr::Post.class_eval do
      def self.execute(*args)
        execute_orig *args
      end
    end
    ActsAsSolr::ParserMethods.module_eval do
      def parse_results(*args)
        parse_results_orig *args
      end
    end

    # clear index
    ActsAsSolr::Post.execute(Solr::Request::Delete.new(:query => '*:*'))

    @solr_disabled = false
  end

  def self.disable
    return if @solr_disabled

    ActsAsSolr::Post.class_eval do
      def self.execute(*args)
        true
      end
    end
    ActsAsSolr::ParserMethods.module_eval do
      def parse_results(*args)
        parse_results_orig nil, args[1]
      end
    end

    @solr_disabled = true
  end

end

class ActiveSupport::TestCase
  def fast_create_with_solr(name, attrs = {}, options = {})
    obj = fast_create_without_solr(name, attrs, options)
    obj.solr_save if options[:search]
    obj
  end
  alias_method_chain :fast_create, :solr
end

# disable solr actions by default
TestSolr.disable