ssickles-tire 0.4.2 → 0.4.2.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. data/lib/tire.rb +3 -18
  2. data/lib/tire/alias.rb +35 -11
  3. data/lib/tire/index.rb +76 -34
  4. data/lib/tire/results/collection.rb +13 -38
  5. data/lib/tire/results/item.rb +0 -19
  6. data/lib/tire/rubyext/to_json.rb +21 -0
  7. data/lib/tire/search.rb +8 -7
  8. data/lib/tire/search/scan.rb +8 -8
  9. data/lib/tire/search/sort.rb +1 -1
  10. data/lib/tire/version.rb +38 -7
  11. data/test/integration/reindex_test.rb +2 -2
  12. data/test/integration/scan_test.rb +1 -1
  13. data/test/test_helper.rb +3 -27
  14. data/test/unit/index_alias_test.rb +17 -3
  15. data/test/unit/index_test.rb +18 -30
  16. data/test/unit/results_collection_test.rb +0 -60
  17. data/test/unit/results_item_test.rb +0 -37
  18. data/test/unit/rubyext_test.rb +3 -3
  19. data/test/unit/search_test.rb +6 -1
  20. data/test/unit/tire_test.rb +0 -15
  21. data/tire.gemspec +13 -30
  22. metadata +41 -153
  23. data/lib/tire/model/callbacks.rb +0 -40
  24. data/lib/tire/model/import.rb +0 -26
  25. data/lib/tire/model/indexing.rb +0 -128
  26. data/lib/tire/model/naming.rb +0 -100
  27. data/lib/tire/model/percolate.rb +0 -99
  28. data/lib/tire/model/persistence.rb +0 -72
  29. data/lib/tire/model/persistence/attributes.rb +0 -143
  30. data/lib/tire/model/persistence/finders.rb +0 -66
  31. data/lib/tire/model/persistence/storage.rb +0 -71
  32. data/lib/tire/model/search.rb +0 -305
  33. data/lib/tire/rubyext/hash.rb +0 -8
  34. data/lib/tire/rubyext/ruby_1_8.rb +0 -54
  35. data/lib/tire/rubyext/symbol.rb +0 -11
  36. data/lib/tire/utils.rb +0 -17
  37. data/test/integration/active_model_indexing_test.rb +0 -51
  38. data/test/integration/active_model_searchable_test.rb +0 -114
  39. data/test/integration/active_record_searchable_test.rb +0 -446
  40. data/test/integration/mongoid_searchable_test.rb +0 -309
  41. data/test/integration/persistent_model_test.rb +0 -117
  42. data/test/models/active_model_article.rb +0 -31
  43. data/test/models/active_model_article_with_callbacks.rb +0 -49
  44. data/test/models/active_model_article_with_custom_document_type.rb +0 -7
  45. data/test/models/active_model_article_with_custom_index_name.rb +0 -7
  46. data/test/models/active_record_models.rb +0 -122
  47. data/test/models/mongoid_models.rb +0 -97
  48. data/test/models/persistent_article.rb +0 -11
  49. data/test/models/persistent_article_in_namespace.rb +0 -12
  50. data/test/models/persistent_article_with_casting.rb +0 -28
  51. data/test/models/persistent_article_with_defaults.rb +0 -11
  52. data/test/models/persistent_articles_with_custom_index_name.rb +0 -10
  53. data/test/models/supermodel_article.rb +0 -17
  54. data/test/models/validated_model.rb +0 -11
  55. data/test/unit/active_model_lint_test.rb +0 -17
  56. data/test/unit/model_callbacks_test.rb +0 -116
  57. data/test/unit/model_import_test.rb +0 -71
  58. data/test/unit/model_persistence_test.rb +0 -516
  59. data/test/unit/model_search_test.rb +0 -899
@@ -1,71 +0,0 @@
1
- require 'test_helper'
2
-
3
- class ImportModel
4
- extend ActiveModel::Naming
5
- include Tire::Model::Search
6
- include Tire::Model::Callbacks
7
-
8
- DATA = (1..4).to_a
9
-
10
- def self.paginate(options={})
11
- options = {:page => 1, :per_page => 1000}.update options
12
- DATA.slice( (options[:page]-1)*options[:per_page]...options[:page]*options[:per_page] )
13
- end
14
-
15
- def self.all(options={})
16
- DATA
17
- end
18
-
19
- def self.count
20
- DATA.size
21
- end
22
- end
23
-
24
- module Tire
25
- module Model
26
-
27
- class ImportTest < Test::Unit::TestCase
28
-
29
- context "Model::Import" do
30
-
31
- should "have the import method" do
32
- assert_respond_to ImportModel, :import
33
- end
34
-
35
- should "paginate the results by default when importing" do
36
- Tire::Index.any_instance.expects(:bulk_store).returns(true).times(2)
37
-
38
- ImportModel.import :per_page => 2
39
- end
40
-
41
- should "call the passed block on every batch, and NOT manipulate the documents array" do
42
- Tire::Index.any_instance.expects(:bulk_store).with { |c,o| c == [1, 2] }
43
- Tire::Index.any_instance.expects(:bulk_store).with { |c,o| c == [3, 4] }
44
-
45
- runs = 0
46
- ImportModel.import :per_page => 2 do |documents|
47
- runs += 1
48
- # Don't forget to return the documents at the end of the block
49
- documents
50
- end
51
-
52
- assert_equal 2, runs
53
- end
54
-
55
- should "manipulate the documents in passed block" do
56
- Tire::Index.any_instance.expects(:bulk_store).with { |c,o| c == [2, 3] }
57
- Tire::Index.any_instance.expects(:bulk_store).with { |c,o| c == [4, 5] }
58
-
59
- ImportModel.import :per_page => 2 do |documents|
60
- # Add 1 to every "document" and return them
61
- documents.map { |d| d + 1 }
62
- end
63
-
64
- end
65
-
66
- end
67
-
68
- end
69
-
70
- end
71
- end
@@ -1,516 +0,0 @@
1
- require 'test_helper'
2
-
3
- module Tire
4
- module Model
5
-
6
- class PersistenceTest < Test::Unit::TestCase
7
-
8
- context "Model" do
9
-
10
- should "have default index name" do
11
- assert_equal 'persistent_articles', PersistentArticle.index_name
12
- assert_equal 'persistent_articles', PersistentArticle.new(:title => 'Test').index_name
13
- end
14
-
15
- should "allow to set custom index name" do
16
- assert_equal 'custom-index-name', PersistentArticleWithCustomIndexName.index_name
17
-
18
- PersistentArticleWithCustomIndexName.index_name "another-index-name"
19
- assert_equal 'another-index-name', PersistentArticleWithCustomIndexName.index_name
20
- assert_equal 'another-index-name', PersistentArticleWithCustomIndexName.index.name
21
- end
22
-
23
- context "with index prefix" do
24
- setup do
25
- Model::Search.index_prefix 'prefix'
26
- end
27
-
28
- teardown do
29
- Model::Search.index_prefix nil
30
- end
31
-
32
- should "have configured prefix in index_name" do
33
- assert_equal 'prefix_persistent_articles', PersistentArticle.index_name
34
- assert_equal 'prefix_persistent_articles', PersistentArticle.new(:title => 'Test').index_name
35
- end
36
-
37
- end
38
-
39
- should "have document_type" do
40
- assert_equal 'persistent_article', PersistentArticle.document_type
41
- assert_equal 'persistent_article', PersistentArticle.new(:title => 'Test').document_type
42
- end
43
-
44
- should "allow to define property" do
45
- assert_nothing_raised do
46
- a = PersistentArticle.new
47
- class << a
48
- property :status
49
- end
50
- end
51
- end
52
-
53
- end
54
-
55
- context "Finders" do
56
-
57
- setup do
58
- @first = { '_id' => 1, '_version' => 1, '_index' => 'persistent_articles', '_type' => 'persistent_article', '_source' => { :title => 'First' } }
59
- @second = { '_id' => 2, '_index' => 'persistent_articles', '_type' => 'persistent_article', '_source' => { :title => 'Second' } }
60
- @third = { '_id' => 3, '_index' => 'persistent_articles', '_type' => 'persistent_article', '_source' => { :title => 'Third' } }
61
- @find_all = { 'hits' => { 'hits' => [
62
- @first,
63
- @second,
64
- @third
65
- ] } }
66
- @find_first = { 'hits' => { 'hits' => [ @first ] } }
67
- @find_last_two = { 'hits' => { 'hits' => [ @second, @third ] } }
68
- @find_twenty_ids = { 'hits' => { 'hits' => 20.times.map { @first } } }
69
- end
70
-
71
- should "find document by numeric ID" do
72
- Configuration.client.expects(:get).returns(mock_response(@first.to_json))
73
- document = PersistentArticle.find 1
74
-
75
- assert_instance_of PersistentArticle, document
76
- assert_equal 1, document.id
77
- assert_equal 1, document.attributes['id']
78
- assert_equal 'First', document.attributes['title']
79
- assert_equal 'First', document.title
80
- end
81
-
82
- should "have _type, _index, _id, _version attributes" do
83
- Configuration.client.expects(:get).returns(mock_response(@first.to_json))
84
- document = PersistentArticle.find 1
85
-
86
- assert_instance_of PersistentArticle, document
87
- assert_equal 1, document.id
88
- assert_equal 1, document.attributes['id']
89
- assert_equal 'persistent_articles', document._index
90
- assert_equal 'persistent_article', document._type
91
- assert_equal 1, document._version
92
- end
93
-
94
- should "find document by string ID" do
95
- Configuration.client.expects(:get).returns(mock_response(@first.to_json))
96
- document = PersistentArticle.find '1'
97
-
98
- assert_instance_of PersistentArticle, document
99
- assert_equal 1, document.id
100
- assert_equal 1, document.attributes['id']
101
- assert_equal 'First', document.attributes['title']
102
- assert_equal 'First', document.title
103
- end
104
-
105
- should "find document by list of IDs" do
106
- Configuration.client.expects(:get).returns(mock_response(@find_last_two.to_json))
107
- documents = PersistentArticle.find 2, 3
108
-
109
- assert_equal 2, documents.count
110
- end
111
-
112
- should "find document by array of IDs" do
113
- Configuration.client.expects(:get).returns(mock_response(@find_last_two.to_json))
114
- documents = PersistentArticle.find [2, 3]
115
-
116
- assert_equal 2, documents.count
117
- end
118
-
119
- should "find all documents listed in IDs array" do
120
- ids = (1..20).to_a
121
- Configuration.client.expects(:get).returns(mock_response(@find_twenty_ids.to_json))
122
- Tire::Search::Search.any_instance.expects(:size).with(ids.size)
123
-
124
- documents = PersistentArticle.find ids
125
- assert_equal ids.size, documents.count
126
- end
127
-
128
- should "find all documents" do
129
- Configuration.client.stubs(:get).returns(mock_response(@find_all.to_json))
130
- documents = PersistentArticle.all
131
-
132
- assert_equal 3, documents.count
133
- assert_equal 'First', documents.first.attributes['title']
134
- assert_equal PersistentArticle.find(:all).map { |e| e.id }, PersistentArticle.all.map { |e| e.id }
135
- end
136
-
137
- should "find first document" do
138
- Configuration.client.expects(:get).returns(mock_response(@find_first.to_json))
139
- document = PersistentArticle.first
140
-
141
- assert_equal 'First', document.attributes['title']
142
- end
143
-
144
- should "raise error when passing incorrect argument" do
145
- assert_raise(ArgumentError) do
146
- PersistentArticle.find :name => 'Test'
147
- end
148
- end
149
-
150
- should_eventually "raise error when document is not found" do
151
- assert_raise(DocumentNotFound) do
152
- PersistentArticle.find 'xyz001'
153
- end
154
- end
155
-
156
- end
157
-
158
- context "Persistent model" do
159
-
160
- setup { @article = PersistentArticle.new :title => 'Test', :tags => [:one, :two] }
161
-
162
- context "attribute methods" do
163
-
164
- should "allow to set attributes on initialization" do
165
- assert_not_nil @article.attributes
166
- assert_equal 'Test', @article.attributes['title']
167
- end
168
-
169
- should "allow to leave attributes blank on initialization" do
170
- assert_nothing_raised { PersistentArticle.new }
171
- end
172
-
173
- should "have getter methods for attributes" do
174
- assert_not_nil @article.title
175
- assert_equal 'Test', @article.title
176
- assert_equal [:one, :two], @article.tags
177
- end
178
-
179
- should "have getter methods for attribute passed as a String" do
180
- article = PersistentArticle.new 'title' => 'Tony Montana'
181
- assert_not_nil article.title
182
- assert_equal 'Tony Montana', article.title
183
- end
184
-
185
- should "raise error when getting unknown attribute" do
186
- assert_raise(NoMethodError) do
187
- @article.krapulitz
188
- end
189
- end
190
-
191
- should "not raise error when getting unset attribute" do
192
- article = PersistentArticle.new :title => 'Test'
193
-
194
- assert_nothing_raised { article.published_on }
195
- assert_nil article.published_on
196
- end
197
-
198
- should "return default value for attribute" do
199
- article = PersistentArticleWithDefaults.new :title => 'Test'
200
- assert_equal [], article.tags
201
- assert_equal false, article.hidden
202
- end
203
-
204
- should "not affect default value" do
205
- article = PersistentArticleWithDefaults.new :title => 'Test'
206
- article.tags << "ruby"
207
-
208
- article.options[:switches] << "switch_1"
209
-
210
- assert_equal [], PersistentArticleWithDefaults.new.tags
211
- assert_equal [], PersistentArticleWithDefaults.new.options[:switches]
212
- end
213
-
214
- should "have query method for attribute" do
215
- assert_equal true, @article.title?
216
- end
217
-
218
- should "raise error when querying for unknown attribute" do
219
- assert_raise(NoMethodError) do
220
- @article.krapulitz?
221
- end
222
- end
223
-
224
- should "not raise error when querying for unset attribute" do
225
- article = PersistentArticle.new :title => 'Test'
226
-
227
- assert_nothing_raised { article.published_on? }
228
- assert ! article.published_on?
229
- end
230
-
231
- should "return true for respond_to? calls for set attributes" do
232
- article = PersistentArticle.new :title => 'Test'
233
- assert article.respond_to?(:title)
234
- end
235
-
236
- should "return false for respond_to? calls for unknown attributes" do
237
- article = PersistentArticle.new :title => 'Test'
238
- assert ! article.respond_to?(:krapulitz)
239
- end
240
-
241
- should "return true for respond_to? calls for defined but unset attributes" do
242
- article = PersistentArticle.new :title => 'Test'
243
-
244
- assert article.respond_to?(:published_on)
245
- end
246
-
247
- should "have attribute names" do
248
- article = PersistentArticle.new :title => 'Test', :tags => ['one', 'two']
249
- assert_equal ['published_on', 'tags', 'title'], article.attribute_names
250
- end
251
-
252
- should "have setter method for attribute" do
253
- @article.title = 'Updated'
254
- assert_equal 'Updated', @article.title
255
- assert_equal 'Updated', @article.attributes['title']
256
- end
257
-
258
- should_eventually "allow to set deeply nested attributes on initialization" do
259
- article = PersistentArticle.new :title => 'Test', :author => { :first_name => 'John', :last_name => 'Smith' }
260
-
261
- assert_equal 'John', article.author.first_name
262
- assert_equal 'Smith', article.author.last_name
263
- assert_equal({ :first_name => 'John', :last_name => 'Smith' }, article.attributes['author'])
264
- end
265
-
266
- should_eventually "allow to set deeply nested attributes on update" do
267
- article = PersistentArticle.new :title => 'Test', :author => { :first_name => 'John', :last_name => 'Smith' }
268
-
269
- article.author.first_name = 'Robert'
270
- article.author.last_name = 'Carpenter'
271
-
272
- assert_equal 'Robert', article.author.first_name
273
- assert_equal 'Carpenter', article.author.last_name
274
- assert_equal({ :first_name => 'Robert', :last_name => 'Carpenter' }, article.attributes['author'])
275
- end
276
-
277
- end
278
-
279
- context "with casting" do
280
-
281
- should "cast the value as custom class" do
282
- article = PersistentArticleWithCastedItem.new :title => 'Test',
283
- :author => { :first_name => 'John', :last_name => 'Smith' }
284
- assert_instance_of Author, article.author
285
- assert_equal 'John', article.author.first_name
286
- end
287
-
288
- should "cast the value as collection of custom classes" do
289
- article = PersistentArticleWithCastedCollection.new :title => 'Test',
290
- :comments => [{:nick => '4chan', :body => 'WHY U NO?'}]
291
- assert_instance_of Array, article.comments
292
- assert_instance_of Comment, article.comments.first
293
- assert_equal '4chan', article.comments.first.nick
294
- end
295
-
296
- should "automatically format strings in UTC format as Time" do
297
- article = PersistentArticle.new :published_on => '2011-11-01T23:00:00Z'
298
- assert_instance_of Time, article.published_on
299
- assert_equal 2011, article.published_on.year
300
- end
301
-
302
- should "cast anonymous Hashes as Hashr instances" do
303
- article = PersistentArticleWithCastedItem.new :stats => { :views => 100, :meta => { :tags => 'A' } }
304
- assert_equal 100, article.stats.views
305
- assert_equal 'A', article.stats.meta.tags
306
- end
307
-
308
- end
309
-
310
- context "when creating" do
311
-
312
- should "save the document with generated ID in the database" do
313
- Configuration.client.expects(:post).
314
- with do |url, payload|
315
- doc = MultiJson.decode(payload)
316
- url == "#{Configuration.url}/persistent_articles/persistent_article/" &&
317
- doc['title'] == 'Test' &&
318
- doc['tags'] == ['one', 'two']
319
- doc['published_on'] == nil
320
- end.
321
- returns(mock_response('{"ok":true,"_id":"abc123"}'))
322
- article = PersistentArticle.create :title => 'Test', :tags => [:one, :two]
323
-
324
- assert article.persisted?, "#{article.inspect} should be `persisted?`"
325
- assert_equal 'abc123', article.id
326
- end
327
-
328
- should "save the document with custom ID in the database" do
329
- Configuration.client.expects(:post).
330
- with do |url, payload|
331
- doc = MultiJson.decode(payload)
332
- url == "#{Configuration.url}/persistent_articles/persistent_article/r2d2" &&
333
- doc['title'] == 'Test' &&
334
- doc['published_on'] == nil
335
- end.
336
- returns(mock_response('{"ok":true,"_id":"r2d2"}'))
337
- article = PersistentArticle.create :id => 'r2d2', :title => 'Test'
338
-
339
- assert_equal 'r2d2', article.id
340
- end
341
-
342
- should "perform model validations" do
343
- Configuration.client.expects(:post).never
344
-
345
- assert ! ValidatedModel.create(:name => nil)
346
- end
347
-
348
- end
349
-
350
- context "when creating" do
351
-
352
- should "set the id property" do
353
- Configuration.client.expects(:post).
354
- with do |url, payload|
355
- doc = MultiJson.decode(payload)
356
- url == "#{Configuration.url}/persistent_articles/persistent_article/" &&
357
- doc['title'] == 'Test'
358
- end.
359
- returns(mock_response('{"ok":true,"_id":"1"}'))
360
-
361
- article = PersistentArticle.create :title => 'Test'
362
- assert_equal '1', article.id
363
- end
364
-
365
- should "not set the id property if already set" do
366
- Configuration.client.expects(:post).
367
- with do |url, payload|
368
- doc = MultiJson.decode(payload)
369
- url == "#{Configuration.url}/persistent_articles/persistent_article/123" &&
370
- doc['title'] == 'Test' &&
371
- doc['published_on'] == nil
372
- end.
373
- returns(mock_response('{"ok":true, "_id":"XXX"}'))
374
-
375
- article = PersistentArticle.create :id => '123', :title => 'Test'
376
- assert_equal '123', article.id
377
- end
378
-
379
- end
380
-
381
- context "when saving" do
382
-
383
- should "save the document with updated attribute" do
384
- article = PersistentArticle.new :id => '1', :title => 'Test'
385
-
386
- Configuration.client.expects(:post).
387
- with do |url, payload|
388
- doc = MultiJson.decode(payload)
389
- url == "#{Configuration.url}/persistent_articles/persistent_article/1" &&
390
- doc['title'] == 'Test' &&
391
- doc['published_on'] == nil
392
- end.
393
- returns(mock_response('{"ok":true,"_id":"1"}'))
394
- assert article.save
395
-
396
- article.title = 'Updated'
397
-
398
- Configuration.client.expects(:post).
399
- with do |url, payload|
400
- doc = MultiJson.decode(payload)
401
- url == "#{Configuration.url}/persistent_articles/persistent_article/1" &&
402
- doc['title'] == 'Updated'
403
- end.
404
- returns(mock_response('{"ok":true,"_id":"1"}'))
405
- assert article.save
406
- end
407
-
408
- should "perform validations" do
409
- article = ValidatedModel.new :name => nil
410
- assert ! article.save
411
- end
412
-
413
- should "set the id property itself" do
414
- article = PersistentArticle.new
415
- article.title = 'Test'
416
-
417
- Configuration.client.expects(:post).with("#{Configuration.url}/persistent_articles/persistent_article/",
418
- article.to_indexed_json).
419
- returns(mock_response('{"ok":true,"_id":"1"}'))
420
- assert article.save
421
- assert_equal '1', article.id
422
- end
423
-
424
- should "not set the id property if already set" do
425
- article = PersistentArticle.new
426
- article.id = '456'
427
- article.title = 'Test'
428
-
429
- Configuration.client.expects(:post).
430
- with do |url, payload|
431
- doc = MultiJson.decode(payload)
432
- url == "#{Configuration.url}/persistent_articles/persistent_article/456" &&
433
- doc['title'] == 'Test'
434
- end.
435
- returns(mock_response('{"ok":true,"_id":"XXX"}'))
436
- assert article.save
437
- assert_equal '456', article.id
438
- end
439
-
440
- end
441
-
442
- context "when destroying" do
443
-
444
- should "delete the document from the database" do
445
- Configuration.client.expects(:post).
446
- with do |url, payload|
447
- doc = MultiJson.decode(payload)
448
- url == "#{Configuration.url}/persistent_articles/persistent_article/123" &&
449
- doc['title'] == 'Test'
450
- end.returns(mock_response('{"ok":true,"_id":"123"}'))
451
-
452
- Configuration.client.expects(:delete).
453
- with("#{Configuration.url}/persistent_articles/persistent_article/123").
454
- returns(mock_response('{"ok":true,"acknowledged":true}', 200))
455
-
456
- article = PersistentArticle.new :id => '123', :title => 'Test'
457
- article.save
458
- article.destroy
459
- end
460
-
461
- end
462
-
463
- context "when updating attributes" do
464
-
465
- should "update single attribute" do
466
- @article.expects(:save).returns(true)
467
-
468
- @article.update_attribute :title, 'Updated'
469
- assert_equal 'Updated', @article.title
470
- end
471
-
472
- should "update all attributes" do
473
- @article.expects(:save).returns(true)
474
-
475
- @article.update_attributes :title => 'Updated', :tags => ['three']
476
- assert_equal 'Updated', @article.title
477
- assert_equal ['three'], @article.tags
478
- end
479
-
480
- end
481
-
482
- end
483
-
484
- context "Persistent model with mapping definition" do
485
-
486
- should "create the index with mapping" do
487
- expected = {
488
- :settings => {},
489
- :mappings => { :persistent_article_with_mapping => {
490
- :properties => { :title => { :type => 'string', :analyzer => 'snowball', :boost => 10 } }
491
- }}
492
- }
493
-
494
- Tire::Index.any_instance.stubs(:exists?).returns(false)
495
- Tire::Index.any_instance.expects(:create).with(expected)
496
-
497
- class ::PersistentArticleWithMapping
498
-
499
- include Tire::Model::Persistence
500
- include Tire::Model::Search
501
- include Tire::Model::Callbacks
502
-
503
- mapping do
504
- property :title, :type => 'string', :analyzer => 'snowball', :boost => 10
505
- end
506
-
507
- end
508
-
509
- assert_equal 'snowball', PersistentArticleWithMapping.mapping[:title][:analyzer]
510
- end
511
-
512
- end
513
-
514
- end
515
- end
516
- end