chewy 0.10.1 → 7.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (243) hide show
  1. checksums.yaml +5 -5
  2. data/.github/CODEOWNERS +1 -0
  3. data/.github/ISSUE_TEMPLATE/bug_report.md +39 -0
  4. data/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
  5. data/.github/PULL_REQUEST_TEMPLATE.md +16 -0
  6. data/.github/workflows/ruby.yml +74 -0
  7. data/.rubocop.yml +28 -23
  8. data/.rubocop_todo.yml +110 -22
  9. data/CHANGELOG.md +480 -298
  10. data/CODE_OF_CONDUCT.md +14 -0
  11. data/CONTRIBUTING.md +63 -0
  12. data/Gemfile +3 -5
  13. data/Guardfile +3 -1
  14. data/LICENSE.txt +1 -1
  15. data/README.md +571 -333
  16. data/chewy.gemspec +12 -15
  17. data/gemfiles/rails.5.2.activerecord.gemfile +11 -0
  18. data/gemfiles/rails.6.0.activerecord.gemfile +11 -0
  19. data/gemfiles/rails.6.1.activerecord.gemfile +13 -0
  20. data/gemfiles/rails.7.0.activerecord.gemfile +13 -0
  21. data/lib/chewy/config.rb +48 -77
  22. data/lib/chewy/errors.rb +4 -10
  23. data/lib/chewy/fields/base.rb +88 -16
  24. data/lib/chewy/fields/root.rb +15 -21
  25. data/lib/chewy/index/actions.rb +67 -38
  26. data/lib/chewy/{type → index}/adapter/active_record.rb +18 -4
  27. data/lib/chewy/{type → index}/adapter/base.rb +11 -12
  28. data/lib/chewy/{type → index}/adapter/object.rb +28 -32
  29. data/lib/chewy/{type → index}/adapter/orm.rb +26 -24
  30. data/lib/chewy/index/aliases.rb +14 -5
  31. data/lib/chewy/index/crutch.rb +40 -0
  32. data/lib/chewy/index/import/bulk_builder.rb +311 -0
  33. data/lib/chewy/{type → index}/import/bulk_request.rb +10 -9
  34. data/lib/chewy/{type → index}/import/journal_builder.rb +11 -12
  35. data/lib/chewy/{type → index}/import/routine.rb +19 -18
  36. data/lib/chewy/{type → index}/import.rb +82 -36
  37. data/lib/chewy/{type → index}/mapping.rb +63 -62
  38. data/lib/chewy/index/observe/active_record_methods.rb +87 -0
  39. data/lib/chewy/index/observe/callback.rb +34 -0
  40. data/lib/chewy/index/observe.rb +17 -0
  41. data/lib/chewy/index/settings.rb +2 -0
  42. data/lib/chewy/index/specification.rb +13 -10
  43. data/lib/chewy/{type → index}/syncer.rb +62 -63
  44. data/lib/chewy/{type → index}/witchcraft.rb +15 -9
  45. data/lib/chewy/{type → index}/wrapper.rb +16 -6
  46. data/lib/chewy/index.rb +68 -93
  47. data/lib/chewy/journal.rb +25 -14
  48. data/lib/chewy/minitest/helpers.rb +91 -18
  49. data/lib/chewy/minitest/search_index_receiver.rb +29 -33
  50. data/lib/chewy/multi_search.rb +62 -0
  51. data/lib/chewy/railtie.rb +8 -24
  52. data/lib/chewy/rake_helper.rb +141 -112
  53. data/lib/chewy/rspec/build_query.rb +12 -0
  54. data/lib/chewy/rspec/helpers.rb +55 -0
  55. data/lib/chewy/rspec/update_index.rb +58 -49
  56. data/lib/chewy/rspec.rb +2 -0
  57. data/lib/chewy/runtime.rb +1 -1
  58. data/lib/chewy/search/loader.rb +19 -41
  59. data/lib/chewy/search/parameters/allow_partial_search_results.rb +27 -0
  60. data/lib/chewy/search/parameters/collapse.rb +16 -0
  61. data/lib/chewy/search/parameters/concerns/query_storage.rb +6 -5
  62. data/lib/chewy/search/parameters/ignore_unavailable.rb +27 -0
  63. data/lib/chewy/search/parameters/indices.rb +78 -0
  64. data/lib/chewy/search/parameters/none.rb +1 -3
  65. data/lib/chewy/search/parameters/order.rb +6 -19
  66. data/lib/chewy/search/parameters/source.rb +5 -1
  67. data/lib/chewy/search/parameters/track_total_hits.rb +16 -0
  68. data/lib/chewy/search/parameters.rb +28 -8
  69. data/lib/chewy/search/query_proxy.rb +9 -2
  70. data/lib/chewy/search/request.rb +207 -157
  71. data/lib/chewy/search/response.rb +5 -5
  72. data/lib/chewy/search/scoping.rb +7 -8
  73. data/lib/chewy/search/scrolling.rb +14 -13
  74. data/lib/chewy/search.rb +7 -26
  75. data/lib/chewy/stash.rb +27 -29
  76. data/lib/chewy/strategy/active_job.rb +2 -2
  77. data/lib/chewy/strategy/atomic.rb +1 -1
  78. data/lib/chewy/strategy/atomic_no_refresh.rb +18 -0
  79. data/lib/chewy/strategy/base.rb +10 -0
  80. data/lib/chewy/strategy/delayed_sidekiq/scheduler.rb +148 -0
  81. data/lib/chewy/strategy/delayed_sidekiq/worker.rb +52 -0
  82. data/lib/chewy/strategy/delayed_sidekiq.rb +17 -0
  83. data/lib/chewy/strategy/lazy_sidekiq.rb +64 -0
  84. data/lib/chewy/strategy/sidekiq.rb +3 -2
  85. data/lib/chewy/strategy.rb +6 -19
  86. data/lib/chewy/version.rb +1 -1
  87. data/lib/chewy.rb +37 -80
  88. data/lib/generators/chewy/install_generator.rb +1 -1
  89. data/lib/tasks/chewy.rake +26 -32
  90. data/migration_guide.md +56 -0
  91. data/spec/chewy/config_spec.rb +27 -57
  92. data/spec/chewy/fields/base_spec.rb +457 -174
  93. data/spec/chewy/fields/root_spec.rb +24 -32
  94. data/spec/chewy/fields/time_fields_spec.rb +5 -5
  95. data/spec/chewy/index/actions_spec.rb +425 -60
  96. data/spec/chewy/{type → index}/adapter/active_record_spec.rb +110 -44
  97. data/spec/chewy/{type → index}/adapter/object_spec.rb +21 -6
  98. data/spec/chewy/index/aliases_spec.rb +3 -3
  99. data/spec/chewy/index/import/bulk_builder_spec.rb +494 -0
  100. data/spec/chewy/{type → index}/import/bulk_request_spec.rb +5 -12
  101. data/spec/chewy/{type → index}/import/journal_builder_spec.rb +22 -30
  102. data/spec/chewy/{type → index}/import/routine_spec.rb +19 -19
  103. data/spec/chewy/{type → index}/import_spec.rb +154 -95
  104. data/spec/chewy/index/mapping_spec.rb +135 -0
  105. data/spec/chewy/index/observe/active_record_methods_spec.rb +68 -0
  106. data/spec/chewy/index/observe/callback_spec.rb +139 -0
  107. data/spec/chewy/index/observe_spec.rb +143 -0
  108. data/spec/chewy/index/settings_spec.rb +3 -1
  109. data/spec/chewy/index/specification_spec.rb +32 -33
  110. data/spec/chewy/{type → index}/syncer_spec.rb +14 -19
  111. data/spec/chewy/{type → index}/witchcraft_spec.rb +34 -21
  112. data/spec/chewy/index/wrapper_spec.rb +100 -0
  113. data/spec/chewy/index_spec.rb +99 -114
  114. data/spec/chewy/journal_spec.rb +56 -101
  115. data/spec/chewy/minitest/helpers_spec.rb +122 -14
  116. data/spec/chewy/minitest/search_index_receiver_spec.rb +24 -26
  117. data/spec/chewy/multi_search_spec.rb +84 -0
  118. data/spec/chewy/rake_helper_spec.rb +325 -101
  119. data/spec/chewy/rspec/build_query_spec.rb +34 -0
  120. data/spec/chewy/rspec/helpers_spec.rb +61 -0
  121. data/spec/chewy/rspec/update_index_spec.rb +106 -102
  122. data/spec/chewy/runtime_spec.rb +2 -2
  123. data/spec/chewy/search/loader_spec.rb +19 -53
  124. data/spec/chewy/search/pagination/kaminari_examples.rb +3 -5
  125. data/spec/chewy/search/pagination/kaminari_spec.rb +1 -1
  126. data/spec/chewy/search/parameters/collapse_spec.rb +5 -0
  127. data/spec/chewy/search/parameters/ignore_unavailable_spec.rb +67 -0
  128. data/spec/chewy/search/parameters/indices_spec.rb +99 -0
  129. data/spec/chewy/search/parameters/none_spec.rb +1 -1
  130. data/spec/chewy/search/parameters/order_spec.rb +18 -11
  131. data/spec/chewy/search/parameters/query_storage_examples.rb +67 -21
  132. data/spec/chewy/search/parameters/search_after_spec.rb +4 -1
  133. data/spec/chewy/search/parameters/source_spec.rb +8 -2
  134. data/spec/chewy/search/parameters/track_total_hits_spec.rb +5 -0
  135. data/spec/chewy/search/parameters_spec.rb +39 -8
  136. data/spec/chewy/search/query_proxy_spec.rb +68 -17
  137. data/spec/chewy/search/request_spec.rb +360 -149
  138. data/spec/chewy/search/response_spec.rb +35 -25
  139. data/spec/chewy/search/scrolling_spec.rb +28 -26
  140. data/spec/chewy/search_spec.rb +73 -53
  141. data/spec/chewy/stash_spec.rb +16 -26
  142. data/spec/chewy/strategy/active_job_spec.rb +23 -10
  143. data/spec/chewy/strategy/atomic_no_refresh_spec.rb +60 -0
  144. data/spec/chewy/strategy/atomic_spec.rb +9 -10
  145. data/spec/chewy/strategy/delayed_sidekiq_spec.rb +190 -0
  146. data/spec/chewy/strategy/lazy_sidekiq_spec.rb +214 -0
  147. data/spec/chewy/strategy/sidekiq_spec.rb +14 -10
  148. data/spec/chewy/strategy_spec.rb +19 -15
  149. data/spec/chewy_spec.rb +17 -110
  150. data/spec/spec_helper.rb +7 -22
  151. data/spec/support/active_record.rb +43 -5
  152. metadata +123 -198
  153. data/.travis.yml +0 -53
  154. data/Appraisals +0 -79
  155. data/LEGACY_DSL.md +0 -497
  156. data/gemfiles/rails.4.0.activerecord.gemfile +0 -14
  157. data/gemfiles/rails.4.1.activerecord.gemfile +0 -14
  158. data/gemfiles/rails.4.2.activerecord.gemfile +0 -15
  159. data/gemfiles/rails.4.2.mongoid.5.1.gemfile +0 -15
  160. data/gemfiles/rails.5.0.activerecord.gemfile +0 -15
  161. data/gemfiles/rails.5.0.mongoid.6.0.gemfile +0 -15
  162. data/gemfiles/rails.5.1.activerecord.gemfile +0 -15
  163. data/gemfiles/rails.5.1.mongoid.6.1.gemfile +0 -15
  164. data/gemfiles/sequel.4.45.gemfile +0 -11
  165. data/lib/chewy/backports/deep_dup.rb +0 -46
  166. data/lib/chewy/backports/duplicable.rb +0 -91
  167. data/lib/chewy/query/compose.rb +0 -68
  168. data/lib/chewy/query/criteria.rb +0 -191
  169. data/lib/chewy/query/filters.rb +0 -227
  170. data/lib/chewy/query/loading.rb +0 -111
  171. data/lib/chewy/query/nodes/and.rb +0 -25
  172. data/lib/chewy/query/nodes/base.rb +0 -17
  173. data/lib/chewy/query/nodes/bool.rb +0 -34
  174. data/lib/chewy/query/nodes/equal.rb +0 -34
  175. data/lib/chewy/query/nodes/exists.rb +0 -20
  176. data/lib/chewy/query/nodes/expr.rb +0 -28
  177. data/lib/chewy/query/nodes/field.rb +0 -110
  178. data/lib/chewy/query/nodes/has_child.rb +0 -15
  179. data/lib/chewy/query/nodes/has_parent.rb +0 -15
  180. data/lib/chewy/query/nodes/has_relation.rb +0 -59
  181. data/lib/chewy/query/nodes/match_all.rb +0 -11
  182. data/lib/chewy/query/nodes/missing.rb +0 -20
  183. data/lib/chewy/query/nodes/not.rb +0 -25
  184. data/lib/chewy/query/nodes/or.rb +0 -25
  185. data/lib/chewy/query/nodes/prefix.rb +0 -19
  186. data/lib/chewy/query/nodes/query.rb +0 -20
  187. data/lib/chewy/query/nodes/range.rb +0 -63
  188. data/lib/chewy/query/nodes/raw.rb +0 -15
  189. data/lib/chewy/query/nodes/regexp.rb +0 -35
  190. data/lib/chewy/query/nodes/script.rb +0 -20
  191. data/lib/chewy/query/pagination.rb +0 -25
  192. data/lib/chewy/query.rb +0 -1098
  193. data/lib/chewy/search/pagination/will_paginate.rb +0 -43
  194. data/lib/chewy/search/parameters/types.rb +0 -20
  195. data/lib/chewy/strategy/resque.rb +0 -27
  196. data/lib/chewy/strategy/shoryuken.rb +0 -40
  197. data/lib/chewy/type/actions.rb +0 -43
  198. data/lib/chewy/type/adapter/mongoid.rb +0 -69
  199. data/lib/chewy/type/adapter/sequel.rb +0 -95
  200. data/lib/chewy/type/crutch.rb +0 -32
  201. data/lib/chewy/type/import/bulk_builder.rb +0 -122
  202. data/lib/chewy/type/observe.rb +0 -78
  203. data/lib/chewy/type.rb +0 -117
  204. data/lib/sequel/plugins/chewy_observe.rb +0 -78
  205. data/spec/chewy/query/criteria_spec.rb +0 -700
  206. data/spec/chewy/query/filters_spec.rb +0 -201
  207. data/spec/chewy/query/loading_spec.rb +0 -124
  208. data/spec/chewy/query/nodes/and_spec.rb +0 -12
  209. data/spec/chewy/query/nodes/bool_spec.rb +0 -14
  210. data/spec/chewy/query/nodes/equal_spec.rb +0 -32
  211. data/spec/chewy/query/nodes/exists_spec.rb +0 -18
  212. data/spec/chewy/query/nodes/has_child_spec.rb +0 -59
  213. data/spec/chewy/query/nodes/has_parent_spec.rb +0 -59
  214. data/spec/chewy/query/nodes/match_all_spec.rb +0 -11
  215. data/spec/chewy/query/nodes/missing_spec.rb +0 -16
  216. data/spec/chewy/query/nodes/not_spec.rb +0 -13
  217. data/spec/chewy/query/nodes/or_spec.rb +0 -12
  218. data/spec/chewy/query/nodes/prefix_spec.rb +0 -16
  219. data/spec/chewy/query/nodes/query_spec.rb +0 -12
  220. data/spec/chewy/query/nodes/range_spec.rb +0 -32
  221. data/spec/chewy/query/nodes/raw_spec.rb +0 -11
  222. data/spec/chewy/query/nodes/regexp_spec.rb +0 -43
  223. data/spec/chewy/query/nodes/script_spec.rb +0 -15
  224. data/spec/chewy/query/pagination/kaminari_spec.rb +0 -5
  225. data/spec/chewy/query/pagination/will_paginate_spec.rb +0 -5
  226. data/spec/chewy/query/pagination_spec.rb +0 -39
  227. data/spec/chewy/query_spec.rb +0 -636
  228. data/spec/chewy/search/pagination/will_paginate_examples.rb +0 -63
  229. data/spec/chewy/search/pagination/will_paginate_spec.rb +0 -23
  230. data/spec/chewy/search/parameters/indices_boost_spec.rb +0 -83
  231. data/spec/chewy/search/parameters/types_spec.rb +0 -5
  232. data/spec/chewy/strategy/resque_spec.rb +0 -46
  233. data/spec/chewy/strategy/shoryuken_spec.rb +0 -64
  234. data/spec/chewy/type/actions_spec.rb +0 -50
  235. data/spec/chewy/type/adapter/mongoid_spec.rb +0 -372
  236. data/spec/chewy/type/adapter/sequel_spec.rb +0 -472
  237. data/spec/chewy/type/import/bulk_builder_spec.rb +0 -279
  238. data/spec/chewy/type/mapping_spec.rb +0 -142
  239. data/spec/chewy/type/observe_spec.rb +0 -137
  240. data/spec/chewy/type/wrapper_spec.rb +0 -98
  241. data/spec/chewy/type_spec.rb +0 -55
  242. data/spec/support/mongoid.rb +0 -93
  243. data/spec/support/sequel.rb +0 -80
@@ -1,279 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Chewy::Type::Import::BulkBuilder do
4
- before { Chewy.massacre }
5
-
6
- subject { described_class.new(type, index: index, delete: delete, fields: fields) }
7
- let(:type) { PlacesIndex::City }
8
- let(:index) { [] }
9
- let(:delete) { [] }
10
- let(:fields) { [] }
11
-
12
- describe '#bulk_body' do
13
- context 'simple bulk', :orm do
14
- before do
15
- stub_model(:city)
16
- stub_index(:places) do
17
- define_type City do
18
- field :name, :rating
19
- end
20
- end
21
- end
22
- let(:cities) { Array.new(3) { |i| City.create!(id: i + 1, name: "City#{i + 17}", rating: 42) } }
23
-
24
- specify { expect(subject.bulk_body).to eq([]) }
25
-
26
- context do
27
- let(:index) { cities }
28
- specify do
29
- expect(subject.bulk_body).to eq([
30
- {index: {_id: 1, data: {'name' => 'City17', 'rating' => 42}}},
31
- {index: {_id: 2, data: {'name' => 'City18', 'rating' => 42}}},
32
- {index: {_id: 3, data: {'name' => 'City19', 'rating' => 42}}}
33
- ])
34
- end
35
- end
36
-
37
- context do
38
- let(:delete) { cities }
39
- specify do
40
- expect(subject.bulk_body).to eq([
41
- {delete: {_id: 1}}, {delete: {_id: 2}}, {delete: {_id: 3}}
42
- ])
43
- end
44
- end
45
-
46
- context do
47
- let(:index) { cities.first(2) }
48
- let(:delete) { [cities.last] }
49
- specify do
50
- expect(subject.bulk_body).to eq([
51
- {index: {_id: 1, data: {'name' => 'City17', 'rating' => 42}}},
52
- {index: {_id: 2, data: {'name' => 'City18', 'rating' => 42}}},
53
- {delete: {_id: 3}}
54
- ])
55
- end
56
-
57
- context ':fields' do
58
- let(:fields) { %w[name] }
59
- specify do
60
- expect(subject.bulk_body).to eq([
61
- {update: {_id: 1, data: {doc: {'name' => 'City17'}}}},
62
- {update: {_id: 2, data: {doc: {'name' => 'City18'}}}},
63
- {delete: {_id: 3}}
64
- ])
65
- end
66
- end
67
- end
68
- end
69
-
70
- context 'parent-child relationship', :orm do
71
- before do
72
- stub_model(:country)
73
- stub_model(:city)
74
- adapter == :sequel ? City.many_to_one(:country) : City.belongs_to(:country)
75
- end
76
-
77
- before do
78
- stub_index(:places) do
79
- define_type Country do
80
- field :name
81
- end
82
-
83
- define_type City do
84
- root parent: 'country', parent_id: -> { country_id } do
85
- field :name
86
- field :rating
87
- end
88
- end
89
- end
90
- end
91
-
92
- before { PlacesIndex::Country.import(country) }
93
- let(:country) { Country.create!(id: 1, name: 'country') }
94
- let(:another_country) { Country.create!(id: 2, name: 'another country') }
95
- let(:city) { City.create!(id: 4, country_id: country.id, name: 'city', rating: 42) }
96
-
97
- context 'indexing' do
98
- let(:index) { [city] }
99
-
100
- specify do
101
- expect(subject.bulk_body).to eq([
102
- {index: {_id: city.id, parent: country.id, data: {'name' => 'city', 'rating' => 42}}}
103
- ])
104
- end
105
-
106
- context do
107
- let(:fields) { %w[name] }
108
-
109
- specify do
110
- expect(subject.bulk_body).to eq([
111
- {update: {_id: city.id, parent: country.id, data: {doc: {'name' => 'city'}}}}
112
- ])
113
- end
114
- end
115
- end
116
-
117
- context 'updating parent' do
118
- before do
119
- PlacesIndex::City.import(city)
120
- city.update_attributes(country_id: another_country.id)
121
- end
122
- let(:index) { [city] }
123
-
124
- specify do
125
- expect(subject.bulk_body).to eq([
126
- {delete: {_id: city.id, parent: country.id.to_s}},
127
- {index: {_id: city.id, parent: another_country.id, data: {'name' => 'city', 'rating' => 42}}}
128
- ])
129
- end
130
-
131
- context do
132
- let(:fields) { %w[name] }
133
-
134
- specify do
135
- expect(subject.bulk_body).to eq([
136
- {delete: {_id: city.id, parent: country.id.to_s}},
137
- {index: {_id: city.id, parent: another_country.id, data: {'name' => 'city', 'rating' => 42}}}
138
- ])
139
- end
140
- end
141
- end
142
-
143
- context 'destroying' do
144
- before { PlacesIndex::City.import(city) }
145
- let(:delete) { [city] }
146
-
147
- specify do
148
- expect(subject.bulk_body).to eq([
149
- {delete: {_id: city.id, parent: country.id.to_s}}
150
- ])
151
- end
152
- end
153
- end
154
-
155
- context 'custom id', :orm do
156
- before do
157
- stub_model(:city)
158
- end
159
-
160
- before do
161
- stub_index(:places) do
162
- define_type City do
163
- root id: -> { name } do
164
- field :rating
165
- end
166
- end
167
- end
168
- end
169
-
170
- let(:london) { City.create(id: 1, name: 'London', rating: 4) }
171
-
172
- specify do
173
- expect { PlacesIndex::City.import(london) }
174
- .to update_index(PlacesIndex::City).and_reindex(london.name)
175
- end
176
-
177
- context 'indexing' do
178
- let(:index) { [london] }
179
-
180
- specify do
181
- expect(subject.bulk_body).to eq([
182
- {index: {_id: london.name, data: {'rating' => 4}}}
183
- ])
184
- end
185
- end
186
-
187
- context 'destroying' do
188
- let(:delete) { [london] }
189
-
190
- specify do
191
- expect(subject.bulk_body).to eq([
192
- {delete: {_id: london.name}}
193
- ])
194
- end
195
- end
196
- end
197
-
198
- context 'crutches' do
199
- before do
200
- stub_index(:places) do
201
- define_type :city do
202
- crutch :names do |collection|
203
- collection.map { |item| [item.id, "Name#{item.id}"] }.to_h
204
- end
205
-
206
- field :name, value: ->(o, c) { c.names[o.id] }
207
- end
208
- end
209
- end
210
-
211
- let(:index) { [double(id: 42)] }
212
-
213
- specify do
214
- expect(subject.bulk_body).to eq([
215
- {index: {_id: 42, data: {'name' => 'Name42'}}}
216
- ])
217
- end
218
-
219
- context 'witchcraft' do
220
- before { PlacesIndex::City.witchcraft! }
221
- specify do
222
- expect(subject.bulk_body).to eq([
223
- {index: {_id: 42, data: {'name' => 'Name42'}}}
224
- ])
225
- end
226
- end
227
- end
228
-
229
- context 'empty ids' do
230
- before do
231
- stub_index(:places) do
232
- define_type :city do
233
- field :name
234
- end
235
- end
236
- end
237
-
238
- let(:index) { [{id: 1, name: 'Name0'}, double(id: '', name: 'Name1'), double(name: 'Name2')] }
239
- let(:delete) { [double(id: '', name: 'Name3'), {name: 'Name4'}, '', 2] }
240
-
241
- specify do
242
- expect(subject.bulk_body).to eq([
243
- {index: {_id: 1, data: {'name' => 'Name0'}}},
244
- {index: {data: {'name' => 'Name1'}}},
245
- {index: {data: {'name' => 'Name2'}}},
246
- {delete: {_id: {'name' => 'Name4'}}},
247
- {delete: {_id: 2}}
248
- ])
249
- end
250
-
251
- context do
252
- let(:fields) { %w[name] }
253
-
254
- specify do
255
- expect(subject.bulk_body).to eq([
256
- {update: {_id: 1, data: {doc: {'name' => 'Name0'}}}},
257
- {delete: {_id: {'name' => 'Name4'}}},
258
- {delete: {_id: 2}}
259
- ])
260
- end
261
- end
262
- end
263
- end
264
-
265
- describe '#index_objects_by_id' do
266
- before do
267
- stub_index(:places) do
268
- define_type :city do
269
- field :name
270
- end
271
- end
272
- end
273
-
274
- let(:index) { [double(id: 1), double(id: 2), double(id: ''), double] }
275
- let(:delete) { [double(id: 3)] }
276
-
277
- specify { expect(subject.index_objects_by_id).to eq('1' => index.first, '2' => index.second) }
278
- end
279
- end
@@ -1,142 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Chewy::Type::Mapping do
4
- let(:product) { ProductsIndex::Product }
5
- let(:review) { ProductsIndex::Review }
6
-
7
- before do
8
- stub_index(:products) do
9
- define_type :product do
10
- root do
11
- field :name, 'surname'
12
- field :title, type: 'string' do
13
- field :subfield1
14
- end
15
- field 'price', type: 'float' do
16
- field :subfield2
17
- end
18
- agg :named_agg do
19
- {avg: {field: 'title.subfield1'}}
20
- end
21
- end
22
- end
23
- define_type :review do
24
- field :title, :body
25
- field :comments do
26
- field :message
27
- field :rating, type: 'long'
28
- end
29
- agg :named_agg do
30
- {avg: {field: 'comments.rating'}}
31
- end
32
- end
33
- end
34
- end
35
-
36
- context 'no root element call' do
37
- before do
38
- stub_index(:products) do
39
- define_type :product do
40
- field :title, type: 'string' do
41
- field :subfield1
42
- end
43
- end
44
- end
45
- end
46
-
47
- specify { expect(product.root_object.children.map(&:name)).to eq([:title]) }
48
- specify { expect(product.root_object.children.map(&:parent)).to eq([product.root_object]) }
49
- specify { expect(product.root_object.children[0].children.map(&:name)).to eq([:subfield1]) }
50
- specify { expect(product.root_object.children[0].children.map(&:parent)).to eq([product.root_object.children[0]]) }
51
-
52
- context 'default root options are set' do
53
- around do |example|
54
- previous_options = Chewy.default_root_options
55
- Chewy.default_root_options = {_all: {enabled: false}}
56
- example.run
57
- Chewy.default_root_options = previous_options
58
- end
59
-
60
- specify { expect(product.mappings_hash[:product]).to include(_all: {enabled: false}) }
61
- end
62
- end
63
-
64
- describe '.agg' do
65
- specify { expect(product._agg_defs[:named_agg].call).to eq(avg: {field: 'title.subfield1'}) }
66
- specify { expect(review._agg_defs[:named_agg].call).to eq(avg: {field: 'comments.rating'}) }
67
- end
68
-
69
- describe '.field' do
70
- specify { expect(product.root_object.children.map(&:name)).to eq(%i[name surname title price]) }
71
- specify { expect(product.root_object.children.map(&:parent)).to eq([product.root_object] * 4) }
72
-
73
- specify { expect(product.root_object.children[0].children.map(&:name)).to eq([]) }
74
- specify { expect(product.root_object.children[1].children.map(&:name)).to eq([]) }
75
-
76
- specify { expect(product.root_object.children[2].children.map(&:name)).to eq([:subfield1]) }
77
- specify { expect(product.root_object.children[2].children.map(&:parent)).to eq([product.root_object.children[2]]) }
78
-
79
- specify { expect(product.root_object.children[3].children.map(&:name)).to eq([:subfield2]) }
80
- specify { expect(product.root_object.children[3].children.map(&:parent)).to eq([product.root_object.children[3]]) }
81
- end
82
-
83
- describe '.mappings_hash' do
84
- specify { expect(Class.new(Chewy::Type).mappings_hash).to eq({}) }
85
- specify { expect(product.mappings_hash).to eq(product.root_object.mappings_hash) }
86
-
87
- context 'parent-child relationship' do
88
- context do
89
- before do
90
- stub_index(:products) do
91
- define_type :product do
92
- root _parent: 'project', parent_id: -> { project_id } do
93
- field :name, 'surname'
94
- end
95
- end
96
- end
97
- end
98
-
99
- specify { expect(product.mappings_hash[:product][:_parent]).to eq(type: 'project') }
100
- end
101
-
102
- context do
103
- before do
104
- stub_index(:products) do
105
- define_type :product do
106
- root parent: {'type' => 'project'}, parent_id: -> { project_id } do
107
- field :name, 'surname'
108
- end
109
- end
110
- end
111
- end
112
-
113
- specify { expect(product.mappings_hash[:product][:_parent]).to eq(type: 'project') }
114
- end
115
- end
116
- end
117
-
118
- describe '.supports_outdated_sync?' do
119
- def type(&block)
120
- stub_index(:cities) do
121
- define_type :city, &block
122
- end
123
- CitiesIndex::City
124
- end
125
-
126
- specify { expect(type.supports_outdated_sync?).to eq(false) }
127
- specify { expect(type { field :updated_at }.supports_outdated_sync?).to eq(true) }
128
- specify { expect(type { field :updated_at, value: -> {} }.supports_outdated_sync?).to eq(false) }
129
- specify do
130
- expect(type do
131
- self.outdated_sync_field = :version
132
- field :updated_at
133
- end.supports_outdated_sync?).to eq(false)
134
- end
135
- specify do
136
- expect(type do
137
- self.outdated_sync_field = :version
138
- field :version
139
- end.supports_outdated_sync?).to eq(true)
140
- end
141
- end
142
- end
@@ -1,137 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Chewy::Type::Observe do
4
- describe '.update_index' do
5
- before do
6
- stub_index(:dummies) do
7
- define_type :dummy
8
- end
9
- end
10
-
11
- let(:backreferenced) { Array.new(3) { |i| double(id: i) } }
12
-
13
- specify do
14
- expect { DummiesIndex::Dummy.update_index(backreferenced) }
15
- .to raise_error Chewy::UndefinedUpdateStrategy
16
- end
17
- specify do
18
- expect { DummiesIndex::Dummy.update_index([]) }
19
- .not_to update_index('dummies#dummy')
20
- end
21
- specify do
22
- expect { DummiesIndex::Dummy.update_index(nil) }
23
- .not_to update_index('dummies#dummy')
24
- end
25
- end
26
-
27
- context 'integration', :orm do
28
- let(:update_condition) { true }
29
-
30
- before do
31
- city_countries_update_proc = if adapter == :sequel
32
- ->(*) { previous_changes.try(:[], :country_id) || country }
33
- else
34
- ->(*) { changes['country_id'] || previous_changes['country_id'] || country }
35
- end
36
-
37
- stub_model(:city) do
38
- update_index(->(city) { "cities##{city.class.name.underscore}" }) { self }
39
- update_index 'countries#country', &city_countries_update_proc
40
- end
41
-
42
- stub_model(:country) do
43
- update_index('cities#city', if: -> { update_condition }) { cities }
44
- update_index(-> { "countries##{self.class.name.underscore}" }, :self)
45
- attr_accessor :update_condition
46
- end
47
-
48
- if adapter == :sequel
49
- City.many_to_one :country
50
- Country.one_to_many :cities
51
- City.plugin :dirty
52
- else
53
- if adapter == :mongoid && Mongoid::VERSION.start_with?('6')
54
- City.belongs_to :country, optional: true
55
- else
56
- City.belongs_to :country
57
- end
58
- Country.has_many :cities
59
- end
60
-
61
- stub_index(:cities) do
62
- define_type City
63
- end
64
-
65
- stub_index(:countries) do
66
- define_type Country
67
- end
68
- end
69
-
70
- context do
71
- let!(:country1) { Chewy.strategy(:atomic) { Country.create!(id: 1, update_condition: update_condition) } }
72
- let!(:country2) { Chewy.strategy(:atomic) { Country.create!(id: 2, update_condition: update_condition) } }
73
- let!(:city) { Chewy.strategy(:atomic) { City.create!(id: 1, country: country1) } }
74
-
75
- specify { expect { city.save! }.to update_index('cities#city').and_reindex(city).only }
76
- specify { expect { city.save! }.to update_index('countries#country').and_reindex(country1).only }
77
-
78
- specify { expect { city.update_attributes!(country: nil) }.to update_index('cities#city').and_reindex(city).only }
79
- specify { expect { city.update_attributes!(country: nil) }.to update_index('countries#country').and_reindex(country1).only }
80
-
81
- specify { expect { city.update_attributes!(country: country2) }.to update_index('cities#city').and_reindex(city).only }
82
- specify { expect { city.update_attributes!(country: country2) }.to update_index('countries#country').and_reindex(country1, country2).only }
83
- end
84
-
85
- context do
86
- let!(:country) do
87
- Chewy.strategy(:atomic) do
88
- cities = Array.new(2) { |i| City.create!(id: i) }
89
- if adapter == :sequel
90
- Country.create(id: 1, update_condition: update_condition).tap do |country|
91
- cities.each { |city| country.add_city(city) }
92
- end
93
- else
94
- Country.create!(id: 1, cities: cities, update_condition: update_condition)
95
- end
96
- end
97
- end
98
-
99
- specify { expect { country.save! }.to update_index('cities#city').and_reindex(country.cities).only }
100
- specify { expect { country.save! }.to update_index('countries#country').and_reindex(country).only }
101
-
102
- context 'conditional update' do
103
- let(:update_condition) { false }
104
- specify { expect { country.save! }.not_to update_index('cities#city') }
105
- end
106
- end
107
- end
108
-
109
- context 'transactions', :active_record do
110
- context do
111
- before { stub_model(:city) { update_index 'cities#city', :self } }
112
- before { stub_index(:cities) { define_type City } }
113
-
114
- specify do
115
- Chewy.strategy(:urgent) do
116
- ActiveRecord::Base.transaction do
117
- expect { City.create! }.not_to update_index('cities#city')
118
- end
119
- end
120
- end
121
- end
122
-
123
- context do
124
- before { allow(Chewy).to receive_messages(use_after_commit_callbacks: false) }
125
- before { stub_model(:city) { update_index 'cities#city', :self } }
126
- before { stub_index(:cities) { define_type City } }
127
-
128
- specify do
129
- Chewy.strategy(:urgent) do
130
- ActiveRecord::Base.transaction do
131
- expect { City.create! }.to update_index('cities#city')
132
- end
133
- end
134
- end
135
- end
136
- end
137
- end
@@ -1,98 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Chewy::Type::Wrapper do
4
- before do
5
- stub_class(:city)
6
- stub_index(:cities) do
7
- define_type City
8
- end
9
- end
10
-
11
- let(:city_type) { CitiesIndex::City }
12
-
13
- describe '.build' do
14
- specify do
15
- expect(city_type.build({}).attributes)
16
- .to eq('id' => nil, '_score' => nil, '_explanation' => nil)
17
- end
18
- specify do
19
- expect(city_type.build('_source' => {name: 'Martin'}).attributes)
20
- .to eq('id' => nil, '_score' => nil, '_explanation' => nil, 'name' => 'Martin')
21
- end
22
- specify do
23
- expect(city_type.build('_id' => 42).attributes)
24
- .to eq('id' => 42, '_score' => nil, '_explanation' => nil)
25
- end
26
- specify do
27
- expect(city_type.build('_id' => 42, '_source' => {'id' => 43}).attributes)
28
- .to eq('id' => 43, '_score' => nil, '_explanation' => nil)
29
- end
30
- specify do
31
- expect(city_type.build('_score' => 42, '_explanation' => {foo: 'bar'}).attributes)
32
- .to eq('id' => nil, '_score' => 42, '_explanation' => {foo: 'bar'})
33
- end
34
- specify do
35
- expect(city_type.build('_score' => 42, 'borogoves' => {foo: 'bar'})._data)
36
- .to eq('_score' => 42, 'borogoves' => {foo: 'bar'})
37
- end
38
- end
39
-
40
- describe '#initialize' do
41
- subject(:city) { city_type.new(name: 'Martin', age: 42) }
42
-
43
- it do
44
- is_expected.to respond_to(:name)
45
- .and respond_to(:age)
46
- .and have_attributes(
47
- name: 'Martin',
48
- age: 42
49
- )
50
- end
51
-
52
- it { expect { city.population }.to raise_error(NoMethodError) }
53
-
54
- context 'highlight' do
55
- subject(:city) do
56
- city_type.new(name: 'Martin', age: 42)
57
- .tap do |city|
58
- city._data = {
59
- 'highlight' => {'name' => ['<b>Mar</b>tin']}
60
- }
61
- end
62
- end
63
-
64
- it do
65
- is_expected.to respond_to(:name_highlight)
66
- .and have_attributes(
67
- name: 'Martin',
68
- name_highlight: '<b>Mar</b>tin'
69
- )
70
- end
71
- end
72
- end
73
-
74
- describe '#==' do
75
- specify { expect(city_type.new(id: 42)).to eq(city_type.new(id: 42)) }
76
- specify { expect(city_type.new(id: 42, age: 55)).to eq(city_type.new(id: 42, age: 54)) }
77
- specify { expect(city_type.new(id: 42)).not_to eq(city_type.new(id: 43)) }
78
- specify { expect(city_type.new(id: 42, age: 55)).not_to eq(city_type.new(id: 43, age: 55)) }
79
- specify { expect(city_type.new(age: 55)).to eq(city_type.new(age: 55)) }
80
- specify { expect(city_type.new(age: 55)).not_to eq(city_type.new(age: 54)) }
81
-
82
- specify { expect(city_type.new(id: '42')).to eq(City.new.tap { |m| allow(m).to receive_messages(id: 42) }) }
83
- specify { expect(city_type.new(id: 42)).not_to eq(City.new.tap { |m| allow(m).to receive_messages(id: 43) }) }
84
- specify { expect(city_type.new(id: 42)).not_to eq(Class.new) }
85
-
86
- context 'models', :orm do
87
- before do
88
- stub_model(:city)
89
- stub_index(:cities) do
90
- define_type City
91
- end
92
- end
93
- specify { expect(city_type.new(id: '42')).to eq(City.new.tap { |m| allow(m).to receive_messages(id: 42) }) }
94
- specify { expect(city_type.new(id: 42)).not_to eq(City.new.tap { |m| allow(m).to receive_messages(id: 43) }) }
95
- specify { expect(city_type.new(id: 42)).not_to eq(Class.new) }
96
- end
97
- end
98
- end