chewy 0.8.4 → 5.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (303) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +1 -0
  3. data/.rubocop.yml +56 -0
  4. data/.rubocop_todo.yml +44 -0
  5. data/.travis.yml +36 -67
  6. data/.yardopts +5 -0
  7. data/Appraisals +63 -58
  8. data/CHANGELOG.md +168 -11
  9. data/Gemfile +16 -9
  10. data/Guardfile +5 -5
  11. data/LEGACY_DSL.md +497 -0
  12. data/README.md +403 -470
  13. data/Rakefile +11 -1
  14. data/chewy.gemspec +12 -15
  15. data/gemfiles/rails.4.0.activerecord.gemfile +9 -9
  16. data/gemfiles/rails.4.1.activerecord.gemfile +9 -9
  17. data/gemfiles/rails.4.2.activerecord.gemfile +8 -8
  18. data/gemfiles/rails.4.2.mongoid.5.2.gemfile +16 -0
  19. data/gemfiles/rails.5.0.activerecord.gemfile +16 -0
  20. data/gemfiles/rails.5.0.mongoid.6.1.gemfile +16 -0
  21. data/gemfiles/rails.5.1.activerecord.gemfile +16 -0
  22. data/gemfiles/rails.5.1.mongoid.6.3.gemfile +16 -0
  23. data/gemfiles/rails.5.2.activerecord.gemfile +16 -0
  24. data/gemfiles/sequel.4.45.gemfile +11 -0
  25. data/lib/chewy/backports/deep_dup.rb +1 -1
  26. data/lib/chewy/backports/duplicable.rb +1 -0
  27. data/lib/chewy/config.rb +53 -21
  28. data/lib/chewy/errors.rb +6 -6
  29. data/lib/chewy/fields/base.rb +59 -29
  30. data/lib/chewy/fields/root.rb +49 -14
  31. data/lib/chewy/index/actions.rb +95 -36
  32. data/lib/chewy/index/aliases.rb +2 -1
  33. data/lib/chewy/index/settings.rb +10 -5
  34. data/lib/chewy/index/specification.rb +60 -0
  35. data/lib/chewy/index.rb +239 -138
  36. data/lib/chewy/journal.rb +55 -0
  37. data/lib/chewy/log_subscriber.rb +8 -8
  38. data/lib/chewy/minitest/helpers.rb +77 -0
  39. data/lib/chewy/minitest/search_index_receiver.rb +80 -0
  40. data/lib/chewy/minitest.rb +1 -0
  41. data/lib/chewy/query/compose.rb +18 -19
  42. data/lib/chewy/query/criteria.rb +34 -24
  43. data/lib/chewy/query/filters.rb +28 -11
  44. data/lib/chewy/query/loading.rb +3 -4
  45. data/lib/chewy/query/nodes/and.rb +1 -1
  46. data/lib/chewy/query/nodes/base.rb +1 -1
  47. data/lib/chewy/query/nodes/bool.rb +6 -4
  48. data/lib/chewy/query/nodes/equal.rb +4 -4
  49. data/lib/chewy/query/nodes/exists.rb +1 -1
  50. data/lib/chewy/query/nodes/expr.rb +2 -2
  51. data/lib/chewy/query/nodes/field.rb +35 -31
  52. data/lib/chewy/query/nodes/has_child.rb +1 -0
  53. data/lib/chewy/query/nodes/has_parent.rb +1 -0
  54. data/lib/chewy/query/nodes/has_relation.rb +10 -12
  55. data/lib/chewy/query/nodes/missing.rb +1 -1
  56. data/lib/chewy/query/nodes/not.rb +1 -1
  57. data/lib/chewy/query/nodes/or.rb +1 -1
  58. data/lib/chewy/query/nodes/prefix.rb +3 -2
  59. data/lib/chewy/query/nodes/query.rb +1 -1
  60. data/lib/chewy/query/nodes/range.rb +9 -9
  61. data/lib/chewy/query/nodes/raw.rb +1 -1
  62. data/lib/chewy/query/nodes/regexp.rb +13 -9
  63. data/lib/chewy/query/nodes/script.rb +4 -4
  64. data/lib/chewy/query/pagination.rb +10 -1
  65. data/lib/chewy/query.rb +286 -170
  66. data/lib/chewy/railtie.rb +7 -6
  67. data/lib/chewy/rake_helper.rb +275 -37
  68. data/lib/chewy/repository.rb +2 -2
  69. data/lib/chewy/rspec/update_index.rb +70 -65
  70. data/lib/chewy/rspec.rb +1 -1
  71. data/lib/chewy/runtime/version.rb +4 -4
  72. data/lib/chewy/search/loader.rb +83 -0
  73. data/lib/chewy/{query → search}/pagination/kaminari.rb +13 -5
  74. data/lib/chewy/search/pagination/will_paginate.rb +43 -0
  75. data/lib/chewy/search/parameters/aggs.rb +16 -0
  76. data/lib/chewy/search/parameters/allow_partial_search_results.rb +27 -0
  77. data/lib/chewy/search/parameters/concerns/bool_storage.rb +24 -0
  78. data/lib/chewy/search/parameters/concerns/hash_storage.rb +23 -0
  79. data/lib/chewy/search/parameters/concerns/integer_storage.rb +14 -0
  80. data/lib/chewy/search/parameters/concerns/query_storage.rb +238 -0
  81. data/lib/chewy/search/parameters/concerns/string_array_storage.rb +23 -0
  82. data/lib/chewy/search/parameters/concerns/string_storage.rb +14 -0
  83. data/lib/chewy/search/parameters/docvalue_fields.rb +12 -0
  84. data/lib/chewy/search/parameters/explain.rb +16 -0
  85. data/lib/chewy/search/parameters/filter.rb +47 -0
  86. data/lib/chewy/search/parameters/highlight.rb +16 -0
  87. data/lib/chewy/search/parameters/indices.rb +123 -0
  88. data/lib/chewy/search/parameters/indices_boost.rb +52 -0
  89. data/lib/chewy/search/parameters/limit.rb +17 -0
  90. data/lib/chewy/search/parameters/load.rb +32 -0
  91. data/lib/chewy/search/parameters/min_score.rb +16 -0
  92. data/lib/chewy/search/parameters/none.rb +27 -0
  93. data/lib/chewy/search/parameters/offset.rb +17 -0
  94. data/lib/chewy/search/parameters/order.rb +64 -0
  95. data/lib/chewy/search/parameters/post_filter.rb +19 -0
  96. data/lib/chewy/search/parameters/preference.rb +16 -0
  97. data/lib/chewy/search/parameters/profile.rb +16 -0
  98. data/lib/chewy/search/parameters/query.rb +19 -0
  99. data/lib/chewy/search/parameters/request_cache.rb +27 -0
  100. data/lib/chewy/search/parameters/rescore.rb +29 -0
  101. data/lib/chewy/search/parameters/script_fields.rb +16 -0
  102. data/lib/chewy/search/parameters/search_after.rb +20 -0
  103. data/lib/chewy/search/parameters/search_type.rb +16 -0
  104. data/lib/chewy/search/parameters/source.rb +73 -0
  105. data/lib/chewy/search/parameters/storage.rb +95 -0
  106. data/lib/chewy/search/parameters/stored_fields.rb +63 -0
  107. data/lib/chewy/search/parameters/suggest.rb +16 -0
  108. data/lib/chewy/search/parameters/terminate_after.rb +16 -0
  109. data/lib/chewy/search/parameters/timeout.rb +16 -0
  110. data/lib/chewy/search/parameters/track_scores.rb +16 -0
  111. data/lib/chewy/search/parameters/types.rb +20 -0
  112. data/lib/chewy/search/parameters/version.rb +16 -0
  113. data/lib/chewy/search/parameters.rb +167 -0
  114. data/lib/chewy/search/query_proxy.rb +257 -0
  115. data/lib/chewy/search/request.rb +1045 -0
  116. data/lib/chewy/search/response.rb +119 -0
  117. data/lib/chewy/search/scoping.rb +50 -0
  118. data/lib/chewy/search/scrolling.rb +134 -0
  119. data/lib/chewy/search.rb +81 -26
  120. data/lib/chewy/stash.rb +79 -0
  121. data/lib/chewy/strategy/active_job.rb +1 -0
  122. data/lib/chewy/strategy/atomic.rb +2 -4
  123. data/lib/chewy/strategy/base.rb +4 -4
  124. data/lib/chewy/strategy/bypass.rb +1 -2
  125. data/lib/chewy/strategy/resque.rb +1 -0
  126. data/lib/chewy/strategy/shoryuken.rb +40 -0
  127. data/lib/chewy/strategy/sidekiq.rb +13 -1
  128. data/lib/chewy/strategy/urgent.rb +1 -1
  129. data/lib/chewy/strategy.rb +19 -10
  130. data/lib/chewy/type/actions.rb +26 -2
  131. data/lib/chewy/type/adapter/active_record.rb +50 -24
  132. data/lib/chewy/type/adapter/base.rb +29 -9
  133. data/lib/chewy/type/adapter/mongoid.rb +19 -10
  134. data/lib/chewy/type/adapter/object.rb +195 -31
  135. data/lib/chewy/type/adapter/orm.rb +69 -33
  136. data/lib/chewy/type/adapter/sequel.rb +37 -19
  137. data/lib/chewy/type/crutch.rb +5 -4
  138. data/lib/chewy/type/import/bulk_builder.rb +122 -0
  139. data/lib/chewy/type/import/bulk_request.rb +78 -0
  140. data/lib/chewy/type/import/journal_builder.rb +45 -0
  141. data/lib/chewy/type/import/routine.rb +138 -0
  142. data/lib/chewy/type/import.rb +150 -176
  143. data/lib/chewy/type/mapping.rb +58 -42
  144. data/lib/chewy/type/observe.rb +21 -15
  145. data/lib/chewy/type/syncer.rb +222 -0
  146. data/lib/chewy/type/witchcraft.rb +89 -34
  147. data/lib/chewy/type/wrapper.rb +48 -16
  148. data/lib/chewy/type.rb +77 -49
  149. data/lib/chewy/version.rb +1 -1
  150. data/lib/chewy.rb +95 -52
  151. data/lib/generators/chewy/install_generator.rb +3 -3
  152. data/lib/sequel/plugins/chewy_observe.rb +4 -19
  153. data/lib/tasks/chewy.rake +91 -28
  154. data/spec/chewy/config_spec.rb +130 -12
  155. data/spec/chewy/fields/base_spec.rb +194 -172
  156. data/spec/chewy/fields/root_spec.rb +123 -17
  157. data/spec/chewy/fields/time_fields_spec.rb +10 -9
  158. data/spec/chewy/index/actions_spec.rb +228 -43
  159. data/spec/chewy/index/aliases_spec.rb +2 -2
  160. data/spec/chewy/index/settings_spec.rb +100 -49
  161. data/spec/chewy/index/specification_spec.rb +169 -0
  162. data/spec/chewy/index_spec.rb +159 -63
  163. data/spec/chewy/journal_spec.rb +268 -0
  164. data/spec/chewy/minitest/helpers_spec.rb +90 -0
  165. data/spec/chewy/minitest/search_index_receiver_spec.rb +120 -0
  166. data/spec/chewy/query/criteria_spec.rb +503 -236
  167. data/spec/chewy/query/filters_spec.rb +96 -68
  168. data/spec/chewy/query/loading_spec.rb +80 -42
  169. data/spec/chewy/query/nodes/and_spec.rb +3 -7
  170. data/spec/chewy/query/nodes/bool_spec.rb +5 -13
  171. data/spec/chewy/query/nodes/equal_spec.rb +20 -20
  172. data/spec/chewy/query/nodes/exists_spec.rb +7 -7
  173. data/spec/chewy/query/nodes/has_child_spec.rb +42 -23
  174. data/spec/chewy/query/nodes/has_parent_spec.rb +42 -23
  175. data/spec/chewy/query/nodes/match_all_spec.rb +2 -2
  176. data/spec/chewy/query/nodes/missing_spec.rb +6 -5
  177. data/spec/chewy/query/nodes/not_spec.rb +5 -7
  178. data/spec/chewy/query/nodes/or_spec.rb +3 -7
  179. data/spec/chewy/query/nodes/prefix_spec.rb +6 -6
  180. data/spec/chewy/query/nodes/query_spec.rb +3 -3
  181. data/spec/chewy/query/nodes/range_spec.rb +19 -19
  182. data/spec/chewy/query/nodes/raw_spec.rb +2 -2
  183. data/spec/chewy/query/nodes/regexp_spec.rb +31 -19
  184. data/spec/chewy/query/nodes/script_spec.rb +5 -5
  185. data/spec/chewy/query/pagination/kaminari_spec.rb +3 -55
  186. data/spec/chewy/query/pagination/will_paginate_spec.rb +5 -0
  187. data/spec/chewy/query/pagination_spec.rb +25 -22
  188. data/spec/chewy/query_spec.rb +510 -505
  189. data/spec/chewy/rake_helper_spec.rb +381 -0
  190. data/spec/chewy/repository_spec.rb +8 -8
  191. data/spec/chewy/rspec/update_index_spec.rb +215 -113
  192. data/spec/chewy/runtime_spec.rb +2 -2
  193. data/spec/chewy/search/loader_spec.rb +117 -0
  194. data/spec/chewy/search/pagination/kaminari_examples.rb +71 -0
  195. data/spec/chewy/search/pagination/kaminari_spec.rb +21 -0
  196. data/spec/chewy/search/pagination/will_paginate_examples.rb +63 -0
  197. data/spec/chewy/search/pagination/will_paginate_spec.rb +23 -0
  198. data/spec/chewy/search/parameters/aggs_spec.rb +5 -0
  199. data/spec/chewy/search/parameters/bool_storage_examples.rb +53 -0
  200. data/spec/chewy/search/parameters/docvalue_fields_spec.rb +5 -0
  201. data/spec/chewy/search/parameters/explain_spec.rb +5 -0
  202. data/spec/chewy/search/parameters/filter_spec.rb +5 -0
  203. data/spec/chewy/search/parameters/hash_storage_examples.rb +59 -0
  204. data/spec/chewy/search/parameters/highlight_spec.rb +5 -0
  205. data/spec/chewy/search/parameters/indices_spec.rb +191 -0
  206. data/spec/chewy/search/parameters/integer_storage_examples.rb +32 -0
  207. data/spec/chewy/search/parameters/limit_spec.rb +5 -0
  208. data/spec/chewy/search/parameters/load_spec.rb +60 -0
  209. data/spec/chewy/search/parameters/min_score_spec.rb +32 -0
  210. data/spec/chewy/search/parameters/none_spec.rb +5 -0
  211. data/spec/chewy/search/parameters/offset_spec.rb +5 -0
  212. data/spec/chewy/search/parameters/order_spec.rb +65 -0
  213. data/spec/chewy/search/parameters/post_filter_spec.rb +5 -0
  214. data/spec/chewy/search/parameters/preference_spec.rb +5 -0
  215. data/spec/chewy/search/parameters/profile_spec.rb +5 -0
  216. data/spec/chewy/search/parameters/query_spec.rb +5 -0
  217. data/spec/chewy/search/parameters/query_storage_examples.rb +388 -0
  218. data/spec/chewy/search/parameters/request_cache_spec.rb +67 -0
  219. data/spec/chewy/search/parameters/rescore_spec.rb +62 -0
  220. data/spec/chewy/search/parameters/script_fields_spec.rb +5 -0
  221. data/spec/chewy/search/parameters/search_after_spec.rb +32 -0
  222. data/spec/chewy/search/parameters/search_type_spec.rb +5 -0
  223. data/spec/chewy/search/parameters/source_spec.rb +156 -0
  224. data/spec/chewy/search/parameters/storage_spec.rb +60 -0
  225. data/spec/chewy/search/parameters/stored_fields_spec.rb +126 -0
  226. data/spec/chewy/search/parameters/string_array_storage_examples.rb +63 -0
  227. data/spec/chewy/search/parameters/string_storage_examples.rb +32 -0
  228. data/spec/chewy/search/parameters/suggest_spec.rb +5 -0
  229. data/spec/chewy/search/parameters/terminate_after_spec.rb +5 -0
  230. data/spec/chewy/search/parameters/timeout_spec.rb +5 -0
  231. data/spec/chewy/search/parameters/track_scores_spec.rb +5 -0
  232. data/spec/chewy/search/parameters/types_spec.rb +5 -0
  233. data/spec/chewy/search/parameters/version_spec.rb +5 -0
  234. data/spec/chewy/search/parameters_spec.rb +145 -0
  235. data/spec/chewy/search/query_proxy_spec.rb +68 -0
  236. data/spec/chewy/search/request_spec.rb +685 -0
  237. data/spec/chewy/search/response_spec.rb +192 -0
  238. data/spec/chewy/search/scrolling_spec.rb +169 -0
  239. data/spec/chewy/search_spec.rb +37 -20
  240. data/spec/chewy/stash_spec.rb +95 -0
  241. data/spec/chewy/strategy/active_job_spec.rb +8 -2
  242. data/spec/chewy/strategy/atomic_spec.rb +4 -1
  243. data/spec/chewy/strategy/resque_spec.rb +8 -2
  244. data/spec/chewy/strategy/shoryuken_spec.rb +66 -0
  245. data/spec/chewy/strategy/sidekiq_spec.rb +10 -2
  246. data/spec/chewy/strategy_spec.rb +6 -6
  247. data/spec/chewy/type/actions_spec.rb +29 -10
  248. data/spec/chewy/type/adapter/active_record_spec.rb +357 -139
  249. data/spec/chewy/type/adapter/mongoid_spec.rb +220 -101
  250. data/spec/chewy/type/adapter/object_spec.rb +129 -40
  251. data/spec/chewy/type/adapter/sequel_spec.rb +304 -152
  252. data/spec/chewy/type/import/bulk_builder_spec.rb +279 -0
  253. data/spec/chewy/type/import/bulk_request_spec.rb +102 -0
  254. data/spec/chewy/type/import/journal_builder_spec.rb +95 -0
  255. data/spec/chewy/type/import/routine_spec.rb +110 -0
  256. data/spec/chewy/type/import_spec.rb +360 -244
  257. data/spec/chewy/type/mapping_spec.rb +96 -29
  258. data/spec/chewy/type/observe_spec.rb +25 -15
  259. data/spec/chewy/type/syncer_spec.rb +123 -0
  260. data/spec/chewy/type/witchcraft_spec.rb +122 -44
  261. data/spec/chewy/type/wrapper_spec.rb +63 -23
  262. data/spec/chewy/type_spec.rb +32 -10
  263. data/spec/chewy_spec.rb +82 -12
  264. data/spec/spec_helper.rb +16 -2
  265. data/spec/support/active_record.rb +6 -2
  266. data/spec/support/class_helpers.rb +4 -19
  267. data/spec/support/mongoid.rb +17 -5
  268. data/spec/support/sequel.rb +6 -1
  269. metadata +250 -57
  270. data/gemfiles/rails.3.2.activerecord.gemfile +0 -15
  271. data/gemfiles/rails.3.2.activerecord.kaminari.gemfile +0 -14
  272. data/gemfiles/rails.3.2.activerecord.will_paginate.gemfile +0 -14
  273. data/gemfiles/rails.4.0.activerecord.kaminari.gemfile +0 -14
  274. data/gemfiles/rails.4.0.activerecord.will_paginate.gemfile +0 -14
  275. data/gemfiles/rails.4.0.mongoid.4.0.0.gemfile +0 -15
  276. data/gemfiles/rails.4.0.mongoid.4.0.0.kaminari.gemfile +0 -14
  277. data/gemfiles/rails.4.0.mongoid.4.0.0.will_paginate.gemfile +0 -14
  278. data/gemfiles/rails.4.0.mongoid.5.1.0.gemfile +0 -15
  279. data/gemfiles/rails.4.0.mongoid.5.1.0.kaminari.gemfile +0 -14
  280. data/gemfiles/rails.4.0.mongoid.5.1.0.will_paginate.gemfile +0 -14
  281. data/gemfiles/rails.4.1.activerecord.kaminari.gemfile +0 -14
  282. data/gemfiles/rails.4.1.activerecord.will_paginate.gemfile +0 -14
  283. data/gemfiles/rails.4.1.mongoid.4.0.0.gemfile +0 -15
  284. data/gemfiles/rails.4.1.mongoid.4.0.0.kaminari.gemfile +0 -14
  285. data/gemfiles/rails.4.1.mongoid.4.0.0.will_paginate.gemfile +0 -14
  286. data/gemfiles/rails.4.1.mongoid.5.1.0.gemfile +0 -15
  287. data/gemfiles/rails.4.1.mongoid.5.1.0.kaminari.gemfile +0 -14
  288. data/gemfiles/rails.4.1.mongoid.5.1.0.will_paginate.gemfile +0 -14
  289. data/gemfiles/rails.4.2.activerecord.kaminari.gemfile +0 -15
  290. data/gemfiles/rails.4.2.activerecord.will_paginate.gemfile +0 -15
  291. data/gemfiles/rails.4.2.mongoid.4.0.0.gemfile +0 -15
  292. data/gemfiles/rails.4.2.mongoid.4.0.0.kaminari.gemfile +0 -14
  293. data/gemfiles/rails.4.2.mongoid.4.0.0.will_paginate.gemfile +0 -14
  294. data/gemfiles/rails.4.2.mongoid.5.1.0.gemfile +0 -15
  295. data/gemfiles/rails.4.2.mongoid.5.1.0.kaminari.gemfile +0 -14
  296. data/gemfiles/rails.4.2.mongoid.5.1.0.will_paginate.gemfile +0 -14
  297. data/gemfiles/rails.5.0.0.beta3.activerecord.gemfile +0 -16
  298. data/gemfiles/rails.5.0.0.beta3.activerecord.kaminari.gemfile +0 -16
  299. data/gemfiles/rails.5.0.0.beta3.activerecord.will_paginate.gemfile +0 -15
  300. data/gemfiles/sequel.4.31.gemfile +0 -13
  301. data/lib/chewy/query/pagination/will_paginate.rb +0 -27
  302. data/lib/chewy/query/scoping.rb +0 -20
  303. data/spec/chewy/query/pagination/will_paginage_spec.rb +0 -60
@@ -15,53 +15,101 @@ describe Chewy::Type::Import do
15
15
  end
16
16
  end
17
17
 
18
- let!(:dummy_cities) { 3.times.map { |i| City.create(id: i + 1, name: "name#{i}") } }
19
- let(:city) { CitiesIndex::City }
18
+ def imported_cities
19
+ CitiesIndex::City.all.map do |city|
20
+ city.attributes.except('_score', '_explanation')
21
+ end
22
+ end
20
23
 
21
- describe '.import', :orm do
22
- specify { expect(city.import).to eq(true) }
23
- specify { expect(city.import([])).to eq(true) }
24
- specify { expect(city.import(dummy_cities)).to eq(true) }
25
- specify { expect(city.import(dummy_cities.map(&:id))).to eq(true) }
24
+ def subscribe_notification
25
+ outer_payload = {}
26
+ ActiveSupport::Notifications.subscribe('import_objects.chewy') do |_name, _start, _finish, _id, payload|
27
+ outer_payload.merge!(payload)
28
+ end
29
+ outer_payload
30
+ end
31
+
32
+ let!(:dummy_cities) { Array.new(3) { |i| City.create(id: i + 1, name: "name#{i}") } }
26
33
 
27
- specify { expect { city.import([]) }.not_to update_index(city) }
28
- specify { expect { city.import }.to update_index(city).and_reindex(dummy_cities) }
29
- specify { expect { city.import dummy_cities }.to update_index(city).and_reindex(dummy_cities) }
30
- specify { expect { city.import dummy_cities.map(&:id) }.to update_index(city).and_reindex(dummy_cities) }
34
+ describe 'index creation on import' do
35
+ let(:dummy_city) { City.create }
36
+
37
+ specify 'lazy (default)' do
38
+ expect(CitiesIndex).to receive(:exists?).and_call_original
39
+ expect(CitiesIndex).to receive(:create!).and_call_original
40
+ CitiesIndex::City.import(dummy_city)
41
+ end
42
+
43
+ specify 'lazy without objects' do
44
+ expect(CitiesIndex).not_to receive(:exists?)
45
+ expect(CitiesIndex).not_to receive(:create!)
46
+ CitiesIndex::City.import([])
47
+ end
48
+
49
+ context 'skip' do
50
+ before do
51
+ # To avoid flaky issues when previous specs were run
52
+ expect(Chewy::Index).to receive(:descendants).and_return([CitiesIndex])
53
+ Chewy.create_indices
54
+ Chewy.config.settings[:skip_index_creation_on_import] = true
55
+ end
56
+ after { Chewy.config.settings[:skip_index_creation_on_import] = nil }
57
+
58
+ specify do
59
+ expect(CitiesIndex).not_to receive(:exists?)
60
+ expect(CitiesIndex).not_to receive(:create!)
61
+ CitiesIndex::City.import(dummy_city)
62
+ end
63
+ end
64
+ end
65
+
66
+ shared_examples 'importing' do
67
+ specify { expect(import).to eq(true) }
68
+ specify { expect(import([])).to eq(true) }
69
+ specify { expect(import(dummy_cities)).to eq(true) }
70
+ specify { expect(import(dummy_cities.map(&:id))).to eq(true) }
71
+
72
+ specify { expect { import([]) }.not_to update_index(CitiesIndex::City) }
73
+ specify { expect { import }.to update_index(CitiesIndex::City).and_reindex(dummy_cities) }
74
+ specify { expect { import dummy_cities }.to update_index(CitiesIndex::City).and_reindex(dummy_cities) }
75
+ specify { expect { import dummy_cities.map(&:id) }.to update_index(CitiesIndex::City).and_reindex(dummy_cities) }
31
76
 
32
77
  describe 'criteria-driven importing' do
33
- let(:names) { %w(name0 name1) }
78
+ let(:names) { %w[name0 name1] }
34
79
 
35
80
  context 'mongoid', :mongoid do
36
- specify { expect { city.import(City.where(:name.in => names)) }.to update_index(city).and_reindex(dummy_cities.first(2)) }
37
- specify { expect { city.import(City.where(:name.in => names).map(&:id)) }.to update_index(city).and_reindex(dummy_cities.first(2)) }
81
+ specify { expect { import(City.where(:name.in => names)) }.to update_index(CitiesIndex::City).and_reindex(dummy_cities.first(2)) }
82
+ specify { expect { import(City.where(:name.in => names).map(&:id)) }.to update_index(CitiesIndex::City).and_reindex(dummy_cities.first(2)) }
38
83
  end
39
84
 
40
85
  context 'active record', :active_record do
41
- specify { expect { city.import(City.where(name: names)) }.to update_index(city).and_reindex(dummy_cities.first(2)) }
42
- specify { expect { city.import(City.where(name: names).map(&:id)) }.to update_index(city).and_reindex(dummy_cities.first(2)) }
86
+ specify { expect { import(City.where(name: names)) }.to update_index(CitiesIndex::City).and_reindex(dummy_cities.first(2)) }
87
+ specify { expect { import(City.where(name: names).map(&:id)) }.to update_index(CitiesIndex::City).and_reindex(dummy_cities.first(2)) }
43
88
  end
44
89
  end
45
90
 
46
91
  specify do
47
92
  dummy_cities.first.destroy
48
- expect { city.import dummy_cities }
49
- .to update_index(city).and_reindex(dummy_cities.from(1)).and_delete(dummy_cities.first)
93
+ expect { import dummy_cities }
94
+ .to update_index(CitiesIndex::City).and_reindex(dummy_cities.from(1)).and_delete(dummy_cities.first)
50
95
  end
51
96
 
52
97
  specify do
53
98
  dummy_cities.first.destroy
54
- expect { city.import dummy_cities.map(&:id) }
55
- .to update_index(city).and_reindex(dummy_cities.from(1)).and_delete(dummy_cities.first)
99
+ expect { import dummy_cities.map(&:id) }
100
+ .to update_index(CitiesIndex::City).and_reindex(dummy_cities.from(1)).and_delete(dummy_cities.first)
56
101
  end
57
102
 
58
103
  specify do
59
104
  dummy_cities.first.destroy
60
105
 
61
106
  imported = []
62
- allow(CitiesIndex.client).to receive(:bulk) { |params| imported << params[:body]; nil }
107
+ allow(CitiesIndex.client).to receive(:bulk) { |params|
108
+ imported << params[:body]
109
+ nil
110
+ }
63
111
 
64
- city.import dummy_cities.map(&:id), batch_size: 2
112
+ import dummy_cities.map(&:id), batch_size: 2
65
113
  expect(imported.flatten).to match_array([
66
114
  {index: {_id: 2, data: {'name' => 'name1'}}},
67
115
  {index: {_id: 3, data: {'name' => 'name2'}}},
@@ -70,61 +118,25 @@ describe Chewy::Type::Import do
70
118
  end
71
119
 
72
120
  context ':bulk_size' do
73
- specify do
74
- dummy_cities.first.destroy
75
-
76
- imported = []
77
- allow(CitiesIndex.client).to receive(:bulk) { |params| imported << params[:body]; nil }
121
+ let!(:dummy_cities) { Array.new(3) { |i| City.create(id: i + 1, name: "name#{i}" * 20) } }
78
122
 
79
- city.import dummy_cities.map(&:id), bulk_size: 1.2.kilobyte
80
- expect(imported.flatten).to match_array([
81
- %Q({"delete":{"_id":1}}),
82
- %Q({"index":{"_id":2}}\n{"name":"name1"}\n{"index":{"_id":3}}\n{"name":"name2"})
83
- ])
84
- end
123
+ specify { expect { import(dummy_cities, bulk_size: 1.2.kilobyte) }.to update_index(CitiesIndex::City).and_reindex(dummy_cities) }
85
124
 
86
125
  context do
87
- let!(:dummy_cities) { 3.times.map { |i| City.create(id: i + 1, name: "name#{i}" * 20) } }
88
-
89
- specify do
90
- dummy_cities.first.destroy
91
-
92
- imported = []
93
- allow(CitiesIndex.client).to receive(:bulk) { |params| imported << params[:body]; nil }
94
-
95
- city.import dummy_cities.map(&:id), bulk_size: 1.2.kilobyte
96
- expect(imported.flatten).to match_array([
97
- %Q({"delete":{"_id":1}}),
98
- %Q({"index":{"_id":2}}\n{"name":"#{'name1' * 20}"}),
99
- %Q({"index":{"_id":3}}\n{"name":"#{'name2' * 20}"})
100
- ])
101
- end
102
-
103
- specify do
104
- expect { city.import dummy_cities.map(&:id), bulk_size: 1.1.kilobyte }.to raise_error ArgumentError
105
- end
126
+ before { expect(Chewy.client).to receive(:bulk).exactly(3).times.and_call_original }
127
+ specify { expect(import(dummy_cities, bulk_size: 1.2.kilobyte)).to eq(true) }
106
128
  end
107
129
  end
108
130
 
109
- specify do
110
- expect(CitiesIndex.client).to receive(:bulk).with(hash_including(refresh: true))
111
- city.import dummy_cities
112
- end
113
-
114
- specify do
115
- expect(CitiesIndex.client).to receive(:bulk).with(hash_including(refresh: false))
116
- city.import dummy_cities, refresh: false
117
- end
118
-
119
131
  context 'scoped' do
120
132
  before do
121
- names = %w(name0 name1)
133
+ names = %w[name0 name1]
122
134
 
123
135
  criteria = case adapter
124
136
  when :mongoid
125
- { :name.in => names }
137
+ {:name.in => names}
126
138
  else
127
- { name: names }
139
+ {name: names}
128
140
  end
129
141
 
130
142
  stub_index(:cities) do
@@ -134,52 +146,40 @@ describe Chewy::Type::Import do
134
146
  end
135
147
  end
136
148
 
137
- specify { expect { city.import }.to update_index(city).and_reindex(dummy_cities.first(2)) }
149
+ specify { expect { import }.to update_index(CitiesIndex::City).and_reindex(dummy_cities.first(2)) }
138
150
 
139
151
  context 'mongoid', :mongoid do
140
152
  specify do
141
- expect { city.import City.where(_id: dummy_cities.first.id) }.to update_index(city).and_reindex(dummy_cities.first).only
153
+ expect { import City.where(_id: dummy_cities.first.id) }.to update_index(CitiesIndex::City).and_reindex(dummy_cities.first).only
142
154
  end
143
155
  end
144
156
 
145
157
  context 'active record', :active_record do
146
158
  specify do
147
- expect { city.import City.where(id: dummy_cities.first.id) }.to update_index(city).and_reindex(dummy_cities.first).only
159
+ expect { import City.where(id: dummy_cities.first.id) }.to update_index(CitiesIndex::City).and_reindex(dummy_cities.first).only
148
160
  end
149
161
  end
150
162
  end
151
163
 
152
164
  context 'instrumentation payload' do
153
165
  specify do
154
- outer_payload = nil
155
- ActiveSupport::Notifications.subscribe('import_objects.chewy') do |name, start, finish, id, payload|
156
- outer_payload = payload
157
- end
158
-
166
+ payload = subscribe_notification
159
167
  dummy_cities.first.destroy
160
- city.import dummy_cities
161
- expect(outer_payload).to eq({type: CitiesIndex::City, import: {delete: 1, index: 2}})
168
+ import dummy_cities
169
+ expect(payload).to eq(type: CitiesIndex::City, import: {delete: 1, index: 2})
162
170
  end
163
171
 
164
172
  specify do
165
- outer_payload = nil
166
- ActiveSupport::Notifications.subscribe('import_objects.chewy') do |name, start, finish, id, payload|
167
- outer_payload = payload
168
- end
169
-
173
+ payload = subscribe_notification
170
174
  dummy_cities.first.destroy
171
- city.import dummy_cities, batch_size: 2
172
- expect(outer_payload).to eq({type: CitiesIndex::City, import: {delete: 1, index: 2}})
175
+ import dummy_cities, batch_size: 2
176
+ expect(payload).to eq(type: CitiesIndex::City, import: {delete: 1, index: 2})
173
177
  end
174
178
 
175
179
  specify do
176
- outer_payload = nil
177
- ActiveSupport::Notifications.subscribe('import_objects.chewy') do |name, start, finish, id, payload|
178
- outer_payload = payload
179
- end
180
-
181
- city.import dummy_cities, batch_size: 2
182
- expect(outer_payload).to eq({type: CitiesIndex::City, import: {index: 3}})
180
+ payload = subscribe_notification
181
+ import dummy_cities, batch_size: 2
182
+ expect(payload).to eq(type: CitiesIndex::City, import: {index: 3})
183
183
  end
184
184
 
185
185
  context do
@@ -191,221 +191,296 @@ describe Chewy::Type::Import do
191
191
  end
192
192
  end
193
193
 
194
- specify do
195
- skip_on_version_gte('2.0', 'format of exception changed in 2.x')
196
- outer_payload = nil
197
- ActiveSupport::Notifications.subscribe('import_objects.chewy') do |name, start, finish, id, payload|
198
- outer_payload = payload
199
- end
200
-
201
- city.import dummy_cities, batch_size: 2
202
- expect(outer_payload).to eq({
203
- type: CitiesIndex::City,
204
- errors: {
205
- index: {
206
- "WriteFailureException; nested: MapperParsingException[object mapping for [city] tried to parse field [name] as object, but got EOF, has a concrete value been provided to it?]; " => ["1"],
207
- "MapperParsingException[object mapping for [city] tried to parse field [name] as object, but got EOF, has a concrete value been provided to it?]" => ["2", "3"]
208
- }
209
- },
210
- import: {index: 3}
211
- })
194
+ let(:mapper_parsing_exception) do
195
+ {
196
+ 'type' => 'mapper_parsing_exception',
197
+ 'reason' => 'object mapping for [name] tried to parse field [name] as object, but found a concrete value'
198
+ }
212
199
  end
213
200
 
214
201
  specify do
215
- skip_on_version_lt('2.0', 'format of exception was changed')
216
- outer_payload = nil
217
- ActiveSupport::Notifications.subscribe('import_objects.chewy') do |name, start, finish, id, payload|
218
- outer_payload = payload
219
- end
220
-
221
- city.import dummy_cities, batch_size: 2
222
- expect(outer_payload).to eq({
223
- type: CitiesIndex::City,
224
- errors: {
225
- index: {
226
- {"type"=>"mapper_parsing_exception", "reason"=>"object mapping for [name] tried to parse field [name] as object, but found a concrete value"} => ["1", "2", "3"]
227
- }
228
- },
229
- import: {index: 3}
230
- })
202
+ payload = subscribe_notification
203
+ import dummy_cities, batch_size: 2
204
+ expect(payload).to eq(type: CitiesIndex::City,
205
+ errors: {index: {mapper_parsing_exception => %w[1 2 3]}},
206
+ import: {index: 3})
231
207
  end
232
208
  end
233
209
  end
234
210
 
235
- context 'error handling' do
236
- context do
237
- before do
238
- stub_index(:cities) do
239
- define_type City do
240
- field :name, type: 'object'
241
- end
242
- end
243
- end
211
+ context 'fields' do
212
+ before { CitiesIndex::City.import!(dummy_cities.first(2)) }
244
213
 
245
- specify { expect(city.import(dummy_cities)).to eq(false) }
246
- specify { expect(city.import(dummy_cities.map(&:id))).to eq(false) }
247
- specify { expect(city.import(dummy_cities, batch_size: 1)).to eq(false) }
214
+ context do
215
+ before { expect(Chewy.client).to receive(:bulk).twice.and_call_original }
216
+ specify { expect(import(dummy_cities, update_fields: [:name])).to eq(true) }
248
217
  end
249
218
 
250
219
  context do
251
- before do
252
- stub_index(:cities) do
253
- define_type City do
254
- field :name, type: 'object', value: ->{ name == 'name1' ? name : {name: name} }
255
- end
256
- end
257
- end
258
-
259
- specify { expect(city.import(dummy_cities)).to eq(false) }
260
- specify { expect(city.import(dummy_cities.map(&:id))).to eq(false) }
261
- specify { expect(city.import(dummy_cities, batch_size: 2)).to eq(false) }
220
+ before { CitiesIndex::City.import!(dummy_cities.last) }
221
+ before { expect(Chewy.client).to receive(:bulk).once.and_call_original }
222
+ specify { expect(import(dummy_cities, update_fields: [:name])).to eq(true) }
262
223
  end
263
224
  end
264
225
 
265
- context 'parent-child relationship', :orm do
266
- let(:country) { Country.create(id: 1, name: 'country') }
267
- let(:another_country) { Country.create(id: 2, name: 'another country') }
268
-
226
+ context 'fields integrational' do
269
227
  before do
270
- stub_model(:country)
271
- stub_model(:city)
272
- adapter == :sequel ? City.many_to_one(:country) : City.belongs_to(:country)
273
- end
274
-
275
- before do
276
- stub_index(:countries) do
277
- define_type Country do
228
+ stub_index(:cities) do
229
+ define_type :city do
278
230
  field :name
279
- end
280
-
281
- define_type City do
282
- root parent: { type: 'country' }, parent_id: -> { country_id } do
283
- field :name
284
- end
231
+ field :object, type: 'object'
285
232
  end
286
233
  end
287
234
  end
288
235
 
289
- before { CountriesIndex::Country.import(country) }
290
-
291
- let(:child_city) { City.create(id: 4, country_id: country.id, name: 'city') }
292
- let(:city) { CountriesIndex::City }
236
+ let(:objects) do
237
+ [
238
+ double('Name1', id: 1, name: 'Name11', object: {foo: 11}),
239
+ double('Name2', id: 2, name: 'Name12', object: 'foo'),
240
+ double('Name3', id: 3, name: 'Name13', object: {foo: 13}),
241
+ double('Name4', id: 4, name: 'Name14', object: 'foo'),
242
+ double('Name5', id: 5, name: 'Name15', object: {foo: 15}),
243
+ double('Name6', id: '', name: 'Name16', object: {foo: 16})
244
+ ]
245
+ end
293
246
 
294
- specify { expect(city.import(child_city)).to eq(true) }
295
- specify { expect { city.import child_city }.to update_index(city).and_reindex(child_city) }
247
+ let(:old_objects) do
248
+ Array.new(6) do |i|
249
+ double("Name#{i + 1}", id: i + 1, name: "Name#{i + 1}", object: {foo: i + 1})
250
+ end
251
+ end
296
252
 
297
253
  specify do
298
- expect(CountriesIndex.client).to receive(:bulk).with(hash_including(
299
- body: [{ index: { _id: child_city.id, parent: country.id, data: { 'name' => 'city' } } }]
300
- ))
254
+ payload = subscribe_notification
255
+
256
+ expect(Chewy.client).to receive(:bulk).twice.and_call_original
257
+ import(objects, update_fields: %i[name])
258
+
259
+ expect(payload).to eq(
260
+ errors: {index: {{'type' => 'mapper_parsing_exception', 'reason' => 'object mapping for [object] tried to parse field [object] as object, but found a concrete value'} => %w[2 4]}},
261
+ import: {index: 6},
262
+ type: CitiesIndex::City
263
+ )
264
+ expect(imported_cities).to match_array([
265
+ {'id' => '1', 'name' => 'Name11', 'object' => {'foo' => 11}},
266
+ {'id' => '3', 'name' => 'Name13', 'object' => {'foo' => 13}},
267
+ {'id' => '5', 'name' => 'Name15', 'object' => {'foo' => 15}}
268
+ ])
269
+ end
301
270
 
302
- city.import child_city
271
+ specify do
272
+ payload = subscribe_notification
273
+
274
+ expect(Chewy.client).to receive(:bulk).at_least(4).at_most(6).times.and_call_original
275
+ import(objects, batch_size: 2, update_fields: %i[name])
276
+
277
+ expect(payload).to eq(
278
+ errors: {index: {{'type' => 'mapper_parsing_exception', 'reason' => 'object mapping for [object] tried to parse field [object] as object, but found a concrete value'} => %w[2 4]}},
279
+ import: {index: 6},
280
+ type: CitiesIndex::City
281
+ )
282
+ expect(imported_cities).to match_array([
283
+ {'id' => '1', 'name' => 'Name11', 'object' => {'foo' => 11}},
284
+ {'id' => '3', 'name' => 'Name13', 'object' => {'foo' => 13}},
285
+ {'id' => '5', 'name' => 'Name15', 'object' => {'foo' => 15}}
286
+ ])
303
287
  end
304
288
 
305
- context 'updating or deleting' do
306
- before { city.import child_city }
289
+ context do
290
+ before { CitiesIndex::City.import!(objects[4]) }
307
291
 
308
292
  specify do
309
- child_city.update_attributes(country_id: another_country.id)
293
+ payload = subscribe_notification
294
+
295
+ expect(Chewy.client).to receive(:bulk).at_least(3).at_most(5).times.and_call_original
296
+ import(objects, batch_size: 2, update_fields: %i[name])
297
+
298
+ expect(payload).to eq(
299
+ errors: {index: {{'type' => 'mapper_parsing_exception', 'reason' => 'object mapping for [object] tried to parse field [object] as object, but found a concrete value'} => %w[2 4]}},
300
+ import: {index: 6},
301
+ type: CitiesIndex::City
302
+ )
303
+ expect(imported_cities).to match_array([
304
+ {'id' => '1', 'name' => 'Name11', 'object' => {'foo' => 11}},
305
+ {'id' => '3', 'name' => 'Name13', 'object' => {'foo' => 13}},
306
+ {'id' => '5', 'name' => 'Name15', 'object' => {'foo' => 15}}
307
+ ])
308
+ end
309
+ end
310
310
 
311
- expect(CountriesIndex.client).to receive(:bulk).with(hash_including(
312
- body: [
313
- { delete: { _id: child_city.id, parent: country.id.to_s } },
314
- { index: { _id: child_city.id, parent: another_country.id, data: { 'name' => 'city' } } }
315
- ]
316
- ))
311
+ context do
312
+ before { CitiesIndex::City.import!(old_objects[1], old_objects[3], objects[4]) }
317
313
 
318
- city.import child_city
314
+ specify do
315
+ payload = subscribe_notification
316
+
317
+ expect(Chewy.client).to receive(:bulk).twice.and_call_original
318
+ import(objects, update_fields: %i[name])
319
+
320
+ expect(payload).to eq(
321
+ import: {index: 6},
322
+ type: CitiesIndex::City
323
+ )
324
+ expect(imported_cities).to match_array([
325
+ {'id' => '1', 'name' => 'Name11', 'object' => {'foo' => 11}},
326
+ {'id' => '2', 'name' => 'Name12', 'object' => {'foo' => 2}},
327
+ {'id' => '3', 'name' => 'Name13', 'object' => {'foo' => 13}},
328
+ {'id' => '4', 'name' => 'Name14', 'object' => {'foo' => 4}},
329
+ {'id' => '5', 'name' => 'Name15', 'object' => {'foo' => 15}}
330
+ ])
319
331
  end
320
332
 
321
333
  specify do
322
- child_city.destroy
334
+ payload = subscribe_notification
335
+
336
+ expect(Chewy.client).to receive(:bulk).at_least(3).at_most(5).times.and_call_original
337
+ import(objects, batch_size: 2, update_fields: %i[name])
338
+
339
+ expect(payload).to eq(
340
+ import: {index: 6},
341
+ type: CitiesIndex::City
342
+ )
343
+ expect(imported_cities).to match_array([
344
+ {'id' => '1', 'name' => 'Name11', 'object' => {'foo' => 11}},
345
+ {'id' => '2', 'name' => 'Name12', 'object' => {'foo' => 2}},
346
+ {'id' => '3', 'name' => 'Name13', 'object' => {'foo' => 13}},
347
+ {'id' => '4', 'name' => 'Name14', 'object' => {'foo' => 4}},
348
+ {'id' => '5', 'name' => 'Name15', 'object' => {'foo' => 15}}
349
+ ])
350
+ end
323
351
 
324
- expect(CountriesIndex.client).to receive(:bulk).with(hash_including(
325
- body: [{ delete: { _id: child_city.id, parent: country.id.to_s } }]
326
- ))
352
+ specify do
353
+ payload = subscribe_notification
354
+
355
+ expect(Chewy.client).to receive(:bulk).once.and_call_original
356
+ import(objects, update_fields: %i[name], update_failover: false)
327
357
 
328
- city.import child_city
358
+ # Full match doesn't work here.
359
+ expect(payload[:errors][:update].keys).to match([
360
+ hash_including('type' => 'document_missing_exception', 'reason' => '[city][1]: document missing'),
361
+ hash_including('type' => 'document_missing_exception', 'reason' => '[city][3]: document missing')
362
+ ])
363
+ expect(payload[:errors][:update].values).to eq([['1'], ['3']])
364
+ expect(imported_cities).to match_array([
365
+ {'id' => '2', 'name' => 'Name12', 'object' => {'foo' => 2}},
366
+ {'id' => '4', 'name' => 'Name14', 'object' => {'foo' => 4}},
367
+ {'id' => '5', 'name' => 'Name15', 'object' => {'foo' => 15}}
368
+ ])
329
369
  end
370
+ end
371
+
372
+ context do
373
+ before { CitiesIndex::City.import!(old_objects) }
330
374
 
331
375
  specify do
332
- child_city.destroy
376
+ payload = subscribe_notification
377
+
378
+ expect(Chewy.client).to receive(:bulk).once.and_call_original
379
+ import(objects, update_fields: %i[name])
380
+
381
+ expect(payload).to eq(
382
+ import: {index: 6},
383
+ type: CitiesIndex::City
384
+ )
385
+ expect(imported_cities).to match_array([
386
+ {'id' => '1', 'name' => 'Name11', 'object' => {'foo' => 1}},
387
+ {'id' => '2', 'name' => 'Name12', 'object' => {'foo' => 2}},
388
+ {'id' => '3', 'name' => 'Name13', 'object' => {'foo' => 3}},
389
+ {'id' => '4', 'name' => 'Name14', 'object' => {'foo' => 4}},
390
+ {'id' => '5', 'name' => 'Name15', 'object' => {'foo' => 5}},
391
+ {'id' => '6', 'name' => 'Name6', 'object' => {'foo' => 6}}
392
+ ])
393
+ end
394
+ end
333
395
 
334
- expect(CountriesIndex.client).to receive(:bulk).with(hash_including(
335
- body: [{ delete: { _id: child_city.id, parent: country.id.to_s } }]
336
- ))
396
+ context do
397
+ before { CitiesIndex::City.import!(old_objects) }
337
398
 
338
- city.import child_city.id
399
+ specify do
400
+ payload = subscribe_notification
401
+
402
+ expect(Chewy.client).to receive(:bulk).once.and_call_original
403
+ import(objects, update_fields: %i[object])
404
+
405
+ expect(payload).to eq(
406
+ errors: {update: {{'type' => 'mapper_parsing_exception', 'reason' => 'object mapping for [object] tried to parse field [object] as object, but found a concrete value'} => %w[2 4]}},
407
+ import: {index: 6},
408
+ type: CitiesIndex::City
409
+ )
410
+ expect(imported_cities).to match_array([
411
+ {'id' => '1', 'name' => 'Name1', 'object' => {'foo' => 11}},
412
+ {'id' => '2', 'name' => 'Name2', 'object' => {'foo' => 2}},
413
+ {'id' => '3', 'name' => 'Name3', 'object' => {'foo' => 13}},
414
+ {'id' => '4', 'name' => 'Name4', 'object' => {'foo' => 4}},
415
+ {'id' => '5', 'name' => 'Name5', 'object' => {'foo' => 15}},
416
+ {'id' => '6', 'name' => 'Name6', 'object' => {'foo' => 6}}
417
+ ])
339
418
  end
340
419
  end
341
420
  end
342
421
 
343
- context 'root id', :orm do
344
- let(:canada) { Country.create(id: 1, name: 'Canada', country_code: 'CA', rating: 4) }
345
- let(:country) { CountriesIndex::Country }
422
+ context 'error handling' do
423
+ context do
424
+ before do
425
+ stub_index(:cities) do
426
+ define_type City do
427
+ field :name, type: 'object'
428
+ end
429
+ end
430
+ end
346
431
 
347
- before do
348
- stub_model(:country)
432
+ specify { expect(import(dummy_cities)).to eq(false) }
433
+ specify { expect(import(dummy_cities.map(&:id))).to eq(false) }
434
+ specify { expect(import(dummy_cities, batch_size: 1)).to eq(false) }
349
435
  end
350
436
 
351
- before do
352
- stub_index(:countries) do
353
- define_type Country do
354
- root _id: -> { country_code } do
355
- field :name
356
- field :rating
437
+ context do
438
+ before do
439
+ stub_index(:cities) do
440
+ define_type City do
441
+ field :name, type: 'object', value: -> { name == 'name1' ? name : {name: name} }
357
442
  end
358
443
  end
359
444
  end
360
- end
361
-
362
- specify { expect(country.import(canada)).to eq(true) }
363
- specify { expect { country.import(canada) }.to update_index(country).and_reindex(canada.country_code) }
364
445
 
365
- specify do
366
- expect(CountriesIndex.client).to receive(:bulk).with(hash_including(
367
- body: [{ index: { _id: canada.country_code, data: { 'name' => 'Canada', 'rating' => 4 } } }]
368
- ))
369
-
370
- country.import canada
446
+ specify { expect(import(dummy_cities)).to eq(false) }
447
+ specify { expect(import(dummy_cities.map(&:id))).to eq(false) }
448
+ specify { expect(import(dummy_cities, batch_size: 2)).to eq(false) }
371
449
  end
450
+ end
372
451
 
373
- specify do
374
- canada.update_attributes(rating: 9)
375
-
376
- expect(CountriesIndex.client).to receive(:bulk).with(hash_including(
377
- body: [{ index: { _id: canada.country_code, data: { 'name' => 'Canada', 'rating' => 9 } } }]
378
- ))
379
-
380
- country.import canada
452
+ context 'default_import_options are set' do
453
+ before do
454
+ CitiesIndex::City.default_import_options(batch_size: 500)
381
455
  end
382
456
 
383
457
  specify do
384
- canada.destroy
385
-
386
- expect(CountriesIndex.client).to receive(:bulk).with(hash_including(
387
- body: [{ delete: { _id: canada.country_code } }]
388
- ))
389
-
390
- country.import canada
458
+ expect(CitiesIndex::City.adapter).to receive(:import).with(any_args, hash_including(batch_size: 500))
459
+ CitiesIndex::City.import
391
460
  end
461
+ end
462
+ end
392
463
 
464
+ describe '.import', :orm do
465
+ def import(*args)
466
+ CitiesIndex::City.import(*args)
393
467
  end
394
468
 
395
- context 'default_import_options is set' do
396
- before do
397
- CitiesIndex::City.default_import_options(batch_size: 500, bulk_size: 1.megabyte)
398
- end
469
+ it_behaves_like 'importing'
399
470
 
400
- specify do
401
- expect(CitiesIndex::City.adapter).to receive(:import).with(batch_size: 500, bulk_size: 1.megabyte)
402
- CitiesIndex::City.import
471
+ context 'parallel' do
472
+ def import(*args)
473
+ options = args.extract_options!
474
+ options[:parallel] = 0
475
+ CitiesIndex::City.import(*args, options)
403
476
  end
477
+
478
+ it_behaves_like 'importing'
404
479
  end
405
480
  end
406
481
 
407
482
  describe '.import!', :orm do
408
- specify { expect { city.import! }.not_to raise_error }
483
+ specify { expect { CitiesIndex::City.import! }.not_to raise_error }
409
484
 
410
485
  context do
411
486
  before do
@@ -416,17 +491,58 @@ describe Chewy::Type::Import do
416
491
  end
417
492
  end
418
493
 
419
- specify { expect { city.import!(dummy_cities) }.to raise_error Chewy::ImportFailed }
494
+ specify { expect { CitiesIndex::City.import!(dummy_cities) }.to raise_error Chewy::ImportFailed }
420
495
  end
496
+ end
421
497
 
422
- context 'when .import fails' do
423
- before do
424
- allow(city).to receive(:import) { raise }
498
+ describe '.compose' do
499
+ before do
500
+ stub_index(:cities) do
501
+ define_type :city do
502
+ crutch :names do |collection|
503
+ collection.map { |o| [o.name, o.name + '42'] }.to_h
504
+ end
505
+ field :name, value: ->(o, c) { c.names[o.name] }
506
+ field :rating
507
+ end
508
+ end
509
+ end
510
+
511
+ specify do
512
+ expect(CitiesIndex::City.compose(double(name: 'Name', rating: 42)))
513
+ .to eq('name' => 'Name42', 'rating' => 42)
514
+ end
515
+
516
+ specify do
517
+ expect(CitiesIndex::City.compose(double(name: 'Name', rating: 42), fields: %i[name]))
518
+ .to eq('name' => 'Name42')
519
+ end
520
+
521
+ context 'witchcraft' do
522
+ before { CitiesIndex::City.witchcraft! }
523
+
524
+ specify do
525
+ expect(CitiesIndex::City.compose(double(name: 'Name', rating: 42)))
526
+ .to eq('name' => 'Name42', 'rating' => 42)
527
+ end
528
+
529
+ specify do
530
+ expect(CitiesIndex::City.compose(double(name: 'Name', rating: 42), fields: %i[name]))
531
+ .to eq('name' => 'Name42')
532
+ end
533
+ end
534
+
535
+ context 'custom crutches' do
536
+ let(:crutches) { double(names: {'Name' => 'Name43'}) }
537
+
538
+ specify do
539
+ expect(CitiesIndex::City.compose(double(name: 'Name', rating: 42), crutches))
540
+ .to eq('name' => 'Name43', 'rating' => 42)
425
541
  end
426
542
 
427
543
  specify do
428
- expect(ActiveSupport::Notifications).to receive(:unsubscribe)
429
- city.import!(dummy_cities) rescue nil
544
+ expect(CitiesIndex::City.compose(double(name: 'Name', rating: 42), crutches, fields: %i[name]))
545
+ .to eq('name' => 'Name43')
430
546
  end
431
547
  end
432
548
  end