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,32 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Elasticsearch::Persistence::ModuleTest < Test::Unit::TestCase
4
- context "The Persistence module" do
5
-
6
- context "client" do
7
- should "have a default client" do
8
- client = Elasticsearch::Persistence.client
9
- assert_not_nil client
10
- assert_instance_of Elasticsearch::Transport::Client, client
11
- end
12
-
13
- should "allow to set a client" do
14
- begin
15
- Elasticsearch::Persistence.client = "Foobar"
16
- assert_equal "Foobar", Elasticsearch::Persistence.client
17
- ensure
18
- Elasticsearch::Persistence.client = nil
19
- end
20
- end
21
-
22
- should "allow to set a client with DSL" do
23
- begin
24
- Elasticsearch::Persistence.client "Foobar"
25
- assert_equal "Foobar", Elasticsearch::Persistence.client
26
- ensure
27
- Elasticsearch::Persistence.client = nil
28
- end
29
- end
30
- end
31
- end
32
- end
@@ -1,51 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Elasticsearch::Persistence::RepositoryClassTest < Test::Unit::TestCase
4
- context "The default repository class" do
5
-
6
- context "when initialized" do
7
- should "be created from the module" do
8
- repository = Elasticsearch::Persistence::Repository.new
9
- assert_instance_of Elasticsearch::Persistence::Repository::Class, repository
10
- end
11
-
12
- should "store and access the options" do
13
- repository = Elasticsearch::Persistence::Repository::Class.new foo: 'bar'
14
- assert_equal 'bar', repository.options[:foo]
15
- end
16
-
17
- should "instance eval a passed block" do
18
- $foo = 100
19
- repository = Elasticsearch::Persistence::Repository::Class.new() { $foo += 1 }
20
- assert_equal 101, $foo
21
- end
22
-
23
- should "call a passed block with self" do
24
- foo = 100
25
- repository = Elasticsearch::Persistence::Repository::Class.new do |r|
26
- assert_instance_of Elasticsearch::Persistence::Repository::Class, r
27
- foo += 1
28
- end
29
- assert_equal 101, foo
30
- end
31
-
32
- should "configure the index name based on options" do
33
- repository = Elasticsearch::Persistence::Repository::Class.new index: 'foobar'
34
- assert_equal 'foobar', repository.index_name
35
- end
36
- end
37
-
38
- should "include the repository methods" do
39
- repository = Elasticsearch::Persistence::Repository::Class.new
40
-
41
- %w( index_name document_type klass
42
- mappings settings client client=
43
- create_index! delete_index! refresh_index!
44
- save delete serialize deserialize
45
- exists? find search ).each do |method|
46
- assert_respond_to repository, method
47
- end
48
- end
49
-
50
- end
51
- end
@@ -1,32 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Elasticsearch::Persistence::RepositoryClientTest < Test::Unit::TestCase
4
- context "The repository client" do
5
- setup do
6
- @shoulda_subject = Class.new() { include Elasticsearch::Persistence::Repository::Client }.new
7
- end
8
-
9
- should "have a default client" do
10
- assert_not_nil subject.client
11
- assert_instance_of Elasticsearch::Transport::Client, subject.client
12
- end
13
-
14
- should "allow to set a client" do
15
- begin
16
- subject.client = "Foobar"
17
- assert_equal "Foobar", subject.client
18
- ensure
19
- subject.client = nil
20
- end
21
- end
22
-
23
- should "allow to set the client with DSL" do
24
- begin
25
- subject.client "Foobar"
26
- assert_equal "Foobar", subject.client
27
- ensure
28
- subject.client = nil
29
- end
30
- end
31
- end
32
- end
@@ -1,388 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Elasticsearch::Persistence::RepositoryFindTest < Test::Unit::TestCase
4
- class MyDocument; end
5
-
6
- context "The repository" do
7
- setup do
8
- @shoulda_subject = Class.new() { include Elasticsearch::Persistence::Repository::Find }.new
9
-
10
- @client = mock
11
- @shoulda_subject.stubs(:document_type).returns(nil)
12
- @shoulda_subject.stubs(:klass).returns(nil)
13
- @shoulda_subject.stubs(:index_name).returns('my_index')
14
- @shoulda_subject.stubs(:client).returns(@client)
15
- end
16
-
17
- context "find method" do
18
- should "find one document when passed a single, literal ID" do
19
- subject.expects(:__find_one).with(1, {})
20
- subject.find(1)
21
- end
22
-
23
- should "find multiple documents when passed multiple IDs" do
24
- subject.expects(:__find_many).with([1, 2], {})
25
- subject.find(1, 2)
26
- end
27
-
28
- should "find multiple documents when passed an array of IDs" do
29
- subject.expects(:__find_many).with([1, 2], {})
30
- subject.find([1, 2])
31
- end
32
-
33
- should "pass the options" do
34
- subject.expects(:__find_one).with(1, { foo: 'bar' })
35
- subject.find(1, foo: 'bar')
36
-
37
- subject.expects(:__find_many).with([1, 2], { foo: 'bar' })
38
- subject.find([1, 2], foo: 'bar')
39
-
40
- subject.expects(:__find_many).with([1, 2], { foo: 'bar' })
41
- subject.find(1, 2, foo: 'bar')
42
- end
43
- end
44
-
45
- context "'exists?' method" do
46
- should "return false when the document does not exist" do
47
- @client.expects(:exists).returns(false)
48
- assert_equal false, subject.exists?('1')
49
- end
50
-
51
- should "return whether document for klass exists" do
52
- subject.expects(:document_type).returns(nil)
53
- subject.expects(:klass).returns(MyDocument).at_least_once
54
- subject.expects(:__get_type_from_class).with(MyDocument).returns('my_document')
55
-
56
- @client
57
- .expects(:exists)
58
- .with do |arguments|
59
- assert_equal 'my_document', arguments[:type]
60
- assert_equal '1', arguments[:id]
61
- true
62
- end
63
- .returns(true)
64
-
65
- assert_equal true, subject.exists?('1')
66
- end
67
-
68
- should "return whether document for document_type exists" do
69
- subject.expects(:document_type).returns('my_document')
70
- subject.expects(:klass).returns(MyDocument).at_most_once
71
- subject.expects(:__get_type_from_class).never
72
-
73
- @client
74
- .expects(:exists)
75
- .with do |arguments|
76
- assert_equal 'my_document', arguments[:type]
77
- assert_equal '1', arguments[:id]
78
- true
79
- end
80
- .returns(true)
81
-
82
- assert_equal true, subject.exists?('1')
83
- end
84
-
85
- should "return whether document exists" do
86
- subject.expects(:klass).returns(nil)
87
- subject.expects(:__get_type_from_class).never
88
-
89
- @client
90
- .expects(:exists)
91
- .with do |arguments|
92
- assert_equal '_all', arguments[:type]
93
- assert_equal '1', arguments[:id]
94
- true
95
- end
96
- .returns(true)
97
-
98
- assert_equal true, subject.exists?('1')
99
- end
100
-
101
- should "pass options to the client" do
102
- @client.expects(:exists).with do |arguments|
103
- assert_equal 'foobarbam', arguments[:index]
104
- assert_equal 'bambam', arguments[:routing]
105
- true
106
- end
107
-
108
- subject.exists? '1', index: 'foobarbam', routing: 'bambam'
109
- end
110
- end
111
-
112
- context "'__find_one' method" do
113
- should "find document based on klass and return a deserialized object" do
114
- subject.expects(:document_type).returns(nil)
115
- subject.expects(:klass).returns(MyDocument).at_least_once
116
- subject.expects(:__get_type_from_class).with(MyDocument).returns('my_document')
117
-
118
- subject.expects(:deserialize).with({'_source' => {'foo' => 'bar'}}).returns(MyDocument.new)
119
-
120
- @client
121
- .expects(:get)
122
- .with do |arguments|
123
- assert_equal 'my_document', arguments[:type]
124
- assert_equal '1', arguments[:id]
125
- true
126
- end
127
- .returns({'_source' => { 'foo' => 'bar' }})
128
-
129
- assert_instance_of MyDocument, subject.__find_one('1')
130
- end
131
-
132
- should "find document based on document_type and return a deserialized object" do
133
- subject.expects(:document_type).returns('my_document')
134
- subject.expects(:klass).returns(MyDocument).at_most_once
135
- subject.expects(:__get_type_from_class).never
136
-
137
- subject.expects(:deserialize).with({'_source' => {'foo' => 'bar'}}).returns(MyDocument.new)
138
-
139
- @client
140
- .expects(:get)
141
- .with do |arguments|
142
- assert_equal 'my_document', arguments[:type]
143
- assert_equal '1', arguments[:id]
144
- true
145
- end
146
- .returns({'_source' => { 'foo' => 'bar' }})
147
-
148
- assert_instance_of MyDocument, subject.__find_one('1')
149
- end
150
-
151
- should "find document and return a deserialized object" do
152
- subject.expects(:document_type).returns(nil)
153
- subject.expects(:klass).returns(nil).at_least_once
154
- subject.expects(:__get_type_from_class).never
155
-
156
- subject.expects(:deserialize).with({'_source' => {'foo' => 'bar'}}).returns(MyDocument.new)
157
-
158
- @client
159
- .expects(:get)
160
- .with do |arguments|
161
- assert_equal '_all', arguments[:type]
162
- assert_equal '1', arguments[:id]
163
- true
164
- end
165
- .returns({'_source' => { 'foo' => 'bar' }})
166
-
167
- assert_instance_of MyDocument, subject.__find_one('1')
168
- end
169
-
170
- should "raise DocumentNotFound exception when the document cannot be found" do
171
- subject.expects(:document_type).returns(nil)
172
- subject.expects(:klass).returns(nil).at_least_once
173
-
174
- subject.expects(:deserialize).never
175
-
176
- @client
177
- .expects(:get)
178
- .raises(Elasticsearch::Transport::Transport::Errors::NotFound)
179
-
180
- assert_raise Elasticsearch::Persistence::Repository::DocumentNotFound do
181
- subject.__find_one('foobar')
182
- end
183
- end
184
-
185
- should "pass other exceptions" do
186
- subject.expects(:klass).returns(nil).at_least_once
187
-
188
- subject.expects(:deserialize).never
189
-
190
- @client
191
- .expects(:get)
192
- .raises(RuntimeError)
193
-
194
- assert_raise RuntimeError do
195
- subject.__find_one('foobar')
196
- end
197
- end
198
-
199
- should "pass options to the client" do
200
- subject.expects(:klass).returns(nil).at_least_once
201
- subject.expects(:deserialize)
202
-
203
- @client
204
- .expects(:get)
205
- .with do |arguments|
206
- assert_equal 'foobarbam', arguments[:index]
207
- assert_equal 'bambam', arguments[:routing]
208
- true
209
- end
210
- .returns({'_source' => { 'foo' => 'bar' }})
211
-
212
- subject.__find_one '1', index: 'foobarbam', routing: 'bambam'
213
- end
214
- end
215
-
216
- context "'__find_many' method" do
217
- setup do
218
- @response = {"docs"=>
219
- [ {"_index"=>"my_index",
220
- "_type"=>"note",
221
- "_id"=>"1",
222
- "_version"=>1,
223
- "found"=>true,
224
- "_source"=>{"id"=>"1", "title"=>"Test 1"}},
225
-
226
- {"_index"=>"my_index",
227
- "_type"=>"note",
228
- "_id"=>"2",
229
- "_version"=>1,
230
- "found"=>true,
231
- "_source"=>{"id"=>"2", "title"=>"Test 2"}}
232
- ]}
233
- end
234
-
235
- should "find documents based on klass and return an Array of deserialized objects" do
236
- subject.expects(:document_type).returns(nil)
237
- subject.expects(:klass).returns(MyDocument).at_least_once
238
- subject.expects(:__get_type_from_class).with(MyDocument).returns('my_document')
239
-
240
- subject
241
- .expects(:deserialize)
242
- .with(@response['docs'][0])
243
- .returns(MyDocument.new)
244
-
245
- subject
246
- .expects(:deserialize)
247
- .with(@response['docs'][1])
248
- .returns(MyDocument.new)
249
-
250
- @client
251
- .expects(:mget)
252
- .with do |arguments|
253
- assert_equal 'my_document', arguments[:type]
254
- assert_equal ['1', '2'], arguments[:body][:ids]
255
- true
256
- end
257
- .returns(@response)
258
-
259
- results = subject.__find_many(['1', '2'])
260
- assert_instance_of MyDocument, results[0]
261
- assert_instance_of MyDocument, results[1]
262
- end
263
-
264
- should "find documents based on document_type and return an Array of deserialized objects" do
265
- subject.expects(:document_type).returns('my_document')
266
- subject.expects(:klass).returns(MyDocument).at_most_once
267
- subject.expects(:__get_type_from_class).never
268
-
269
- subject.expects(:deserialize).twice
270
-
271
- @client
272
- .expects(:mget)
273
- .with do |arguments|
274
- assert_equal 'my_document', arguments[:type]
275
- assert_equal ['1', '2'], arguments[:body][:ids]
276
- true
277
- end
278
- .returns(@response)
279
-
280
- subject.__find_many(['1', '2'])
281
- end
282
-
283
- should "find documents and return an Array of deserialized objects" do
284
- subject.expects(:document_type).returns(nil)
285
- subject.expects(:klass).returns(nil).at_least_once
286
- subject.expects(:__get_type_from_class).never
287
-
288
- subject
289
- .expects(:deserialize)
290
- .with(@response['docs'][0])
291
- .returns(MyDocument.new)
292
-
293
- subject
294
- .expects(:deserialize)
295
- .with(@response['docs'][1])
296
- .returns(MyDocument.new)
297
-
298
- @client
299
- .expects(:mget)
300
- .with do |arguments|
301
- assert_equal '_all', arguments[:type]
302
- assert_equal ['1', '2'], arguments[:body][:ids]
303
- true
304
- end
305
- .returns(@response)
306
-
307
- results = subject.__find_many(['1', '2'])
308
-
309
- assert_equal 2, results.size
310
-
311
- assert_instance_of MyDocument, results[0]
312
- assert_instance_of MyDocument, results[1]
313
- end
314
-
315
- should "find keep missing documents in the result as nil" do
316
- @response = {"docs"=>
317
- [ {"_index"=>"my_index",
318
- "_type"=>"note",
319
- "_id"=>"1",
320
- "_version"=>1,
321
- "found"=>true,
322
- "_source"=>{"id"=>"1", "title"=>"Test 1"}},
323
-
324
- {"_index"=>"my_index",
325
- "_type"=>"note",
326
- "_id"=>"3",
327
- "_version"=>1,
328
- "found"=>false},
329
-
330
- {"_index"=>"my_index",
331
- "_type"=>"note",
332
- "_id"=>"2",
333
- "_version"=>1,
334
- "found"=>true,
335
- "_source"=>{"id"=>"2", "title"=>"Test 2"}}
336
- ]}
337
-
338
- subject.expects(:document_type).returns(nil)
339
- subject.expects(:klass).returns(MyDocument).at_least_once
340
- subject.expects(:__get_type_from_class).with(MyDocument).returns('my_document')
341
-
342
- subject
343
- .expects(:deserialize)
344
- .with(@response['docs'][0])
345
- .returns(MyDocument.new)
346
-
347
- subject
348
- .expects(:deserialize)
349
- .with(@response['docs'][2])
350
- .returns(MyDocument.new)
351
-
352
- @client
353
- .expects(:mget)
354
- .with do |arguments|
355
- assert_equal 'my_document', arguments[:type]
356
- assert_equal ['1', '3', '2'], arguments[:body][:ids]
357
- true
358
- end
359
- .returns(@response)
360
-
361
- results = subject.__find_many(['1', '3', '2'])
362
-
363
- assert_equal 3, results.size
364
-
365
- assert_instance_of MyDocument, results[0]
366
- assert_instance_of NilClass, results[1]
367
- assert_instance_of MyDocument, results[2]
368
- end
369
-
370
- should "pass options to the client" do
371
- subject.expects(:klass).returns(nil).at_least_once
372
- subject.expects(:deserialize).twice
373
-
374
- @client
375
- .expects(:mget)
376
- .with do |arguments|
377
- assert_equal 'foobarbam', arguments[:index]
378
- assert_equal 'bambam', arguments[:routing]
379
- true
380
- end
381
- .returns(@response)
382
-
383
- subject.__find_many ['1', '2'], index: 'foobarbam', routing: 'bambam'
384
- end
385
- end
386
-
387
- end
388
- end