Skip to content

[RAILS] Convert tests to rspec #842

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions elasticsearch-rails/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ source 'https://rubygems.org'
# Specify your gem's dependencies in elasticsearch-rails.gemspec
gemspec

# TODO: Figure out how to specify dependency on local elasticsearch-model without endless "Resolving dependencies"
# if File.exists? File.expand_path("../../elasticsearch-model", __FILE__)
# gem 'elasticsearch-model', :path => File.expand_path("../../elasticsearch-model", __FILE__), :require => true
# end



gem 'elasticsearch-model', :path => File.expand_path("../../elasticsearch-model", __FILE__), :require => false
gem 'elasticsearch-persistence', :path => File.expand_path("../../elasticsearch-persistence", __FILE__), :require => false


group :development, :testing do
gem 'rspec'
gem 'pry-nav'
gem 'sqlite3' unless defined?(JRUBY_VERSION)
end
20 changes: 6 additions & 14 deletions elasticsearch-rails/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,16 @@ task :test => 'test:unit'
# ----- Test tasks ------------------------------------------------------------

require 'rake/testtask'
require 'rspec/core/rake_task'

namespace :test do
Rake::TestTask.new(:unit) do |test|
test.libs << 'lib' << 'test'
test.test_files = FileList["test/unit/**/*_test.rb"]
test.verbose = false
test.warning = false
end

Rake::TestTask.new(:integration) do |test|
test.libs << 'lib' << 'test'
test.test_files = FileList["test/integration/**/*_test.rb"]
test.verbose = false
test.warning = false
end
RSpec::Core::RakeTask.new(:spec)

Rake::TestTask.new(:all) do |test|
test.libs << 'lib' << 'test'
test.test_files = FileList["test/unit/**/*_test.rb", "test/integration/**/*_test.rb"]
test.verbose = false
test.warning = false
test.deps = [ :spec ] unless defined?(JRUBY_VERSION)
end
end

Expand Down
81 changes: 81 additions & 0 deletions elasticsearch-rails/spec/instrumentation_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
require 'spec_helper'

describe 'ActiveSupport::Instrumentation integration' do

before(:all) do
class DummyInstrumentationModel
extend Elasticsearch::Model::Searching::ClassMethods

def self.index_name; 'foo'; end
def self.document_type; 'bar'; end
end
end

after(:all) do
remove_classes(DummyInstrumentationModel)
end

let(:response_document) do
{ 'took' => '5ms',
'hits' => { 'total' => 123,
'max_score' => 456,
'hits' => [] } }
end

let(:search) do
Elasticsearch::Model::Searching::SearchRequest.new(DummyInstrumentationModel, 'foo')
end

let(:client) do
double('client', search: response_document)
end

before do
allow(DummyInstrumentationModel).to receive(:client).and_return(client)
Elasticsearch::Rails::Instrumentation::Railtie.run_initializers
end

context 'SearchRequest#execute!' do

it 'wraps the method with instrumentation' do
expect(search).to respond_to(:execute_without_instrumentation!)
expect(search).to respond_to(:execute_with_instrumentation!)
end
end

context 'Model#search' do

before do
expect(ActiveSupport::Notifications).to receive(:instrument).with('search.elasticsearch',
{ klass: 'DummyInstrumentationModel',
name: 'Search',
search: { body: query,
index: 'foo',
type: 'bar' } }).and_return({})
end

let(:query) do
{ query: { match: { foo: 'bar' } } }
end

let(:logged_message) do
@logger.logged(:debug).first
end

it 'publishes a notification' do
expect(DummyInstrumentationModel.search(query).response).to eq({})
end

context 'when a message is logged', unless: defined?(RUBY_VERSION) && RUBY_VERSION > '2.2' do

let(:query) do
{ query: { match: { moo: 'bam' } } }
end

it 'prints the debug information to the log' do
expect(logged_message).to match(/DummyInstrumentationModel Search \(\d+\.\d+ms\)/)
expect(logged_message).to match(/body\: \{query\: \{match\: \{moo\: "bam"\}\}\}\}/)
end
end
end
end
17 changes: 17 additions & 0 deletions elasticsearch-rails/spec/lograge_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'spec_helper'
require 'action_pack'
require 'lograge'
require 'elasticsearch/rails/lograge'

describe 'ActiveSupport::Instrumentation integration' do

before do
Elasticsearch::Rails::Lograge::Railtie.run_initializers
end

it 'customizes the Lograge configuration' do
expect(Elasticsearch::Rails::Lograge::Railtie.initializers
.select { |i| i.name == 'elasticsearch.lograge' }
.first).not_to be_nil
end
end
45 changes: 45 additions & 0 deletions elasticsearch-rails/spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require 'pry-nav'
require 'active_record'
require 'elasticsearch/model'
require 'elasticsearch/rails'
require 'rails/railtie'
require 'elasticsearch/rails/instrumentation'


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

if ::ActiveRecord::Base.respond_to?(:raise_in_transactional_callbacks) && ::ActiveRecord::VERSION::MAJOR.to_s < '5'
::ActiveRecord::Base.raise_in_transactional_callbacks = true
end
end
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
67 changes: 0 additions & 67 deletions elasticsearch-rails/test/test_helper.rb

This file was deleted.

This file was deleted.

21 changes: 0 additions & 21 deletions elasticsearch-rails/test/unit/instrumentation/lograge_test.rb

This file was deleted.