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,94 @@
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 do
21
+
22
+ describe '#client' do
23
+
24
+ it 'should have a default' do
25
+ expect(Elasticsearch::Model.client).to be_a(Elasticsearch::Transport::Client)
26
+ end
27
+ end
28
+
29
+ describe '#client=' do
30
+
31
+ before do
32
+ Elasticsearch::Model.client = 'Foobar'
33
+ end
34
+
35
+ it 'should allow the client to be set' do
36
+ expect(Elasticsearch::Model.client).to eq('Foobar')
37
+ end
38
+ end
39
+
40
+ describe 'mixin' do
41
+
42
+ before(:all) do
43
+ class ::DummyIncludingModel; end
44
+ class ::DummyIncludingModelWithSearchMethodDefined
45
+ def self.search(query, options={})
46
+ "SEARCH"
47
+ end
48
+ end
49
+
50
+ DummyIncludingModel.__send__ :include, Elasticsearch::Model
51
+ DummyIncludingModelWithSearchMethodDefined.__send__ :include, Elasticsearch::Model
52
+ end
53
+
54
+ after(:all) do
55
+ remove_classes(DummyIncludingModel, DummyIncludingModelWithSearchMethodDefined)
56
+ end
57
+
58
+ it 'should include and set up the proxy' do
59
+ expect(DummyIncludingModel).to respond_to(:__elasticsearch__)
60
+ expect(DummyIncludingModel.new).to respond_to(:__elasticsearch__)
61
+ end
62
+
63
+ it 'should delegate methods to the proxy' do
64
+ expect(DummyIncludingModel).to respond_to(:search)
65
+ expect(DummyIncludingModel).to respond_to(:mapping)
66
+ expect(DummyIncludingModel).to respond_to(:settings)
67
+ expect(DummyIncludingModel).to respond_to(:index_name)
68
+ expect(DummyIncludingModel).to respond_to(:document_type)
69
+ expect(DummyIncludingModel).to respond_to(:import)
70
+ end
71
+
72
+ it 'should not interfere with existing methods' do
73
+ expect(DummyIncludingModelWithSearchMethodDefined.search('foo')).to eq('SEARCH')
74
+ end
75
+ end
76
+
77
+ describe '#settings' do
78
+
79
+ it 'allows access to the settings' do
80
+ expect(Elasticsearch::Model.settings).to eq({})
81
+ end
82
+
83
+ context 'when settings are changed' do
84
+
85
+ before do
86
+ Elasticsearch::Model.settings[:foo] = 'bar'
87
+ end
88
+
89
+ it 'persists the changes' do
90
+ expect(Elasticsearch::Model.settings[:foo]).to eq('bar')
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,72 @@
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::Multimodel do
21
+
22
+ let(:multimodel) do
23
+ Elasticsearch::Model::Multimodel.new(model_1, model_2)
24
+ end
25
+
26
+ let(:model_1) do
27
+ double('Foo', index_name: 'foo_index', document_type: 'foo', to_ary: nil)
28
+ end
29
+
30
+ let(:model_2) do
31
+ double('Bar', index_name: 'bar_index', document_type: 'bar', to_ary: nil)
32
+ end
33
+
34
+ it 'has an index name' do
35
+ expect(multimodel.index_name).to eq(['foo_index', 'bar_index'])
36
+ end
37
+
38
+ it 'has an document type' do
39
+ expect(multimodel.document_type).to eq(['foo', 'bar'])
40
+ end
41
+
42
+ it 'has a client' do
43
+ expect(multimodel.client).to eq(Elasticsearch::Model.client)
44
+ end
45
+
46
+ describe 'the model registry' do
47
+
48
+ before(:all) do
49
+
50
+ class JustAModel
51
+ include Elasticsearch::Model
52
+ end
53
+
54
+ class JustAnotherModel
55
+ include Elasticsearch::Model
56
+ end
57
+ end
58
+
59
+ after(:all) do
60
+ remove_classes(JustAModel, JustAnotherModel)
61
+ end
62
+
63
+ let(:multimodel) do
64
+ Elasticsearch::Model::Multimodel.new
65
+ end
66
+
67
+ it 'includes model in the registry' do
68
+ expect(multimodel.models).to include(JustAModel)
69
+ expect(multimodel.models).to include(JustAnotherModel)
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,203 @@
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 'naming' do
21
+
22
+ before(:all) do
23
+ class ::DummyNamingModel
24
+ extend ActiveModel::Naming
25
+
26
+ extend Elasticsearch::Model::Naming::ClassMethods
27
+ include Elasticsearch::Model::Naming::InstanceMethods
28
+ end
29
+
30
+ module ::MyNamespace
31
+ class DummyNamingModelInNamespace
32
+ extend ActiveModel::Naming
33
+
34
+ extend Elasticsearch::Model::Naming::ClassMethods
35
+ include Elasticsearch::Model::Naming::InstanceMethods
36
+ end
37
+ end
38
+ end
39
+
40
+ after(:all) do
41
+ remove_classes(DummyNamingModel, MyNamespace)
42
+ end
43
+
44
+ it 'returns the default index name' do
45
+ expect(DummyNamingModel.index_name).to eq('dummy_naming_models')
46
+ expect(DummyNamingModel.new.index_name).to eq('dummy_naming_models')
47
+ end
48
+
49
+ it 'returns the sanitized defualt index name for namespaced models' do
50
+ expect(::MyNamespace::DummyNamingModelInNamespace.index_name).to eq('my_namespace-dummy_naming_model_in_namespaces')
51
+ expect(::MyNamespace::DummyNamingModelInNamespace.new.index_name).to eq('my_namespace-dummy_naming_model_in_namespaces')
52
+ end
53
+
54
+ it 'returns nil' do
55
+ expect(DummyNamingModel.document_type).to be_nil
56
+ expect(DummyNamingModel.new.document_type).to be_nil
57
+ end
58
+
59
+ describe '#index_name' do
60
+
61
+ context 'when the index name is set on the class' do
62
+
63
+ before do
64
+ DummyNamingModel.index_name 'foobar'
65
+ end
66
+
67
+ it 'sets the index_name' do
68
+ expect(DummyNamingModel.index_name).to eq('foobar')
69
+ end
70
+ end
71
+
72
+ context 'when the index name is set on an instance' do
73
+
74
+ before do
75
+ instance.index_name 'foobar_d'
76
+ end
77
+
78
+ let(:instance) do
79
+ DummyNamingModel.new
80
+ end
81
+
82
+ it 'sets the index name on the instance' do
83
+ expect(instance.index_name).to eq('foobar_d')
84
+ end
85
+
86
+ context 'when the index name is set with a proc' do
87
+
88
+ before do
89
+ modifier = 'r'
90
+ instance.index_name Proc.new{ "foobar_#{modifier}" }
91
+ end
92
+
93
+ it 'sets the index name on the instance' do
94
+ expect(instance.index_name).to eq('foobar_r')
95
+ end
96
+ end
97
+ end
98
+ end
99
+
100
+ describe '#index_name=' do
101
+
102
+ before do
103
+ DummyNamingModel.index_name = 'foobar_index_S'
104
+ end
105
+
106
+ it 'changes the index name' do
107
+ expect(DummyNamingModel.index_name).to eq('foobar_index_S')
108
+ end
109
+
110
+ context 'when the method is called on an instance' do
111
+
112
+ let(:instance) do
113
+ DummyNamingModel.new
114
+ end
115
+
116
+ before do
117
+ instance.index_name = 'foobar_index_s'
118
+ end
119
+
120
+ it 'changes the index name' do
121
+ expect(instance.index_name).to eq('foobar_index_s')
122
+ end
123
+
124
+ it 'does not change the index name on the class' do
125
+ expect(DummyNamingModel.index_name).to eq('foobar_index_S')
126
+ end
127
+ end
128
+
129
+ context 'when the index name is changed with a proc' do
130
+
131
+ before do
132
+ modifier2 = 'y'
133
+ DummyNamingModel.index_name = Proc.new{ "foobar_index_#{modifier2}" }
134
+ end
135
+
136
+ it 'changes the index name' do
137
+ expect(DummyNamingModel.index_name).to eq('foobar_index_y')
138
+ end
139
+ end
140
+ end
141
+
142
+ describe '#document_type' do
143
+
144
+ it 'returns nil' do
145
+ expect(DummyNamingModel.document_type).to be_nil
146
+ end
147
+
148
+ context 'when the method is called with an argument' do
149
+
150
+ before do
151
+ DummyNamingModel.document_type 'foo'
152
+ end
153
+
154
+ it 'changes the document type' do
155
+ expect(DummyNamingModel.document_type).to eq('foo')
156
+ end
157
+ end
158
+
159
+ context 'when the method is called on an instance' do
160
+
161
+ let(:instance) do
162
+ DummyNamingModel.new
163
+ end
164
+
165
+ before do
166
+ instance.document_type 'foobar_d'
167
+ end
168
+
169
+ it 'changes the document type' do
170
+ expect(instance.document_type).to eq('foobar_d')
171
+ end
172
+ end
173
+ end
174
+
175
+ describe '#document_type=' do
176
+
177
+ context 'when the method is called on the class' do
178
+
179
+ before do
180
+ DummyNamingModel.document_type = 'foo_z'
181
+ end
182
+
183
+ it 'changes the document type' do
184
+ expect(DummyNamingModel.document_type).to eq('foo_z')
185
+ end
186
+ end
187
+
188
+ context 'when the method is called on an instance' do
189
+
190
+ let(:instance) do
191
+ DummyNamingModel.new
192
+ end
193
+
194
+ before do
195
+ instance.document_type = 'foobar_b'
196
+ end
197
+
198
+ it 'changes the document type' do
199
+ expect(instance.document_type).to eq('foobar_b')
200
+ end
201
+ end
202
+ end
203
+ end
@@ -0,0 +1,124 @@
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::Proxy do
21
+
22
+ before(:all) do
23
+ class ::DummyProxyModel
24
+ include Elasticsearch::Model::Proxy
25
+
26
+ def self.foo
27
+ 'classy foo'
28
+ end
29
+
30
+ def bar
31
+ 'insta barr'
32
+ end
33
+
34
+ def as_json(options)
35
+ {foo: 'bar'}
36
+ end
37
+ end
38
+
39
+ class ::DummyProxyModelWithCallbacks
40
+ def self.before_save(&block)
41
+ (@callbacks ||= {})[block.hash] = block
42
+ end
43
+
44
+ def changes_to_save
45
+ {:foo => ['One', 'Two']}
46
+ end
47
+ end
48
+
49
+ DummyProxyModelWithCallbacks.__send__ :include, Elasticsearch::Model::Proxy
50
+ end
51
+
52
+ after(:all) do
53
+ remove_classes(DummyProxyModel, DummyProxyModelWithCallbacks)
54
+ end
55
+
56
+ it 'sets up a proxy method on the class' do
57
+ expect(DummyProxyModel).to respond_to(:__elasticsearch__)
58
+ end
59
+
60
+ it 'sets up a proxy method on instances' do
61
+ expect(DummyProxyModel.new).to respond_to(:__elasticsearch__)
62
+ end
63
+
64
+ it 'sets up hooks for before_save callbacks' do
65
+ expect(DummyProxyModelWithCallbacks).to respond_to(:before_save)
66
+ end
67
+
68
+ it 'delegates methods to the target' do
69
+ expect(DummyProxyModel.__elasticsearch__).to respond_to(:foo)
70
+ expect(DummyProxyModel.__elasticsearch__.foo).to eq('classy foo')
71
+ expect(DummyProxyModel.new.__elasticsearch__).to respond_to(:bar)
72
+ expect(DummyProxyModel.new.__elasticsearch__.bar).to eq('insta barr')
73
+
74
+ expect {
75
+ DummyProxyModel.__elasticsearch__.xoxo
76
+ }.to raise_exception(NoMethodError)
77
+
78
+ expect {
79
+ DummyProxyModel.new.__elasticsearch__.xoxo
80
+ }.to raise_exception(NoMethodError)
81
+ end
82
+
83
+ it 'returns the proxy class from an instance proxy' do
84
+ expect(DummyProxyModel.new.__elasticsearch__.class.class).to eq(Elasticsearch::Model::Proxy::ClassMethodsProxy)
85
+ end
86
+
87
+ it 'returns the origin class from an instance proxy' do
88
+ expect(DummyProxyModel.new.__elasticsearch__.klass).to eq(DummyProxyModel)
89
+ end
90
+
91
+ it 'delegates #as_json from the proxy to the target' do
92
+ expect(DummyProxyModel.new.__elasticsearch__.as_json).to eq(foo: 'bar')
93
+ end
94
+
95
+ it 'includes the proxy in the inspect string' do
96
+ expect(DummyProxyModel.__elasticsearch__.inspect).to match(/PROXY/)
97
+ expect(DummyProxyModel.new.__elasticsearch__.inspect).to match(/PROXY/)
98
+ end
99
+
100
+ context 'when instances are cloned' do
101
+
102
+ let!(:model) do
103
+ DummyProxyModel.new
104
+ end
105
+
106
+ let!(:model_target) do
107
+ model.__elasticsearch__.target
108
+ end
109
+
110
+ let!(:duplicate) do
111
+ model.dup
112
+ end
113
+
114
+ let!(:duplicate_target) do
115
+ duplicate.__elasticsearch__.target
116
+ end
117
+
118
+ it 'resets the proxy target' do
119
+ expect(model).not_to eq(duplicate)
120
+ expect(model).to eq(model_target)
121
+ expect(duplicate).to eq(duplicate_target)
122
+ end
123
+ end
124
+ 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::Response::Aggregations do
21
+
22
+ before(:all) do
23
+ class OriginClass
24
+ def self.index_name; 'foo'; end
25
+ def self.document_type; 'bar'; end
26
+ end
27
+ end
28
+
29
+ after(:all) do
30
+ remove_classes(OriginClass)
31
+ end
32
+
33
+ let(:response_document) do
34
+ {
35
+ 'aggregations' => {
36
+ 'foo' => {'bar' => 10 },
37
+ 'price' => { 'doc_count' => 123,
38
+ 'min' => { 'value' => 1.0},
39
+ 'max' => { 'value' => 99 }
40
+ }
41
+ }
42
+ }
43
+ end
44
+
45
+ let(:search) do
46
+ Elasticsearch::Model::Searching::SearchRequest.new(OriginClass, '*').tap do |request|
47
+ allow(request).to receive(:execute!).and_return(response_document)
48
+ end
49
+ end
50
+
51
+ let(:aggregations) do
52
+ Elasticsearch::Model::Response::Response.new(OriginClass, search).aggregations
53
+ end
54
+
55
+ describe 'method delegation' do
56
+
57
+ it 'delegates methods to the response document' do
58
+ expect(aggregations.foo).to be_a(Hashie::Mash)
59
+ expect(aggregations.foo.bar).to be(10)
60
+ end
61
+ end
62
+
63
+ describe '#doc_count' do
64
+
65
+ it 'returns the doc count value from the response document' do
66
+ expect(aggregations.price.doc_count).to eq(123)
67
+ end
68
+ end
69
+
70
+ describe '#min' do
71
+
72
+ it 'returns the min value from the response document' do
73
+ expect(aggregations.price.min.value).to eq(1)
74
+ end
75
+ end
76
+
77
+ describe '#max' do
78
+
79
+ it 'returns the max value from the response document' do
80
+ expect(aggregations.price.max.value).to eq(99)
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,107 @@
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::Response::Base do
21
+
22
+ before(:all) do
23
+ class DummyBaseClass
24
+ include Elasticsearch::Model::Response::Base
25
+ end
26
+
27
+ class OriginClass
28
+ def self.index_name; 'foo'; end
29
+ def self.document_type; 'bar'; end
30
+ end
31
+ end
32
+
33
+ after(:all) do
34
+ remove_classes(DummyBaseClass, OriginClass)
35
+ end
36
+
37
+ let(:response_document) do
38
+ { 'hits' => { 'total' => 123, 'max_score' => 456, 'hits' => [] } }
39
+ end
40
+
41
+ let(:search) do
42
+ Elasticsearch::Model::Searching::SearchRequest.new(OriginClass, '*').tap do |request|
43
+ allow(request).to receive(:execute!).and_return(response_document)
44
+ end
45
+ end
46
+
47
+ let(:response) do
48
+ Elasticsearch::Model::Response::Response.new(OriginClass, search)
49
+ end
50
+
51
+ let(:response_base) do
52
+ DummyBaseClass.new(OriginClass, response)
53
+ end
54
+
55
+ describe '#klass' do
56
+
57
+ it 'returns the class' do
58
+ expect(response.klass).to be(OriginClass)
59
+ end
60
+ end
61
+
62
+ describe '#response' do
63
+
64
+ it 'returns the response object' do
65
+ expect(response_base.response).to eq(response)
66
+ end
67
+ end
68
+
69
+ describe 'response document' do
70
+
71
+ it 'returns the response document' do
72
+ expect(response_base.response.response).to eq(response_document)
73
+ end
74
+ end
75
+
76
+ describe '#total' do
77
+
78
+ it 'returns the total' do
79
+ expect(response_base.total).to eq(123)
80
+ end
81
+ end
82
+
83
+ describe '#max_score' do
84
+
85
+ it 'returns the total' do
86
+ expect(response_base.max_score).to eq(456)
87
+ end
88
+ end
89
+
90
+ describe '#results' do
91
+
92
+ it 'raises a NotImplemented error' do
93
+ expect {
94
+ response_base.results
95
+ }.to raise_exception(Elasticsearch::Model::NotImplemented)
96
+ end
97
+ end
98
+
99
+ describe '#records' do
100
+
101
+ it 'raises a NotImplemented error' do
102
+ expect {
103
+ response_base.records
104
+ }.to raise_exception(Elasticsearch::Model::NotImplemented)
105
+ end
106
+ end
107
+ end