forked from elastic/elasticsearch-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspec_helper.rb
161 lines (145 loc) · 4.22 KB
/
spec_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
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
require 'pry-nav'
require 'kaminari'
require 'kaminari/version'
require 'will_paginate'
require 'will_paginate/collection'
require 'elasticsearch/model'
require 'hashie/version'
require 'active_model'
require 'mongoid'
require 'yaml'
require 'active_record'
unless defined?(ELASTICSEARCH_URL)
ELASTICSEARCH_URL = ENV['ELASTICSEARCH_URL'] || "localhost:#{(ENV['TEST_CLUSTER_PORT'] || 9200)}"
end
RSpec.configure do |config|
config.formatter = 'documentation'
config.color = true
config.before(:suite) do
require 'ansi'
tracer = ::Logger.new(STDERR)
tracer.formatter = lambda { |s, d, p, m| "#{m.gsub(/^.*$/) { |n| ' ' + n }.ansi(:faint)}\n" }
Elasticsearch::Model.client = Elasticsearch::Client.new host: ELASTICSEARCH_URL,
tracer: (ENV['QUIET'] ? nil : tracer)
unless ActiveRecord::Base.connected?
ActiveRecord::Base.establish_connection( :adapter => 'sqlite3', :database => ":memory:" )
end
require 'support/app'
if ::ActiveRecord::Base.respond_to?(:raise_in_transactional_callbacks) && ::ActiveRecord::VERSION::MAJOR.to_s < '5'
::ActiveRecord::Base.raise_in_transactional_callbacks = true
end
end
config.after(:all) do
drop_all_tables!
delete_all_indices!
end
end
# Is the ActiveRecord version at least 4.0?
#
# @return [ true, false ] Whether the ActiveRecord version is at least 4.0.
#
# @since 6.0.1
def active_record_at_least_4?
defined?(::ActiveRecord) && ::ActiveRecord::VERSION::MAJOR >= 4
end
# Delete all documents from the indices of the provided list of models.
#
# @param [ Array<ActiveRecord::Base> ] models The list of models.
#
# @return [ true ]
#
# @since 6.0.1
def clear_indices(*models)
models.each do |model|
begin; Elasticsearch::Model.client.delete_by_query(index: model.index_name, q: '*'); rescue; end
end and true
end
# Delete all documents from the tables of the provided list of models.
#
# @param [ Array<ActiveRecord::Base> ] models The list of models.
#
# @return [ true ]
#
# @since 6.0.1
def clear_tables(*models)
begin; models.map(&:delete_all); rescue; end and true
end
# Drop all tables of models registered as subclasses of ActiveRecord::Base.
#
# @return [ true ]
#
# @since 6.0.1
def drop_all_tables!
ActiveRecord::Base.descendants.each do |model|
begin
ActiveRecord::Schema.define do
drop_table model
end if model.table_exists?
rescue
end
end and true
end
# Drop all indices of models registered as subclasses of ActiveRecord::Base.
#
# @return [ true ]
#
# @since 6.0.1
def delete_all_indices!
client = Elasticsearch::Model.client
ActiveRecord::Base.descendants.each do |model|
begin
client.indices.delete(index: model.index_name) if model.__elasticsearch__.index_exists?
rescue
end
end and true
end
# Remove all classes.
#
# @param [ Array<Class> ] classes The list of classes to remove.
#
# @return [ true ]
#
# @since 6.0.1
def remove_classes(*classes)
classes.each do |_class|
Object.send(:remove_const, _class.name.to_sym) if defined?(_class)
end and true
end
# Determine whether the tests with Mongoid should be run.
# Depends on whether MongoDB is running on the default host and port, `localhost:27017`.
#
# @return [ true, false ]
#
# @since 6.0.1
def test_mongoid?
$mongoid_available ||= begin
require 'mongoid'
if defined?(Mongo) # older versions of Mongoid use the driver, Moped
client = Mongo::Client.new(['localhost:27017'])
Timeout.timeout(1) do
client.database.command(ping: 1) && true
end
end and true
rescue Timeout::Error, LoadError, Mongo::Error => e
client.close
$stderr.puts("MongoDB not installed or running: #{e}")
end
end
# Connect Mongoid and set up its Logger if Mongoid tests should be run.
#
# @since 6.0.1
def connect_mongoid(source)
if test_mongoid?
$stderr.puts "Mongoid #{Mongoid::VERSION}", '-'*80
if !ENV['QUIET'] == 'true'
logger = ::Logger.new($stderr)
logger.formatter = lambda { |s, d, p, m| " #{m.ansi(:faint, :cyan)}\n" }
logger.level = ::Logger::DEBUG
Mongoid.logger = logger
Mongo::Logger.logger = logger
else
Mongo::Logger.logger.level = ::Logger::WARN
end
Mongoid.connect_to(source)
end
end