elasticsearch-model 6.0.0 → 6.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (118) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +5 -0
  3. data/README.md +14 -7
  4. data/Rakefile +27 -36
  5. data/elasticsearch-model.gemspec +1 -1
  6. data/examples/activerecord_mapping_completion.rb +2 -15
  7. data/gemfiles/3.0.gemfile +6 -1
  8. data/gemfiles/4.0.gemfile +7 -1
  9. data/gemfiles/5.0.gemfile +6 -0
  10. data/lib/elasticsearch/model.rb +15 -8
  11. data/lib/elasticsearch/model/adapters/active_record.rb +7 -26
  12. data/lib/elasticsearch/model/indexing.rb +5 -3
  13. data/lib/elasticsearch/model/naming.rb +6 -1
  14. data/lib/elasticsearch/model/response.rb +2 -2
  15. data/lib/elasticsearch/model/response/pagination.rb +2 -192
  16. data/lib/elasticsearch/model/response/pagination/kaminari.rb +109 -0
  17. data/lib/elasticsearch/model/response/pagination/will_paginate.rb +95 -0
  18. data/lib/elasticsearch/model/response/result.rb +1 -1
  19. data/lib/elasticsearch/model/version.rb +1 -1
  20. data/spec/elasticsearch/model/adapter_spec.rb +119 -0
  21. data/spec/elasticsearch/model/adapters/active_record/associations_spec.rb +334 -0
  22. data/spec/elasticsearch/model/adapters/active_record/basic_spec.rb +340 -0
  23. data/spec/elasticsearch/model/adapters/active_record/dynamic_index_name_spec.rb +18 -0
  24. data/spec/elasticsearch/model/adapters/active_record/import_spec.rb +187 -0
  25. data/spec/elasticsearch/model/adapters/active_record/multi_model_spec.rb +110 -0
  26. data/spec/elasticsearch/model/adapters/active_record/namespaced_model_spec.rb +38 -0
  27. data/spec/elasticsearch/model/adapters/active_record/pagination_spec.rb +315 -0
  28. data/spec/elasticsearch/model/adapters/active_record/parent_child_spec.rb +75 -0
  29. data/spec/elasticsearch/model/adapters/active_record/serialization_spec.rb +61 -0
  30. data/spec/elasticsearch/model/adapters/active_record_spec.rb +207 -0
  31. data/spec/elasticsearch/model/adapters/default_spec.rb +41 -0
  32. data/spec/elasticsearch/model/adapters/mongoid/basic_spec.rb +267 -0
  33. data/spec/elasticsearch/model/adapters/mongoid/multi_model_spec.rb +66 -0
  34. data/spec/elasticsearch/model/adapters/mongoid_spec.rb +235 -0
  35. data/spec/elasticsearch/model/adapters/multiple_spec.rb +125 -0
  36. data/spec/elasticsearch/model/callbacks_spec.rb +33 -0
  37. data/spec/elasticsearch/model/client_spec.rb +66 -0
  38. data/spec/elasticsearch/model/hash_wrapper_spec.rb +12 -0
  39. data/spec/elasticsearch/model/importing_spec.rb +214 -0
  40. data/spec/elasticsearch/model/indexing_spec.rb +918 -0
  41. data/spec/elasticsearch/model/module_spec.rb +101 -0
  42. data/spec/elasticsearch/model/multimodel_spec.rb +55 -0
  43. data/spec/elasticsearch/model/naming_inheritance_spec.rb +184 -0
  44. data/spec/elasticsearch/model/naming_spec.rb +186 -0
  45. data/spec/elasticsearch/model/proxy_spec.rb +107 -0
  46. data/spec/elasticsearch/model/response/aggregations_spec.rb +66 -0
  47. data/spec/elasticsearch/model/response/base_spec.rb +90 -0
  48. data/spec/elasticsearch/model/response/pagination/kaminari_spec.rb +410 -0
  49. data/spec/elasticsearch/model/response/pagination/will_paginate_spec.rb +262 -0
  50. data/spec/elasticsearch/model/response/records_spec.rb +118 -0
  51. data/spec/elasticsearch/model/response/response_spec.rb +131 -0
  52. data/spec/elasticsearch/model/response/result_spec.rb +122 -0
  53. data/spec/elasticsearch/model/response/results_spec.rb +56 -0
  54. data/spec/elasticsearch/model/searching_search_request_spec.rb +112 -0
  55. data/spec/elasticsearch/model/searching_spec.rb +49 -0
  56. data/spec/elasticsearch/model/serializing_spec.rb +22 -0
  57. data/spec/spec_helper.rb +161 -0
  58. data/spec/support/app.rb +21 -0
  59. data/spec/support/app/answer.rb +33 -0
  60. data/spec/support/app/article.rb +22 -0
  61. data/spec/support/app/article_for_pagination.rb +12 -0
  62. data/spec/support/app/article_with_custom_serialization.rb +13 -0
  63. data/spec/support/app/article_with_dynamic_index_name.rb +15 -0
  64. data/spec/support/app/author.rb +9 -0
  65. data/spec/support/app/authorship.rb +4 -0
  66. data/spec/support/app/category.rb +3 -0
  67. data/spec/support/app/comment.rb +3 -0
  68. data/spec/support/app/episode.rb +11 -0
  69. data/spec/support/app/image.rb +19 -0
  70. data/spec/support/app/import_article.rb +12 -0
  71. data/spec/support/app/mongoid_article.rb +21 -0
  72. data/spec/support/app/namespaced_book.rb +10 -0
  73. data/spec/support/app/parent_and_child_searchable.rb +24 -0
  74. data/spec/support/app/post.rb +14 -0
  75. data/spec/support/app/question.rb +27 -0
  76. data/spec/support/app/searchable.rb +48 -0
  77. data/spec/support/app/series.rb +11 -0
  78. data/spec/support/model.json +1 -0
  79. data/{test → spec}/support/model.yml +0 -0
  80. metadata +129 -86
  81. data/test/integration/active_record_associations_parent_child_test.rb +0 -188
  82. data/test/integration/active_record_associations_test.rb +0 -339
  83. data/test/integration/active_record_basic_test.rb +0 -255
  84. data/test/integration/active_record_custom_serialization_test.rb +0 -67
  85. data/test/integration/active_record_import_test.rb +0 -168
  86. data/test/integration/active_record_namespaced_model_test.rb +0 -56
  87. data/test/integration/active_record_pagination_test.rb +0 -149
  88. data/test/integration/dynamic_index_name_test.rb +0 -52
  89. data/test/integration/mongoid_basic_test.rb +0 -240
  90. data/test/integration/multiple_models_test.rb +0 -176
  91. data/test/support/model.json +0 -1
  92. data/test/test_helper.rb +0 -92
  93. data/test/unit/adapter_active_record_test.rb +0 -157
  94. data/test/unit/adapter_default_test.rb +0 -41
  95. data/test/unit/adapter_mongoid_test.rb +0 -161
  96. data/test/unit/adapter_multiple_test.rb +0 -106
  97. data/test/unit/adapter_test.rb +0 -69
  98. data/test/unit/callbacks_test.rb +0 -31
  99. data/test/unit/client_test.rb +0 -27
  100. data/test/unit/hash_wrapper_test.rb +0 -13
  101. data/test/unit/importing_test.rb +0 -224
  102. data/test/unit/indexing_test.rb +0 -720
  103. data/test/unit/module_test.rb +0 -68
  104. data/test/unit/multimodel_test.rb +0 -38
  105. data/test/unit/naming_inheritance_test.rb +0 -94
  106. data/test/unit/naming_test.rb +0 -103
  107. data/test/unit/proxy_test.rb +0 -98
  108. data/test/unit/response_aggregations_test.rb +0 -46
  109. data/test/unit/response_base_test.rb +0 -40
  110. data/test/unit/response_pagination_kaminari_test.rb +0 -433
  111. data/test/unit/response_pagination_will_paginate_test.rb +0 -398
  112. data/test/unit/response_records_test.rb +0 -91
  113. data/test/unit/response_result_test.rb +0 -90
  114. data/test/unit/response_results_test.rb +0 -34
  115. data/test/unit/response_test.rb +0 -104
  116. data/test/unit/searching_search_request_test.rb +0 -78
  117. data/test/unit/searching_test.rb +0 -41
  118. data/test/unit/serializing_test.rb +0 -17
@@ -1,161 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Elasticsearch::Model::AdapterMongoidTest < Test::Unit::TestCase
4
- context "Adapter Mongoid module: " do
5
- class ::DummyClassForMongoid
6
- RESPONSE = Struct.new('DummyMongoidResponse') do
7
- def response
8
- { 'hits' => {'hits' => [ {'_id' => 2}, {'_id' => 1} ]} }
9
- end
10
- end.new
11
-
12
- def response
13
- RESPONSE
14
- end
15
-
16
- def ids
17
- [2, 1]
18
- end
19
- end
20
-
21
- setup do
22
- @records = [ stub(id: 1, inspect: '<Model-1>'), stub(id: 2, inspect: '<Model-2>') ]
23
- ::Symbol.any_instance.stubs(:in).returns(@records)
24
- end
25
-
26
- should "have the register condition" do
27
- assert_not_nil Elasticsearch::Model::Adapter.adapters[Elasticsearch::Model::Adapter::Mongoid]
28
- assert_equal false, Elasticsearch::Model::Adapter.adapters[Elasticsearch::Model::Adapter::Mongoid].call(DummyClassForMongoid)
29
- end
30
-
31
- context "Records" do
32
- setup do
33
- DummyClassForMongoid.__send__ :include, Elasticsearch::Model::Adapter::Mongoid::Records
34
- end
35
-
36
- should "have the implementation" do
37
- assert_instance_of Module, Elasticsearch::Model::Adapter::Mongoid::Records
38
-
39
- instance = DummyClassForMongoid.new
40
- instance.expects(:klass).returns(mock('class', where: @records))
41
-
42
- assert_equal @records, instance.records
43
- end
44
-
45
- should "reorder the records based on hits order" do
46
- @records.instance_variable_set(:@records, @records)
47
-
48
- instance = DummyClassForMongoid.new
49
- instance.expects(:klass).returns(mock('class', where: @records))
50
-
51
- assert_equal [1, 2], @records. to_a.map(&:id)
52
- assert_equal [2, 1], instance.records.to_a.map(&:id)
53
- end
54
-
55
- should "not reorder records when SQL order is present" do
56
- @records.instance_variable_set(:@records, @records)
57
-
58
- instance = DummyClassForMongoid.new
59
- instance.expects(:klass).returns(stub('class', where: @records)).at_least_once
60
- instance.records.expects(:asc).returns(@records)
61
-
62
- assert_equal [2, 1], instance.records.to_a.map(&:id)
63
- assert_equal [1, 2], instance.asc.to_a.map(&:id)
64
- end
65
- end
66
-
67
- context "Callbacks" do
68
- should "register hooks for automatically updating the index" do
69
- DummyClassForMongoid.expects(:after_create)
70
- DummyClassForMongoid.expects(:after_update)
71
- DummyClassForMongoid.expects(:after_destroy)
72
-
73
- Elasticsearch::Model::Adapter::Mongoid::Callbacks.included(DummyClassForMongoid)
74
- end
75
- end
76
-
77
- context "Importing" do
78
- should "implement the __find_in_batches method" do
79
- relation = mock()
80
- relation.stubs(:no_timeout).returns([])
81
- DummyClassForMongoid.expects(:all).returns(relation)
82
-
83
- DummyClassForMongoid.__send__ :extend, Elasticsearch::Model::Adapter::Mongoid::Importing
84
- DummyClassForMongoid.__find_in_batches do; end
85
- end
86
-
87
- context "when transforming models" do
88
- setup do
89
- @transform = DummyClassForMongoid.__transform
90
- end
91
-
92
- should "provide an object that responds to #call" do
93
- assert_respond_to @transform, :call
94
- end
95
-
96
- should "provide basic transformation" do
97
- model = mock("model", id: 1, as_indexed_json: {})
98
- assert_equal @transform.call(model), { index: { _id: "1", data: {} } }
99
- end
100
- end
101
-
102
- should "limit the relation to a specific scope" do
103
- relation = mock()
104
- relation.stubs(:no_timeout).returns(relation)
105
- relation.expects(:published).returns(relation)
106
- relation.expects(:each_slice).returns([])
107
- DummyClassForMongoid.expects(:all).returns(relation)
108
-
109
- DummyClassForMongoid.__send__ :extend, Elasticsearch::Model::Adapter::Mongoid::Importing
110
- DummyClassForMongoid.__find_in_batches(scope: :published) do; end
111
- end
112
-
113
- context "when limit the relation with proc" do
114
- setup do
115
- @query = Proc.new { where(color: "red") }
116
- end
117
- should "query with a specific criteria" do
118
- relation = mock()
119
- relation.stubs(:no_timeout).returns(relation)
120
- relation.expects(:class_exec).returns(relation)
121
- relation.expects(:each_slice).returns([])
122
- DummyClassForMongoid.expects(:all).returns(relation)
123
-
124
- DummyClassForMongoid.__find_in_batches(query: @query) do; end
125
- end
126
- end
127
-
128
- context "when limit the relation with hash" do
129
- setup do
130
- @query = { color: "red" }
131
- end
132
- should "query with a specific criteria" do
133
- relation = mock()
134
- relation.stubs(:no_timeout).returns(relation)
135
- relation.expects(:where).with(@query).returns(relation)
136
- relation.expects(:each_slice).returns([])
137
- DummyClassForMongoid.expects(:all).returns(relation)
138
-
139
- DummyClassForMongoid.__find_in_batches(query: @query) do; end
140
- end
141
- end
142
-
143
- should "preprocess the batch if option provided" do
144
- class << DummyClassForMongoid
145
- # Updates/transforms the batch while fetching it from the database
146
- # (eg. with information from an external system)
147
- #
148
- def update_batch(batch)
149
- batch.collect { |b| b.to_s + '!' }
150
- end
151
- end
152
-
153
- DummyClassForMongoid.expects(:__find_in_batches).returns( [:a, :b] )
154
-
155
- DummyClassForMongoid.__find_in_batches(preprocess: :update_batch) do |batch|
156
- assert_same_elements ["a!", "b!"], batch
157
- end
158
- end
159
- end
160
- end
161
- end
@@ -1,106 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Elasticsearch::Model::MultipleTest < Test::Unit::TestCase
4
-
5
- context "Adapter for multiple models" do
6
-
7
- class ::DummyOne
8
- include Elasticsearch::Model
9
-
10
- index_name 'dummy'
11
- document_type 'dummy_one'
12
-
13
- def self.find(ids)
14
- ids.map { |id| new(id) }
15
- end
16
-
17
- attr_reader :id
18
-
19
- def initialize(id)
20
- @id = id.to_i
21
- end
22
- end
23
-
24
- module ::Namespace
25
- class DummyTwo
26
- include Elasticsearch::Model
27
-
28
- index_name 'dummy'
29
- document_type 'dummy_two'
30
-
31
- def self.find(ids)
32
- ids.map { |id| new(id) }
33
- end
34
-
35
- attr_reader :id
36
-
37
- def initialize(id)
38
- @id = id.to_i
39
- end
40
- end
41
- end
42
-
43
- class ::DummyTwo
44
- include Elasticsearch::Model
45
-
46
- index_name 'other_index'
47
- document_type 'dummy_two'
48
-
49
- def self.find(ids)
50
- ids.map { |id| new(id) }
51
- end
52
-
53
- attr_reader :id
54
-
55
- def initialize(id)
56
- @id = id.to_i
57
- end
58
- end
59
-
60
- HITS = [{_index: 'dummy',
61
- _type: 'dummy_two',
62
- _id: '2',
63
- }, {
64
- _index: 'dummy',
65
- _type: 'dummy_one',
66
- _id: '2',
67
- }, {
68
- _index: 'other_index',
69
- _type: 'dummy_two',
70
- _id: '1',
71
- }, {
72
- _index: 'dummy',
73
- _type: 'dummy_two',
74
- _id: '1',
75
- }, {
76
- _index: 'dummy',
77
- _type: 'dummy_one',
78
- _id: '3'}]
79
-
80
- setup do
81
- @multimodel = Elasticsearch::Model::Multimodel.new(DummyOne, DummyTwo, Namespace::DummyTwo)
82
- end
83
-
84
- context "when returning records" do
85
- setup do
86
- @multimodel.class.send :include, Elasticsearch::Model::Adapter::Multiple::Records
87
- @multimodel.expects(:response).at_least_once.returns(stub(response: { 'hits' => { 'hits' => HITS } }))
88
- end
89
-
90
- should "keep the order from response" do
91
- assert_instance_of Module, Elasticsearch::Model::Adapter::Multiple::Records
92
- records = @multimodel.records
93
-
94
- assert_equal 5, records.count
95
-
96
- assert_kind_of ::Namespace::DummyTwo, records[0]
97
- assert_kind_of ::DummyOne, records[1]
98
- assert_kind_of ::DummyTwo, records[2]
99
- assert_kind_of ::Namespace::DummyTwo, records[3]
100
- assert_kind_of ::DummyOne, records[4]
101
-
102
- assert_equal [2, 2, 1, 1, 3], records.map(&:id)
103
- end
104
- end
105
- end
106
- end
@@ -1,69 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Elasticsearch::Model::AdapterTest < Test::Unit::TestCase
4
- context "Adapter module" do
5
- class ::DummyAdapterClass; end
6
- class ::DummyAdapterClassWithAdapter; end
7
- class ::DummyAdapter
8
- Records = Module.new
9
- Callbacks = Module.new
10
- Importing = Module.new
11
- end
12
-
13
- should "return an Adapter instance" do
14
- assert_instance_of Elasticsearch::Model::Adapter::Adapter,
15
- Elasticsearch::Model::Adapter.from_class(DummyAdapterClass)
16
- end
17
-
18
- should "return a list of adapters" do
19
- Elasticsearch::Model::Adapter::Adapter.expects(:adapters)
20
- Elasticsearch::Model::Adapter.adapters
21
- end
22
-
23
- should "register an adapter" do
24
- begin
25
- Elasticsearch::Model::Adapter::Adapter.expects(:register)
26
- Elasticsearch::Model::Adapter.register(:foo, lambda { |c| false })
27
- ensure
28
- Elasticsearch::Model::Adapter::Adapter.instance_variable_set(:@adapters, {})
29
- end
30
- end
31
- end
32
-
33
- context "Adapter class" do
34
- should "register an adapter" do
35
- begin
36
- Elasticsearch::Model::Adapter::Adapter.register(:foo, lambda { |c| false })
37
- assert Elasticsearch::Model::Adapter::Adapter.adapters[:foo]
38
- ensure
39
- Elasticsearch::Model::Adapter::Adapter.instance_variable_set(:@adapters, {})
40
- end
41
- end
42
-
43
- should "return the default adapter" do
44
- adapter = Elasticsearch::Model::Adapter::Adapter.new(DummyAdapterClass)
45
- assert_equal Elasticsearch::Model::Adapter::Default, adapter.adapter
46
- end
47
-
48
- should "return a specific adapter" do
49
- Elasticsearch::Model::Adapter::Adapter.register(DummyAdapter,
50
- lambda { |c| c == DummyAdapterClassWithAdapter })
51
-
52
- adapter = Elasticsearch::Model::Adapter::Adapter.new(DummyAdapterClassWithAdapter)
53
- assert_equal DummyAdapter, adapter.adapter
54
- end
55
-
56
- should "return the modules" do
57
- assert_nothing_raised do
58
- Elasticsearch::Model::Adapter::Adapter.register(DummyAdapter,
59
- lambda { |c| c == DummyAdapterClassWithAdapter })
60
-
61
- adapter = Elasticsearch::Model::Adapter::Adapter.new(DummyAdapterClassWithAdapter)
62
-
63
- assert_instance_of Module, adapter.records_mixin
64
- assert_instance_of Module, adapter.callbacks_mixin
65
- assert_instance_of Module, adapter.importing_mixin
66
- end
67
- end
68
- end
69
- end
@@ -1,31 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Elasticsearch::Model::CallbacksTest < Test::Unit::TestCase
4
- context "Callbacks module" do
5
- class ::DummyCallbacksModel
6
- end
7
-
8
- module DummyCallbacksAdapter
9
- module CallbacksMixin
10
- end
11
-
12
- def callbacks_mixin
13
- CallbacksMixin
14
- end; module_function :callbacks_mixin
15
- end
16
-
17
- should "include the callbacks mixin from adapter" do
18
- Elasticsearch::Model::Adapter.expects(:from_class)
19
- .with(DummyCallbacksModel)
20
- .returns(DummyCallbacksAdapter)
21
-
22
- ::DummyCallbacksModel.expects(:__send__).with do |method, parameter|
23
- assert_equal :include, method
24
- assert_equal DummyCallbacksAdapter::CallbacksMixin, parameter
25
- true
26
- end
27
-
28
- Elasticsearch::Model::Callbacks.included(DummyCallbacksModel)
29
- end
30
- end
31
- end
@@ -1,27 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Elasticsearch::Model::ClientTest < Test::Unit::TestCase
4
- context "Client module" do
5
- class ::DummyClientModel
6
- extend Elasticsearch::Model::Client::ClassMethods
7
- include Elasticsearch::Model::Client::InstanceMethods
8
- end
9
-
10
- should "have the default client method" do
11
- assert_instance_of Elasticsearch::Transport::Client, DummyClientModel.client
12
- assert_instance_of Elasticsearch::Transport::Client, DummyClientModel.new.client
13
- end
14
-
15
- should "set the client for the model" do
16
- DummyClientModel.client = 'foobar'
17
- assert_equal 'foobar', DummyClientModel.client
18
- assert_equal 'foobar', DummyClientModel.new.client
19
- end
20
-
21
- should "set the client for a model instance" do
22
- instance = DummyClientModel.new
23
- instance.client = 'moobam'
24
- assert_equal 'moobam', instance.client
25
- end
26
- end
27
- end
@@ -1,13 +0,0 @@
1
- require 'test_helper'
2
-
3
- require 'hashie/version'
4
-
5
- class Elasticsearch::Model::HashWrapperTest < Test::Unit::TestCase
6
- context "The HashWrapper class" do
7
- should "not print the warning for re-defined methods" do
8
- Hashie.logger.expects(:warn).never
9
-
10
- subject = Elasticsearch::Model::HashWrapper.new(:foo => 'bar', :sort => true)
11
- end if Hashie::VERSION >= '3.5.3'
12
- end
13
- end
@@ -1,224 +0,0 @@
1
- require 'test_helper'
2
-
3
- class Elasticsearch::Model::ImportingTest < Test::Unit::TestCase
4
- context "Importing module" do
5
- class ::DummyImportingModel
6
- end
7
-
8
- module ::DummyImportingAdapter
9
- module ImportingMixin
10
- def __find_in_batches(options={}, &block)
11
- yield if block_given?
12
- end
13
- def __transform
14
- lambda {|a|}
15
- end
16
- end
17
-
18
- def importing_mixin
19
- ImportingMixin
20
- end; module_function :importing_mixin
21
- end
22
-
23
- should "include methods from the module and adapter" do
24
- Elasticsearch::Model::Adapter.expects(:from_class)
25
- .with(DummyImportingModel)
26
- .returns(DummyImportingAdapter)
27
-
28
- DummyImportingModel.__send__ :include, Elasticsearch::Model::Importing
29
-
30
- assert_respond_to DummyImportingModel, :import
31
- assert_respond_to DummyImportingModel, :__find_in_batches
32
- end
33
-
34
- should "call the client when importing" do
35
- Elasticsearch::Model::Adapter.expects(:from_class)
36
- .with(DummyImportingModel)
37
- .returns(DummyImportingAdapter)
38
-
39
- DummyImportingModel.__send__ :include, Elasticsearch::Model::Importing
40
-
41
- client = mock('client')
42
- client.expects(:bulk).returns({'items' => []})
43
-
44
- DummyImportingModel.expects(:client).returns(client)
45
- DummyImportingModel.expects(:index_name).returns('foo')
46
- DummyImportingModel.expects(:document_type).returns('foo')
47
- DummyImportingModel.stubs(:index_exists?).returns(true)
48
- DummyImportingModel.stubs(:__batch_to_bulk)
49
- assert_equal 0, DummyImportingModel.import
50
- end
51
-
52
- should "return the number of errors" do
53
- Elasticsearch::Model::Adapter.expects(:from_class)
54
- .with(DummyImportingModel)
55
- .returns(DummyImportingAdapter)
56
-
57
- DummyImportingModel.__send__ :include, Elasticsearch::Model::Importing
58
-
59
- client = mock('client')
60
- client.expects(:bulk).returns({'items' => [ {'index' => {}}, {'index' => {'error' => 'FAILED'}} ]})
61
-
62
- DummyImportingModel.stubs(:client).returns(client)
63
- DummyImportingModel.stubs(:index_name).returns('foo')
64
- DummyImportingModel.stubs(:document_type).returns('foo')
65
- DummyImportingModel.stubs(:index_exists?).returns(true)
66
- DummyImportingModel.stubs(:__batch_to_bulk)
67
-
68
- assert_equal 1, DummyImportingModel.import
69
- end
70
-
71
- should "return an array of error elements" do
72
- Elasticsearch::Model::Adapter.expects(:from_class)
73
- .with(DummyImportingModel)
74
- .returns(DummyImportingAdapter)
75
-
76
- DummyImportingModel.__send__ :include, Elasticsearch::Model::Importing
77
-
78
- client = mock('client')
79
- client.expects(:bulk).returns({'items' => [ {'index' => {}}, {'index' => {'error' => 'FAILED'}} ]})
80
-
81
- DummyImportingModel.stubs(:client).returns(client)
82
- DummyImportingModel.stubs(:index_name).returns('foo')
83
- DummyImportingModel.stubs(:document_type).returns('foo')
84
- DummyImportingModel.stubs(:index_exists?).returns(true)
85
- DummyImportingModel.stubs(:__batch_to_bulk)
86
-
87
- assert_equal [{'index' => {'error' => 'FAILED'}}], DummyImportingModel.import(return: 'errors')
88
- end
89
-
90
- should "yield the response" do
91
- Elasticsearch::Model::Adapter.expects(:from_class)
92
- .with(DummyImportingModel)
93
- .returns(DummyImportingAdapter)
94
-
95
- DummyImportingModel.__send__ :include, Elasticsearch::Model::Importing
96
-
97
- client = mock('client')
98
- client.expects(:bulk).returns({'items' => [ {'index' => {}}, {'index' => {'error' => 'FAILED'}} ]})
99
-
100
- DummyImportingModel.stubs(:client).returns(client)
101
- DummyImportingModel.stubs(:index_name).returns('foo')
102
- DummyImportingModel.stubs(:document_type).returns('foo')
103
- DummyImportingModel.stubs(:index_exists?).returns(true)
104
- DummyImportingModel.stubs(:__batch_to_bulk)
105
-
106
- DummyImportingModel.import do |response|
107
- assert_equal 2, response['items'].size
108
- end
109
- end
110
-
111
- context "when the index does not exist" do
112
- should "raise an exception" do
113
- Elasticsearch::Model::Adapter.expects(:from_class)
114
- .with(DummyImportingModel)
115
- .returns(DummyImportingAdapter)
116
-
117
- DummyImportingModel.__send__ :include, Elasticsearch::Model::Importing
118
-
119
- DummyImportingModel.expects(:index_name).returns('foo')
120
- DummyImportingModel.expects(:document_type).returns('foo')
121
- DummyImportingModel.expects(:index_exists?).returns(false)
122
-
123
- assert_raise ArgumentError do
124
- DummyImportingModel.import
125
- end
126
- end
127
- end
128
-
129
- context "with the force option" do
130
- should "delete and create the index" do
131
- DummyImportingModel.expects(:__find_in_batches).with do |options|
132
- assert_equal 'bar', options[:foo]
133
- assert_nil options[:force]
134
- true
135
- end
136
-
137
- DummyImportingModel.expects(:create_index!).with do |options|
138
- assert_equal true, options[:force]
139
- true
140
- end
141
-
142
- DummyImportingModel.expects(:index_name).returns('foo')
143
- DummyImportingModel.expects(:document_type).returns('foo')
144
-
145
- DummyImportingModel.import force: true, foo: 'bar'
146
- end
147
- end
148
-
149
- context "with the refresh option" do
150
- should "refresh the index" do
151
- DummyImportingModel.expects(:__find_in_batches).with do |options|
152
- assert_equal 'bar', options[:foo]
153
- assert_nil options[:refresh]
154
- true
155
- end
156
-
157
- DummyImportingModel.expects(:refresh_index!).with do |options|
158
- assert_equal 'foo', options[:index]
159
- true
160
- end
161
-
162
- DummyImportingModel.expects(:index_name).returns('foo')
163
- DummyImportingModel.expects(:document_type).returns('foo')
164
- DummyImportingModel.stubs(:index_exists?).returns(true)
165
-
166
- DummyImportingModel.import refresh: true, foo: 'bar'
167
- end
168
- end
169
-
170
- should "allow passing a different index / type" do
171
- Elasticsearch::Model::Adapter.expects(:from_class)
172
- .with(DummyImportingModel)
173
- .returns(DummyImportingAdapter)
174
-
175
- DummyImportingModel.__send__ :include, Elasticsearch::Model::Importing
176
-
177
- client = mock('client')
178
-
179
- client
180
- .expects(:bulk)
181
- .with do |options|
182
- assert_equal 'my-new-index', options[:index]
183
- assert_equal 'my-other-type', options[:type]
184
- true
185
- end
186
- .returns({'items' => [ {'index' => {} }]})
187
-
188
- DummyImportingModel.stubs(:client).returns(client)
189
- DummyImportingModel.stubs(:index_exists?).returns(true)
190
- DummyImportingModel.stubs(:__batch_to_bulk)
191
-
192
- DummyImportingModel.import index: 'my-new-index', type: 'my-other-type'
193
- end
194
-
195
- should "use the default transform from adapter" do
196
- client = mock('client', bulk: {'items' => []})
197
- transform = lambda {|a|}
198
-
199
- DummyImportingModel.stubs(:client).returns(client)
200
- DummyImportingModel.stubs(:index_exists?).returns(true)
201
- DummyImportingModel.expects(:__transform).returns(transform)
202
- DummyImportingModel.expects(:__batch_to_bulk).with(anything, transform)
203
-
204
- DummyImportingModel.import index: 'foo', type: 'bar'
205
- end
206
-
207
- should "use the transformer from options" do
208
- client = mock('client', bulk: {'items' => []})
209
- transform = lambda {|a|}
210
-
211
- DummyImportingModel.stubs(:client).returns(client)
212
- DummyImportingModel.stubs(:index_exists?).returns(true)
213
- DummyImportingModel.expects(:__batch_to_bulk).with(anything, transform)
214
-
215
- DummyImportingModel.import index: 'foo', type: 'bar', transform: transform
216
- end
217
-
218
- should "raise an ArgumentError if transform doesn't respond to the call method" do
219
- assert_raise ArgumentError do
220
- DummyImportingModel.import index: 'foo', type: 'bar', transform: "not_callable"
221
- end
222
- end
223
- end
224
- end