chewy 7.1.0 → 7.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (131) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +58 -0
  3. data/.rubocop.yml +13 -8
  4. data/.rubocop_todo.yml +110 -22
  5. data/CHANGELOG.md +53 -0
  6. data/Gemfile +0 -7
  7. data/Guardfile +3 -1
  8. data/README.md +282 -245
  9. data/chewy.gemspec +3 -5
  10. data/gemfiles/rails.5.2.activerecord.gemfile +8 -14
  11. data/gemfiles/rails.6.0.activerecord.gemfile +8 -14
  12. data/gemfiles/rails.6.1.activerecord.gemfile +8 -14
  13. data/lib/chewy.rb +21 -75
  14. data/lib/chewy/config.rb +40 -40
  15. data/lib/chewy/errors.rb +0 -12
  16. data/lib/chewy/fields/base.rb +11 -1
  17. data/lib/chewy/fields/root.rb +3 -4
  18. data/lib/chewy/index.rb +46 -87
  19. data/lib/chewy/index/actions.rb +51 -32
  20. data/lib/chewy/{type → index}/adapter/active_record.rb +12 -3
  21. data/lib/chewy/{type → index}/adapter/base.rb +2 -3
  22. data/lib/chewy/{type → index}/adapter/object.rb +27 -31
  23. data/lib/chewy/{type → index}/adapter/orm.rb +11 -14
  24. data/lib/chewy/{type → index}/crutch.rb +5 -5
  25. data/lib/chewy/{type → index}/import.rb +36 -27
  26. data/lib/chewy/{type → index}/import/bulk_builder.rb +15 -13
  27. data/lib/chewy/{type → index}/import/bulk_request.rb +6 -6
  28. data/lib/chewy/{type → index}/import/journal_builder.rb +10 -10
  29. data/lib/chewy/{type → index}/import/routine.rb +15 -14
  30. data/lib/chewy/{type → index}/mapping.rb +26 -31
  31. data/lib/chewy/{type → index}/observe.rb +9 -19
  32. data/lib/chewy/index/specification.rb +1 -0
  33. data/lib/chewy/{type → index}/syncer.rb +60 -57
  34. data/lib/chewy/{type → index}/witchcraft.rb +11 -7
  35. data/lib/chewy/{type → index}/wrapper.rb +2 -2
  36. data/lib/chewy/journal.rb +8 -8
  37. data/lib/chewy/minitest/helpers.rb +9 -13
  38. data/lib/chewy/minitest/search_index_receiver.rb +22 -26
  39. data/lib/chewy/railtie.rb +4 -2
  40. data/lib/chewy/rake_helper.rb +82 -107
  41. data/lib/chewy/rspec/update_index.rb +47 -43
  42. data/lib/chewy/search.rb +4 -17
  43. data/lib/chewy/search/loader.rb +18 -30
  44. data/lib/chewy/search/parameters.rb +4 -2
  45. data/lib/chewy/search/parameters/concerns/query_storage.rb +2 -2
  46. data/lib/chewy/search/parameters/source.rb +5 -1
  47. data/lib/chewy/search/query_proxy.rb +9 -2
  48. data/lib/chewy/search/request.rb +82 -86
  49. data/lib/chewy/search/response.rb +4 -4
  50. data/lib/chewy/search/scoping.rb +6 -7
  51. data/lib/chewy/search/scrolling.rb +11 -11
  52. data/lib/chewy/stash.rb +14 -22
  53. data/lib/chewy/strategy.rb +3 -19
  54. data/lib/chewy/strategy/sidekiq.rb +1 -0
  55. data/lib/chewy/version.rb +1 -1
  56. data/lib/generators/chewy/install_generator.rb +1 -1
  57. data/lib/tasks/chewy.rake +10 -22
  58. data/migration_guide.md +14 -0
  59. data/spec/chewy/config_spec.rb +14 -39
  60. data/spec/chewy/fields/base_spec.rb +412 -148
  61. data/spec/chewy/fields/root_spec.rb +16 -24
  62. data/spec/chewy/fields/time_fields_spec.rb +5 -5
  63. data/spec/chewy/index/actions_spec.rb +270 -24
  64. data/spec/chewy/{type → index}/adapter/active_record_spec.rb +68 -40
  65. data/spec/chewy/{type → index}/adapter/object_spec.rb +21 -6
  66. data/spec/chewy/{type → index}/import/bulk_builder_spec.rb +23 -31
  67. data/spec/chewy/{type → index}/import/bulk_request_spec.rb +5 -6
  68. data/spec/chewy/{type → index}/import/journal_builder_spec.rb +9 -15
  69. data/spec/chewy/{type → index}/import/routine_spec.rb +16 -16
  70. data/spec/chewy/{type → index}/import_spec.rb +102 -98
  71. data/spec/chewy/{type → index}/mapping_spec.rb +46 -54
  72. data/spec/chewy/index/observe_spec.rb +116 -0
  73. data/spec/chewy/index/settings_spec.rb +3 -1
  74. data/spec/chewy/index/specification_spec.rb +7 -17
  75. data/spec/chewy/{type → index}/syncer_spec.rb +14 -19
  76. data/spec/chewy/{type → index}/witchcraft_spec.rb +20 -22
  77. data/spec/chewy/index/wrapper_spec.rb +100 -0
  78. data/spec/chewy/index_spec.rb +59 -102
  79. data/spec/chewy/journal_spec.rb +9 -22
  80. data/spec/chewy/minitest/helpers_spec.rb +13 -15
  81. data/spec/chewy/minitest/search_index_receiver_spec.rb +22 -26
  82. data/spec/chewy/multi_search_spec.rb +4 -5
  83. data/spec/chewy/rake_helper_spec.rb +145 -55
  84. data/spec/chewy/rspec/update_index_spec.rb +74 -71
  85. data/spec/chewy/search/loader_spec.rb +19 -37
  86. data/spec/chewy/search/pagination/kaminari_examples.rb +3 -5
  87. data/spec/chewy/search/pagination/kaminari_spec.rb +1 -1
  88. data/spec/chewy/search/parameters/indices_spec.rb +2 -8
  89. data/spec/chewy/search/parameters/order_spec.rb +1 -1
  90. data/spec/chewy/search/parameters/query_storage_examples.rb +67 -21
  91. data/spec/chewy/search/parameters/search_after_spec.rb +4 -1
  92. data/spec/chewy/search/parameters/source_spec.rb +8 -2
  93. data/spec/chewy/search/parameters_spec.rb +12 -3
  94. data/spec/chewy/search/query_proxy_spec.rb +68 -17
  95. data/spec/chewy/search/request_spec.rb +222 -74
  96. data/spec/chewy/search/response_spec.rb +12 -12
  97. data/spec/chewy/search/scrolling_spec.rb +7 -9
  98. data/spec/chewy/search_spec.rb +32 -35
  99. data/spec/chewy/stash_spec.rb +9 -21
  100. data/spec/chewy/strategy/active_job_spec.rb +8 -8
  101. data/spec/chewy/strategy/atomic_spec.rb +9 -10
  102. data/spec/chewy/strategy/sidekiq_spec.rb +8 -8
  103. data/spec/chewy/strategy_spec.rb +19 -15
  104. data/spec/chewy_spec.rb +14 -100
  105. data/spec/spec_helper.rb +2 -21
  106. data/spec/support/active_record.rb +15 -5
  107. metadata +44 -103
  108. data/.circleci/config.yml +0 -214
  109. data/Appraisals +0 -81
  110. data/gemfiles/rails.5.2.mongoid.6.4.gemfile +0 -17
  111. data/gemfiles/sequel.4.45.gemfile +0 -11
  112. data/lib/chewy/search/pagination/will_paginate.rb +0 -43
  113. data/lib/chewy/strategy/resque.rb +0 -27
  114. data/lib/chewy/strategy/shoryuken.rb +0 -40
  115. data/lib/chewy/type.rb +0 -120
  116. data/lib/chewy/type/actions.rb +0 -43
  117. data/lib/chewy/type/adapter/mongoid.rb +0 -67
  118. data/lib/chewy/type/adapter/sequel.rb +0 -93
  119. data/lib/sequel/plugins/chewy_observe.rb +0 -63
  120. data/spec/chewy/search/pagination/will_paginate_examples.rb +0 -63
  121. data/spec/chewy/search/pagination/will_paginate_spec.rb +0 -23
  122. data/spec/chewy/strategy/resque_spec.rb +0 -46
  123. data/spec/chewy/strategy/shoryuken_spec.rb +0 -70
  124. data/spec/chewy/type/actions_spec.rb +0 -50
  125. data/spec/chewy/type/adapter/mongoid_spec.rb +0 -372
  126. data/spec/chewy/type/adapter/sequel_spec.rb +0 -472
  127. data/spec/chewy/type/observe_spec.rb +0 -137
  128. data/spec/chewy/type/wrapper_spec.rb +0 -100
  129. data/spec/chewy/type_spec.rb +0 -55
  130. data/spec/support/mongoid.rb +0 -93
  131. data/spec/support/sequel.rb +0 -80
@@ -46,18 +46,18 @@ describe Chewy::Fields::Root do
46
46
  before do
47
47
  stub_model(:city)
48
48
  stub_index(:places) do
49
- define_type City
49
+ index_scope City
50
50
  end
51
51
  end
52
52
 
53
53
  let(:city) { City.new(name: 'London', rating: 100) }
54
54
 
55
55
  specify do
56
- expect(PlacesIndex::City.root.compose(city))
56
+ expect(PlacesIndex.root.compose(city))
57
57
  .to match(hash_including('name' => 'London', 'rating' => 100))
58
58
  end
59
59
  specify do
60
- expect(PlacesIndex::City.root.compose(city, fields: %i[name borogoves]))
60
+ expect(PlacesIndex.root.compose(city, fields: %i[name borogoves]))
61
61
  .to eq('name' => 'London')
62
62
  end
63
63
  end
@@ -65,20 +65,18 @@ describe Chewy::Fields::Root do
65
65
  context 'has children' do
66
66
  before do
67
67
  stub_index(:places) do
68
- define_type :city do
69
- field :name, :rating
70
- end
68
+ field :name, :rating
71
69
  end
72
70
  end
73
71
 
74
72
  let(:city) { double(name: 'London', rating: 100) }
75
73
 
76
74
  specify do
77
- expect(PlacesIndex::City.root.compose(city))
75
+ expect(PlacesIndex.root.compose(city))
78
76
  .to eq('name' => 'London', 'rating' => 100)
79
77
  end
80
78
  specify do
81
- expect(PlacesIndex::City.root.compose(city, fields: %i[name borogoves]))
79
+ expect(PlacesIndex.root.compose(city, fields: %i[name borogoves]))
82
80
  .to eq('name' => 'London')
83
81
  end
84
82
  end
@@ -86,21 +84,19 @@ describe Chewy::Fields::Root do
86
84
  context 'root value provided' do
87
85
  before do
88
86
  stub_index(:places) do
89
- define_type :city do
90
- root value: ->(o) { {name: o.name + 'Modified', rating: o.rating.next} }
91
- end
87
+ root value: ->(o) { {name: "#{o.name}Modified", rating: o.rating.next} }
92
88
  end
93
89
  end
94
90
 
95
91
  let(:city) { double(name: 'London', rating: 100) }
96
92
 
97
93
  specify do
98
- expect(PlacesIndex::City.root.compose(city))
94
+ expect(PlacesIndex.root.compose(city))
99
95
  .to eq('name' => 'LondonModified', 'rating' => 101)
100
96
  end
101
97
 
102
98
  specify do
103
- expect(PlacesIndex::City.root.compose(city, fields: %i[name borogoves]))
99
+ expect(PlacesIndex.root.compose(city, fields: %i[name borogoves]))
104
100
  .to eq('name' => 'LondonModified')
105
101
  end
106
102
  end
@@ -108,11 +104,9 @@ describe Chewy::Fields::Root do
108
104
  context 'complex evaluations' do
109
105
  before do
110
106
  stub_index(:places) do
111
- define_type :city do
112
- root value: ->(o) { {name: o.name + 'Modified', rating: o.rating.next} } do
113
- field :name, value: ->(o) { o[:name] + 'Modified' }
114
- field :rating
115
- end
107
+ root value: ->(o) { {name: "#{o.name}Modified", rating: o.rating.next} } do
108
+ field :name, value: ->(o) { "#{o[:name]}Modified" }
109
+ field :rating
116
110
  end
117
111
  end
118
112
  end
@@ -120,12 +114,12 @@ describe Chewy::Fields::Root do
120
114
  let(:city) { double(name: 'London', rating: 100) }
121
115
 
122
116
  specify do
123
- expect(PlacesIndex::City.root.compose(city))
117
+ expect(PlacesIndex.root.compose(city))
124
118
  .to eq('name' => 'LondonModifiedModified', 'rating' => 101)
125
119
  end
126
120
 
127
121
  specify do
128
- expect(PlacesIndex::City.root.compose(city, fields: %i[name borogoves]))
122
+ expect(PlacesIndex.root.compose(city, fields: %i[name borogoves]))
129
123
  .to eq('name' => 'LondonModifiedModified')
130
124
  end
131
125
  end
@@ -134,14 +128,12 @@ describe Chewy::Fields::Root do
134
128
  describe '#child_hash' do
135
129
  before do
136
130
  stub_index(:places) do
137
- define_type :city do
138
- field :name, :rating
139
- end
131
+ field :name, :rating
140
132
  end
141
133
  end
142
134
 
143
135
  specify do
144
- expect(PlacesIndex::City.root.child_hash).to match(
136
+ expect(PlacesIndex.root.child_hash).to match(
145
137
  name: an_instance_of(Chewy::Fields::Base).and(have_attributes(name: :name)),
146
138
  rating: an_instance_of(Chewy::Fields::Base).and(have_attributes(name: :rating))
147
139
  )
@@ -5,14 +5,12 @@ describe 'Time fields' do
5
5
 
6
6
  before do
7
7
  stub_index(:posts) do
8
- define_type :post do
9
- field :published_at, type: 'date'
10
- end
8
+ field :published_at, type: 'date'
11
9
  end
12
10
  end
13
11
 
14
12
  before do
15
- PostsIndex::Post.import(
13
+ PostsIndex.import(
16
14
  double(published_at: ActiveSupport::TimeZone[-28_800].parse('2014/12/18 19:00')),
17
15
  double(published_at: ActiveSupport::TimeZone[-21_600].parse('2014/12/18 20:00')),
18
16
  double(published_at: ActiveSupport::TimeZone[-21_600].parse('2014/12/17 20:00'))
@@ -24,5 +22,7 @@ describe 'Time fields' do
24
22
 
25
23
  specify { expect(PostsIndex.total).to eq(3) }
26
24
  specify { expect(PostsIndex.filter(range: {published_at: {gte: range.min, lte: range.max}}).size).to eq(1) }
27
- specify { expect(PostsIndex.filter(range: {published_at: {gt: range.min.utc, lt: (range.max + 1.hour).utc}}).size).to eq(2) }
25
+ specify do
26
+ expect(PostsIndex.filter(range: {published_at: {gt: range.min.utc, lt: (range.max + 1.hour).utc}}).size).to eq(2)
27
+ end
28
28
  end
@@ -76,9 +76,15 @@ describe Chewy::Index::Actions do
76
76
  end
77
77
 
78
78
  specify do
79
- expect { DummiesIndex.create! }.to raise_error(Elasticsearch::Transport::Transport::Errors::BadRequest).with_message(/already exists.*dummies/)
79
+ expect do
80
+ DummiesIndex.create!
81
+ end.to raise_error(Elasticsearch::Transport::Transport::Errors::BadRequest).with_message(/already exists.*dummies/)
82
+ end
83
+ specify do
84
+ expect do
85
+ DummiesIndex.create!('2013')
86
+ end.to raise_error(Elasticsearch::Transport::Transport::Errors::BadRequest).with_message(/Invalid alias name \[dummies\]/)
80
87
  end
81
- specify { expect { DummiesIndex.create!('2013') }.to raise_error(Elasticsearch::Transport::Transport::Errors::BadRequest).with_message(/Invalid alias name \[dummies\]/) }
82
88
  end
83
89
 
84
90
  context do
@@ -92,7 +98,9 @@ describe Chewy::Index::Actions do
92
98
  specify { expect(DummiesIndex.aliases).to eq(['dummies']) }
93
99
  specify { expect(DummiesIndex.indexes).to eq(['dummies_2013']) }
94
100
  specify do
95
- expect { DummiesIndex.create!('2013') }.to raise_error(Elasticsearch::Transport::Transport::Errors::BadRequest).with_message(/already exists.*dummies_2013/)
101
+ expect do
102
+ DummiesIndex.create!('2013')
103
+ end.to raise_error(Elasticsearch::Transport::Transport::Errors::BadRequest).with_message(/already exists.*dummies_2013/)
96
104
  end
97
105
  specify { expect(DummiesIndex.create!('2014')['acknowledged']).to eq(true) }
98
106
 
@@ -183,7 +191,11 @@ describe Chewy::Index::Actions do
183
191
 
184
192
  describe '.delete!' do
185
193
  specify { expect { DummiesIndex.delete! }.to raise_error(Elasticsearch::Transport::Transport::Errors::NotFound) }
186
- specify { expect { DummiesIndex.delete!('2013') }.to raise_error(Elasticsearch::Transport::Transport::Errors::NotFound) }
194
+ specify do
195
+ expect do
196
+ DummiesIndex.delete!('2013')
197
+ end.to raise_error(Elasticsearch::Transport::Transport::Errors::NotFound)
198
+ end
187
199
 
188
200
  context do
189
201
  before do
@@ -337,7 +349,7 @@ describe Chewy::Index::Actions do
337
349
  before do
338
350
  stub_model(:city)
339
351
  stub_index(:cities) do
340
- define_type City
352
+ index_scope City
341
353
  end
342
354
  end
343
355
  let!(:dummy_cities) { Array.new(3) { |i| City.create(id: i + 1, name: "name#{i}") } }
@@ -359,9 +371,8 @@ describe Chewy::Index::Actions do
359
371
  context do
360
372
  before do
361
373
  stub_index(:cities) do
362
- define_type City do
363
- field :name, type: 'object'
364
- end
374
+ index_scope City
375
+ field :name, type: 'object'
365
376
  end
366
377
  end
367
378
 
@@ -373,7 +384,7 @@ describe Chewy::Index::Actions do
373
384
  before do
374
385
  stub_model(:city)
375
386
  stub_index(:cities) do
376
- define_type City
387
+ index_scope City
377
388
  end
378
389
  end
379
390
  let!(:dummy_cities) { Array.new(3) { |i| City.create(id: i + 1, name: "name#{i}") } }
@@ -395,9 +406,8 @@ describe Chewy::Index::Actions do
395
406
  context do
396
407
  before do
397
408
  stub_index(:cities) do
398
- define_type City do
399
- field :name, type: 'object'
400
- end
409
+ index_scope City
410
+ field :name, type: 'object'
401
411
  end
402
412
  end
403
413
 
@@ -409,7 +419,7 @@ describe Chewy::Index::Actions do
409
419
  before do
410
420
  stub_model(:city)
411
421
  stub_index(:cities) do
412
- define_type City
422
+ index_scope City
413
423
  end
414
424
  end
415
425
 
@@ -500,7 +510,7 @@ describe Chewy::Index::Actions do
500
510
  before do
501
511
  stub_index(:cities) do
502
512
  settings index: {refresh_interval: '2s'}
503
- define_type City
513
+ index_scope City
504
514
  end
505
515
  end
506
516
 
@@ -511,9 +521,11 @@ describe Chewy::Index::Actions do
511
521
  end
512
522
 
513
523
  specify do
514
- expect(CitiesIndex.client.indices).to receive(:put_settings).with(index: name, body: before_import_body).once
524
+ expect(CitiesIndex.client.indices)
525
+ .to receive(:put_settings).with(index: name, body: before_import_body).once
515
526
  expect(CitiesIndex.client.indices).to receive(:put_settings).with(index: name, body: after_import_body).once
516
- expect(CitiesIndex).to receive(:import).with(suffix: suffix, journal: false, refresh: false).and_call_original
527
+ expect(CitiesIndex)
528
+ .to receive(:import).with(suffix: suffix, journal: false, refresh: false).and_call_original
517
529
  expect(CitiesIndex.reset!(suffix)).to eq(true)
518
530
  end
519
531
 
@@ -574,12 +586,11 @@ describe Chewy::Index::Actions do
574
586
  xcontext 'applying journal' do
575
587
  before do
576
588
  stub_index(:cities) do
577
- define_type City do
578
- field :name, value: (lambda do
579
- sleep(rating)
580
- name
581
- end)
582
- end
589
+ index_scope City
590
+ field :name, value: (lambda do
591
+ sleep(rating)
592
+ name
593
+ end)
583
594
  end
584
595
  end
585
596
 
@@ -649,18 +660,253 @@ describe Chewy::Index::Actions do
649
660
 
650
661
  context 'other options' do
651
662
  specify do
652
- expect(CitiesIndex::City).to receive(:import).with(parallel: true, journal: false).once.and_return(true)
663
+ expect(CitiesIndex).to receive(:import).with(parallel: true, journal: false).once.and_return(true)
653
664
  expect(CitiesIndex.reset!(parallel: true)).to eq(true)
654
665
  end
655
666
 
656
667
  specify do
657
- expect(CitiesIndex::City).to receive(:import).with(suffix: 'suffix', parallel: true, journal: false, refresh: true).once.and_return(true)
668
+ expect(CitiesIndex)
669
+ .to receive(:import)
670
+ .with(suffix: 'suffix', parallel: true, journal: false, refresh: true)
671
+ .once.and_return(true)
658
672
  expect(CitiesIndex.reset!('suffix', parallel: true)).to eq(true)
659
673
  end
660
674
  end
661
675
  end
662
676
 
677
+ describe '.reset' do
678
+ before do
679
+ stub_model(:city)
680
+ stub_index(:cities) do
681
+ index_scope City
682
+ end
683
+ end
684
+
685
+ context do
686
+ before { City.create!(id: 1, name: 'Moscow') }
687
+
688
+ specify { expect(CitiesIndex.reset).to eq(true) }
689
+ specify { expect(CitiesIndex.reset('2013')).to eq(true) }
690
+
691
+ context do
692
+ before { CitiesIndex.reset }
693
+
694
+ specify { expect(CitiesIndex.all).to have(1).item }
695
+ specify { expect(CitiesIndex.aliases).to eq([]) }
696
+ specify { expect(CitiesIndex.indexes).to eq(['cities']) }
697
+ end
698
+ end
699
+ end
700
+
701
+ describe '.sync' do
702
+ before do
703
+ stub_model(:city)
704
+ stub_index(:cities) do
705
+ index_scope City
706
+ field :name
707
+ field :updated_at, type: 'date'
708
+ end
709
+ end
710
+
711
+ let!(:cities) { Array.new(3) { |i| City.create!(name: "Name#{i + 1}") } }
712
+
713
+ before do
714
+ CitiesIndex.import
715
+ cities.first.destroy
716
+ cities.last.update(name: 'Name5')
717
+ end
718
+
719
+ let!(:additional_city) { City.create!(name: 'Name4') }
720
+
721
+ specify do
722
+ expect(CitiesIndex.sync).to match(
723
+ count: 3,
724
+ missing: contain_exactly(cities.first.id.to_s, additional_city.id.to_s),
725
+ outdated: [cities.last.id.to_s]
726
+ )
727
+ end
728
+ specify do
729
+ expect { CitiesIndex.sync }.to update_index(CitiesIndex)
730
+ .and_reindex(additional_city, cities.last)
731
+ .and_delete(cities.first).only
732
+ end
733
+ end
734
+
663
735
  describe '.journal' do
664
736
  specify { expect(DummiesIndex.journal).to be_a(Chewy::Journal) }
665
737
  end
738
+
739
+ describe '.clear_cache' do
740
+ before do
741
+ stub_model(:city)
742
+ stub_index(:cities) do
743
+ index_scope City
744
+ end
745
+ end
746
+
747
+ let(:index_name) { 'test_index' }
748
+ let(:index_name_with_prefix) { 'cities_test_index' }
749
+ let(:unexisted_index_name) { 'wrong_index' }
750
+
751
+ context 'with existing index' do
752
+ before do
753
+ CitiesIndex.create(index_name)
754
+ end
755
+
756
+ specify do
757
+ expect(CitiesIndex)
758
+ .to receive(:clear_cache)
759
+ .and_call_original
760
+ expect { CitiesIndex.clear_cache({index: index_name_with_prefix}) }
761
+ .not_to raise_error
762
+ end
763
+ end
764
+
765
+ context 'with unexisting index' do
766
+ specify do
767
+ expect(CitiesIndex)
768
+ .to receive(:clear_cache)
769
+ .and_call_original
770
+ expect { CitiesIndex.clear_cache({index: unexisted_index_name}) }
771
+ .to raise_error Elasticsearch::Transport::Transport::Errors::NotFound
772
+ end
773
+ end
774
+
775
+ context 'without arguments' do
776
+ before do
777
+ CitiesIndex.create
778
+ end
779
+
780
+ specify do
781
+ expect(CitiesIndex)
782
+ .to receive(:clear_cache)
783
+ .and_call_original
784
+ expect { CitiesIndex.clear_cache }
785
+ .not_to raise_error
786
+ end
787
+ end
788
+ end
789
+
790
+ describe '.reindex' do
791
+ before do
792
+ stub_model(:city)
793
+ stub_index(:cities) do
794
+ index_scope City
795
+ end
796
+ CitiesIndex.create(source_index)
797
+ DummiesIndex.create(dest_index)
798
+ end
799
+
800
+ let(:source_index) { 'source_index' }
801
+ let(:source_index_with_prefix) { 'cities_source_index' }
802
+ let(:dest_index) { 'dest_index' }
803
+ let(:dest_index_with_prefix) { 'dummies_dest_index' }
804
+ let(:unexisting_index) { 'wrong_index' }
805
+
806
+ context 'with existing indexes' do
807
+ specify do
808
+ expect(CitiesIndex)
809
+ .to receive(:reindex)
810
+ .and_call_original
811
+ expect { CitiesIndex.reindex(source: source_index_with_prefix, dest: dest_index_with_prefix) }
812
+ .not_to raise_error
813
+ end
814
+ end
815
+
816
+ context 'with unexisting indexes' do
817
+ context 'source index' do
818
+ specify do
819
+ expect(CitiesIndex)
820
+ .to receive(:reindex)
821
+ .and_call_original
822
+ expect { CitiesIndex.reindex(source: unexisting_index, dest: dest_index_with_prefix) }
823
+ .to raise_error Elasticsearch::Transport::Transport::Errors::NotFound
824
+ end
825
+ end
826
+
827
+ context 'dest index' do
828
+ specify do
829
+ expect(CitiesIndex)
830
+ .to receive(:reindex)
831
+ .and_call_original
832
+ expect { CitiesIndex.reindex(source: source_index_with_prefix, dest: unexisting_index) }
833
+ .not_to raise_error
834
+ end
835
+ end
836
+ end
837
+
838
+ context 'with missing indexes' do
839
+ context 'without dest index' do
840
+ specify do
841
+ expect(DummiesIndex)
842
+ .to receive(:reindex)
843
+ .and_call_original
844
+ expect { DummiesIndex.reindex(source: source_index_with_prefix) }
845
+ .not_to raise_error
846
+ end
847
+ end
848
+
849
+ context 'without source index' do
850
+ specify do
851
+ expect(CitiesIndex)
852
+ .to receive(:reindex)
853
+ .and_call_original
854
+ expect { CitiesIndex.reindex(dest: dest_index_with_prefix) }
855
+ .not_to raise_error
856
+ end
857
+ end
858
+ end
859
+ end
860
+
861
+ describe 'update_mapping' do
862
+ before do
863
+ stub_model(:city)
864
+ stub_index(:cities) do
865
+ index_scope City
866
+ end
867
+ CitiesIndex.create
868
+ end
869
+
870
+ let(:index_name) { CitiesIndex.index_name }
871
+ let(:body_hash) { {properties: {new_field: {type: :text}}} }
872
+ let(:unexisting_index) { 'wrong_index' }
873
+ let(:empty_body_hash) { {} }
874
+
875
+ context 'with existing index' do
876
+ specify do
877
+ expect { CitiesIndex.update_mapping(index_name, body_hash) }
878
+ .not_to raise_error
879
+ end
880
+ end
881
+
882
+ context 'with unexisting arguments' do
883
+ context 'index name' do
884
+ specify do
885
+ expect { CitiesIndex.update_mapping(unexisting_index, body_hash) }
886
+ .to raise_error Elasticsearch::Transport::Transport::Errors::NotFound
887
+ end
888
+ end
889
+
890
+ context 'body hash' do
891
+ specify do
892
+ expect { CitiesIndex.update_mapping(index_name, empty_body_hash) }
893
+ .not_to raise_error
894
+ end
895
+ end
896
+ end
897
+
898
+ context 'with only argument' do
899
+ specify do
900
+ expect { CitiesIndex.update_mapping(index_name) }
901
+ .not_to raise_error
902
+ end
903
+ end
904
+
905
+ context 'without arguments' do
906
+ specify do
907
+ expect { CitiesIndex.update_mapping }
908
+ .not_to raise_error
909
+ end
910
+ end
911
+ end
666
912
  end