chewy 0.8.4 → 5.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +1 -0
- data/.rubocop.yml +56 -0
- data/.rubocop_todo.yml +44 -0
- data/.travis.yml +36 -67
- data/.yardopts +5 -0
- data/Appraisals +63 -58
- data/CHANGELOG.md +168 -11
- data/Gemfile +16 -9
- data/Guardfile +5 -5
- data/LEGACY_DSL.md +497 -0
- data/README.md +403 -470
- data/Rakefile +11 -1
- data/chewy.gemspec +12 -15
- data/gemfiles/rails.4.0.activerecord.gemfile +9 -9
- data/gemfiles/rails.4.1.activerecord.gemfile +9 -9
- data/gemfiles/rails.4.2.activerecord.gemfile +8 -8
- data/gemfiles/rails.4.2.mongoid.5.2.gemfile +16 -0
- data/gemfiles/rails.5.0.activerecord.gemfile +16 -0
- data/gemfiles/rails.5.0.mongoid.6.1.gemfile +16 -0
- data/gemfiles/rails.5.1.activerecord.gemfile +16 -0
- data/gemfiles/rails.5.1.mongoid.6.3.gemfile +16 -0
- data/gemfiles/rails.5.2.activerecord.gemfile +16 -0
- data/gemfiles/sequel.4.45.gemfile +11 -0
- data/lib/chewy/backports/deep_dup.rb +1 -1
- data/lib/chewy/backports/duplicable.rb +1 -0
- data/lib/chewy/config.rb +53 -21
- data/lib/chewy/errors.rb +6 -6
- data/lib/chewy/fields/base.rb +59 -29
- data/lib/chewy/fields/root.rb +49 -14
- data/lib/chewy/index/actions.rb +95 -36
- data/lib/chewy/index/aliases.rb +2 -1
- data/lib/chewy/index/settings.rb +10 -5
- data/lib/chewy/index/specification.rb +60 -0
- data/lib/chewy/index.rb +239 -138
- data/lib/chewy/journal.rb +55 -0
- data/lib/chewy/log_subscriber.rb +8 -8
- data/lib/chewy/minitest/helpers.rb +77 -0
- data/lib/chewy/minitest/search_index_receiver.rb +80 -0
- data/lib/chewy/minitest.rb +1 -0
- data/lib/chewy/query/compose.rb +18 -19
- data/lib/chewy/query/criteria.rb +34 -24
- data/lib/chewy/query/filters.rb +28 -11
- data/lib/chewy/query/loading.rb +3 -4
- data/lib/chewy/query/nodes/and.rb +1 -1
- data/lib/chewy/query/nodes/base.rb +1 -1
- data/lib/chewy/query/nodes/bool.rb +6 -4
- data/lib/chewy/query/nodes/equal.rb +4 -4
- data/lib/chewy/query/nodes/exists.rb +1 -1
- data/lib/chewy/query/nodes/expr.rb +2 -2
- data/lib/chewy/query/nodes/field.rb +35 -31
- data/lib/chewy/query/nodes/has_child.rb +1 -0
- data/lib/chewy/query/nodes/has_parent.rb +1 -0
- data/lib/chewy/query/nodes/has_relation.rb +10 -12
- data/lib/chewy/query/nodes/missing.rb +1 -1
- data/lib/chewy/query/nodes/not.rb +1 -1
- data/lib/chewy/query/nodes/or.rb +1 -1
- data/lib/chewy/query/nodes/prefix.rb +3 -2
- data/lib/chewy/query/nodes/query.rb +1 -1
- data/lib/chewy/query/nodes/range.rb +9 -9
- data/lib/chewy/query/nodes/raw.rb +1 -1
- data/lib/chewy/query/nodes/regexp.rb +13 -9
- data/lib/chewy/query/nodes/script.rb +4 -4
- data/lib/chewy/query/pagination.rb +10 -1
- data/lib/chewy/query.rb +286 -170
- data/lib/chewy/railtie.rb +7 -6
- data/lib/chewy/rake_helper.rb +275 -37
- data/lib/chewy/repository.rb +2 -2
- data/lib/chewy/rspec/update_index.rb +70 -65
- data/lib/chewy/rspec.rb +1 -1
- data/lib/chewy/runtime/version.rb +4 -4
- data/lib/chewy/search/loader.rb +83 -0
- data/lib/chewy/{query → search}/pagination/kaminari.rb +13 -5
- data/lib/chewy/search/pagination/will_paginate.rb +43 -0
- data/lib/chewy/search/parameters/aggs.rb +16 -0
- data/lib/chewy/search/parameters/allow_partial_search_results.rb +27 -0
- data/lib/chewy/search/parameters/concerns/bool_storage.rb +24 -0
- data/lib/chewy/search/parameters/concerns/hash_storage.rb +23 -0
- data/lib/chewy/search/parameters/concerns/integer_storage.rb +14 -0
- data/lib/chewy/search/parameters/concerns/query_storage.rb +238 -0
- data/lib/chewy/search/parameters/concerns/string_array_storage.rb +23 -0
- data/lib/chewy/search/parameters/concerns/string_storage.rb +14 -0
- data/lib/chewy/search/parameters/docvalue_fields.rb +12 -0
- data/lib/chewy/search/parameters/explain.rb +16 -0
- data/lib/chewy/search/parameters/filter.rb +47 -0
- data/lib/chewy/search/parameters/highlight.rb +16 -0
- data/lib/chewy/search/parameters/indices.rb +123 -0
- data/lib/chewy/search/parameters/indices_boost.rb +52 -0
- data/lib/chewy/search/parameters/limit.rb +17 -0
- data/lib/chewy/search/parameters/load.rb +32 -0
- data/lib/chewy/search/parameters/min_score.rb +16 -0
- data/lib/chewy/search/parameters/none.rb +27 -0
- data/lib/chewy/search/parameters/offset.rb +17 -0
- data/lib/chewy/search/parameters/order.rb +64 -0
- data/lib/chewy/search/parameters/post_filter.rb +19 -0
- data/lib/chewy/search/parameters/preference.rb +16 -0
- data/lib/chewy/search/parameters/profile.rb +16 -0
- data/lib/chewy/search/parameters/query.rb +19 -0
- data/lib/chewy/search/parameters/request_cache.rb +27 -0
- data/lib/chewy/search/parameters/rescore.rb +29 -0
- data/lib/chewy/search/parameters/script_fields.rb +16 -0
- data/lib/chewy/search/parameters/search_after.rb +20 -0
- data/lib/chewy/search/parameters/search_type.rb +16 -0
- data/lib/chewy/search/parameters/source.rb +73 -0
- data/lib/chewy/search/parameters/storage.rb +95 -0
- data/lib/chewy/search/parameters/stored_fields.rb +63 -0
- data/lib/chewy/search/parameters/suggest.rb +16 -0
- data/lib/chewy/search/parameters/terminate_after.rb +16 -0
- data/lib/chewy/search/parameters/timeout.rb +16 -0
- data/lib/chewy/search/parameters/track_scores.rb +16 -0
- data/lib/chewy/search/parameters/types.rb +20 -0
- data/lib/chewy/search/parameters/version.rb +16 -0
- data/lib/chewy/search/parameters.rb +167 -0
- data/lib/chewy/search/query_proxy.rb +257 -0
- data/lib/chewy/search/request.rb +1045 -0
- data/lib/chewy/search/response.rb +119 -0
- data/lib/chewy/search/scoping.rb +50 -0
- data/lib/chewy/search/scrolling.rb +134 -0
- data/lib/chewy/search.rb +81 -26
- data/lib/chewy/stash.rb +79 -0
- data/lib/chewy/strategy/active_job.rb +1 -0
- data/lib/chewy/strategy/atomic.rb +2 -4
- data/lib/chewy/strategy/base.rb +4 -4
- data/lib/chewy/strategy/bypass.rb +1 -2
- data/lib/chewy/strategy/resque.rb +1 -0
- data/lib/chewy/strategy/shoryuken.rb +40 -0
- data/lib/chewy/strategy/sidekiq.rb +13 -1
- data/lib/chewy/strategy/urgent.rb +1 -1
- data/lib/chewy/strategy.rb +19 -10
- data/lib/chewy/type/actions.rb +26 -2
- data/lib/chewy/type/adapter/active_record.rb +50 -24
- data/lib/chewy/type/adapter/base.rb +29 -9
- data/lib/chewy/type/adapter/mongoid.rb +19 -10
- data/lib/chewy/type/adapter/object.rb +195 -31
- data/lib/chewy/type/adapter/orm.rb +69 -33
- data/lib/chewy/type/adapter/sequel.rb +37 -19
- data/lib/chewy/type/crutch.rb +5 -4
- data/lib/chewy/type/import/bulk_builder.rb +122 -0
- data/lib/chewy/type/import/bulk_request.rb +78 -0
- data/lib/chewy/type/import/journal_builder.rb +45 -0
- data/lib/chewy/type/import/routine.rb +138 -0
- data/lib/chewy/type/import.rb +150 -176
- data/lib/chewy/type/mapping.rb +58 -42
- data/lib/chewy/type/observe.rb +21 -15
- data/lib/chewy/type/syncer.rb +222 -0
- data/lib/chewy/type/witchcraft.rb +89 -34
- data/lib/chewy/type/wrapper.rb +48 -16
- data/lib/chewy/type.rb +77 -49
- data/lib/chewy/version.rb +1 -1
- data/lib/chewy.rb +95 -52
- data/lib/generators/chewy/install_generator.rb +3 -3
- data/lib/sequel/plugins/chewy_observe.rb +4 -19
- data/lib/tasks/chewy.rake +91 -28
- data/spec/chewy/config_spec.rb +130 -12
- data/spec/chewy/fields/base_spec.rb +194 -172
- data/spec/chewy/fields/root_spec.rb +123 -17
- data/spec/chewy/fields/time_fields_spec.rb +10 -9
- data/spec/chewy/index/actions_spec.rb +228 -43
- data/spec/chewy/index/aliases_spec.rb +2 -2
- data/spec/chewy/index/settings_spec.rb +100 -49
- data/spec/chewy/index/specification_spec.rb +169 -0
- data/spec/chewy/index_spec.rb +159 -63
- data/spec/chewy/journal_spec.rb +268 -0
- data/spec/chewy/minitest/helpers_spec.rb +90 -0
- data/spec/chewy/minitest/search_index_receiver_spec.rb +120 -0
- data/spec/chewy/query/criteria_spec.rb +503 -236
- data/spec/chewy/query/filters_spec.rb +96 -68
- data/spec/chewy/query/loading_spec.rb +80 -42
- data/spec/chewy/query/nodes/and_spec.rb +3 -7
- data/spec/chewy/query/nodes/bool_spec.rb +5 -13
- data/spec/chewy/query/nodes/equal_spec.rb +20 -20
- data/spec/chewy/query/nodes/exists_spec.rb +7 -7
- data/spec/chewy/query/nodes/has_child_spec.rb +42 -23
- data/spec/chewy/query/nodes/has_parent_spec.rb +42 -23
- data/spec/chewy/query/nodes/match_all_spec.rb +2 -2
- data/spec/chewy/query/nodes/missing_spec.rb +6 -5
- data/spec/chewy/query/nodes/not_spec.rb +5 -7
- data/spec/chewy/query/nodes/or_spec.rb +3 -7
- data/spec/chewy/query/nodes/prefix_spec.rb +6 -6
- data/spec/chewy/query/nodes/query_spec.rb +3 -3
- data/spec/chewy/query/nodes/range_spec.rb +19 -19
- data/spec/chewy/query/nodes/raw_spec.rb +2 -2
- data/spec/chewy/query/nodes/regexp_spec.rb +31 -19
- data/spec/chewy/query/nodes/script_spec.rb +5 -5
- data/spec/chewy/query/pagination/kaminari_spec.rb +3 -55
- data/spec/chewy/query/pagination/will_paginate_spec.rb +5 -0
- data/spec/chewy/query/pagination_spec.rb +25 -22
- data/spec/chewy/query_spec.rb +510 -505
- data/spec/chewy/rake_helper_spec.rb +381 -0
- data/spec/chewy/repository_spec.rb +8 -8
- data/spec/chewy/rspec/update_index_spec.rb +215 -113
- data/spec/chewy/runtime_spec.rb +2 -2
- data/spec/chewy/search/loader_spec.rb +117 -0
- data/spec/chewy/search/pagination/kaminari_examples.rb +71 -0
- data/spec/chewy/search/pagination/kaminari_spec.rb +21 -0
- data/spec/chewy/search/pagination/will_paginate_examples.rb +63 -0
- data/spec/chewy/search/pagination/will_paginate_spec.rb +23 -0
- data/spec/chewy/search/parameters/aggs_spec.rb +5 -0
- data/spec/chewy/search/parameters/bool_storage_examples.rb +53 -0
- data/spec/chewy/search/parameters/docvalue_fields_spec.rb +5 -0
- data/spec/chewy/search/parameters/explain_spec.rb +5 -0
- data/spec/chewy/search/parameters/filter_spec.rb +5 -0
- data/spec/chewy/search/parameters/hash_storage_examples.rb +59 -0
- data/spec/chewy/search/parameters/highlight_spec.rb +5 -0
- data/spec/chewy/search/parameters/indices_spec.rb +191 -0
- data/spec/chewy/search/parameters/integer_storage_examples.rb +32 -0
- data/spec/chewy/search/parameters/limit_spec.rb +5 -0
- data/spec/chewy/search/parameters/load_spec.rb +60 -0
- data/spec/chewy/search/parameters/min_score_spec.rb +32 -0
- data/spec/chewy/search/parameters/none_spec.rb +5 -0
- data/spec/chewy/search/parameters/offset_spec.rb +5 -0
- data/spec/chewy/search/parameters/order_spec.rb +65 -0
- data/spec/chewy/search/parameters/post_filter_spec.rb +5 -0
- data/spec/chewy/search/parameters/preference_spec.rb +5 -0
- data/spec/chewy/search/parameters/profile_spec.rb +5 -0
- data/spec/chewy/search/parameters/query_spec.rb +5 -0
- data/spec/chewy/search/parameters/query_storage_examples.rb +388 -0
- data/spec/chewy/search/parameters/request_cache_spec.rb +67 -0
- data/spec/chewy/search/parameters/rescore_spec.rb +62 -0
- data/spec/chewy/search/parameters/script_fields_spec.rb +5 -0
- data/spec/chewy/search/parameters/search_after_spec.rb +32 -0
- data/spec/chewy/search/parameters/search_type_spec.rb +5 -0
- data/spec/chewy/search/parameters/source_spec.rb +156 -0
- data/spec/chewy/search/parameters/storage_spec.rb +60 -0
- data/spec/chewy/search/parameters/stored_fields_spec.rb +126 -0
- data/spec/chewy/search/parameters/string_array_storage_examples.rb +63 -0
- data/spec/chewy/search/parameters/string_storage_examples.rb +32 -0
- data/spec/chewy/search/parameters/suggest_spec.rb +5 -0
- data/spec/chewy/search/parameters/terminate_after_spec.rb +5 -0
- data/spec/chewy/search/parameters/timeout_spec.rb +5 -0
- data/spec/chewy/search/parameters/track_scores_spec.rb +5 -0
- data/spec/chewy/search/parameters/types_spec.rb +5 -0
- data/spec/chewy/search/parameters/version_spec.rb +5 -0
- data/spec/chewy/search/parameters_spec.rb +145 -0
- data/spec/chewy/search/query_proxy_spec.rb +68 -0
- data/spec/chewy/search/request_spec.rb +685 -0
- data/spec/chewy/search/response_spec.rb +192 -0
- data/spec/chewy/search/scrolling_spec.rb +169 -0
- data/spec/chewy/search_spec.rb +37 -20
- data/spec/chewy/stash_spec.rb +95 -0
- data/spec/chewy/strategy/active_job_spec.rb +8 -2
- data/spec/chewy/strategy/atomic_spec.rb +4 -1
- data/spec/chewy/strategy/resque_spec.rb +8 -2
- data/spec/chewy/strategy/shoryuken_spec.rb +66 -0
- data/spec/chewy/strategy/sidekiq_spec.rb +10 -2
- data/spec/chewy/strategy_spec.rb +6 -6
- data/spec/chewy/type/actions_spec.rb +29 -10
- data/spec/chewy/type/adapter/active_record_spec.rb +357 -139
- data/spec/chewy/type/adapter/mongoid_spec.rb +220 -101
- data/spec/chewy/type/adapter/object_spec.rb +129 -40
- data/spec/chewy/type/adapter/sequel_spec.rb +304 -152
- data/spec/chewy/type/import/bulk_builder_spec.rb +279 -0
- data/spec/chewy/type/import/bulk_request_spec.rb +102 -0
- data/spec/chewy/type/import/journal_builder_spec.rb +95 -0
- data/spec/chewy/type/import/routine_spec.rb +110 -0
- data/spec/chewy/type/import_spec.rb +360 -244
- data/spec/chewy/type/mapping_spec.rb +96 -29
- data/spec/chewy/type/observe_spec.rb +25 -15
- data/spec/chewy/type/syncer_spec.rb +123 -0
- data/spec/chewy/type/witchcraft_spec.rb +122 -44
- data/spec/chewy/type/wrapper_spec.rb +63 -23
- data/spec/chewy/type_spec.rb +32 -10
- data/spec/chewy_spec.rb +82 -12
- data/spec/spec_helper.rb +16 -2
- data/spec/support/active_record.rb +6 -2
- data/spec/support/class_helpers.rb +4 -19
- data/spec/support/mongoid.rb +17 -5
- data/spec/support/sequel.rb +6 -1
- metadata +250 -57
- data/gemfiles/rails.3.2.activerecord.gemfile +0 -15
- data/gemfiles/rails.3.2.activerecord.kaminari.gemfile +0 -14
- data/gemfiles/rails.3.2.activerecord.will_paginate.gemfile +0 -14
- data/gemfiles/rails.4.0.activerecord.kaminari.gemfile +0 -14
- data/gemfiles/rails.4.0.activerecord.will_paginate.gemfile +0 -14
- data/gemfiles/rails.4.0.mongoid.4.0.0.gemfile +0 -15
- data/gemfiles/rails.4.0.mongoid.4.0.0.kaminari.gemfile +0 -14
- data/gemfiles/rails.4.0.mongoid.4.0.0.will_paginate.gemfile +0 -14
- data/gemfiles/rails.4.0.mongoid.5.1.0.gemfile +0 -15
- data/gemfiles/rails.4.0.mongoid.5.1.0.kaminari.gemfile +0 -14
- data/gemfiles/rails.4.0.mongoid.5.1.0.will_paginate.gemfile +0 -14
- data/gemfiles/rails.4.1.activerecord.kaminari.gemfile +0 -14
- data/gemfiles/rails.4.1.activerecord.will_paginate.gemfile +0 -14
- data/gemfiles/rails.4.1.mongoid.4.0.0.gemfile +0 -15
- data/gemfiles/rails.4.1.mongoid.4.0.0.kaminari.gemfile +0 -14
- data/gemfiles/rails.4.1.mongoid.4.0.0.will_paginate.gemfile +0 -14
- data/gemfiles/rails.4.1.mongoid.5.1.0.gemfile +0 -15
- data/gemfiles/rails.4.1.mongoid.5.1.0.kaminari.gemfile +0 -14
- data/gemfiles/rails.4.1.mongoid.5.1.0.will_paginate.gemfile +0 -14
- data/gemfiles/rails.4.2.activerecord.kaminari.gemfile +0 -15
- data/gemfiles/rails.4.2.activerecord.will_paginate.gemfile +0 -15
- data/gemfiles/rails.4.2.mongoid.4.0.0.gemfile +0 -15
- data/gemfiles/rails.4.2.mongoid.4.0.0.kaminari.gemfile +0 -14
- data/gemfiles/rails.4.2.mongoid.4.0.0.will_paginate.gemfile +0 -14
- data/gemfiles/rails.4.2.mongoid.5.1.0.gemfile +0 -15
- data/gemfiles/rails.4.2.mongoid.5.1.0.kaminari.gemfile +0 -14
- data/gemfiles/rails.4.2.mongoid.5.1.0.will_paginate.gemfile +0 -14
- data/gemfiles/rails.5.0.0.beta3.activerecord.gemfile +0 -16
- data/gemfiles/rails.5.0.0.beta3.activerecord.kaminari.gemfile +0 -16
- data/gemfiles/rails.5.0.0.beta3.activerecord.will_paginate.gemfile +0 -15
- data/gemfiles/sequel.4.31.gemfile +0 -13
- data/lib/chewy/query/pagination/will_paginate.rb +0 -27
- data/lib/chewy/query/scoping.rb +0 -20
- 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
|
-
|
19
|
-
|
18
|
+
def imported_cities
|
19
|
+
CitiesIndex::City.all.map do |city|
|
20
|
+
city.attributes.except('_score', '_explanation')
|
21
|
+
end
|
22
|
+
end
|
20
23
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
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
|
-
|
28
|
-
|
29
|
-
|
30
|
-
specify
|
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
|
78
|
+
let(:names) { %w[name0 name1] }
|
34
79
|
|
35
80
|
context 'mongoid', :mongoid do
|
36
|
-
specify { expect {
|
37
|
-
specify { expect {
|
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 {
|
42
|
-
specify { expect {
|
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 {
|
49
|
-
.to update_index(
|
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 {
|
55
|
-
.to update_index(
|
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|
|
107
|
+
allow(CitiesIndex.client).to receive(:bulk) { |params|
|
108
|
+
imported << params[:body]
|
109
|
+
nil
|
110
|
+
}
|
63
111
|
|
64
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
133
|
+
names = %w[name0 name1]
|
122
134
|
|
123
135
|
criteria = case adapter
|
124
136
|
when :mongoid
|
125
|
-
{
|
137
|
+
{:name.in => names}
|
126
138
|
else
|
127
|
-
{
|
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 {
|
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 {
|
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 {
|
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
|
-
|
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
|
-
|
161
|
-
expect(
|
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
|
-
|
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
|
-
|
172
|
-
expect(
|
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
|
-
|
177
|
-
|
178
|
-
|
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
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
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
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
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 '
|
236
|
-
|
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
|
-
|
246
|
-
|
247
|
-
specify { expect(
|
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
|
252
|
-
|
253
|
-
|
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 '
|
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
|
-
|
271
|
-
|
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
|
-
|
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
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
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
|
-
|
295
|
-
|
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
|
-
|
299
|
-
|
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
|
-
|
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
|
306
|
-
before {
|
289
|
+
context do
|
290
|
+
before { CitiesIndex::City.import!(objects[4]) }
|
307
291
|
|
308
292
|
specify do
|
309
|
-
|
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
|
-
|
312
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
325
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
335
|
-
|
336
|
-
))
|
396
|
+
context do
|
397
|
+
before { CitiesIndex::City.import!(old_objects) }
|
337
398
|
|
338
|
-
|
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 '
|
344
|
-
|
345
|
-
|
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
|
-
|
348
|
-
|
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
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
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
|
-
|
366
|
-
expect(
|
367
|
-
|
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
|
-
|
374
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
401
|
-
|
402
|
-
|
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 {
|
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 {
|
494
|
+
specify { expect { CitiesIndex::City.import!(dummy_cities) }.to raise_error Chewy::ImportFailed }
|
420
495
|
end
|
496
|
+
end
|
421
497
|
|
422
|
-
|
423
|
-
|
424
|
-
|
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(
|
429
|
-
|
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
|