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,303 @@
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