elasticsearch-persistence 5.1.0 → 6.0.0.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +4 -4
  2. data/.rspec +2 -0
  3. data/Gemfile +9 -0
  4. data/README.md +164 -323
  5. data/Rakefile +8 -8
  6. data/elasticsearch-persistence.gemspec +4 -5
  7. data/lib/elasticsearch/persistence.rb +2 -110
  8. data/lib/elasticsearch/persistence/repository.rb +212 -53
  9. data/lib/elasticsearch/persistence/repository/dsl.rb +94 -0
  10. data/lib/elasticsearch/persistence/repository/find.rb +27 -10
  11. data/lib/elasticsearch/persistence/repository/response/results.rb +17 -5
  12. data/lib/elasticsearch/persistence/repository/search.rb +15 -4
  13. data/lib/elasticsearch/persistence/repository/serialize.rb +65 -7
  14. data/lib/elasticsearch/persistence/repository/store.rb +38 -44
  15. data/lib/elasticsearch/persistence/version.rb +1 -1
  16. data/spec/repository/find_spec.rb +179 -0
  17. data/spec/repository/response/results_spec.rb +105 -0
  18. data/spec/repository/search_spec.rb +181 -0
  19. data/spec/repository/serialize_spec.rb +53 -0
  20. data/spec/repository/store_spec.rb +327 -0
  21. data/spec/repository_spec.rb +716 -0
  22. data/spec/spec_helper.rb +28 -0
  23. metadata +25 -80
  24. data/lib/elasticsearch/persistence/client.rb +0 -51
  25. data/lib/elasticsearch/persistence/model.rb +0 -153
  26. data/lib/elasticsearch/persistence/model/base.rb +0 -87
  27. data/lib/elasticsearch/persistence/model/errors.rb +0 -8
  28. data/lib/elasticsearch/persistence/model/find.rb +0 -180
  29. data/lib/elasticsearch/persistence/model/rails.rb +0 -47
  30. data/lib/elasticsearch/persistence/model/store.rb +0 -254
  31. data/lib/elasticsearch/persistence/model/utils.rb +0 -0
  32. data/lib/elasticsearch/persistence/repository/class.rb +0 -71
  33. data/lib/elasticsearch/persistence/repository/naming.rb +0 -115
  34. data/lib/rails/generators/elasticsearch/model/model_generator.rb +0 -21
  35. data/lib/rails/generators/elasticsearch/model/templates/model.rb.tt +0 -9
  36. data/lib/rails/generators/elasticsearch_generator.rb +0 -2
  37. data/test/integration/model/model_basic_test.rb +0 -238
  38. data/test/integration/repository/custom_class_test.rb +0 -85
  39. data/test/integration/repository/customized_class_test.rb +0 -82
  40. data/test/integration/repository/default_class_test.rb +0 -116
  41. data/test/integration/repository/virtus_model_test.rb +0 -118
  42. data/test/test_helper.rb +0 -55
  43. data/test/unit/model_base_test.rb +0 -72
  44. data/test/unit/model_find_test.rb +0 -153
  45. data/test/unit/model_gateway_test.rb +0 -101
  46. data/test/unit/model_rails_test.rb +0 -112
  47. data/test/unit/model_store_test.rb +0 -576
  48. data/test/unit/persistence_test.rb +0 -32
  49. data/test/unit/repository_class_test.rb +0 -51
  50. data/test/unit/repository_client_test.rb +0 -32
  51. data/test/unit/repository_find_test.rb +0 -388
  52. data/test/unit/repository_indexing_test.rb +0 -37
  53. data/test/unit/repository_module_test.rb +0 -146
  54. data/test/unit/repository_naming_test.rb +0 -146
  55. data/test/unit/repository_response_results_test.rb +0 -98
  56. data/test/unit/repository_search_test.rb +0 -117
  57. data/test/unit/repository_serialize_test.rb +0 -57
  58. data/test/unit/repository_store_test.rb +0 -303
@@ -1,57 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Elasticsearch::Persistence::RepositorySerializeTest < Test::Unit::TestCase
4
- context "The repository serialization" do
5
- class DummyDocument
6
- def to_hash
7
- { foo: 'bar' }
8
- end
9
- end
10
-
11
- class MyDocument; end
12
-
13
- setup do
14
- @shoulda_subject = Class.new() { include Elasticsearch::Persistence::Repository::Serialize }.new
15
- end
16
-
17
- context "serialize" do
18
- should "call #to_hash on passed object" do
19
- document = DummyDocument.new
20
- assert_equal( { foo: 'bar' }, subject.serialize(document))
21
- end
22
- end
23
-
24
- context "deserialize" do
25
- should "get the class name from #klass" do
26
- subject.expects(:klass)
27
- .returns(MyDocument)
28
-
29
- MyDocument.expects(:new)
30
-
31
- subject.deserialize( {} )
32
- end
33
-
34
- should "get the class name from Elasticsearch _type" do
35
- subject.expects(:klass)
36
- .returns(nil)
37
-
38
- subject.expects(:__get_klass_from_type)
39
- .returns(MyDocument)
40
-
41
- MyDocument.expects(:new)
42
-
43
- subject.deserialize( {} )
44
- end
45
-
46
- should "create the class instance with _source attributes" do
47
- subject.expects(:klass).returns(nil)
48
-
49
- subject.expects(:__get_klass_from_type).returns(MyDocument)
50
-
51
- MyDocument.expects(:new).with({ 'foo' => 'bar' })
52
-
53
- subject.deserialize( {'_source' => { 'foo' => 'bar' } } )
54
- end
55
- end
56
- end
57
- end
@@ -1,303 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Elasticsearch::Persistence::RepositoryStoreTest < Test::Unit::TestCase
4
- context "The repository store" do
5
- class MyDocument; end
6
-
7
- setup do
8
- @shoulda_subject = Class.new() do
9
- include Elasticsearch::Persistence::Repository::Store
10
- include Elasticsearch::Persistence::Repository::Naming
11
- end.new
12
- @shoulda_subject.stubs(:index_name).returns('test')
13
- end
14
-
15
- context "save" do
16
- should "serialize the document, get type from klass and index it" do
17
- subject.expects(:serialize).returns({foo: 'bar'})
18
- subject.expects(:document_type).returns(nil)
19
- subject.expects(:klass).at_least_once.returns(MyDocument)
20
- subject.expects(:__get_type_from_class).with(MyDocument).at_least_once.returns('my_document')
21
- subject.expects(:__get_id_from_document).returns('1')
22
-
23
- client = mock
24
- client.expects(:index).with do |arguments|
25
- assert_equal 'my_document', arguments[:type]
26
- assert_equal '1', arguments[:id]
27
- assert_equal({foo: 'bar'}, arguments[:body])
28
- true
29
- end
30
- subject.expects(:client).returns(client)
31
-
32
- subject.save({foo: 'bar'})
33
- end
34
-
35
- should "serialize the document, get type from document class and index it" do
36
- subject.expects(:serialize).returns({foo: 'bar'})
37
- subject.expects(:document_type).returns(nil)
38
- subject.expects(:klass).at_least_once.returns(nil)
39
- subject.expects(:__get_type_from_class).with(MyDocument).returns('my_document')
40
- subject.expects(:__get_id_from_document).returns('1')
41
-
42
- client = mock
43
- client.expects(:index).with do |arguments|
44
- assert_equal 'my_document', arguments[:type]
45
- assert_equal '1', arguments[:id]
46
- assert_equal({foo: 'bar'}, arguments[:body])
47
- true
48
- end
49
- subject.expects(:client).returns(client)
50
-
51
- subject.save(MyDocument.new)
52
- end
53
-
54
- should "serialize the document, get type from document_type and index it" do
55
- subject.expects(:serialize).returns({foo: 'bar'})
56
-
57
- subject.expects(:document_type).returns('my_document')
58
-
59
- subject.expects(:klass).never
60
- subject.expects(:__get_type_from_class).never
61
-
62
- subject.expects(:__get_id_from_document).returns('1')
63
-
64
- client = mock
65
- client.expects(:index).with do |arguments|
66
- assert_equal 'my_document', arguments[:type]
67
- assert_equal '1', arguments[:id]
68
- assert_equal({foo: 'bar'}, arguments[:body])
69
- true
70
- end
71
- subject.expects(:client).returns(client)
72
-
73
- subject.save(MyDocument.new)
74
- end
75
-
76
- should "pass the options to the client" do
77
- subject.expects(:serialize).returns({foo: 'bar'})
78
- subject.expects(:document_type).returns(nil)
79
- subject.expects(:klass).at_least_once.returns(MyDocument)
80
- subject.expects(:__get_type_from_class).with(MyDocument).returns('my_document')
81
- subject.expects(:__get_id_from_document).returns('1')
82
-
83
- client = mock
84
- client.expects(:index).with do |arguments|
85
- assert_equal 'foobarbam', arguments[:index]
86
- assert_equal 'bambam', arguments[:routing]
87
- true
88
- end
89
- subject.expects(:client).returns(client)
90
-
91
- subject.save({foo: 'bar'}, { index: 'foobarbam', routing: 'bambam' })
92
- end
93
- end
94
-
95
- context "update" do
96
- should "get the ID from first argument and :doc from options" do
97
- subject.expects(:serialize).never
98
- subject.expects(:document_type).returns('mydoc')
99
- subject.expects(:__extract_id_from_document).never
100
-
101
- client = mock
102
- client.expects(:update).with do |arguments|
103
- assert_equal '1', arguments[:id]
104
- assert_equal 'mydoc', arguments[:type]
105
- assert_equal({doc: { foo: 'bar' }}, arguments[:body])
106
- true
107
- end
108
- subject.expects(:client).returns(client)
109
-
110
- subject.update('1', doc: { foo: 'bar' })
111
- end
112
-
113
- should "get the ID from first argument and :script from options" do
114
- subject.expects(:document_type).returns('mydoc')
115
- subject.expects(:__extract_id_from_document).never
116
-
117
- client = mock
118
- client.expects(:update).with do |arguments|
119
- assert_equal '1', arguments[:id]
120
- assert_equal 'mydoc', arguments[:type]
121
- assert_equal({script: 'ctx._source.foo += 1'}, arguments[:body])
122
- true
123
- end
124
- subject.expects(:client).returns(client)
125
-
126
- subject.update('1', script: 'ctx._source.foo += 1')
127
- end
128
-
129
- should "get the ID from first argument and :script with :upsert from options" do
130
- subject.expects(:document_type).returns('mydoc')
131
- subject.expects(:__extract_id_from_document).never
132
-
133
- client = mock
134
- client.expects(:update).with do |arguments|
135
- assert_equal '1', arguments[:id]
136
- assert_equal 'mydoc', arguments[:type]
137
- assert_equal({script: 'ctx._source.foo += 1', upsert: { foo: 1 }}, arguments[:body])
138
- true
139
- end
140
- subject.expects(:client).returns(client)
141
-
142
- subject.update('1', script: 'ctx._source.foo += 1', upsert: { foo: 1 })
143
- end
144
-
145
- should "get the ID and :doc from document" do
146
- subject.expects(:document_type).returns('mydoc')
147
-
148
- client = mock
149
- client.expects(:update).with do |arguments|
150
- assert_equal '1', arguments[:id]
151
- assert_equal 'mydoc', arguments[:type]
152
- assert_equal({doc: { foo: 'bar' }}, arguments[:body])
153
- true
154
- end
155
- subject.expects(:client).returns(client)
156
-
157
- subject.update(id: '1', foo: 'bar')
158
- end
159
-
160
- should "get the ID and :script from document" do
161
- subject.expects(:document_type).returns('mydoc')
162
-
163
- client = mock
164
- client.expects(:update).with do |arguments|
165
- assert_equal '1', arguments[:id]
166
- assert_equal 'mydoc', arguments[:type]
167
- assert_equal({script: 'ctx._source.foo += 1'}, arguments[:body])
168
- true
169
- end
170
- subject.expects(:client).returns(client)
171
-
172
- subject.update(id: '1', script: 'ctx._source.foo += 1')
173
- end
174
-
175
- should "get the ID and :script with :upsert from document" do
176
- subject.expects(:document_type).returns('mydoc')
177
-
178
- client = mock
179
- client.expects(:update).with do |arguments|
180
- assert_equal '1', arguments[:id]
181
- assert_equal 'mydoc', arguments[:type]
182
- assert_equal({script: 'ctx._source.foo += 1', upsert: { foo: 1 } }, arguments[:body])
183
- true
184
- end
185
- subject.expects(:client).returns(client)
186
-
187
- subject.update(id: '1', script: 'ctx._source.foo += 1', upsert: { foo: 1 })
188
- end
189
-
190
- should "override the type from params" do
191
- subject.expects(:document_type).never
192
-
193
- client = mock
194
- client.expects(:update).with do |arguments|
195
- assert_equal '1', arguments[:id]
196
- assert_equal 'foo', arguments[:type]
197
- assert_equal({script: 'ctx._source.foo += 1'}, arguments[:body])
198
- true
199
- end
200
- subject.expects(:client).returns(client)
201
-
202
- subject.update(id: '1', script: 'ctx._source.foo += 1', type: 'foo')
203
- end
204
-
205
- should "raise an exception when passed incorrect argument" do
206
- assert_raise(ArgumentError) { subject.update(MyDocument.new, foo: 'bar') }
207
- end
208
- end
209
-
210
- context "delete" do
211
- should "get type from klass when passed only ID" do
212
- subject.expects(:serialize).never
213
- subject.expects(:document_type).returns(nil)
214
- subject.expects(:klass).at_least_once.returns(MyDocument)
215
- subject.expects(:__get_type_from_class).with(MyDocument).returns('my_document')
216
- subject.expects(:__get_id_from_document).never
217
-
218
- client = mock
219
- client.expects(:delete).with do |arguments|
220
- assert_equal 'my_document', arguments[:type]
221
- assert_equal '1', arguments[:id]
222
- true
223
- end
224
- subject.expects(:client).returns(client)
225
-
226
- subject.delete('1')
227
- end
228
-
229
- should "get ID from document and type from klass when passed a document" do
230
- subject.expects(:serialize).returns({id: '1', foo: 'bar'})
231
- subject.expects(:document_type).returns(nil)
232
- subject.expects(:klass).at_least_once.returns(MyDocument)
233
- subject.expects(:__get_type_from_class).with(MyDocument).returns('my_document')
234
- subject.expects(:__get_id_from_document).with({id: '1', foo: 'bar'}).returns('1')
235
-
236
- client = mock
237
- client.expects(:delete).with do |arguments|
238
- assert_equal 'my_document', arguments[:type]
239
- assert_equal '1', arguments[:id]
240
- true
241
- end
242
- subject.expects(:client).returns(client)
243
-
244
- subject.delete({id: '1', foo: 'bar'})
245
- end
246
-
247
- should "get ID from document and type from document_type when passed a document" do
248
- subject.expects(:serialize).returns({id: '1', foo: 'bar'})
249
-
250
- subject.expects(:document_type).returns('my_document')
251
-
252
- subject.expects(:klass).never
253
- subject.expects(:__get_type_from_class).never
254
-
255
- subject.expects(:__get_id_from_document).with({id: '1', foo: 'bar'}).returns('1')
256
-
257
- client = mock
258
- client.expects(:delete).with do |arguments|
259
- assert_equal 'my_document', arguments[:type]
260
- assert_equal '1', arguments[:id]
261
- true
262
- end
263
- subject.expects(:client).returns(client)
264
-
265
- subject.delete({id: '1', foo: 'bar'})
266
- end
267
-
268
- should "get ID and type from document when passed a document" do
269
- subject.expects(:serialize).returns({id: '1', foo: 'bar'})
270
- subject.expects(:document_type).returns(nil)
271
- subject.expects(:klass).at_least_once.returns(nil)
272
- subject.expects(:__get_type_from_class).with(MyDocument).returns('my_document')
273
- subject.expects(:__get_id_from_document).with({id: '1', foo: 'bar'}).returns('1')
274
-
275
- client = mock
276
- client.expects(:delete).with do |arguments|
277
- assert_equal 'my_document', arguments[:type]
278
- assert_equal '1', arguments[:id]
279
- true
280
- end
281
- subject.expects(:client).returns(client)
282
-
283
- subject.delete(MyDocument.new)
284
- end
285
-
286
- should "pass the options to the client" do
287
- subject.expects(:document_type).returns(nil)
288
- subject.expects(:klass).at_least_once.returns(MyDocument)
289
- subject.expects(:__get_type_from_class).returns('my_document')
290
-
291
- client = mock
292
- client.expects(:delete).with do |arguments|
293
- assert_equal 'foobarbam', arguments[:index]
294
- assert_equal 'bambam', arguments[:routing]
295
- true
296
- end
297
- subject.expects(:client).returns(client)
298
-
299
- subject.delete('1', index: 'foobarbam', routing: 'bambam')
300
- end
301
- end
302
- end
303
- end