Skip to content

[MODEL] Port remainder of Elasticsearch::Model unit tests to rspec #836

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 2 commits into from
Sep 17, 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
34 changes: 34 additions & 0 deletions elasticsearch-model/spec/elasticsearch/model/callbacks_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'spec_helper'

describe Elasticsearch::Model::Callbacks do

before(:all) do
class ::DummyCallbacksModel
end

module DummyCallbacksAdapter
module CallbacksMixin
end

def callbacks_mixin
CallbacksMixin
end; module_function :callbacks_mixin
end
end

after(:all) do
Object.send(:remove_const, :DummyCallbacksModel) if defined?(DummyCallbacksModel)
Object.send(:remove_const, :DummyCallbacksAdapter) if defined?(DummyCallbacksAdapter)
end

context 'when a model includes the Callbacks module' do

before do
Elasticsearch::Model::Callbacks.included(DummyCallbacksModel)
end

it 'includes the callbacks mixin from the model adapter' do
expect(DummyCallbacksModel.ancestors).to include(Elasticsearch::Model::Adapter::Default::Callbacks)
end
end
end
66 changes: 66 additions & 0 deletions elasticsearch-model/spec/elasticsearch/model/client_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
require 'spec_helper'

describe Elasticsearch::Model::Client do

before(:all) do
class ::DummyClientModel
extend Elasticsearch::Model::Client::ClassMethods
include Elasticsearch::Model::Client::InstanceMethods
end
end

after(:all) do
Object.send(:remove_const, :DummyClientModel) if defined?(DummyClientModel)
end

context 'when a class includes the client module class methods' do

it 'defines the client module class methods on the model' do
expect(DummyClientModel.client).to be_a(Elasticsearch::Transport::Client)
end
end

context 'when a class includes the client module instance methods' do

it 'defines the client module class methods on the model' do
expect(DummyClientModel.new.client).to be_a(Elasticsearch::Transport::Client)
end
end

context 'when the client is set on the class' do

around do |example|
original_client = DummyClientModel.client
DummyClientModel.client = 'foobar'
example.run
DummyClientModel.client = original_client
end

it 'sets the client on the class' do
expect(DummyClientModel.client).to eq('foobar')
end

it 'sets the client on an instance' do
expect(DummyClientModel.new.client).to eq('foobar')
end
end

context 'when the client is set on an instance' do

before do
model_instance.client = 'foo'
end

let(:model_instance) do
DummyClientModel.new
end

it 'sets the client on an instance' do
expect(model_instance.client).to eq('foo')
end

it 'does not set the client on the class' do
expect(DummyClientModel.client).to be_a(Elasticsearch::Transport::Client)
end
end
end
12 changes: 12 additions & 0 deletions elasticsearch-model/spec/elasticsearch/model/hash_wrapper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'spec_helper'

describe Elasticsearch::Model::HashWrapper, if: Hashie::VERSION >= '3.5.3' do

before do
expect(Hashie.logger).to receive(:warn).never
end

it 'does not print a warning for re-defined methods' do
Elasticsearch::Model::HashWrapper.new(:foo => 'bar', :sort => true)
end
end
215 changes: 215 additions & 0 deletions elasticsearch-model/spec/elasticsearch/model/importing_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
require 'spec_helper'

describe Elasticsearch::Model::Importing do

before(:all) do
class DummyImportingModel
end

module DummyImportingAdapter
module ImportingMixin
def __find_in_batches(options={}, &block)
yield if block_given?
end
def __transform
lambda {|a|}
end
end

def importing_mixin
ImportingMixin
end; module_function :importing_mixin
end
end

after(:all) do
Object.send(:remove_const, :DummyImportingModel) if defined?(DummyImportingModel)
Object.send(:remove_const, :DummyImportingAdapter) if defined?(DummyImportingAdapter)
end

before do
allow(Elasticsearch::Model::Adapter).to receive(:from_class).with(DummyImportingModel).and_return(DummyImportingAdapter)
DummyImportingModel.__send__ :include, Elasticsearch::Model::Importing
end

context 'when a model includes the Importing module' do

it 'provides importing methods' do
expect(DummyImportingModel.respond_to?(:import)).to be(true)
expect(DummyImportingModel.respond_to?(:__find_in_batches)).to be(true)
end
end

describe '#import' do

before do
allow(DummyImportingModel).to receive(:index_name).and_return('foo')
allow(DummyImportingModel).to receive(:document_type).and_return('foo')
allow(DummyImportingModel).to receive(:index_exists?).and_return(true)
allow(DummyImportingModel).to receive(:__batch_to_bulk)
allow(client).to receive(:bulk).and_return(response)
end

let(:client) do
double('client')
end

let(:response) do
{ 'items' => [] }
end

context 'when no options are provided' do

before do
expect(DummyImportingModel).to receive(:client).and_return(client)
allow(DummyImportingModel).to receive(:index_exists?).and_return(true)
end

it 'uses the client to import documents' do
expect(DummyImportingModel.import).to eq(0)
end
end

context 'when there is an error' do

before do
expect(DummyImportingModel).to receive(:client).and_return(client)
allow(DummyImportingModel).to receive(:index_exists?).and_return(true)
end

let(:response) do
{ 'items' => [{ 'index' => { } }, { 'index' => { 'error' => 'FAILED' } }] }
end

it 'returns the number of errors' do
expect(DummyImportingModel.import).to eq(1)
end

context 'when the method is called with the option to return the errors' do

it 'returns the errors' do
expect(DummyImportingModel.import(return: 'errors')).to eq([{ 'index' => { 'error' => 'FAILED' } }])
end
end

context 'when the method is called with a block' do

it 'yields the response to the block' do
DummyImportingModel.import do |response|
expect(response['items'].size).to eq(2)
end
end
end
end

context 'when the index does not exist' do

before do
allow(DummyImportingModel).to receive(:index_exists?).and_return(false)
end

it 'raises an exception' do
expect {
DummyImportingModel.import
}.to raise_exception(ArgumentError)
end
end

context 'when the method is called with the force option' do

before do
expect(DummyImportingModel).to receive(:create_index!).with(force: true, index: 'foo').and_return(true)
expect(DummyImportingModel).to receive(:__find_in_batches).with(foo: 'bar').and_return(true)
end

it 'deletes and creates the index' do
expect(DummyImportingModel.import(force: true, foo: 'bar')).to eq(0)
end
end

context 'when the method is called with the refresh option' do

before do
expect(DummyImportingModel).to receive(:refresh_index!).with(index: 'foo').and_return(true)
expect(DummyImportingModel).to receive(:__find_in_batches).with(foo: 'bar').and_return(true)
end

it 'refreshes the index' do
expect(DummyImportingModel.import(refresh: true, foo: 'bar')).to eq(0)
end
end

context 'when a different index name is provided' do

before do
expect(DummyImportingModel).to receive(:client).and_return(client)
expect(client).to receive(:bulk).with(body: nil, index: 'my-new-index', type: 'foo').and_return(response)
end

it 'uses the alternate index name' do
expect(DummyImportingModel.import(index: 'my-new-index')).to eq(0)
end
end

context 'when a different document type is provided' do

before do
expect(DummyImportingModel).to receive(:client).and_return(client)
expect(client).to receive(:bulk).with(body: nil, index: 'foo', type: 'my-new-type').and_return(response)
end

it 'uses the alternate index name' do
expect(DummyImportingModel.import(type: 'my-new-type')).to eq(0)
end
end

context 'the transform method' do

before do
expect(DummyImportingModel).to receive(:client).and_return(client)
expect(DummyImportingModel).to receive(:__transform).and_return(transform)
expect(DummyImportingModel).to receive(:__batch_to_bulk).with(anything, transform)
end

let(:transform) do
lambda {|a|}
end

it 'applies the transform method to the results' do
expect(DummyImportingModel.import).to eq(0)
end
end

context 'when a transform is provided as an option' do

context 'when the transform option is not a lambda' do

let(:transform) do
'not_callable'
end

it 'raises an error' do
expect {
DummyImportingModel.import(transform: transform)
}.to raise_exception(ArgumentError)
end
end

context 'when the transform option is a lambda' do

before do
expect(DummyImportingModel).to receive(:client).and_return(client)
expect(DummyImportingModel).to receive(:__batch_to_bulk).with(anything, transform)
end

let(:transform) do
lambda {|a|}
end

it 'applies the transform lambda to the results' do
expect(DummyImportingModel.import(transform: transform)).to eq(0)
end
end
end
end
end
Loading