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
@@ -5,11 +5,11 @@ describe Chewy::Strategy::Atomic, :orm do
5
5
 
6
6
  before do
7
7
  stub_model(:country) do
8
- update_index('countries#country') { self }
8
+ update_index('countries') { self }
9
9
  end
10
10
 
11
11
  stub_index(:countries) do
12
- define_type Country
12
+ index_scope Country
13
13
  end
14
14
  end
15
15
 
@@ -18,35 +18,34 @@ describe Chewy::Strategy::Atomic, :orm do
18
18
 
19
19
  specify do
20
20
  expect { [country, other_country].map(&:save!) }
21
- .to update_index(CountriesIndex::Country, strategy: :atomic)
21
+ .to update_index(CountriesIndex, strategy: :atomic)
22
22
  .and_reindex(country, other_country).only
23
23
  end
24
24
 
25
25
  specify do
26
26
  expect { [country, other_country].map(&:destroy) }
27
- .to update_index(CountriesIndex::Country, strategy: :atomic)
27
+ .to update_index(CountriesIndex, strategy: :atomic)
28
28
  .and_delete(country, other_country).only
29
29
  end
30
30
 
31
31
  context do
32
32
  before do
33
33
  stub_index(:countries) do
34
- define_type Country do
35
- root id: -> { country_code } do
36
- end
34
+ index_scope Country
35
+ root id: -> { country_code } do
37
36
  end
38
37
  end
39
38
  end
40
39
 
41
40
  specify do
42
41
  expect { [country, other_country].map(&:save!) }
43
- .to update_index(CountriesIndex::Country, strategy: :atomic)
42
+ .to update_index(CountriesIndex, strategy: :atomic)
44
43
  .and_reindex('HL', 'WD').only
45
44
  end
46
45
 
47
46
  specify do
48
47
  expect { [country, other_country].map(&:destroy) }
49
- .to update_index(CountriesIndex::Country, strategy: :atomic)
48
+ .to update_index(CountriesIndex, strategy: :atomic)
50
49
  .and_delete('HL', 'WD').only
51
50
  end
52
51
 
@@ -55,7 +54,7 @@ describe Chewy::Strategy::Atomic, :orm do
55
54
  country.save!
56
55
  other_country.destroy
57
56
  end
58
- .to update_index(CountriesIndex::Country, strategy: :atomic)
57
+ .to update_index(CountriesIndex, strategy: :atomic)
59
58
  .and_reindex('HL').and_delete('WD').only
60
59
  end
61
60
  end
@@ -0,0 +1,190 @@
1
+ require 'spec_helper'
2
+
3
+ if defined?(Sidekiq)
4
+ require 'sidekiq/testing'
5
+ require 'mock_redis'
6
+
7
+ describe Chewy::Strategy::DelayedSidekiq do
8
+ around do |example|
9
+ Chewy.strategy(:bypass) { example.run }
10
+ end
11
+
12
+ before do
13
+ redis = MockRedis.new
14
+ allow(Sidekiq).to receive(:redis).and_yield(redis)
15
+ Sidekiq::Worker.clear_all
16
+ end
17
+
18
+ before do
19
+ stub_model(:city) do
20
+ update_index('cities') { self }
21
+ end
22
+
23
+ stub_index(:cities) do
24
+ index_scope City
25
+ end
26
+ end
27
+
28
+ let(:city) { City.create!(name: 'hello') }
29
+ let(:other_city) { City.create!(name: 'world') }
30
+
31
+ it 'does not trigger immediate reindex due to it`s async nature' do
32
+ expect { [city, other_city].map(&:save!) }
33
+ .not_to update_index(CitiesIndex, strategy: :delayed_sidekiq)
34
+ end
35
+
36
+ it "respects 'refresh: false' options" do
37
+ allow(Chewy).to receive(:disable_refresh_async).and_return(true)
38
+ expect(CitiesIndex).to receive(:import!).with([city.id, other_city.id], refresh: false)
39
+ scheduler = Chewy::Strategy::DelayedSidekiq::Scheduler.new(CitiesIndex, [city.id, other_city.id])
40
+ scheduler.postpone
41
+ Chewy::Strategy::DelayedSidekiq::Worker.drain
42
+ end
43
+
44
+ context 'with default config' do
45
+ it 'does schedule a job that triggers reindex with default options' do
46
+ Timecop.freeze do
47
+ expect(Sidekiq::Client).to receive(:push).with(
48
+ hash_including(
49
+ 'queue' => 'chewy',
50
+ 'at' => (Time.current.to_i.ceil(-1) + 2.seconds).to_i,
51
+ 'class' => Chewy::Strategy::DelayedSidekiq::Worker,
52
+ 'args' => ['CitiesIndex', an_instance_of(Integer)]
53
+ )
54
+ ).and_call_original
55
+
56
+ expect($stdout).not_to receive(:puts)
57
+
58
+ Sidekiq::Testing.inline! do
59
+ expect { [city, other_city].map(&:save!) }
60
+ .to update_index(CitiesIndex, strategy: :delayed_sidekiq)
61
+ .and_reindex(city, other_city).only
62
+ end
63
+ end
64
+ end
65
+ end
66
+
67
+ context 'with custom config' do
68
+ before do
69
+ CitiesIndex.strategy_config(
70
+ delayed_sidekiq: {
71
+ reindex_wrapper: lambda { |&reindex|
72
+ puts 'hello'
73
+ reindex.call
74
+ },
75
+ margin: 5,
76
+ latency: 60
77
+ }
78
+ )
79
+ end
80
+
81
+ it 'respects :strategy_config options' do
82
+ Timecop.freeze do
83
+ expect(Sidekiq::Client).to receive(:push).with(
84
+ hash_including(
85
+ 'queue' => 'chewy',
86
+ 'at' => (60.seconds.from_now.change(sec: 0) + 5.seconds).to_i,
87
+ 'class' => Chewy::Strategy::DelayedSidekiq::Worker,
88
+ 'args' => ['CitiesIndex', an_instance_of(Integer)]
89
+ )
90
+ ).and_call_original
91
+
92
+ expect($stdout).to receive(:puts).with('hello') # check that reindex_wrapper works
93
+
94
+ Sidekiq::Testing.inline! do
95
+ expect { [city, other_city].map(&:save!) }
96
+ .to update_index(CitiesIndex, strategy: :delayed_sidekiq)
97
+ .and_reindex(city, other_city).only
98
+ end
99
+ end
100
+ end
101
+ end
102
+
103
+ context 'two reindex call within the timewindow' do
104
+ it 'accumulates all ids does the reindex one time' do
105
+ Timecop.freeze do
106
+ expect(CitiesIndex).to receive(:import!).with([other_city.id, city.id]).once
107
+ scheduler = Chewy::Strategy::DelayedSidekiq::Scheduler.new(CitiesIndex, [city.id])
108
+ scheduler.postpone
109
+ scheduler = Chewy::Strategy::DelayedSidekiq::Scheduler.new(CitiesIndex, [other_city.id])
110
+ scheduler.postpone
111
+ Chewy::Strategy::DelayedSidekiq::Worker.drain
112
+ end
113
+ end
114
+
115
+ context 'one call with update_fields another one without update_fields' do
116
+ it 'does reindex of all fields' do
117
+ Timecop.freeze do
118
+ expect(CitiesIndex).to receive(:import!).with([other_city.id, city.id]).once
119
+ scheduler = Chewy::Strategy::DelayedSidekiq::Scheduler.new(CitiesIndex, [city.id], update_fields: ['name'])
120
+ scheduler.postpone
121
+ scheduler = Chewy::Strategy::DelayedSidekiq::Scheduler.new(CitiesIndex, [other_city.id])
122
+ scheduler.postpone
123
+ Chewy::Strategy::DelayedSidekiq::Worker.drain
124
+ end
125
+ end
126
+ end
127
+
128
+ context 'both calls with different update fields' do
129
+ it 'deos reindex with union of fields' do
130
+ Timecop.freeze do
131
+ expect(CitiesIndex).to receive(:import!).with([other_city.id, city.id], update_fields: %w[description name]).once
132
+ scheduler = Chewy::Strategy::DelayedSidekiq::Scheduler.new(CitiesIndex, [city.id], update_fields: ['name'])
133
+ scheduler.postpone
134
+ scheduler = Chewy::Strategy::DelayedSidekiq::Scheduler.new(CitiesIndex, [other_city.id], update_fields: ['description'])
135
+ scheduler.postpone
136
+ Chewy::Strategy::DelayedSidekiq::Worker.drain
137
+ end
138
+ end
139
+ end
140
+ end
141
+
142
+ context 'two calls within different timewindows' do
143
+ it 'does two separate reindexes' do
144
+ Timecop.freeze do
145
+ expect(CitiesIndex).to receive(:import!).with([city.id]).once
146
+ expect(CitiesIndex).to receive(:import!).with([other_city.id]).once
147
+ Timecop.travel(20.seconds.ago) do
148
+ scheduler = Chewy::Strategy::DelayedSidekiq::Scheduler.new(CitiesIndex, [city.id])
149
+ scheduler.postpone
150
+ end
151
+ scheduler = Chewy::Strategy::DelayedSidekiq::Scheduler.new(CitiesIndex, [other_city.id])
152
+ scheduler.postpone
153
+ Chewy::Strategy::DelayedSidekiq::Worker.drain
154
+ end
155
+ end
156
+ end
157
+
158
+ context 'first call has update_fields' do
159
+ it 'does first reindex with the expected update_fields and second without update_fields' do
160
+ Timecop.freeze do
161
+ expect(CitiesIndex).to receive(:import!).with([city.id], update_fields: ['name']).once
162
+ expect(CitiesIndex).to receive(:import!).with([other_city.id]).once
163
+ Timecop.travel(20.seconds.ago) do
164
+ scheduler = Chewy::Strategy::DelayedSidekiq::Scheduler.new(CitiesIndex, [city.id], update_fields: ['name'])
165
+ scheduler.postpone
166
+ end
167
+ scheduler = Chewy::Strategy::DelayedSidekiq::Scheduler.new(CitiesIndex, [other_city.id])
168
+ scheduler.postpone
169
+ Chewy::Strategy::DelayedSidekiq::Worker.drain
170
+ end
171
+ end
172
+ end
173
+
174
+ context 'both calls have update_fields option' do
175
+ it 'does both reindexes with their expected update_fields option' do
176
+ Timecop.freeze do
177
+ expect(CitiesIndex).to receive(:import!).with([city.id], update_fields: ['name']).once
178
+ expect(CitiesIndex).to receive(:import!).with([other_city.id], update_fields: ['description']).once
179
+ Timecop.travel(20.seconds.ago) do
180
+ scheduler = Chewy::Strategy::DelayedSidekiq::Scheduler.new(CitiesIndex, [city.id], update_fields: ['name'])
181
+ scheduler.postpone
182
+ end
183
+ scheduler = Chewy::Strategy::DelayedSidekiq::Scheduler.new(CitiesIndex, [other_city.id], update_fields: ['description'])
184
+ scheduler.postpone
185
+ Chewy::Strategy::DelayedSidekiq::Worker.drain
186
+ end
187
+ end
188
+ end
189
+ end
190
+ end
@@ -0,0 +1,214 @@
1
+ require 'spec_helper'
2
+
3
+ if defined?(::Sidekiq)
4
+ require 'sidekiq/testing'
5
+
6
+ describe Chewy::Strategy::LazySidekiq do
7
+ around do |example|
8
+ sidekiq_settings = Chewy.settings[:sidekiq]
9
+ Chewy.settings[:sidekiq] = {queue: 'low'}
10
+ Chewy.strategy(:bypass) { example.run }
11
+ Chewy.settings[:sidekiq] = sidekiq_settings
12
+ end
13
+ before { ::Sidekiq::Worker.clear_all }
14
+
15
+ context 'strategy' do
16
+ before do
17
+ stub_model(:city) do
18
+ update_index('cities') { self }
19
+ end
20
+
21
+ stub_index(:cities) do
22
+ index_scope City
23
+ end
24
+ end
25
+
26
+ let(:city) { City.create!(name: 'hello') }
27
+ let(:other_city) { City.create!(name: 'world') }
28
+
29
+ it 'does not update indices synchronously' do
30
+ expect { [city, other_city].map(&:save!) }
31
+ .not_to update_index(CitiesIndex, strategy: :lazy_sidekiq)
32
+ end
33
+
34
+ it 'updates indices asynchronously on record save' do
35
+ expect(::Sidekiq::Client).to receive(:push)
36
+ .with(hash_including(
37
+ 'class' => Chewy::Strategy::LazySidekiq::IndicesUpdateWorker,
38
+ 'queue' => 'low'
39
+ ))
40
+ .and_call_original
41
+ .once
42
+ ::Sidekiq::Testing.inline! do
43
+ expect { [city, other_city].map(&:save!) }
44
+ .to update_index(CitiesIndex, strategy: :lazy_sidekiq)
45
+ .and_reindex(city, other_city).only
46
+ end
47
+ end
48
+
49
+ it 'updates indices asynchronously with falling back to sidekiq strategy on record destroy' do
50
+ expect(::Sidekiq::Client).not_to receive(:push)
51
+ .with(hash_including(
52
+ 'class' => Chewy::Strategy::LazySidekiq::IndicesUpdateWorker,
53
+ 'queue' => 'low'
54
+ ))
55
+ expect(::Sidekiq::Client).to receive(:push)
56
+ .with(hash_including(
57
+ 'class' => Chewy::Strategy::Sidekiq::Worker,
58
+ 'queue' => 'low',
59
+ 'args' => ['CitiesIndex', [city.id, other_city.id]]
60
+ ))
61
+ .and_call_original
62
+ .once
63
+ ::Sidekiq::Testing.inline! do
64
+ expect { [city, other_city].map(&:destroy) }.to update_index(CitiesIndex, strategy: :sidekiq)
65
+ end
66
+ end
67
+
68
+ it 'calls Index#import!' do
69
+ allow(City).to receive(:where).with(id: [city.id, other_city.id]).and_return([city, other_city])
70
+ expect(city).to receive(:run_chewy_callbacks).and_call_original
71
+ expect(other_city).to receive(:run_chewy_callbacks).and_call_original
72
+
73
+ expect do
74
+ ::Sidekiq::Testing.inline! do
75
+ Chewy::Strategy::LazySidekiq::IndicesUpdateWorker.new.perform({'City' => [city.id, other_city.id]})
76
+ end
77
+ end.to update_index(CitiesIndex).and_reindex(city, other_city).only
78
+ end
79
+
80
+ context 'when Chewy.disable_refresh_async is true' do
81
+ before do
82
+ allow(Chewy).to receive(:disable_refresh_async).and_return(true)
83
+ end
84
+
85
+ it 'calls Index#import! with refresh false' do
86
+ allow(City).to receive(:where).with(id: [city.id, other_city.id]).and_return([city, other_city])
87
+ expect(city).to receive(:run_chewy_callbacks).and_call_original
88
+ expect(other_city).to receive(:run_chewy_callbacks).and_call_original
89
+
90
+ expect do
91
+ ::Sidekiq::Testing.inline! do
92
+ Chewy::Strategy::LazySidekiq::IndicesUpdateWorker.new.perform({'City' => [city.id, other_city.id]})
93
+ end
94
+ end.to update_index(CitiesIndex).and_reindex(city, other_city).only.no_refresh
95
+ end
96
+ end
97
+ end
98
+
99
+ context 'integration' do
100
+ around { |example| ::Sidekiq::Testing.inline! { example.run } }
101
+
102
+ let(:update_condition) { true }
103
+
104
+ before do
105
+ city_model
106
+ country_model
107
+
108
+ City.belongs_to :country
109
+ Country.has_many :cities
110
+
111
+ stub_index(:cities) do
112
+ index_scope City
113
+ end
114
+
115
+ stub_index(:countries) do
116
+ index_scope Country
117
+ end
118
+ end
119
+
120
+ context 'state dependent' do
121
+ let(:city_model) do
122
+ stub_model(:city) do
123
+ update_index(-> { 'cities' }, :self)
124
+ update_index('countries') { changes['country_id'] || previous_changes['country_id'] || country }
125
+ end
126
+ end
127
+
128
+ let(:country_model) do
129
+ stub_model(:country) do
130
+ update_index('cities', if: -> { state_dependent_update_condition }) { cities }
131
+ update_index(-> { 'countries' }, :self)
132
+ attr_accessor :state_dependent_update_condition
133
+ end
134
+ end
135
+
136
+ context 'city updates' do
137
+ let!(:country1) { Country.create!(id: 1) }
138
+ let!(:country2) { Country.create!(id: 2) }
139
+ let!(:city) { City.create!(id: 1, country: country1) }
140
+
141
+ it 'does not update index of removed entity because model state on the moment of save cannot be fetched' do
142
+ expect { city.update!(country: nil) }.not_to update_index('countries', strategy: :lazy_sidekiq)
143
+ end
144
+ it 'does not update index of removed entity because model state on the moment of save cannot be fetched' do
145
+ expect { city.update!(country: country2) }.to update_index('countries', strategy: :lazy_sidekiq).and_reindex(country2).only
146
+ end
147
+ end
148
+
149
+ context 'country updates' do
150
+ let!(:country) do
151
+ cities = Array.new(2) { |i| City.create!(id: i) }
152
+ Country.create!(id: 1, cities: cities, state_dependent_update_condition: update_condition)
153
+ end
154
+
155
+ it 'does not update index because state of attribute cannot be fetched' do
156
+ expect { country.save! }.not_to update_index('cities', strategy: :lazy_sidekiq)
157
+ end
158
+ end
159
+ end
160
+
161
+ context 'state independent' do
162
+ let(:city_model) do
163
+ stub_model(:city) do
164
+ update_index(-> { 'cities' }, :self)
165
+ update_index('countries') { country }
166
+ end
167
+ end
168
+
169
+ let(:country_model) do
170
+ stub_model(:country) do
171
+ update_index('cities', if: -> { state_independent_update_condition }) { cities }
172
+ update_index(-> { 'countries' }, :self)
173
+ end
174
+ end
175
+
176
+ before do
177
+ allow_any_instance_of(Country).to receive(:state_independent_update_condition).and_return(update_condition)
178
+ end
179
+
180
+ context 'when city updates' do
181
+ let!(:country1) { Country.create!(id: 1) }
182
+ let!(:country2) { Country.create!(id: 2) }
183
+ let!(:city) { City.create!(id: 1, country: country1) }
184
+
185
+ specify { expect { city.save! }.to update_index('cities', strategy: :lazy_sidekiq).and_reindex(city).only }
186
+ specify { expect { city.save! }.to update_index('countries', strategy: :lazy_sidekiq).and_reindex(country1).only }
187
+
188
+ specify { expect { city.destroy }.not_to update_index('cities').and_reindex(city).only }
189
+ specify { expect { city.destroy }.to update_index('countries', strategy: :sidekiq).and_reindex(country1).only }
190
+
191
+ specify { expect { city.update!(country: nil) }.to update_index('cities', strategy: :lazy_sidekiq).and_reindex(city).only }
192
+ specify { expect { city.update!(country: country2) }.to update_index('cities', strategy: :lazy_sidekiq).and_reindex(city).only }
193
+ end
194
+
195
+ context 'when country updates' do
196
+ let!(:country) do
197
+ cities = Array.new(2) { |i| City.create!(id: i) }
198
+ Country.create!(id: 1, cities: cities)
199
+ end
200
+ specify { expect { country.save! }.to update_index('cities', strategy: :lazy_sidekiq).and_reindex(country.cities).only }
201
+ specify { expect { country.save! }.to update_index('countries', strategy: :lazy_sidekiq).and_reindex(country).only }
202
+
203
+ specify { expect { country.destroy }.to update_index('cities', strategy: :sidekiq).and_reindex(country.cities).only }
204
+ specify { expect { country.destroy }.not_to update_index('countries').and_reindex(country).only }
205
+
206
+ context 'when update condition is false' do
207
+ let(:update_condition) { false }
208
+ specify { expect { country.save! }.not_to update_index('cities', strategy: :lazy_sidekiq) }
209
+ end
210
+ end
211
+ end
212
+ end
213
+ end
214
+ end
@@ -4,15 +4,20 @@ if defined?(::Sidekiq)
4
4
  require 'sidekiq/testing'
5
5
 
6
6
  describe Chewy::Strategy::Sidekiq do
7
- around { |example| Chewy.strategy(:bypass) { example.run } }
7
+ around do |example|
8
+ sidekiq_settings = Chewy.settings[:sidekiq]
9
+ Chewy.settings[:sidekiq] = {queue: 'low'}
10
+ Chewy.strategy(:bypass) { example.run }
11
+ Chewy.settings[:sidekiq] = sidekiq_settings
12
+ end
8
13
  before { ::Sidekiq::Worker.clear_all }
9
14
  before do
10
15
  stub_model(:city) do
11
- update_index('cities#city') { self }
16
+ update_index('cities') { self }
12
17
  end
13
18
 
14
19
  stub_index(:cities) do
15
- define_type City
20
+ index_scope City
16
21
  end
17
22
  end
18
23
 
@@ -21,28 +26,27 @@ if defined?(::Sidekiq)
21
26
 
22
27
  specify do
23
28
  expect { [city, other_city].map(&:save!) }
24
- .not_to update_index(CitiesIndex::City, strategy: :sidekiq)
29
+ .not_to update_index(CitiesIndex, strategy: :sidekiq)
25
30
  end
26
31
 
27
32
  specify do
28
- Chewy.settings[:sidekiq] = {queue: 'low'}
29
33
  expect(::Sidekiq::Client).to receive(:push).with(hash_including('queue' => 'low')).and_call_original
30
34
  ::Sidekiq::Testing.inline! do
31
35
  expect { [city, other_city].map(&:save!) }
32
- .to update_index(CitiesIndex::City, strategy: :sidekiq)
36
+ .to update_index(CitiesIndex, strategy: :sidekiq)
33
37
  .and_reindex(city, other_city).only
34
38
  end
35
39
  end
36
40
 
37
41
  specify do
38
- expect(CitiesIndex::City).to receive(:import!).with([city.id, other_city.id], suffix: '201601')
39
- Chewy::Strategy::Sidekiq::Worker.new.perform('CitiesIndex::City', [city.id, other_city.id], suffix: '201601')
42
+ expect(CitiesIndex).to receive(:import!).with([city.id, other_city.id], suffix: '201601')
43
+ Chewy::Strategy::Sidekiq::Worker.new.perform('CitiesIndex', [city.id, other_city.id], suffix: '201601')
40
44
  end
41
45
 
42
46
  specify do
43
47
  allow(Chewy).to receive(:disable_refresh_async).and_return(true)
44
- expect(CitiesIndex::City).to receive(:import!).with([city.id, other_city.id], suffix: '201601', refresh: false)
45
- Chewy::Strategy::Sidekiq::Worker.new.perform('CitiesIndex::City', [city.id, other_city.id], suffix: '201601')
48
+ expect(CitiesIndex).to receive(:import!).with([city.id, other_city.id], suffix: '201601', refresh: false)
49
+ Chewy::Strategy::Sidekiq::Worker.new.perform('CitiesIndex', [city.id, other_city.id], suffix: '201601')
46
50
  end
47
51
  end
48
52
  end
@@ -14,7 +14,10 @@ describe Chewy::Strategy do
14
14
  end
15
15
 
16
16
  describe '#push' do
17
- specify { expect { strategy.push(:unexistant) }.to raise_error(RuntimeError).with_message("Can't find update strategy `unexistant`") }
17
+ specify do
18
+ expect { strategy.push(:unexistant) }
19
+ .to raise_error(RuntimeError).with_message("Can't find update strategy `unexistant`")
20
+ end
18
21
 
19
22
  specify do
20
23
  expect { strategy.push(:atomic) }
@@ -35,7 +38,10 @@ describe Chewy::Strategy do
35
38
  end
36
39
 
37
40
  describe '#wrap' do
38
- specify { expect { strategy.wrap(:unexistant) {} }.to raise_error(RuntimeError).with_message("Can't find update strategy `unexistant`") }
41
+ specify do
42
+ expect { strategy.wrap(:unexistant) {} }
43
+ .to raise_error(RuntimeError).with_message("Can't find update strategy `unexistant`")
44
+ end
39
45
 
40
46
  specify do
41
47
  expect do
@@ -49,11 +55,11 @@ describe Chewy::Strategy do
49
55
  context 'nesting', :orm do
50
56
  before do
51
57
  stub_model(:city) do
52
- update_index('cities#city') { self }
58
+ update_index('cities') { self }
53
59
  end
54
60
 
55
61
  stub_index(:cities) do
56
- define_type City
62
+ index_scope City
57
63
  end
58
64
  end
59
65
 
@@ -64,12 +70,12 @@ describe Chewy::Strategy do
64
70
  around { |example| Chewy.strategy(:bypass) { example.run } }
65
71
 
66
72
  specify do
67
- expect(CitiesIndex::City).not_to receive(:import!)
73
+ expect(CitiesIndex).not_to receive(:import!)
68
74
  [city, other_city].map(&:save!)
69
75
  end
70
76
 
71
77
  specify do
72
- expect(CitiesIndex::City).to receive(:import!).with([city.id, other_city.id]).once
78
+ expect(CitiesIndex).to receive(:import!).with([city.id, other_city.id]).once
73
79
  Chewy.strategy(:atomic) { [city, other_city].map(&:save!) }
74
80
  end
75
81
  end
@@ -78,41 +84,39 @@ describe Chewy::Strategy do
78
84
  around { |example| Chewy.strategy(:urgent) { example.run } }
79
85
 
80
86
  specify do
81
- expect(CitiesIndex::City).to receive(:import!).at_least(2).times
87
+ expect(CitiesIndex).to receive(:import!).at_least(2).times
82
88
  [city, other_city].map(&:save!)
83
89
  end
84
90
 
85
91
  specify do
86
- expect(CitiesIndex::City).to receive(:import!).with([city.id, other_city.id]).once
92
+ expect(CitiesIndex).to receive(:import!).with([city.id, other_city.id]).once
87
93
  Chewy.strategy(:atomic) { [city, other_city].map(&:save!) }
88
94
  end
89
95
 
90
96
  context 'hash passed to urgent' do
91
97
  before do
92
- stub_index(:cities) do
93
- define_type :city
94
- end
98
+ stub_index(:cities)
95
99
 
96
100
  stub_model(:city) do
97
- update_index('cities#city') { {name: name} }
101
+ update_index('cities') { {name: name} }
98
102
  end
99
103
  end
100
104
 
101
105
  specify do
102
106
  [city, other_city].map(&:save!)
103
- expect(CitiesIndex::City.total_count).to eq(4)
107
+ expect(CitiesIndex.total_count).to eq(4)
104
108
  end
105
109
 
106
110
  context do
107
111
  before do
108
112
  stub_model(:city) do
109
- update_index('cities#city') { {id: id.to_s, name: name} }
113
+ update_index('cities') { {id: id.to_s, name: name} }
110
114
  end
111
115
  end
112
116
 
113
117
  specify do
114
118
  [city, other_city].map(&:save!)
115
- expect(CitiesIndex::City.total_count).to eq(2)
119
+ expect(CitiesIndex.total_count).to eq(2)
116
120
  end
117
121
  end
118
122
  end