chewy 7.3.6 → 7.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +2 -28
  3. data/.rubocop.yml +1 -1
  4. data/CHANGELOG.md +18 -1
  5. data/Gemfile +4 -4
  6. data/README.md +24 -1
  7. data/chewy.gemspec +3 -17
  8. data/gemfiles/base.gemfile +12 -0
  9. data/gemfiles/rails.6.1.activerecord.gemfile +2 -1
  10. data/gemfiles/rails.7.0.activerecord.gemfile +2 -1
  11. data/gemfiles/rails.7.1.activerecord.gemfile +2 -1
  12. data/lib/chewy/config.rb +15 -11
  13. data/lib/chewy/elastic_client.rb +31 -0
  14. data/lib/chewy/fields/root.rb +1 -1
  15. data/lib/chewy/index/adapter/active_record.rb +1 -1
  16. data/lib/chewy/index/adapter/object.rb +3 -3
  17. data/lib/chewy/index/adapter/orm.rb +2 -2
  18. data/lib/chewy/index/import/bulk_builder.rb +2 -2
  19. data/lib/chewy/index/import.rb +2 -2
  20. data/lib/chewy/index/observe/active_record_methods.rb +1 -1
  21. data/lib/chewy/index/syncer.rb +1 -1
  22. data/lib/chewy/minitest/search_index_receiver.rb +3 -1
  23. data/lib/chewy/rake_helper.rb +3 -3
  24. data/lib/chewy/rspec/update_index.rb +3 -3
  25. data/lib/chewy/runtime/version.rb +1 -1
  26. data/lib/chewy/search/parameters/indices.rb +1 -1
  27. data/lib/chewy/search/parameters/storage.rb +1 -1
  28. data/lib/chewy/search/parameters.rb +3 -3
  29. data/lib/chewy/search.rb +6 -4
  30. data/lib/chewy/strategy/delayed_sidekiq/scheduler.rb +3 -0
  31. data/lib/chewy/strategy/delayed_sidekiq.rb +13 -0
  32. data/lib/chewy/version.rb +1 -1
  33. data/lib/chewy.rb +3 -7
  34. data/spec/chewy/elastic_client_spec.rb +26 -0
  35. data/spec/chewy/index/actions_spec.rb +4 -4
  36. data/spec/chewy/index/import/bulk_builder_spec.rb +3 -3
  37. data/spec/chewy/index/import_spec.rb +3 -3
  38. data/spec/chewy/minitest/helpers_spec.rb +1 -1
  39. data/spec/chewy/minitest/search_index_receiver_spec.rb +6 -4
  40. data/spec/chewy/rake_helper_spec.rb +4 -4
  41. data/spec/chewy/rspec/helpers_spec.rb +1 -1
  42. data/spec/chewy/search/pagination/kaminari_examples.rb +1 -1
  43. data/spec/chewy/search/pagination/kaminari_spec.rb +1 -1
  44. data/spec/chewy/strategy/active_job_spec.rb +8 -8
  45. data/spec/chewy/strategy/delayed_sidekiq_spec.rb +12 -0
  46. data/spec/chewy/strategy/lazy_sidekiq_spec.rb +10 -10
  47. data/spec/chewy/strategy/sidekiq_spec.rb +4 -4
  48. data/spec/chewy_spec.rb +7 -4
  49. data/spec/spec_helper.rb +1 -1
  50. metadata +8 -269
  51. data/gemfiles/rails.5.2.activerecord.gemfile +0 -11
  52. data/gemfiles/rails.6.0.activerecord.gemfile +0 -11
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ describe Chewy::ElasticClient do
4
+ describe 'payload inspection' do
5
+ let(:filter) { instance_double('Proc') }
6
+ let!(:filter_previous_value) { Chewy.before_es_request_filter }
7
+
8
+ before do
9
+ Chewy.massacre
10
+ stub_index(:products) do
11
+ field :id, type: :integer
12
+ end
13
+ ProductsIndex.create
14
+ Chewy.before_es_request_filter = filter
15
+ end
16
+
17
+ after do
18
+ Chewy.before_es_request_filter = filter_previous_value
19
+ end
20
+
21
+ it 'call filter with the request body' do
22
+ expect(filter).to receive(:call).with(:search, [{body: {size: 0}, index: ['products']}], {})
23
+ Chewy.client.search({index: ['products'], body: {size: 0}}).to_a
24
+ end
25
+ end
26
+ end
@@ -610,7 +610,7 @@ describe Chewy::Index::Actions do
610
610
  specify 'with journal application' do
611
611
  cities
612
612
  p 'cities created1'
613
- ::ActiveRecord::Base.connection.close if defined?(::ActiveRecord::Base)
613
+ ActiveRecord::Base.connection.close if defined?(ActiveRecord::Base)
614
614
  [
615
615
  parallel_update,
616
616
  Thread.new do
@@ -619,7 +619,7 @@ describe Chewy::Index::Actions do
619
619
  p 'end reset1'
620
620
  end
621
621
  ].map(&:join)
622
- ::ActiveRecord::Base.connection.reconnect! if defined?(::ActiveRecord::Base)
622
+ ActiveRecord::Base.connection.reconnect! if defined?(ActiveRecord::Base)
623
623
  p 'expect1'
624
624
  expect(CitiesIndex::City.pluck(:_id, :name)).to contain_exactly(%w[1 NewName1], %w[2 Name2], %w[3 NewName3])
625
625
  p 'end expect1'
@@ -628,7 +628,7 @@ describe Chewy::Index::Actions do
628
628
  specify 'without journal application' do
629
629
  cities
630
630
  p 'cities created2'
631
- ::ActiveRecord::Base.connection.close if defined?(::ActiveRecord::Base)
631
+ ActiveRecord::Base.connection.close if defined?(ActiveRecord::Base)
632
632
  [
633
633
  parallel_update,
634
634
  Thread.new do
@@ -637,7 +637,7 @@ describe Chewy::Index::Actions do
637
637
  p 'end reset2'
638
638
  end
639
639
  ].map(&:join)
640
- ::ActiveRecord::Base.connection.reconnect! if defined?(::ActiveRecord::Base)
640
+ ActiveRecord::Base.connection.reconnect! if defined?(ActiveRecord::Base)
641
641
  p 'expect2'
642
642
  expect(CitiesIndex::City.pluck(:_id, :name)).to contain_exactly(%w[1 Name1], %w[2 Name2], %w[3 Name3])
643
643
  p 'end expect2'
@@ -132,7 +132,7 @@ describe Chewy::Index::Import::BulkBuilder do
132
132
  before do
133
133
  stub_index(:cities) do
134
134
  crutch :names do |collection|
135
- collection.map { |item| [item.id, "Name#{item.id}"] }.to_h
135
+ collection.to_h { |item| [item.id, "Name#{item.id}"] }
136
136
  end
137
137
 
138
138
  field :name, value: ->(o, c) { c.names[o.id] }
@@ -198,7 +198,7 @@ describe Chewy::Index::Import::BulkBuilder do
198
198
  index_scope Comment
199
199
 
200
200
  crutch :content_with_crutches do |collection| # collection here is a current batch of products
201
- collection.map { |comment| [comment.id, "[crutches] #{comment.content}"] }.to_h
201
+ collection.to_h { |comment| [comment.id, "[crutches] #{comment.content}"] }
202
202
  end
203
203
 
204
204
  field :content
@@ -272,7 +272,7 @@ describe Chewy::Index::Import::BulkBuilder do
272
272
  default_import_options raw_import: ->(hash) { SimpleComment.new(hash) }
273
273
 
274
274
  crutch :content_with_crutches do |collection| # collection here is a current batch of products
275
- collection.map { |comment| [comment.id, "[crutches] #{comment.content}"] }.to_h
275
+ collection.to_h { |comment| [comment.id, "[crutches] #{comment.content}"] }
276
276
  end
277
277
 
278
278
  field :content
@@ -215,8 +215,8 @@ describe Chewy::Index::Import do
215
215
  payload = subscribe_notification
216
216
  import dummy_cities, batch_size: 2
217
217
  expect(payload).to eq(index: CitiesIndex,
218
- errors: {index: {mapper_parsing_exception => %w[1 2 3]}},
219
- import: {index: 3})
218
+ errors: {index: {mapper_parsing_exception => %w[1 2 3]}},
219
+ import: {index: 3})
220
220
  end
221
221
  end
222
222
  end
@@ -567,7 +567,7 @@ describe Chewy::Index::Import do
567
567
  before do
568
568
  stub_index(:cities) do
569
569
  crutch :names do |collection|
570
- collection.map { |o| [o.name, "#{o.name}42"] }.to_h
570
+ collection.to_h { |o| [o.name, "#{o.name}42"] }
571
571
  end
572
572
  field :name, value: ->(o, c) { c.names[o.name] }
573
573
  field :rating
@@ -10,7 +10,7 @@ describe :minitest_helper do
10
10
  expect(haystack).to include(needle)
11
11
  end
12
12
 
13
- include ::Chewy::Minitest::Helpers
13
+ include Chewy::Minitest::Helpers
14
14
 
15
15
  def assert_equal(expected, actual, message)
16
16
  raise message unless expected == actual
@@ -24,6 +24,8 @@ describe :search_index_receiver do
24
24
  SearchIndexReceiver.new
25
25
  end
26
26
 
27
+ let(:dummy_class) { Struct.new(:id) }
28
+
27
29
  before do
28
30
  stub_index(:dummies) do
29
31
  root value: ->(_o) { {} }
@@ -82,12 +84,12 @@ describe :search_index_receiver do
82
84
  end
83
85
 
84
86
  specify 'validates that an object was indexed' do
85
- dummy = OpenStruct.new(id: 1)
87
+ dummy = dummy_class.new(1)
86
88
  expect(receiver.indexed?(dummy, DummiesIndex)).to be(true)
87
89
  end
88
90
 
89
91
  specify 'doesn\'t validate than unindexed objects were indexed' do
90
- dummy = OpenStruct.new(id: 2)
92
+ dummy = dummy_class.new(2)
91
93
  expect(receiver.indexed?(dummy, DummiesIndex)).to be(false)
92
94
  end
93
95
  end
@@ -98,12 +100,12 @@ describe :search_index_receiver do
98
100
  end
99
101
 
100
102
  specify 'validates than an object was deleted' do
101
- dummy = OpenStruct.new(id: 1)
103
+ dummy = dummy_class.new(1)
102
104
  expect(receiver.deleted?(dummy, DummiesIndex)).to be(true)
103
105
  end
104
106
 
105
107
  specify 'doesn\'t validate than undeleted objects were deleted' do
106
- dummy = OpenStruct.new(id: 2)
108
+ dummy = dummy_class.new(2)
107
109
  expect(receiver.deleted?(dummy, DummiesIndex)).to be(false)
108
110
  end
109
111
  end
@@ -105,10 +105,10 @@ Total: \\d+s\\Z
105
105
  expect { described_class.reset(only: [CitiesIndex], output: output) }
106
106
  .to update_index(CitiesIndex)
107
107
  expect(output.string).to include(
108
- "############################################################\n"\
109
- "WARN: You are risking to lose some changes during the reset.\n" \
110
- " Please consider enabling journaling.\n" \
111
- ' See https://github.com/toptal/chewy#journaling'
108
+ "############################################################\n" \
109
+ "WARN: You are risking to lose some changes during the reset.\n " \
110
+ "Please consider enabling journaling.\n " \
111
+ 'See https://github.com/toptal/chewy#journaling'
112
112
  )
113
113
  end
114
114
  end
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe :rspec_helper do
4
- include ::Chewy::Rspec::Helpers
4
+ include Chewy::Rspec::Helpers
5
5
 
6
6
  before do
7
7
  stub_model(:city)
@@ -24,7 +24,7 @@ shared_examples :kaminari do |request_base_class|
24
24
  let(:data) { Array.new(10) { |i| {id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next}.stringify_keys! } }
25
25
 
26
26
  before { ProductsIndex.import!(data.map { |h| double(h) }) }
27
- before { allow(::Kaminari.config).to receive_messages(default_per_page: 3) }
27
+ before { allow(Kaminari.config).to receive_messages(default_per_page: 3) }
28
28
 
29
29
  describe '#per, #page' do
30
30
  specify { expect(search.map { |e| e.attributes.except(*except_fields) }).to match_array(data) }
@@ -6,7 +6,7 @@ describe Chewy::Search::Pagination::Kaminari do
6
6
  let(:data) { Array.new(12) { |i| {id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next}.stringify_keys! } }
7
7
 
8
8
  before { ProductsIndex.import!(data.map { |h| double(h) }) }
9
- before { allow(::Kaminari.config).to receive_messages(default_per_page: 17) }
9
+ before { allow(Kaminari.config).to receive_messages(default_per_page: 17) }
10
10
 
11
11
  specify { expect(search.objects.class).to eq(Kaminari::PaginatableArray) }
12
12
  specify { expect(search.objects.total_count).to eq(12) }
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- if defined?(::ActiveJob)
3
+ if defined?(ActiveJob)
4
4
  describe Chewy::Strategy::ActiveJob do
5
5
  around do |example|
6
6
  active_job_settings = Chewy.settings[:active_job]
@@ -9,12 +9,12 @@ if defined?(::ActiveJob)
9
9
  Chewy.settings[:active_job] = active_job_settings
10
10
  end
11
11
  before(:all) do
12
- ::ActiveJob::Base.logger = Chewy.logger
12
+ ActiveJob::Base.logger = Chewy.logger
13
13
  end
14
14
  before do
15
- ::ActiveJob::Base.queue_adapter = :test
16
- ::ActiveJob::Base.queue_adapter.enqueued_jobs.clear
17
- ::ActiveJob::Base.queue_adapter.performed_jobs.clear
15
+ ActiveJob::Base.queue_adapter = :test
16
+ ActiveJob::Base.queue_adapter.enqueued_jobs.clear
17
+ ActiveJob::Base.queue_adapter.performed_jobs.clear
18
18
  end
19
19
 
20
20
  before do
@@ -39,7 +39,7 @@ if defined?(::ActiveJob)
39
39
  Chewy.strategy(:active_job) do
40
40
  [city, other_city].map(&:save!)
41
41
  end
42
- enqueued_job = ::ActiveJob::Base.queue_adapter.enqueued_jobs.first
42
+ enqueued_job = ActiveJob::Base.queue_adapter.enqueued_jobs.first
43
43
  expect(enqueued_job[:job]).to eq(Chewy::Strategy::ActiveJob::Worker)
44
44
  expect(enqueued_job[:queue]).to eq('low')
45
45
  end
@@ -48,12 +48,12 @@ if defined?(::ActiveJob)
48
48
  Chewy.strategy(:active_job) do
49
49
  [city, other_city].map(&:save!)
50
50
  end
51
- enqueued_job = ::ActiveJob::Base.queue_adapter.enqueued_jobs.first
51
+ enqueued_job = ActiveJob::Base.queue_adapter.enqueued_jobs.first
52
52
  expect(enqueued_job[:queue]).to eq('low')
53
53
  end
54
54
 
55
55
  specify do
56
- ::ActiveJob::Base.queue_adapter = :inline
56
+ ActiveJob::Base.queue_adapter = :inline
57
57
  expect { [city, other_city].map(&:save!) }
58
58
  .to update_index(CitiesIndex, strategy: :active_job)
59
59
  .and_reindex(city, other_city).only
@@ -186,5 +186,17 @@ if defined?(Sidekiq)
186
186
  end
187
187
  end
188
188
  end
189
+
190
+ describe '#clear_delayed_sidekiq_timechunks test helper' do
191
+ it 'clears redis from the timechunk sorted sets to avoid leak between tests' do
192
+ timechunks_set = -> { Sidekiq.redis { |redis| redis.zrange('chewy:delayed_sidekiq:CitiesIndex:timechunks', 0, -1) } }
193
+
194
+ expect { CitiesIndex.import!([1], strategy: :delayed_sidekiq) }
195
+ .to change { timechunks_set.call.size }.by(1)
196
+
197
+ expect { Chewy::Strategy::DelayedSidekiq.clear_timechunks! }
198
+ .to change { timechunks_set.call.size }.to(0)
199
+ end
200
+ end
189
201
  end
190
202
  end
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- if defined?(::Sidekiq)
3
+ if defined?(Sidekiq)
4
4
  require 'sidekiq/testing'
5
5
 
6
6
  describe Chewy::Strategy::LazySidekiq do
@@ -10,7 +10,7 @@ if defined?(::Sidekiq)
10
10
  Chewy.strategy(:bypass) { example.run }
11
11
  Chewy.settings[:sidekiq] = sidekiq_settings
12
12
  end
13
- before { ::Sidekiq::Worker.clear_all }
13
+ before { Sidekiq::Worker.clear_all }
14
14
 
15
15
  context 'strategy' do
16
16
  before do
@@ -32,14 +32,14 @@ if defined?(::Sidekiq)
32
32
  end
33
33
 
34
34
  it 'updates indices asynchronously on record save' do
35
- expect(::Sidekiq::Client).to receive(:push)
35
+ expect(Sidekiq::Client).to receive(:push)
36
36
  .with(hash_including(
37
37
  'class' => Chewy::Strategy::LazySidekiq::IndicesUpdateWorker,
38
38
  'queue' => 'low'
39
39
  ))
40
40
  .and_call_original
41
41
  .once
42
- ::Sidekiq::Testing.inline! do
42
+ Sidekiq::Testing.inline! do
43
43
  expect { [city, other_city].map(&:save!) }
44
44
  .to update_index(CitiesIndex, strategy: :lazy_sidekiq)
45
45
  .and_reindex(city, other_city).only
@@ -47,12 +47,12 @@ if defined?(::Sidekiq)
47
47
  end
48
48
 
49
49
  it 'updates indices asynchronously with falling back to sidekiq strategy on record destroy' do
50
- expect(::Sidekiq::Client).not_to receive(:push)
50
+ expect(Sidekiq::Client).not_to receive(:push)
51
51
  .with(hash_including(
52
52
  'class' => Chewy::Strategy::LazySidekiq::IndicesUpdateWorker,
53
53
  'queue' => 'low'
54
54
  ))
55
- expect(::Sidekiq::Client).to receive(:push)
55
+ expect(Sidekiq::Client).to receive(:push)
56
56
  .with(hash_including(
57
57
  'class' => Chewy::Strategy::Sidekiq::Worker,
58
58
  'queue' => 'low',
@@ -60,7 +60,7 @@ if defined?(::Sidekiq)
60
60
  ))
61
61
  .and_call_original
62
62
  .once
63
- ::Sidekiq::Testing.inline! do
63
+ Sidekiq::Testing.inline! do
64
64
  expect { [city, other_city].map(&:destroy) }.to update_index(CitiesIndex, strategy: :sidekiq)
65
65
  end
66
66
  end
@@ -71,7 +71,7 @@ if defined?(::Sidekiq)
71
71
  expect(other_city).to receive(:run_chewy_callbacks).and_call_original
72
72
 
73
73
  expect do
74
- ::Sidekiq::Testing.inline! do
74
+ Sidekiq::Testing.inline! do
75
75
  Chewy::Strategy::LazySidekiq::IndicesUpdateWorker.new.perform({'City' => [city.id, other_city.id]})
76
76
  end
77
77
  end.to update_index(CitiesIndex).and_reindex(city, other_city).only
@@ -88,7 +88,7 @@ if defined?(::Sidekiq)
88
88
  expect(other_city).to receive(:run_chewy_callbacks).and_call_original
89
89
 
90
90
  expect do
91
- ::Sidekiq::Testing.inline! do
91
+ Sidekiq::Testing.inline! do
92
92
  Chewy::Strategy::LazySidekiq::IndicesUpdateWorker.new.perform({'City' => [city.id, other_city.id]})
93
93
  end
94
94
  end.to update_index(CitiesIndex).and_reindex(city, other_city).only.no_refresh
@@ -97,7 +97,7 @@ if defined?(::Sidekiq)
97
97
  end
98
98
 
99
99
  context 'integration' do
100
- around { |example| ::Sidekiq::Testing.inline! { example.run } }
100
+ around { |example| Sidekiq::Testing.inline! { example.run } }
101
101
 
102
102
  let(:update_condition) { true }
103
103
 
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- if defined?(::Sidekiq)
3
+ if defined?(Sidekiq)
4
4
  require 'sidekiq/testing'
5
5
 
6
6
  describe Chewy::Strategy::Sidekiq do
@@ -10,7 +10,7 @@ if defined?(::Sidekiq)
10
10
  Chewy.strategy(:bypass) { example.run }
11
11
  Chewy.settings[:sidekiq] = sidekiq_settings
12
12
  end
13
- before { ::Sidekiq::Worker.clear_all }
13
+ before { Sidekiq::Worker.clear_all }
14
14
  before do
15
15
  stub_model(:city) do
16
16
  update_index('cities') { self }
@@ -30,8 +30,8 @@ if defined?(::Sidekiq)
30
30
  end
31
31
 
32
32
  specify do
33
- expect(::Sidekiq::Client).to receive(:push).with(hash_including('queue' => 'low')).and_call_original
34
- ::Sidekiq::Testing.inline! do
33
+ expect(Sidekiq::Client).to receive(:push).with(hash_including('queue' => 'low')).and_call_original
34
+ Sidekiq::Testing.inline! do
35
35
  expect { [city, other_city].map(&:save!) }
36
36
  .to update_index(CitiesIndex, strategy: :sidekiq)
37
37
  .and_reindex(city, other_city).only
data/spec/chewy_spec.rb CHANGED
@@ -57,18 +57,21 @@ describe Chewy do
57
57
 
58
58
  before do
59
59
  Chewy.current[:chewy_client] = nil
60
- allow(Chewy).to receive_messages(configuration: {transport_options: {proc: faraday_block}})
60
+ end
61
+
62
+ specify do
63
+ expect(Chewy).to receive_messages(configuration: {transport_options: {proc: faraday_block}})
61
64
 
62
- allow(::Elasticsearch::Client).to receive(:new).with(expected_client_config) do |*_args, &passed_block|
65
+ expect(Elasticsearch::Client).to receive(:new).with(expected_client_config) do |*_args, &passed_block|
63
66
  # RSpec's `with(..., &block)` was used previously, but doesn't actually do
64
67
  # any verification of the passed block (even of its presence).
65
68
  expect(passed_block.source_location).to eq(faraday_block.source_location)
66
69
 
67
70
  mock_client
68
71
  end
69
- end
70
72
 
71
- its(:client) { is_expected.to eq(mock_client) }
73
+ expect(Chewy.client).to be_a(Chewy::ElasticClient)
74
+ end
72
75
 
73
76
  after { Chewy.current[:chewy_client] = initial_client }
74
77
  end
data/spec/spec_helper.rb CHANGED
@@ -9,7 +9,7 @@ require 'rspec/collection_matchers'
9
9
 
10
10
  require 'timecop'
11
11
 
12
- Kaminari::Hooks.init if defined?(::Kaminari::Hooks)
12
+ Kaminari::Hooks.init if defined?(Kaminari::Hooks)
13
13
 
14
14
  require 'support/fail_helpers'
15
15
  require 'support/class_helpers'