elasticsearch-model 0.1.9 → 7.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +3 -2
- data/Gemfile +22 -0
- data/LICENSE.txt +199 -10
- data/README.md +96 -43
- data/Rakefile +49 -35
- data/elasticsearch-model.gemspec +53 -41
- data/examples/activerecord_article.rb +18 -1
- data/examples/activerecord_associations.rb +86 -17
- data/examples/activerecord_custom_analyzer.rb +152 -0
- data/examples/activerecord_mapping_completion.rb +33 -16
- data/examples/activerecord_mapping_edge_ngram.rb +118 -0
- data/examples/couchbase_article.rb +17 -0
- data/examples/datamapper_article.rb +28 -1
- data/examples/mongoid_article.rb +17 -0
- data/examples/ohm_article.rb +17 -0
- data/examples/riak_article.rb +17 -0
- data/gemfiles/3.0.gemfile +23 -1
- data/gemfiles/4.0.gemfile +25 -1
- data/gemfiles/5.0.gemfile +35 -0
- data/gemfiles/6.0.gemfile +36 -0
- data/lib/elasticsearch/model/adapter.rb +17 -0
- data/lib/elasticsearch/model/adapters/active_record.rb +31 -27
- data/lib/elasticsearch/model/adapters/default.rb +17 -0
- data/lib/elasticsearch/model/adapters/mongoid.rb +27 -3
- data/lib/elasticsearch/model/adapters/multiple.rb +29 -4
- data/lib/elasticsearch/model/callbacks.rb +17 -0
- data/lib/elasticsearch/model/client.rb +17 -0
- data/lib/elasticsearch/model/ext/active_record.rb +17 -0
- data/lib/elasticsearch/model/hash_wrapper.rb +32 -0
- data/lib/elasticsearch/model/importing.rb +61 -17
- data/lib/elasticsearch/model/indexing.rb +87 -49
- data/lib/elasticsearch/model/multimodel.rb +17 -0
- data/lib/elasticsearch/model/naming.rb +33 -2
- data/lib/elasticsearch/model/proxy.rb +61 -18
- data/lib/elasticsearch/model/response/aggregations.rb +55 -0
- data/lib/elasticsearch/model/response/base.rb +25 -3
- data/lib/elasticsearch/model/response/pagination/kaminari.rb +126 -0
- data/lib/elasticsearch/model/response/pagination/will_paginate.rb +112 -0
- data/lib/elasticsearch/model/response/pagination.rb +19 -192
- data/lib/elasticsearch/model/response/records.rb +17 -1
- data/lib/elasticsearch/model/response/result.rb +19 -2
- data/lib/elasticsearch/model/response/results.rb +17 -0
- data/lib/elasticsearch/model/response/suggestions.rb +20 -1
- data/lib/elasticsearch/model/response.rb +28 -10
- data/lib/elasticsearch/model/searching.rb +17 -0
- data/lib/elasticsearch/model/serializing.rb +17 -0
- data/lib/elasticsearch/model/version.rb +18 -1
- data/lib/elasticsearch/model.rb +36 -39
- data/spec/elasticsearch/model/adapter_spec.rb +136 -0
- data/spec/elasticsearch/model/adapters/active_record/associations_spec.rb +351 -0
- data/spec/elasticsearch/model/adapters/active_record/basic_spec.rb +395 -0
- data/spec/elasticsearch/model/adapters/active_record/dynamic_index_name_spec.rb +35 -0
- data/spec/elasticsearch/model/adapters/active_record/import_spec.rb +193 -0
- data/spec/elasticsearch/model/adapters/active_record/multi_model_spec.rb +127 -0
- data/spec/elasticsearch/model/adapters/active_record/namespaced_model_spec.rb +55 -0
- data/spec/elasticsearch/model/adapters/active_record/pagination_spec.rb +332 -0
- data/spec/elasticsearch/model/adapters/active_record/parent_child_spec.rb +92 -0
- data/spec/elasticsearch/model/adapters/active_record/serialization_spec.rb +78 -0
- data/spec/elasticsearch/model/adapters/active_record_spec.rb +224 -0
- data/spec/elasticsearch/model/adapters/default_spec.rb +58 -0
- data/spec/elasticsearch/model/adapters/mongoid/basic_spec.rb +284 -0
- data/spec/elasticsearch/model/adapters/mongoid/multi_model_spec.rb +83 -0
- data/spec/elasticsearch/model/adapters/mongoid_spec.rb +252 -0
- data/spec/elasticsearch/model/adapters/multiple_spec.rb +142 -0
- data/spec/elasticsearch/model/callbacks_spec.rb +50 -0
- data/spec/elasticsearch/model/client_spec.rb +83 -0
- data/spec/elasticsearch/model/hash_wrapper_spec.rb +29 -0
- data/spec/elasticsearch/model/importing_spec.rb +243 -0
- data/spec/elasticsearch/model/indexing_spec.rb +1014 -0
- data/spec/elasticsearch/model/module_spec.rb +94 -0
- data/spec/elasticsearch/model/multimodel_spec.rb +72 -0
- data/spec/elasticsearch/model/naming_spec.rb +203 -0
- data/spec/elasticsearch/model/proxy_spec.rb +124 -0
- data/spec/elasticsearch/model/response/aggregations_spec.rb +83 -0
- data/spec/elasticsearch/model/response/base_spec.rb +107 -0
- data/spec/elasticsearch/model/response/pagination/kaminari_spec.rb +472 -0
- data/spec/elasticsearch/model/response/pagination/will_paginate_spec.rb +279 -0
- data/spec/elasticsearch/model/response/records_spec.rb +135 -0
- data/spec/elasticsearch/model/response/response_spec.rb +148 -0
- data/spec/elasticsearch/model/response/result_spec.rb +139 -0
- data/spec/elasticsearch/model/response/results_spec.rb +73 -0
- data/spec/elasticsearch/model/searching_search_request_spec.rb +129 -0
- data/spec/elasticsearch/model/searching_spec.rb +66 -0
- data/spec/elasticsearch/model/serializing_spec.rb +39 -0
- data/spec/spec_helper.rb +193 -0
- data/spec/support/app/answer.rb +50 -0
- data/spec/support/app/article.rb +39 -0
- data/spec/support/app/article_for_pagination.rb +29 -0
- data/spec/support/app/article_no_type.rb +37 -0
- data/spec/support/app/article_with_custom_serialization.rb +30 -0
- data/spec/support/app/article_with_dynamic_index_name.rb +32 -0
- data/spec/support/app/author.rb +26 -0
- data/spec/support/app/authorship.rb +21 -0
- data/spec/support/app/category.rb +20 -0
- data/spec/support/app/comment.rb +20 -0
- data/spec/support/app/episode.rb +28 -0
- data/spec/support/app/image.rb +36 -0
- data/spec/support/app/import_article.rb +29 -0
- data/spec/support/app/mongoid_article.rb +38 -0
- data/spec/support/app/namespaced_book.rb +27 -0
- data/spec/support/app/parent_and_child_searchable.rb +41 -0
- data/spec/support/app/post.rb +31 -0
- data/spec/support/app/question.rb +44 -0
- data/spec/support/app/searchable.rb +65 -0
- data/spec/support/app/series.rb +28 -0
- data/spec/support/app.rb +46 -0
- data/spec/support/model.json +1 -0
- data/{test → spec}/support/model.yml +0 -0
- metadata +175 -121
- data/test/integration/active_record_associations_parent_child.rb +0 -139
- data/test/integration/active_record_associations_test.rb +0 -326
- data/test/integration/active_record_basic_test.rb +0 -234
- data/test/integration/active_record_custom_serialization_test.rb +0 -62
- data/test/integration/active_record_import_test.rb +0 -109
- data/test/integration/active_record_namespaced_model_test.rb +0 -49
- data/test/integration/active_record_pagination_test.rb +0 -145
- data/test/integration/dynamic_index_name_test.rb +0 -47
- data/test/integration/mongoid_basic_test.rb +0 -177
- data/test/integration/multiple_models_test.rb +0 -172
- data/test/support/model.json +0 -1
- data/test/test_helper.rb +0 -93
- data/test/unit/adapter_active_record_test.rb +0 -157
- data/test/unit/adapter_default_test.rb +0 -41
- data/test/unit/adapter_mongoid_test.rb +0 -104
- data/test/unit/adapter_multiple_test.rb +0 -106
- data/test/unit/adapter_test.rb +0 -69
- data/test/unit/callbacks_test.rb +0 -31
- data/test/unit/client_test.rb +0 -27
- data/test/unit/importing_test.rb +0 -203
- data/test/unit/indexing_test.rb +0 -650
- data/test/unit/module_test.rb +0 -57
- data/test/unit/multimodel_test.rb +0 -38
- data/test/unit/naming_test.rb +0 -103
- data/test/unit/proxy_test.rb +0 -100
- data/test/unit/response_base_test.rb +0 -40
- data/test/unit/response_pagination_kaminari_test.rb +0 -433
- data/test/unit/response_pagination_will_paginate_test.rb +0 -398
- data/test/unit/response_records_test.rb +0 -91
- data/test/unit/response_result_test.rb +0 -90
- data/test/unit/response_results_test.rb +0 -31
- data/test/unit/response_test.rb +0 -104
- data/test/unit/searching_search_request_test.rb +0 -78
- data/test/unit/searching_test.rb +0 -41
- data/test/unit/serializing_test.rb +0 -17
@@ -0,0 +1,252 @@
|
|
1
|
+
# Licensed to Elasticsearch B.V. under one or more contributor
|
2
|
+
# license agreements. See the NOTICE file distributed with
|
3
|
+
# this work for additional information regarding copyright
|
4
|
+
# ownership. Elasticsearch B.V. licenses this file to you under
|
5
|
+
# the Apache License, Version 2.0 (the "License"); you may
|
6
|
+
# not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing,
|
12
|
+
# software distributed under the License is distributed on an
|
13
|
+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14
|
+
# KIND, either express or implied. See the License for the
|
15
|
+
# specific language governing permissions and limitations
|
16
|
+
# under the License.
|
17
|
+
|
18
|
+
require 'spec_helper'
|
19
|
+
|
20
|
+
describe Elasticsearch::Model::Adapter::Mongoid do
|
21
|
+
|
22
|
+
before(:all) do
|
23
|
+
class DummyClassForMongoid; end
|
24
|
+
::Symbol.class_eval { def in; self; end }
|
25
|
+
end
|
26
|
+
|
27
|
+
after(:all) do
|
28
|
+
Elasticsearch::Model::Adapter::Adapter.adapters.delete(DummyClassForMongoid)
|
29
|
+
remove_classes(DummyClassForMongoid)
|
30
|
+
end
|
31
|
+
|
32
|
+
let(:response) do
|
33
|
+
{ 'hits' => {'hits' => [ {'_id' => 2}, {'_id' => 1} ]} }
|
34
|
+
end
|
35
|
+
|
36
|
+
let(:ids) do
|
37
|
+
[2, 1]
|
38
|
+
end
|
39
|
+
|
40
|
+
let(:record_1) do
|
41
|
+
double('record').tap do |rec|
|
42
|
+
allow(rec).to receive(:id).and_return(1)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
let(:record_2) do
|
47
|
+
double('record').tap do |rec|
|
48
|
+
allow(rec).to receive(:load).and_return(true)
|
49
|
+
allow(rec).to receive(:id).and_return(2)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
let(:records) do
|
54
|
+
[record_1, record_2]
|
55
|
+
end
|
56
|
+
|
57
|
+
let(:model) do
|
58
|
+
DummyClassForMongoid.new.tap do |m|
|
59
|
+
allow(m).to receive(:response).and_return(double('response', response: response))
|
60
|
+
allow(m).to receive(:ids).and_return(ids)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
describe 'adapter registration' do
|
65
|
+
|
66
|
+
it 'registers an adapater' do
|
67
|
+
expect(Elasticsearch::Model::Adapter.adapters[Elasticsearch::Model::Adapter::Mongoid]).not_to be_nil
|
68
|
+
expect(Elasticsearch::Model::Adapter.adapters[Elasticsearch::Model::Adapter::Mongoid].call(DummyClassForMongoid)).to be(false)
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'registers the records module' do
|
72
|
+
expect(Elasticsearch::Model::Adapter::Mongoid::Records).to be_a(Module)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
describe '#records' do
|
77
|
+
|
78
|
+
before(:all) do
|
79
|
+
DummyClassForMongoid.__send__ :include, Elasticsearch::Model::Adapter::Mongoid::Records
|
80
|
+
end
|
81
|
+
|
82
|
+
let(:instance) do
|
83
|
+
model.tap do |inst|
|
84
|
+
allow(inst).to receive(:klass).and_return(double('class', where: records)).at_least(:once)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'returns the records' do
|
89
|
+
expect(instance.records).to eq(records)
|
90
|
+
end
|
91
|
+
|
92
|
+
context 'when an order is not defined for the Mongoid query' do
|
93
|
+
|
94
|
+
context 'when the records have a different order than the hits' do
|
95
|
+
|
96
|
+
before do
|
97
|
+
records.instance_variable_set(:@records, records)
|
98
|
+
end
|
99
|
+
|
100
|
+
it 'reorders the records based on hits order' do
|
101
|
+
expect(records.collect(&:id)).to eq([1, 2])
|
102
|
+
expect(instance.records.to_a.collect(&:id)).to eq([2, 1])
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
context 'when an order is defined for the Mongoid query' do
|
107
|
+
|
108
|
+
context 'when the records have a different order than the hits' do
|
109
|
+
|
110
|
+
before do
|
111
|
+
records.instance_variable_set(:@records, records)
|
112
|
+
expect(instance.records).to receive(:asc).and_return(records)
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'reorders the records based on hits order' do
|
116
|
+
expect(records.collect(&:id)).to eq([1, 2])
|
117
|
+
expect(instance.records.to_a.collect(&:id)).to eq([2, 1])
|
118
|
+
expect(instance.asc.to_a.collect(&:id)).to eq([1, 2])
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
describe 'callbacks registration' do
|
125
|
+
|
126
|
+
before do
|
127
|
+
expect(DummyClassForMongoid).to receive(:after_create).once
|
128
|
+
expect(DummyClassForMongoid).to receive(:after_update).once
|
129
|
+
expect(DummyClassForMongoid).to receive(:after_destroy).once
|
130
|
+
end
|
131
|
+
|
132
|
+
it 'should register the model class for callbacks' do
|
133
|
+
Elasticsearch::Model::Adapter::Mongoid::Callbacks.included(DummyClassForMongoid)
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
describe 'importing' do
|
139
|
+
|
140
|
+
before(:all) do
|
141
|
+
DummyClassForMongoid.__send__ :extend, Elasticsearch::Model::Adapter::Mongoid::Importing
|
142
|
+
end
|
143
|
+
|
144
|
+
let(:relation) do
|
145
|
+
double('relation', each_slice: []).tap do |rel|
|
146
|
+
allow(rel).to receive(:published).and_return(rel)
|
147
|
+
allow(rel).to receive(:no_timeout).and_return(rel)
|
148
|
+
allow(rel).to receive(:class_exec).and_return(rel)
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
before do
|
153
|
+
allow(DummyClassForMongoid).to receive(:all).and_return(relation)
|
154
|
+
end
|
155
|
+
|
156
|
+
context 'when a scope is specified' do
|
157
|
+
|
158
|
+
it 'applies the scope' do
|
159
|
+
expect(DummyClassForMongoid.__find_in_batches(scope: :published) do; end).to eq([])
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
context 'query criteria specified as a proc' do
|
164
|
+
|
165
|
+
let(:query) do
|
166
|
+
Proc.new { where(color: "red") }
|
167
|
+
end
|
168
|
+
|
169
|
+
it 'execites the query' do
|
170
|
+
expect(DummyClassForMongoid.__find_in_batches(query: query) do; end).to eq([])
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
context 'query criteria specified as a hash' do
|
175
|
+
|
176
|
+
before do
|
177
|
+
expect(relation).to receive(:where).with(color: 'red').and_return(relation)
|
178
|
+
end
|
179
|
+
|
180
|
+
let(:query) do
|
181
|
+
{ color: "red" }
|
182
|
+
end
|
183
|
+
|
184
|
+
it 'execites the query' do
|
185
|
+
expect(DummyClassForMongoid.__find_in_batches(query: query) do; end).to eq([])
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
context 'when preprocessing batches' do
|
190
|
+
|
191
|
+
context 'if the query returns results' do
|
192
|
+
|
193
|
+
before do
|
194
|
+
class << DummyClassForMongoid
|
195
|
+
def find_in_batches(options = {}, &block)
|
196
|
+
yield [:a, :b]
|
197
|
+
end
|
198
|
+
|
199
|
+
def update_batch(batch)
|
200
|
+
batch.collect { |b| b.to_s + '!' }
|
201
|
+
end
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
it 'applies the preprocessing method' do
|
206
|
+
DummyClassForMongoid.__find_in_batches(preprocess: :update_batch) do |batch|
|
207
|
+
expect(batch).to match(['a!', 'b!'])
|
208
|
+
end
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
context 'if the query does not return results' do
|
213
|
+
|
214
|
+
before do
|
215
|
+
class << DummyClassForMongoid
|
216
|
+
def find_in_batches(options = {}, &block)
|
217
|
+
yield [:a, :b]
|
218
|
+
end
|
219
|
+
|
220
|
+
def update_batch(batch)
|
221
|
+
[]
|
222
|
+
end
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
it 'applies the preprocessing method' do
|
227
|
+
DummyClassForMongoid.__find_in_batches(preprocess: :update_batch) do |batch|
|
228
|
+
expect(batch).to match([])
|
229
|
+
end
|
230
|
+
end
|
231
|
+
end
|
232
|
+
end
|
233
|
+
|
234
|
+
context 'when transforming models' do
|
235
|
+
|
236
|
+
let(:instance) do
|
237
|
+
model.tap do |inst|
|
238
|
+
allow(inst).to receive(:as_indexed_json).and_return({})
|
239
|
+
allow(inst).to receive(:id).and_return(1)
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
243
|
+
it 'returns an proc' do
|
244
|
+
expect(DummyClassForMongoid.__transform.respond_to?(:call)).to be(true)
|
245
|
+
end
|
246
|
+
|
247
|
+
it 'provides a default transformation' do
|
248
|
+
expect(DummyClassForMongoid.__transform.call(instance)).to eq(index: { _id: '1', data: {} })
|
249
|
+
end
|
250
|
+
end
|
251
|
+
end
|
252
|
+
end
|
@@ -0,0 +1,142 @@
|
|
1
|
+
# Licensed to Elasticsearch B.V. under one or more contributor
|
2
|
+
# license agreements. See the NOTICE file distributed with
|
3
|
+
# this work for additional information regarding copyright
|
4
|
+
# ownership. Elasticsearch B.V. licenses this file to you under
|
5
|
+
# the Apache License, Version 2.0 (the "License"); you may
|
6
|
+
# not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing,
|
12
|
+
# software distributed under the License is distributed on an
|
13
|
+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14
|
+
# KIND, either express or implied. See the License for the
|
15
|
+
# specific language governing permissions and limitations
|
16
|
+
# under the License.
|
17
|
+
|
18
|
+
require 'spec_helper'
|
19
|
+
|
20
|
+
describe Elasticsearch::Model::Adapter::Multiple do
|
21
|
+
|
22
|
+
before(:all) do
|
23
|
+
class DummyOne
|
24
|
+
include Elasticsearch::Model
|
25
|
+
|
26
|
+
index_name 'dummy'
|
27
|
+
document_type 'dummy_one'
|
28
|
+
|
29
|
+
def self.find(ids)
|
30
|
+
ids.map { |id| new(id) }
|
31
|
+
end
|
32
|
+
|
33
|
+
attr_reader :id
|
34
|
+
|
35
|
+
def initialize(id)
|
36
|
+
@id = id.to_i
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
module Namespace
|
41
|
+
class DummyTwo
|
42
|
+
include Elasticsearch::Model
|
43
|
+
|
44
|
+
index_name 'dummy'
|
45
|
+
document_type 'dummy_two'
|
46
|
+
|
47
|
+
def self.find(ids)
|
48
|
+
ids.map { |id| new(id) }
|
49
|
+
end
|
50
|
+
|
51
|
+
attr_reader :id
|
52
|
+
|
53
|
+
def initialize(id)
|
54
|
+
@id = id.to_i
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
class DummyTwo
|
60
|
+
include Elasticsearch::Model
|
61
|
+
|
62
|
+
index_name 'other_index'
|
63
|
+
document_type 'dummy_two'
|
64
|
+
|
65
|
+
def self.find(ids)
|
66
|
+
ids.map { |id| new(id) }
|
67
|
+
end
|
68
|
+
|
69
|
+
attr_reader :id
|
70
|
+
|
71
|
+
def initialize(id)
|
72
|
+
@id = id.to_i
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
after(:all) do
|
78
|
+
[DummyOne, Namespace::DummyTwo, DummyTwo].each do |adapter|
|
79
|
+
Elasticsearch::Model::Adapter::Adapter.adapters.delete(adapter)
|
80
|
+
end
|
81
|
+
Namespace.send(:remove_const, :DummyTwo) if defined?(Namespace::DummyTwo)
|
82
|
+
remove_classes(DummyOne, DummyTwo, Namespace)
|
83
|
+
end
|
84
|
+
|
85
|
+
let(:hits) do
|
86
|
+
[
|
87
|
+
{
|
88
|
+
_index: 'dummy',
|
89
|
+
_type: 'dummy_two',
|
90
|
+
_id: '2'
|
91
|
+
},
|
92
|
+
{
|
93
|
+
_index: 'dummy',
|
94
|
+
_type: 'dummy_one',
|
95
|
+
_id: '2'
|
96
|
+
},
|
97
|
+
{
|
98
|
+
_index: 'other_index',
|
99
|
+
_type: 'dummy_two',
|
100
|
+
_id: '1'
|
101
|
+
},
|
102
|
+
{
|
103
|
+
_index: 'dummy',
|
104
|
+
_type: 'dummy_two',
|
105
|
+
_id: '1'
|
106
|
+
},
|
107
|
+
{
|
108
|
+
_index: 'dummy',
|
109
|
+
_type: 'dummy_one',
|
110
|
+
_id: '3'
|
111
|
+
}
|
112
|
+
]
|
113
|
+
end
|
114
|
+
|
115
|
+
let(:response) do
|
116
|
+
double('response', response: { 'hits' => { 'hits' => hits } })
|
117
|
+
end
|
118
|
+
|
119
|
+
let(:multimodel) do
|
120
|
+
Elasticsearch::Model::Multimodel.new(DummyOne, DummyTwo, Namespace::DummyTwo)
|
121
|
+
end
|
122
|
+
|
123
|
+
describe '#records' do
|
124
|
+
|
125
|
+
before do
|
126
|
+
multimodel.class.send :include, Elasticsearch::Model::Adapter::Multiple::Records
|
127
|
+
expect(multimodel).to receive(:response).at_least(:once).and_return(response)
|
128
|
+
end
|
129
|
+
|
130
|
+
it 'instantiates the correct types of instances' do
|
131
|
+
expect(multimodel.records[0]).to be_a(Namespace::DummyTwo)
|
132
|
+
expect(multimodel.records[1]).to be_a(DummyOne)
|
133
|
+
expect(multimodel.records[2]).to be_a(DummyTwo)
|
134
|
+
expect(multimodel.records[3]).to be_a(Namespace::DummyTwo)
|
135
|
+
expect(multimodel.records[4]).to be_a(DummyOne)
|
136
|
+
end
|
137
|
+
|
138
|
+
it 'returns the results in the correct order' do
|
139
|
+
expect(multimodel.records.map(&:id)).to eq([2, 2, 1, 1, 3])
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# Licensed to Elasticsearch B.V. under one or more contributor
|
2
|
+
# license agreements. See the NOTICE file distributed with
|
3
|
+
# this work for additional information regarding copyright
|
4
|
+
# ownership. Elasticsearch B.V. licenses this file to you under
|
5
|
+
# the Apache License, Version 2.0 (the "License"); you may
|
6
|
+
# not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing,
|
12
|
+
# software distributed under the License is distributed on an
|
13
|
+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14
|
+
# KIND, either express or implied. See the License for the
|
15
|
+
# specific language governing permissions and limitations
|
16
|
+
# under the License.
|
17
|
+
|
18
|
+
require 'spec_helper'
|
19
|
+
|
20
|
+
describe Elasticsearch::Model::Callbacks do
|
21
|
+
|
22
|
+
before(:all) do
|
23
|
+
class ::DummyCallbacksModel
|
24
|
+
end
|
25
|
+
|
26
|
+
module DummyCallbacksAdapter
|
27
|
+
module CallbacksMixin
|
28
|
+
end
|
29
|
+
|
30
|
+
def callbacks_mixin
|
31
|
+
CallbacksMixin
|
32
|
+
end; module_function :callbacks_mixin
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
after(:all) do
|
37
|
+
remove_classes(DummyCallbacksModel, DummyCallbacksAdapter)
|
38
|
+
end
|
39
|
+
|
40
|
+
context 'when a model includes the Callbacks module' do
|
41
|
+
|
42
|
+
before do
|
43
|
+
Elasticsearch::Model::Callbacks.included(DummyCallbacksModel)
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'includes the callbacks mixin from the model adapter' do
|
47
|
+
expect(DummyCallbacksModel.ancestors).to include(Elasticsearch::Model::Adapter::Default::Callbacks)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
# Licensed to Elasticsearch B.V. under one or more contributor
|
2
|
+
# license agreements. See the NOTICE file distributed with
|
3
|
+
# this work for additional information regarding copyright
|
4
|
+
# ownership. Elasticsearch B.V. licenses this file to you under
|
5
|
+
# the Apache License, Version 2.0 (the "License"); you may
|
6
|
+
# not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing,
|
12
|
+
# software distributed under the License is distributed on an
|
13
|
+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14
|
+
# KIND, either express or implied. See the License for the
|
15
|
+
# specific language governing permissions and limitations
|
16
|
+
# under the License.
|
17
|
+
|
18
|
+
require 'spec_helper'
|
19
|
+
|
20
|
+
describe Elasticsearch::Model::Client do
|
21
|
+
|
22
|
+
before(:all) do
|
23
|
+
class ::DummyClientModel
|
24
|
+
extend Elasticsearch::Model::Client::ClassMethods
|
25
|
+
include Elasticsearch::Model::Client::InstanceMethods
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
after(:all) do
|
30
|
+
remove_classes(DummyClientModel)
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'when a class includes the client module class methods' do
|
34
|
+
|
35
|
+
it 'defines the client module class methods on the model' do
|
36
|
+
expect(DummyClientModel.client).to be_a(Elasticsearch::Transport::Client)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context 'when a class includes the client module instance methods' do
|
41
|
+
|
42
|
+
it 'defines the client module class methods on the model' do
|
43
|
+
expect(DummyClientModel.new.client).to be_a(Elasticsearch::Transport::Client)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'when the client is set on the class' do
|
48
|
+
|
49
|
+
around do |example|
|
50
|
+
original_client = DummyClientModel.client
|
51
|
+
DummyClientModel.client = 'foobar'
|
52
|
+
example.run
|
53
|
+
DummyClientModel.client = original_client
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'sets the client on the class' do
|
57
|
+
expect(DummyClientModel.client).to eq('foobar')
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'sets the client on an instance' do
|
61
|
+
expect(DummyClientModel.new.client).to eq('foobar')
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context 'when the client is set on an instance' do
|
66
|
+
|
67
|
+
before do
|
68
|
+
model_instance.client = 'foo'
|
69
|
+
end
|
70
|
+
|
71
|
+
let(:model_instance) do
|
72
|
+
DummyClientModel.new
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'sets the client on an instance' do
|
76
|
+
expect(model_instance.client).to eq('foo')
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'does not set the client on the class' do
|
80
|
+
expect(DummyClientModel.client).to be_a(Elasticsearch::Transport::Client)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# Licensed to Elasticsearch B.V. under one or more contributor
|
2
|
+
# license agreements. See the NOTICE file distributed with
|
3
|
+
# this work for additional information regarding copyright
|
4
|
+
# ownership. Elasticsearch B.V. licenses this file to you under
|
5
|
+
# the Apache License, Version 2.0 (the "License"); you may
|
6
|
+
# not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
#
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
#
|
11
|
+
# Unless required by applicable law or agreed to in writing,
|
12
|
+
# software distributed under the License is distributed on an
|
13
|
+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
14
|
+
# KIND, either express or implied. See the License for the
|
15
|
+
# specific language governing permissions and limitations
|
16
|
+
# under the License.
|
17
|
+
|
18
|
+
require 'spec_helper'
|
19
|
+
|
20
|
+
describe Elasticsearch::Model::HashWrapper, if: Hashie::VERSION >= '3.5.3' do
|
21
|
+
|
22
|
+
before do
|
23
|
+
expect(Hashie.logger).to receive(:warn).never
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'does not print a warning for re-defined methods' do
|
27
|
+
Elasticsearch::Model::HashWrapper.new(:foo => 'bar', :sort => true)
|
28
|
+
end
|
29
|
+
end
|