elasticsearch-persistence-queryable 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (99) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/CHANGELOG.md +16 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE.txt +13 -0
  6. data/README.md +678 -0
  7. data/Rakefile +57 -0
  8. data/elasticsearch-persistence.gemspec +57 -0
  9. data/examples/music/album.rb +34 -0
  10. data/examples/music/artist.rb +50 -0
  11. data/examples/music/artists/_form.html.erb +8 -0
  12. data/examples/music/artists/artists_controller.rb +67 -0
  13. data/examples/music/artists/artists_controller_test.rb +53 -0
  14. data/examples/music/artists/index.html.erb +57 -0
  15. data/examples/music/artists/show.html.erb +51 -0
  16. data/examples/music/assets/application.css +226 -0
  17. data/examples/music/assets/autocomplete.css +48 -0
  18. data/examples/music/assets/blank_cover.png +0 -0
  19. data/examples/music/assets/form.css +113 -0
  20. data/examples/music/index_manager.rb +60 -0
  21. data/examples/music/search/index.html.erb +93 -0
  22. data/examples/music/search/search_controller.rb +41 -0
  23. data/examples/music/search/search_controller_test.rb +9 -0
  24. data/examples/music/search/search_helper.rb +15 -0
  25. data/examples/music/suggester.rb +45 -0
  26. data/examples/music/template.rb +392 -0
  27. data/examples/music/vendor/assets/jquery-ui-1.10.4.custom.min.css +7 -0
  28. data/examples/music/vendor/assets/jquery-ui-1.10.4.custom.min.js +6 -0
  29. data/examples/notes/.gitignore +7 -0
  30. data/examples/notes/Gemfile +28 -0
  31. data/examples/notes/README.markdown +36 -0
  32. data/examples/notes/application.rb +238 -0
  33. data/examples/notes/config.ru +7 -0
  34. data/examples/notes/test.rb +118 -0
  35. data/lib/elasticsearch/per_thread_registry.rb +53 -0
  36. data/lib/elasticsearch/persistence/client.rb +51 -0
  37. data/lib/elasticsearch/persistence/inheritence.rb +9 -0
  38. data/lib/elasticsearch/persistence/model/base.rb +95 -0
  39. data/lib/elasticsearch/persistence/model/callbacks.rb +37 -0
  40. data/lib/elasticsearch/persistence/model/errors.rb +9 -0
  41. data/lib/elasticsearch/persistence/model/find.rb +155 -0
  42. data/lib/elasticsearch/persistence/model/gateway_delegation.rb +23 -0
  43. data/lib/elasticsearch/persistence/model/hash_wrapper.rb +17 -0
  44. data/lib/elasticsearch/persistence/model/rails.rb +39 -0
  45. data/lib/elasticsearch/persistence/model/store.rb +271 -0
  46. data/lib/elasticsearch/persistence/model.rb +148 -0
  47. data/lib/elasticsearch/persistence/null_relation.rb +56 -0
  48. data/lib/elasticsearch/persistence/query_cache.rb +68 -0
  49. data/lib/elasticsearch/persistence/querying.rb +21 -0
  50. data/lib/elasticsearch/persistence/relation/delegation.rb +130 -0
  51. data/lib/elasticsearch/persistence/relation/finder_methods.rb +39 -0
  52. data/lib/elasticsearch/persistence/relation/merger.rb +179 -0
  53. data/lib/elasticsearch/persistence/relation/query_builder.rb +279 -0
  54. data/lib/elasticsearch/persistence/relation/query_methods.rb +362 -0
  55. data/lib/elasticsearch/persistence/relation/search_option_methods.rb +44 -0
  56. data/lib/elasticsearch/persistence/relation/spawn_methods.rb +61 -0
  57. data/lib/elasticsearch/persistence/relation.rb +110 -0
  58. data/lib/elasticsearch/persistence/repository/class.rb +71 -0
  59. data/lib/elasticsearch/persistence/repository/find.rb +73 -0
  60. data/lib/elasticsearch/persistence/repository/naming.rb +115 -0
  61. data/lib/elasticsearch/persistence/repository/response/results.rb +105 -0
  62. data/lib/elasticsearch/persistence/repository/search.rb +156 -0
  63. data/lib/elasticsearch/persistence/repository/serialize.rb +31 -0
  64. data/lib/elasticsearch/persistence/repository/store.rb +94 -0
  65. data/lib/elasticsearch/persistence/repository.rb +77 -0
  66. data/lib/elasticsearch/persistence/scoping/default.rb +137 -0
  67. data/lib/elasticsearch/persistence/scoping/named.rb +70 -0
  68. data/lib/elasticsearch/persistence/scoping.rb +52 -0
  69. data/lib/elasticsearch/persistence/version.rb +5 -0
  70. data/lib/elasticsearch/persistence.rb +157 -0
  71. data/lib/elasticsearch/rails_compatibility.rb +17 -0
  72. data/lib/rails/generators/elasticsearch/model/model_generator.rb +21 -0
  73. data/lib/rails/generators/elasticsearch/model/templates/model.rb.tt +9 -0
  74. data/lib/rails/generators/elasticsearch_generator.rb +2 -0
  75. data/lib/rails/instrumentation/railtie.rb +31 -0
  76. data/lib/rails/instrumentation.rb +10 -0
  77. data/test/integration/model/model_basic_test.rb +157 -0
  78. data/test/integration/repository/custom_class_test.rb +85 -0
  79. data/test/integration/repository/customized_class_test.rb +82 -0
  80. data/test/integration/repository/default_class_test.rb +114 -0
  81. data/test/integration/repository/virtus_model_test.rb +114 -0
  82. data/test/test_helper.rb +53 -0
  83. data/test/unit/model_base_test.rb +48 -0
  84. data/test/unit/model_find_test.rb +148 -0
  85. data/test/unit/model_gateway_test.rb +99 -0
  86. data/test/unit/model_rails_test.rb +88 -0
  87. data/test/unit/model_store_test.rb +514 -0
  88. data/test/unit/persistence_test.rb +32 -0
  89. data/test/unit/repository_class_test.rb +51 -0
  90. data/test/unit/repository_client_test.rb +32 -0
  91. data/test/unit/repository_find_test.rb +388 -0
  92. data/test/unit/repository_indexing_test.rb +37 -0
  93. data/test/unit/repository_module_test.rb +146 -0
  94. data/test/unit/repository_naming_test.rb +146 -0
  95. data/test/unit/repository_response_results_test.rb +98 -0
  96. data/test/unit/repository_search_test.rb +117 -0
  97. data/test/unit/repository_serialize_test.rb +57 -0
  98. data/test/unit/repository_store_test.rb +303 -0
  99. metadata +487 -0
@@ -0,0 +1,388 @@
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
@@ -0,0 +1,37 @@
1
+ require 'test_helper'
2
+
3
+ class Elasticsearch::Persistence::RepositoryIndexingTest < Test::Unit::TestCase
4
+ context "The repository index methods" do
5
+ class MyDocument; end
6
+
7
+ setup do
8
+ @shoulda_subject = Class.new() { include Elasticsearch::Model::Indexing::ClassMethods }.new
9
+ @shoulda_subject.stubs(:index_name).returns('my_index')
10
+ @shoulda_subject.stubs(:document_type).returns('my_document')
11
+ end
12
+
13
+ should "have the convenience index management methods" do
14
+ %w( create_index! delete_index! refresh_index! ).each do |method|
15
+ assert_respond_to subject, method
16
+ end
17
+ end
18
+
19
+ context "mappings" do
20
+ should "configure the mappings for the type" do
21
+ subject.mappings do
22
+ indexes :title
23
+ end
24
+
25
+ assert_equal( {:"my_document"=>{:properties=>{:title=>{:type=>"string"}}}}, subject.mappings.to_hash )
26
+ end
27
+ end
28
+
29
+ context "settings" do
30
+ should "configure the settings for the index" do
31
+ subject.settings foo: 'bar'
32
+ assert_equal( {foo: 'bar'}, subject.settings.to_hash)
33
+ end
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,146 @@
1
+ require 'test_helper'
2
+
3
+ class Elasticsearch::Persistence::RepositoryModuleTest < Test::Unit::TestCase
4
+ context "The repository module" do
5
+
6
+ class DummyModel
7
+ def initialize(attributes={})
8
+ @attributes = attributes
9
+ end
10
+
11
+ def to_hash
12
+ @attributes
13
+ end
14
+
15
+ def inspect
16
+ "<Note #{@attributes.inspect}>"
17
+ end
18
+ end
19
+
20
+ setup do
21
+ class DummyRepository
22
+ include Elasticsearch::Persistence::Repository
23
+ end
24
+ end
25
+
26
+ teardown do
27
+ Elasticsearch::Persistence::RepositoryModuleTest.__send__ :remove_const, :DummyRepository
28
+ end
29
+
30
+ context "when included" do
31
+ should "set up the gateway for the class and instance" do
32
+ assert_respond_to DummyRepository, :gateway
33
+ assert_respond_to DummyRepository.new, :gateway
34
+
35
+ assert_instance_of Elasticsearch::Persistence::Repository::Class, DummyRepository.gateway
36
+ assert_instance_of Elasticsearch::Persistence::Repository::Class, DummyRepository.new.gateway
37
+ end
38
+
39
+ should "proxy repository methods from the class to the gateway" do
40
+ class DummyRepository
41
+ include Elasticsearch::Persistence::Repository
42
+
43
+ index :foobar
44
+ klass DummyModel
45
+ type :my_dummy_model
46
+ mapping { indexes :title, analyzer: 'snowball' }
47
+ end
48
+
49
+ repository = DummyRepository.new
50
+
51
+ assert_equal :foobar, DummyRepository.index
52
+ assert_equal DummyModel, DummyRepository.klass
53
+ assert_equal :my_dummy_model, DummyRepository.type
54
+ assert_equal 'snowball', DummyRepository.mappings.to_hash[:my_dummy_model][:properties][:title][:analyzer]
55
+
56
+ assert_equal :foobar, repository.index
57
+ assert_equal DummyModel, repository.klass
58
+ assert_equal :my_dummy_model, repository.type
59
+ assert_equal 'snowball', repository.mappings.to_hash[:my_dummy_model][:properties][:title][:analyzer]
60
+ end
61
+
62
+ should "correctly delegate to the gateway" do
63
+ repository = DummyRepository.new
64
+ assert_instance_of Method, repository.method(:index)
65
+ end
66
+
67
+ should "proxy repository methods from the instance to the gateway" do
68
+ class DummyRepository
69
+ include Elasticsearch::Persistence::Repository
70
+ end
71
+
72
+ repository = DummyRepository.new
73
+ repository.index :foobar
74
+ repository.klass DummyModel
75
+ repository.type :my_dummy_model
76
+ repository.mapping { indexes :title, analyzer: 'snowball' }
77
+
78
+ assert_equal :foobar, DummyRepository.index
79
+ assert_equal DummyModel, DummyRepository.klass
80
+ assert_equal :my_dummy_model, DummyRepository.type
81
+ assert_equal 'snowball', DummyRepository.mappings.to_hash[:my_dummy_model][:properties][:title][:analyzer]
82
+
83
+ assert_equal :foobar, repository.index
84
+ assert_equal DummyModel, repository.klass
85
+ assert_equal :my_dummy_model, repository.type
86
+ assert_equal 'snowball', repository.mappings.to_hash[:my_dummy_model][:properties][:title][:analyzer]
87
+ end
88
+
89
+ should "allow to define gateway methods in the class definition via block passed to the gateway method" do
90
+ class DummyRepositoryWithGatewaySerialize
91
+ include Elasticsearch::Persistence::Repository
92
+
93
+ gateway do
94
+ def serialize(document)
95
+ 'FAKE!'
96
+ end
97
+ end
98
+ end
99
+
100
+ repository = DummyRepositoryWithGatewaySerialize.new
101
+ repository.client.transport.logger = Logger.new(STDERR)
102
+
103
+ client = mock
104
+ client.expects(:index).with do |arguments|
105
+ assert_equal('xxx', arguments[:id])
106
+ assert_equal('FAKE!', arguments[:body])
107
+ true
108
+ end
109
+ repository.gateway.expects(:client).returns(client)
110
+
111
+ repository.gateway.expects(:__get_id_from_document).returns('xxx')
112
+
113
+ repository.save( id: '123', foo: 'bar' )
114
+ end
115
+ end
116
+
117
+ should "allow to define gateway methods in the class definition via regular method definition" do
118
+ class DummyRepositoryWithDirectSerialize
119
+ include Elasticsearch::Persistence::Repository
120
+
121
+ def serialize(document)
122
+ 'FAKE IN CLASS!'
123
+ end
124
+ end
125
+
126
+ repository = DummyRepositoryWithDirectSerialize.new
127
+ repository.client.transport.logger = Logger.new(STDERR)
128
+
129
+ client = mock
130
+ client.expects(:index).with do |arguments|
131
+ assert_equal('xxx', arguments[:id])
132
+ assert_equal('FAKE IN CLASS!', arguments[:body])
133
+ true
134
+ end
135
+ repository.gateway.expects(:client).returns(client)
136
+
137
+ repository.gateway.expects(:__get_id_from_document).returns('xxx')
138
+
139
+ repository.save( id: '123', foo: 'bar' )
140
+ end
141
+
142
+ should "configure the index name in the shortcut initializer" do
143
+ assert_equal 'repository', Elasticsearch::Persistence::Repository.new.index_name
144
+ end
145
+ end
146
+ end