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.
Files changed (145) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +3 -2
  3. data/Gemfile +22 -0
  4. data/LICENSE.txt +199 -10
  5. data/README.md +96 -43
  6. data/Rakefile +49 -35
  7. data/elasticsearch-model.gemspec +53 -41
  8. data/examples/activerecord_article.rb +18 -1
  9. data/examples/activerecord_associations.rb +86 -17
  10. data/examples/activerecord_custom_analyzer.rb +152 -0
  11. data/examples/activerecord_mapping_completion.rb +33 -16
  12. data/examples/activerecord_mapping_edge_ngram.rb +118 -0
  13. data/examples/couchbase_article.rb +17 -0
  14. data/examples/datamapper_article.rb +28 -1
  15. data/examples/mongoid_article.rb +17 -0
  16. data/examples/ohm_article.rb +17 -0
  17. data/examples/riak_article.rb +17 -0
  18. data/gemfiles/3.0.gemfile +23 -1
  19. data/gemfiles/4.0.gemfile +25 -1
  20. data/gemfiles/5.0.gemfile +35 -0
  21. data/gemfiles/6.0.gemfile +36 -0
  22. data/lib/elasticsearch/model/adapter.rb +17 -0
  23. data/lib/elasticsearch/model/adapters/active_record.rb +31 -27
  24. data/lib/elasticsearch/model/adapters/default.rb +17 -0
  25. data/lib/elasticsearch/model/adapters/mongoid.rb +27 -3
  26. data/lib/elasticsearch/model/adapters/multiple.rb +29 -4
  27. data/lib/elasticsearch/model/callbacks.rb +17 -0
  28. data/lib/elasticsearch/model/client.rb +17 -0
  29. data/lib/elasticsearch/model/ext/active_record.rb +17 -0
  30. data/lib/elasticsearch/model/hash_wrapper.rb +32 -0
  31. data/lib/elasticsearch/model/importing.rb +61 -17
  32. data/lib/elasticsearch/model/indexing.rb +87 -49
  33. data/lib/elasticsearch/model/multimodel.rb +17 -0
  34. data/lib/elasticsearch/model/naming.rb +33 -2
  35. data/lib/elasticsearch/model/proxy.rb +61 -18
  36. data/lib/elasticsearch/model/response/aggregations.rb +55 -0
  37. data/lib/elasticsearch/model/response/base.rb +25 -3
  38. data/lib/elasticsearch/model/response/pagination/kaminari.rb +126 -0
  39. data/lib/elasticsearch/model/response/pagination/will_paginate.rb +112 -0
  40. data/lib/elasticsearch/model/response/pagination.rb +19 -192
  41. data/lib/elasticsearch/model/response/records.rb +17 -1
  42. data/lib/elasticsearch/model/response/result.rb +19 -2
  43. data/lib/elasticsearch/model/response/results.rb +17 -0
  44. data/lib/elasticsearch/model/response/suggestions.rb +20 -1
  45. data/lib/elasticsearch/model/response.rb +28 -10
  46. data/lib/elasticsearch/model/searching.rb +17 -0
  47. data/lib/elasticsearch/model/serializing.rb +17 -0
  48. data/lib/elasticsearch/model/version.rb +18 -1
  49. data/lib/elasticsearch/model.rb +36 -39
  50. data/spec/elasticsearch/model/adapter_spec.rb +136 -0
  51. data/spec/elasticsearch/model/adapters/active_record/associations_spec.rb +351 -0
  52. data/spec/elasticsearch/model/adapters/active_record/basic_spec.rb +395 -0
  53. data/spec/elasticsearch/model/adapters/active_record/dynamic_index_name_spec.rb +35 -0
  54. data/spec/elasticsearch/model/adapters/active_record/import_spec.rb +193 -0
  55. data/spec/elasticsearch/model/adapters/active_record/multi_model_spec.rb +127 -0
  56. data/spec/elasticsearch/model/adapters/active_record/namespaced_model_spec.rb +55 -0
  57. data/spec/elasticsearch/model/adapters/active_record/pagination_spec.rb +332 -0
  58. data/spec/elasticsearch/model/adapters/active_record/parent_child_spec.rb +92 -0
  59. data/spec/elasticsearch/model/adapters/active_record/serialization_spec.rb +78 -0
  60. data/spec/elasticsearch/model/adapters/active_record_spec.rb +224 -0
  61. data/spec/elasticsearch/model/adapters/default_spec.rb +58 -0
  62. data/spec/elasticsearch/model/adapters/mongoid/basic_spec.rb +284 -0
  63. data/spec/elasticsearch/model/adapters/mongoid/multi_model_spec.rb +83 -0
  64. data/spec/elasticsearch/model/adapters/mongoid_spec.rb +252 -0
  65. data/spec/elasticsearch/model/adapters/multiple_spec.rb +142 -0
  66. data/spec/elasticsearch/model/callbacks_spec.rb +50 -0
  67. data/spec/elasticsearch/model/client_spec.rb +83 -0
  68. data/spec/elasticsearch/model/hash_wrapper_spec.rb +29 -0
  69. data/spec/elasticsearch/model/importing_spec.rb +243 -0
  70. data/spec/elasticsearch/model/indexing_spec.rb +1014 -0
  71. data/spec/elasticsearch/model/module_spec.rb +94 -0
  72. data/spec/elasticsearch/model/multimodel_spec.rb +72 -0
  73. data/spec/elasticsearch/model/naming_spec.rb +203 -0
  74. data/spec/elasticsearch/model/proxy_spec.rb +124 -0
  75. data/spec/elasticsearch/model/response/aggregations_spec.rb +83 -0
  76. data/spec/elasticsearch/model/response/base_spec.rb +107 -0
  77. data/spec/elasticsearch/model/response/pagination/kaminari_spec.rb +472 -0
  78. data/spec/elasticsearch/model/response/pagination/will_paginate_spec.rb +279 -0
  79. data/spec/elasticsearch/model/response/records_spec.rb +135 -0
  80. data/spec/elasticsearch/model/response/response_spec.rb +148 -0
  81. data/spec/elasticsearch/model/response/result_spec.rb +139 -0
  82. data/spec/elasticsearch/model/response/results_spec.rb +73 -0
  83. data/spec/elasticsearch/model/searching_search_request_spec.rb +129 -0
  84. data/spec/elasticsearch/model/searching_spec.rb +66 -0
  85. data/spec/elasticsearch/model/serializing_spec.rb +39 -0
  86. data/spec/spec_helper.rb +193 -0
  87. data/spec/support/app/answer.rb +50 -0
  88. data/spec/support/app/article.rb +39 -0
  89. data/spec/support/app/article_for_pagination.rb +29 -0
  90. data/spec/support/app/article_no_type.rb +37 -0
  91. data/spec/support/app/article_with_custom_serialization.rb +30 -0
  92. data/spec/support/app/article_with_dynamic_index_name.rb +32 -0
  93. data/spec/support/app/author.rb +26 -0
  94. data/spec/support/app/authorship.rb +21 -0
  95. data/spec/support/app/category.rb +20 -0
  96. data/spec/support/app/comment.rb +20 -0
  97. data/spec/support/app/episode.rb +28 -0
  98. data/spec/support/app/image.rb +36 -0
  99. data/spec/support/app/import_article.rb +29 -0
  100. data/spec/support/app/mongoid_article.rb +38 -0
  101. data/spec/support/app/namespaced_book.rb +27 -0
  102. data/spec/support/app/parent_and_child_searchable.rb +41 -0
  103. data/spec/support/app/post.rb +31 -0
  104. data/spec/support/app/question.rb +44 -0
  105. data/spec/support/app/searchable.rb +65 -0
  106. data/spec/support/app/series.rb +28 -0
  107. data/spec/support/app.rb +46 -0
  108. data/spec/support/model.json +1 -0
  109. data/{test → spec}/support/model.yml +0 -0
  110. metadata +175 -121
  111. data/test/integration/active_record_associations_parent_child.rb +0 -139
  112. data/test/integration/active_record_associations_test.rb +0 -326
  113. data/test/integration/active_record_basic_test.rb +0 -234
  114. data/test/integration/active_record_custom_serialization_test.rb +0 -62
  115. data/test/integration/active_record_import_test.rb +0 -109
  116. data/test/integration/active_record_namespaced_model_test.rb +0 -49
  117. data/test/integration/active_record_pagination_test.rb +0 -145
  118. data/test/integration/dynamic_index_name_test.rb +0 -47
  119. data/test/integration/mongoid_basic_test.rb +0 -177
  120. data/test/integration/multiple_models_test.rb +0 -172
  121. data/test/support/model.json +0 -1
  122. data/test/test_helper.rb +0 -93
  123. data/test/unit/adapter_active_record_test.rb +0 -157
  124. data/test/unit/adapter_default_test.rb +0 -41
  125. data/test/unit/adapter_mongoid_test.rb +0 -104
  126. data/test/unit/adapter_multiple_test.rb +0 -106
  127. data/test/unit/adapter_test.rb +0 -69
  128. data/test/unit/callbacks_test.rb +0 -31
  129. data/test/unit/client_test.rb +0 -27
  130. data/test/unit/importing_test.rb +0 -203
  131. data/test/unit/indexing_test.rb +0 -650
  132. data/test/unit/module_test.rb +0 -57
  133. data/test/unit/multimodel_test.rb +0 -38
  134. data/test/unit/naming_test.rb +0 -103
  135. data/test/unit/proxy_test.rb +0 -100
  136. data/test/unit/response_base_test.rb +0 -40
  137. data/test/unit/response_pagination_kaminari_test.rb +0 -433
  138. data/test/unit/response_pagination_will_paginate_test.rb +0 -398
  139. data/test/unit/response_records_test.rb +0 -91
  140. data/test/unit/response_result_test.rb +0 -90
  141. data/test/unit/response_results_test.rb +0 -31
  142. data/test/unit/response_test.rb +0 -104
  143. data/test/unit/searching_search_request_test.rb +0 -78
  144. data/test/unit/searching_test.rb +0 -41
  145. 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