Skip to content

Commit d930a93

Browse files
committed
[MODEL] Port remainder of Elasticsearch::Model unit tests to rspec (elastic#836)
* [MODEL] Port remainder of Elasticsearch::Model unit tests to rspec * [MODEL] Minor spacing fixes
1 parent d92efd8 commit d930a93

30 files changed

+1914
-1553
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
require 'spec_helper'
2+
3+
describe Elasticsearch::Model::Callbacks do
4+
5+
before(:all) do
6+
class ::DummyCallbacksModel
7+
end
8+
9+
module DummyCallbacksAdapter
10+
module CallbacksMixin
11+
end
12+
13+
def callbacks_mixin
14+
CallbacksMixin
15+
end; module_function :callbacks_mixin
16+
end
17+
end
18+
19+
after(:all) do
20+
Object.send(:remove_const, :DummyCallbacksModel) if defined?(DummyCallbacksModel)
21+
Object.send(:remove_const, :DummyCallbacksAdapter) if defined?(DummyCallbacksAdapter)
22+
end
23+
24+
context 'when a model includes the Callbacks module' do
25+
26+
before do
27+
Elasticsearch::Model::Callbacks.included(DummyCallbacksModel)
28+
end
29+
30+
it 'includes the callbacks mixin from the model adapter' do
31+
expect(DummyCallbacksModel.ancestors).to include(Elasticsearch::Model::Adapter::Default::Callbacks)
32+
end
33+
end
34+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
require 'spec_helper'
2+
3+
describe Elasticsearch::Model::Client do
4+
5+
before(:all) do
6+
class ::DummyClientModel
7+
extend Elasticsearch::Model::Client::ClassMethods
8+
include Elasticsearch::Model::Client::InstanceMethods
9+
end
10+
end
11+
12+
after(:all) do
13+
Object.send(:remove_const, :DummyClientModel) if defined?(DummyClientModel)
14+
end
15+
16+
context 'when a class includes the client module class methods' do
17+
18+
it 'defines the client module class methods on the model' do
19+
expect(DummyClientModel.client).to be_a(Elasticsearch::Transport::Client)
20+
end
21+
end
22+
23+
context 'when a class includes the client module instance methods' do
24+
25+
it 'defines the client module class methods on the model' do
26+
expect(DummyClientModel.new.client).to be_a(Elasticsearch::Transport::Client)
27+
end
28+
end
29+
30+
context 'when the client is set on the class' do
31+
32+
around do |example|
33+
original_client = DummyClientModel.client
34+
DummyClientModel.client = 'foobar'
35+
example.run
36+
DummyClientModel.client = original_client
37+
end
38+
39+
it 'sets the client on the class' do
40+
expect(DummyClientModel.client).to eq('foobar')
41+
end
42+
43+
it 'sets the client on an instance' do
44+
expect(DummyClientModel.new.client).to eq('foobar')
45+
end
46+
end
47+
48+
context 'when the client is set on an instance' do
49+
50+
before do
51+
model_instance.client = 'foo'
52+
end
53+
54+
let(:model_instance) do
55+
DummyClientModel.new
56+
end
57+
58+
it 'sets the client on an instance' do
59+
expect(model_instance.client).to eq('foo')
60+
end
61+
62+
it 'does not set the client on the class' do
63+
expect(DummyClientModel.client).to be_a(Elasticsearch::Transport::Client)
64+
end
65+
end
66+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
require 'spec_helper'
2+
3+
describe Elasticsearch::Model::HashWrapper, if: Hashie::VERSION >= '3.5.3' do
4+
5+
before do
6+
expect(Hashie.logger).to receive(:warn).never
7+
end
8+
9+
it 'does not print a warning for re-defined methods' do
10+
Elasticsearch::Model::HashWrapper.new(:foo => 'bar', :sort => true)
11+
end
12+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
require 'spec_helper'
2+
3+
describe Elasticsearch::Model::Importing do
4+
5+
before(:all) do
6+
class DummyImportingModel
7+
end
8+
9+
module DummyImportingAdapter
10+
module ImportingMixin
11+
def __find_in_batches(options={}, &block)
12+
yield if block_given?
13+
end
14+
def __transform
15+
lambda {|a|}
16+
end
17+
end
18+
19+
def importing_mixin
20+
ImportingMixin
21+
end; module_function :importing_mixin
22+
end
23+
end
24+
25+
after(:all) do
26+
Object.send(:remove_const, :DummyImportingModel) if defined?(DummyImportingModel)
27+
Object.send(:remove_const, :DummyImportingAdapter) if defined?(DummyImportingAdapter)
28+
end
29+
30+
before do
31+
allow(Elasticsearch::Model::Adapter).to receive(:from_class).with(DummyImportingModel).and_return(DummyImportingAdapter)
32+
DummyImportingModel.__send__ :include, Elasticsearch::Model::Importing
33+
end
34+
35+
context 'when a model includes the Importing module' do
36+
37+
it 'provides importing methods' do
38+
expect(DummyImportingModel.respond_to?(:import)).to be(true)
39+
expect(DummyImportingModel.respond_to?(:__find_in_batches)).to be(true)
40+
end
41+
end
42+
43+
describe '#import' do
44+
45+
before do
46+
allow(DummyImportingModel).to receive(:index_name).and_return('foo')
47+
allow(DummyImportingModel).to receive(:document_type).and_return('foo')
48+
allow(DummyImportingModel).to receive(:index_exists?).and_return(true)
49+
allow(DummyImportingModel).to receive(:__batch_to_bulk)
50+
allow(client).to receive(:bulk).and_return(response)
51+
end
52+
53+
let(:client) do
54+
double('client')
55+
end
56+
57+
let(:response) do
58+
{ 'items' => [] }
59+
end
60+
61+
context 'when no options are provided' do
62+
63+
before do
64+
expect(DummyImportingModel).to receive(:client).and_return(client)
65+
allow(DummyImportingModel).to receive(:index_exists?).and_return(true)
66+
end
67+
68+
it 'uses the client to import documents' do
69+
expect(DummyImportingModel.import).to eq(0)
70+
end
71+
end
72+
73+
context 'when there is an error' do
74+
75+
before do
76+
expect(DummyImportingModel).to receive(:client).and_return(client)
77+
allow(DummyImportingModel).to receive(:index_exists?).and_return(true)
78+
end
79+
80+
let(:response) do
81+
{ 'items' => [{ 'index' => { } }, { 'index' => { 'error' => 'FAILED' } }] }
82+
end
83+
84+
it 'returns the number of errors' do
85+
expect(DummyImportingModel.import).to eq(1)
86+
end
87+
88+
context 'when the method is called with the option to return the errors' do
89+
90+
it 'returns the errors' do
91+
expect(DummyImportingModel.import(return: 'errors')).to eq([{ 'index' => { 'error' => 'FAILED' } }])
92+
end
93+
end
94+
95+
context 'when the method is called with a block' do
96+
97+
it 'yields the response to the block' do
98+
DummyImportingModel.import do |response|
99+
expect(response['items'].size).to eq(2)
100+
end
101+
end
102+
end
103+
end
104+
105+
context 'when the index does not exist' do
106+
107+
before do
108+
allow(DummyImportingModel).to receive(:index_exists?).and_return(false)
109+
end
110+
111+
it 'raises an exception' do
112+
expect {
113+
DummyImportingModel.import
114+
}.to raise_exception(ArgumentError)
115+
end
116+
end
117+
118+
context 'when the method is called with the force option' do
119+
120+
before do
121+
expect(DummyImportingModel).to receive(:create_index!).with(force: true, index: 'foo').and_return(true)
122+
expect(DummyImportingModel).to receive(:__find_in_batches).with(foo: 'bar').and_return(true)
123+
end
124+
125+
it 'deletes and creates the index' do
126+
expect(DummyImportingModel.import(force: true, foo: 'bar')).to eq(0)
127+
end
128+
end
129+
130+
context 'when the method is called with the refresh option' do
131+
132+
before do
133+
expect(DummyImportingModel).to receive(:refresh_index!).with(index: 'foo').and_return(true)
134+
expect(DummyImportingModel).to receive(:__find_in_batches).with(foo: 'bar').and_return(true)
135+
end
136+
137+
it 'refreshes the index' do
138+
expect(DummyImportingModel.import(refresh: true, foo: 'bar')).to eq(0)
139+
end
140+
end
141+
142+
context 'when a different index name is provided' do
143+
144+
before do
145+
expect(DummyImportingModel).to receive(:client).and_return(client)
146+
expect(client).to receive(:bulk).with(body: nil, index: 'my-new-index', type: 'foo').and_return(response)
147+
end
148+
149+
it 'uses the alternate index name' do
150+
expect(DummyImportingModel.import(index: 'my-new-index')).to eq(0)
151+
end
152+
end
153+
154+
context 'when a different document type is provided' do
155+
156+
before do
157+
expect(DummyImportingModel).to receive(:client).and_return(client)
158+
expect(client).to receive(:bulk).with(body: nil, index: 'foo', type: 'my-new-type').and_return(response)
159+
end
160+
161+
it 'uses the alternate index name' do
162+
expect(DummyImportingModel.import(type: 'my-new-type')).to eq(0)
163+
end
164+
end
165+
166+
context 'the transform method' do
167+
168+
before do
169+
expect(DummyImportingModel).to receive(:client).and_return(client)
170+
expect(DummyImportingModel).to receive(:__transform).and_return(transform)
171+
expect(DummyImportingModel).to receive(:__batch_to_bulk).with(anything, transform)
172+
end
173+
174+
let(:transform) do
175+
lambda {|a|}
176+
end
177+
178+
it 'applies the transform method to the results' do
179+
expect(DummyImportingModel.import).to eq(0)
180+
end
181+
end
182+
183+
context 'when a transform is provided as an option' do
184+
185+
context 'when the transform option is not a lambda' do
186+
187+
let(:transform) do
188+
'not_callable'
189+
end
190+
191+
it 'raises an error' do
192+
expect {
193+
DummyImportingModel.import(transform: transform)
194+
}.to raise_exception(ArgumentError)
195+
end
196+
end
197+
198+
context 'when the transform option is a lambda' do
199+
200+
before do
201+
expect(DummyImportingModel).to receive(:client).and_return(client)
202+
expect(DummyImportingModel).to receive(:__batch_to_bulk).with(anything, transform)
203+
end
204+
205+
let(:transform) do
206+
lambda {|a|}
207+
end
208+
209+
it 'applies the transform lambda to the results' do
210+
expect(DummyImportingModel.import(transform: transform)).to eq(0)
211+
end
212+
end
213+
end
214+
end
215+
end

0 commit comments

Comments
 (0)