Skip to content

Commit 8052b50

Browse files
committed
[RAILS] Convert tests to rspec
1 parent 5ff5ae6 commit 8052b50

File tree

8 files changed

+157
-170
lines changed

8 files changed

+157
-170
lines changed

elasticsearch-rails/Gemfile

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ source 'https://rubygems.org'
33
# Specify your gem's dependencies in elasticsearch-rails.gemspec
44
gemspec
55

6-
# TODO: Figure out how to specify dependency on local elasticsearch-model without endless "Resolving dependencies"
7-
# if File.exists? File.expand_path("../../elasticsearch-model", __FILE__)
8-
# gem 'elasticsearch-model', :path => File.expand_path("../../elasticsearch-model", __FILE__), :require => true
9-
# end
10-
11-
12-
136
gem 'elasticsearch-model', :path => File.expand_path("../../elasticsearch-model", __FILE__), :require => false
147
gem 'elasticsearch-persistence', :path => File.expand_path("../../elasticsearch-persistence", __FILE__), :require => false
8+
9+
10+
group :development, :testing do
11+
gem 'rspec'
12+
gem 'pry-nav'
13+
gem 'sqlite3' unless defined?(JRUBY_VERSION)
14+
end

elasticsearch-rails/Rakefile

+6-14
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,16 @@ task :test => 'test:unit'
77
# ----- Test tasks ------------------------------------------------------------
88

99
require 'rake/testtask'
10+
require 'rspec/core/rake_task'
11+
1012
namespace :test do
11-
Rake::TestTask.new(:unit) do |test|
12-
test.libs << 'lib' << 'test'
13-
test.test_files = FileList["test/unit/**/*_test.rb"]
14-
test.verbose = false
15-
test.warning = false
16-
end
1713

18-
Rake::TestTask.new(:integration) do |test|
19-
test.libs << 'lib' << 'test'
20-
test.test_files = FileList["test/integration/**/*_test.rb"]
21-
test.verbose = false
22-
test.warning = false
23-
end
14+
RSpec::Core::RakeTask.new(:spec)
2415

2516
Rake::TestTask.new(:all) do |test|
26-
test.libs << 'lib' << 'test'
27-
test.test_files = FileList["test/unit/**/*_test.rb", "test/integration/**/*_test.rb"]
17+
test.verbose = false
18+
test.warning = false
19+
test.deps = [ :spec ]
2820
end
2921
end
3022

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
require 'spec_helper'
2+
3+
describe 'ActiveSupport::Instrumentation integration' do
4+
5+
before(:all) do
6+
class DummyInstrumentationModel
7+
extend Elasticsearch::Model::Searching::ClassMethods
8+
9+
def self.index_name; 'foo'; end
10+
def self.document_type; 'bar'; end
11+
end
12+
end
13+
14+
after(:all) do
15+
remove_classes(DummyInstrumentationModel)
16+
end
17+
18+
let(:response_document) do
19+
{ 'took' => '5ms',
20+
'hits' => { 'total' => 123,
21+
'max_score' => 456,
22+
'hits' => [] } }
23+
end
24+
25+
let(:search) do
26+
Elasticsearch::Model::Searching::SearchRequest.new(DummyInstrumentationModel, 'foo')
27+
end
28+
29+
let(:client) do
30+
double('client', search: response_document)
31+
end
32+
33+
before do
34+
allow(DummyInstrumentationModel).to receive(:client).and_return(client)
35+
Elasticsearch::Rails::Instrumentation::Railtie.run_initializers
36+
end
37+
38+
context 'SearchRequest#execute!' do
39+
40+
it 'wraps the method with instrumentation' do
41+
expect(search).to respond_to(:execute_without_instrumentation!)
42+
expect(search).to respond_to(:execute_with_instrumentation!)
43+
end
44+
end
45+
46+
context 'Model#search' do
47+
48+
before do
49+
expect(ActiveSupport::Notifications).to receive(:instrument).with('search.elasticsearch',
50+
{ klass: 'DummyInstrumentationModel',
51+
name: 'Search',
52+
search: { body: query,
53+
index: 'foo',
54+
type: 'bar' } }).and_return({})
55+
end
56+
57+
let(:query) do
58+
{ query: { match: { foo: 'bar' } } }
59+
end
60+
61+
let(:logged_message) do
62+
@logger.logged(:debug).first
63+
end
64+
65+
it 'publishes a notification' do
66+
expect(DummyInstrumentationModel.search(query).response).to eq({})
67+
end
68+
69+
context 'when a message is logged', unless: defined?(RUBY_VERSION) && RUBY_VERSION > '2.2' do
70+
71+
let(:query) do
72+
{ query: { match: { moo: 'bam' } } }
73+
end
74+
75+
it 'prints the debug information to the log' do
76+
expect(logged_message).to match(/DummyInstrumentationModel Search \(\d+\.\d+ms\)/)
77+
expect(logged_message).to match(/body\: \{query\: \{match\: \{moo\: "bam"\}\}\}\}/)
78+
end
79+
end
80+
end
81+
end
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require 'spec_helper'
2+
require 'action_pack'
3+
require 'lograge'
4+
require 'elasticsearch/rails/lograge'
5+
6+
describe 'ActiveSupport::Instrumentation integration' do
7+
8+
before do
9+
Elasticsearch::Rails::Lograge::Railtie.run_initializers
10+
end
11+
12+
it 'customizes the Lograge configuration' do
13+
expect(Elasticsearch::Rails::Lograge::Railtie.initializers
14+
.select { |i| i.name == 'elasticsearch.lograge' }
15+
.first).not_to be_nil
16+
end
17+
end
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
require 'pry-nav'
2+
require 'active_record'
3+
require 'active_model'
4+
require 'elasticsearch/model'
5+
require 'elasticsearch/rails'
6+
require 'rails/railtie'
7+
require 'elasticsearch/rails/instrumentation'
8+
9+
10+
unless defined?(ELASTICSEARCH_URL)
11+
ELASTICSEARCH_URL = ENV['ELASTICSEARCH_URL'] || "localhost:#{(ENV['TEST_CLUSTER_PORT'] || 9200)}"
12+
end
13+
14+
RSpec.configure do |config|
15+
config.formatter = 'documentation'
16+
config.color = true
17+
18+
config.before(:suite) do
19+
require 'ansi'
20+
tracer = ::Logger.new(STDERR)
21+
tracer.formatter = lambda { |s, d, p, m| "#{m.gsub(/^.*$/) { |n| ' ' + n }.ansi(:faint)}\n" }
22+
Elasticsearch::Model.client = Elasticsearch::Client.new host: ELASTICSEARCH_URL,
23+
tracer: (ENV['QUIET'] ? nil : tracer)
24+
25+
unless ActiveRecord::Base.connected?
26+
ActiveRecord::Base.establish_connection( :adapter => 'sqlite3', :database => ":memory:" )
27+
end
28+
29+
if ::ActiveRecord::Base.respond_to?(:raise_in_transactional_callbacks) && ::ActiveRecord::VERSION::MAJOR.to_s < '5'
30+
::ActiveRecord::Base.raise_in_transactional_callbacks = true
31+
end
32+
end
33+
end
34+
35+
# Remove all classes.
36+
#
37+
# @param [ Array<Class> ] classes The list of classes to remove.
38+
#
39+
# @return [ true ]
40+
#
41+
# @since 6.0.1
42+
def remove_classes(*classes)
43+
classes.each do |_class|
44+
Object.send(:remove_const, _class.name.to_sym) if defined?(_class)
45+
end and true
46+
end

elasticsearch-rails/test/test_helper.rb

-67
This file was deleted.

elasticsearch-rails/test/unit/instrumentation/instrumentation_test.rb

-61
This file was deleted.

elasticsearch-rails/test/unit/instrumentation/lograge_test.rb

-21
This file was deleted.

0 commit comments

Comments
 (0)