chewy 5.1.0 → 7.2.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (234) hide show
  1. checksums.yaml +4 -4
  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 +73 -0
  7. data/.rubocop.yml +13 -8
  8. data/.rubocop_todo.yml +110 -22
  9. data/CHANGELOG.md +449 -347
  10. data/CODE_OF_CONDUCT.md +14 -0
  11. data/CONTRIBUTING.md +63 -0
  12. data/Gemfile +3 -7
  13. data/Guardfile +3 -1
  14. data/LICENSE.txt +1 -1
  15. data/README.md +423 -311
  16. data/chewy.gemspec +8 -10
  17. data/gemfiles/rails.5.2.activerecord.gemfile +9 -14
  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 +42 -60
  22. data/lib/chewy/errors.rb +4 -10
  23. data/lib/chewy/fields/base.rb +80 -20
  24. data/lib/chewy/fields/root.rb +7 -17
  25. data/lib/chewy/index/actions.rb +62 -35
  26. data/lib/chewy/{type → index}/adapter/active_record.rb +18 -4
  27. data/lib/chewy/{type → index}/adapter/base.rb +2 -3
  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/{type → index}/crutch.rb +5 -5
  32. data/lib/chewy/index/import/bulk_builder.rb +311 -0
  33. data/lib/chewy/{type → index}/import/bulk_request.rb +6 -7
  34. data/lib/chewy/{type → index}/import/journal_builder.rb +11 -12
  35. data/lib/chewy/{type → index}/import/routine.rb +17 -16
  36. data/lib/chewy/{type → index}/import.rb +51 -33
  37. data/lib/chewy/{type → index}/mapping.rb +32 -37
  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/specification.rb +1 -0
  42. data/lib/chewy/{type → index}/syncer.rb +61 -62
  43. data/lib/chewy/{type → index}/witchcraft.rb +15 -9
  44. data/lib/chewy/{type → index}/wrapper.rb +13 -3
  45. data/lib/chewy/index.rb +46 -96
  46. data/lib/chewy/journal.rb +25 -14
  47. data/lib/chewy/minitest/helpers.rb +86 -13
  48. data/lib/chewy/minitest/search_index_receiver.rb +22 -26
  49. data/lib/chewy/multi_search.rb +62 -0
  50. data/lib/chewy/railtie.rb +6 -20
  51. data/lib/chewy/rake_helper.rb +136 -108
  52. data/lib/chewy/rspec/build_query.rb +12 -0
  53. data/lib/chewy/rspec/helpers.rb +55 -0
  54. data/lib/chewy/rspec/update_index.rb +55 -44
  55. data/lib/chewy/rspec.rb +2 -0
  56. data/lib/chewy/runtime.rb +1 -1
  57. data/lib/chewy/search/loader.rb +19 -41
  58. data/lib/chewy/search/parameters/collapse.rb +16 -0
  59. data/lib/chewy/search/parameters/concerns/query_storage.rb +2 -2
  60. data/lib/chewy/search/parameters/ignore_unavailable.rb +27 -0
  61. data/lib/chewy/search/parameters/indices.rb +12 -57
  62. data/lib/chewy/search/parameters/none.rb +1 -3
  63. data/lib/chewy/search/parameters/order.rb +6 -19
  64. data/lib/chewy/search/parameters/source.rb +5 -1
  65. data/lib/chewy/search/parameters/track_total_hits.rb +16 -0
  66. data/lib/chewy/search/parameters.rb +7 -4
  67. data/lib/chewy/search/query_proxy.rb +9 -2
  68. data/lib/chewy/search/request.rb +180 -154
  69. data/lib/chewy/search/response.rb +5 -5
  70. data/lib/chewy/search/scoping.rb +7 -8
  71. data/lib/chewy/search/scrolling.rb +16 -13
  72. data/lib/chewy/search.rb +7 -22
  73. data/lib/chewy/stash.rb +19 -30
  74. data/lib/chewy/strategy/active_job.rb +2 -2
  75. data/lib/chewy/strategy/atomic_no_refresh.rb +18 -0
  76. data/lib/chewy/strategy/base.rb +10 -0
  77. data/lib/chewy/strategy/lazy_sidekiq.rb +64 -0
  78. data/lib/chewy/strategy/sidekiq.rb +3 -2
  79. data/lib/chewy/strategy.rb +5 -19
  80. data/lib/chewy/version.rb +1 -1
  81. data/lib/chewy.rb +36 -80
  82. data/lib/generators/chewy/install_generator.rb +1 -1
  83. data/lib/tasks/chewy.rake +26 -32
  84. data/migration_guide.md +56 -0
  85. data/spec/chewy/config_spec.rb +15 -61
  86. data/spec/chewy/fields/base_spec.rb +432 -145
  87. data/spec/chewy/fields/root_spec.rb +20 -28
  88. data/spec/chewy/fields/time_fields_spec.rb +5 -5
  89. data/spec/chewy/index/actions_spec.rb +388 -55
  90. data/spec/chewy/{type → index}/adapter/active_record_spec.rb +110 -44
  91. data/spec/chewy/{type → index}/adapter/object_spec.rb +21 -6
  92. data/spec/chewy/index/aliases_spec.rb +3 -3
  93. data/spec/chewy/index/import/bulk_builder_spec.rb +494 -0
  94. data/spec/chewy/{type → index}/import/bulk_request_spec.rb +5 -12
  95. data/spec/chewy/{type → index}/import/journal_builder_spec.rb +14 -22
  96. data/spec/chewy/{type → index}/import/routine_spec.rb +19 -19
  97. data/spec/chewy/{type → index}/import_spec.rb +149 -96
  98. data/spec/chewy/index/mapping_spec.rb +135 -0
  99. data/spec/chewy/index/observe/active_record_methods_spec.rb +68 -0
  100. data/spec/chewy/index/observe/callback_spec.rb +139 -0
  101. data/spec/chewy/index/observe_spec.rb +143 -0
  102. data/spec/chewy/index/settings_spec.rb +3 -1
  103. data/spec/chewy/index/specification_spec.rb +20 -30
  104. data/spec/chewy/{type → index}/syncer_spec.rb +14 -19
  105. data/spec/chewy/{type → index}/witchcraft_spec.rb +34 -21
  106. data/spec/chewy/index/wrapper_spec.rb +100 -0
  107. data/spec/chewy/index_spec.rb +69 -137
  108. data/spec/chewy/journal_spec.rb +46 -91
  109. data/spec/chewy/minitest/helpers_spec.rb +122 -14
  110. data/spec/chewy/minitest/search_index_receiver_spec.rb +24 -26
  111. data/spec/chewy/multi_search_spec.rb +84 -0
  112. data/spec/chewy/rake_helper_spec.rb +293 -101
  113. data/spec/chewy/rspec/build_query_spec.rb +34 -0
  114. data/spec/chewy/rspec/helpers_spec.rb +61 -0
  115. data/spec/chewy/rspec/update_index_spec.rb +106 -102
  116. data/spec/chewy/runtime_spec.rb +2 -2
  117. data/spec/chewy/search/loader_spec.rb +19 -53
  118. data/spec/chewy/search/pagination/kaminari_examples.rb +3 -5
  119. data/spec/chewy/search/pagination/kaminari_spec.rb +1 -1
  120. data/spec/chewy/search/parameters/collapse_spec.rb +5 -0
  121. data/spec/chewy/search/parameters/ignore_unavailable_spec.rb +67 -0
  122. data/spec/chewy/search/parameters/indices_spec.rb +26 -118
  123. data/spec/chewy/search/parameters/none_spec.rb +1 -1
  124. data/spec/chewy/search/parameters/order_spec.rb +18 -11
  125. data/spec/chewy/search/parameters/query_storage_examples.rb +67 -21
  126. data/spec/chewy/search/parameters/search_after_spec.rb +4 -1
  127. data/spec/chewy/search/parameters/source_spec.rb +8 -2
  128. data/spec/chewy/search/parameters/track_total_hits_spec.rb +5 -0
  129. data/spec/chewy/search/parameters_spec.rb +23 -7
  130. data/spec/chewy/search/query_proxy_spec.rb +68 -17
  131. data/spec/chewy/search/request_spec.rb +344 -149
  132. data/spec/chewy/search/response_spec.rb +35 -25
  133. data/spec/chewy/search/scrolling_spec.rb +28 -26
  134. data/spec/chewy/search_spec.rb +69 -59
  135. data/spec/chewy/stash_spec.rb +16 -26
  136. data/spec/chewy/strategy/active_job_spec.rb +23 -10
  137. data/spec/chewy/strategy/atomic_no_refresh_spec.rb +60 -0
  138. data/spec/chewy/strategy/atomic_spec.rb +9 -10
  139. data/spec/chewy/strategy/lazy_sidekiq_spec.rb +214 -0
  140. data/spec/chewy/strategy/sidekiq_spec.rb +14 -10
  141. data/spec/chewy/strategy_spec.rb +19 -15
  142. data/spec/chewy_spec.rb +17 -110
  143. data/spec/spec_helper.rb +6 -29
  144. data/spec/support/active_record.rb +43 -5
  145. metadata +102 -198
  146. data/.travis.yml +0 -45
  147. data/Appraisals +0 -81
  148. data/LEGACY_DSL.md +0 -497
  149. data/gemfiles/rails.4.0.activerecord.gemfile +0 -15
  150. data/gemfiles/rails.4.1.activerecord.gemfile +0 -15
  151. data/gemfiles/rails.4.2.activerecord.gemfile +0 -16
  152. data/gemfiles/rails.4.2.mongoid.5.2.gemfile +0 -16
  153. data/gemfiles/rails.5.0.activerecord.gemfile +0 -16
  154. data/gemfiles/rails.5.0.mongoid.6.1.gemfile +0 -16
  155. data/gemfiles/rails.5.1.activerecord.gemfile +0 -16
  156. data/gemfiles/rails.5.1.mongoid.6.3.gemfile +0 -16
  157. data/gemfiles/sequel.4.45.gemfile +0 -11
  158. data/lib/chewy/backports/deep_dup.rb +0 -46
  159. data/lib/chewy/backports/duplicable.rb +0 -91
  160. data/lib/chewy/query/compose.rb +0 -68
  161. data/lib/chewy/query/criteria.rb +0 -191
  162. data/lib/chewy/query/filters.rb +0 -244
  163. data/lib/chewy/query/loading.rb +0 -110
  164. data/lib/chewy/query/nodes/and.rb +0 -25
  165. data/lib/chewy/query/nodes/base.rb +0 -17
  166. data/lib/chewy/query/nodes/bool.rb +0 -34
  167. data/lib/chewy/query/nodes/equal.rb +0 -34
  168. data/lib/chewy/query/nodes/exists.rb +0 -20
  169. data/lib/chewy/query/nodes/expr.rb +0 -28
  170. data/lib/chewy/query/nodes/field.rb +0 -110
  171. data/lib/chewy/query/nodes/has_child.rb +0 -15
  172. data/lib/chewy/query/nodes/has_parent.rb +0 -15
  173. data/lib/chewy/query/nodes/has_relation.rb +0 -59
  174. data/lib/chewy/query/nodes/match_all.rb +0 -11
  175. data/lib/chewy/query/nodes/missing.rb +0 -20
  176. data/lib/chewy/query/nodes/not.rb +0 -25
  177. data/lib/chewy/query/nodes/or.rb +0 -25
  178. data/lib/chewy/query/nodes/prefix.rb +0 -19
  179. data/lib/chewy/query/nodes/query.rb +0 -20
  180. data/lib/chewy/query/nodes/range.rb +0 -63
  181. data/lib/chewy/query/nodes/raw.rb +0 -15
  182. data/lib/chewy/query/nodes/regexp.rb +0 -35
  183. data/lib/chewy/query/nodes/script.rb +0 -20
  184. data/lib/chewy/query/pagination.rb +0 -25
  185. data/lib/chewy/query.rb +0 -1142
  186. data/lib/chewy/search/pagination/will_paginate.rb +0 -43
  187. data/lib/chewy/search/parameters/types.rb +0 -20
  188. data/lib/chewy/strategy/resque.rb +0 -27
  189. data/lib/chewy/strategy/shoryuken.rb +0 -40
  190. data/lib/chewy/type/actions.rb +0 -43
  191. data/lib/chewy/type/adapter/mongoid.rb +0 -67
  192. data/lib/chewy/type/adapter/sequel.rb +0 -93
  193. data/lib/chewy/type/import/bulk_builder.rb +0 -122
  194. data/lib/chewy/type/observe.rb +0 -82
  195. data/lib/chewy/type.rb +0 -117
  196. data/lib/sequel/plugins/chewy_observe.rb +0 -63
  197. data/spec/chewy/query/criteria_spec.rb +0 -700
  198. data/spec/chewy/query/filters_spec.rb +0 -201
  199. data/spec/chewy/query/loading_spec.rb +0 -124
  200. data/spec/chewy/query/nodes/and_spec.rb +0 -12
  201. data/spec/chewy/query/nodes/bool_spec.rb +0 -14
  202. data/spec/chewy/query/nodes/equal_spec.rb +0 -32
  203. data/spec/chewy/query/nodes/exists_spec.rb +0 -18
  204. data/spec/chewy/query/nodes/has_child_spec.rb +0 -59
  205. data/spec/chewy/query/nodes/has_parent_spec.rb +0 -59
  206. data/spec/chewy/query/nodes/match_all_spec.rb +0 -11
  207. data/spec/chewy/query/nodes/missing_spec.rb +0 -16
  208. data/spec/chewy/query/nodes/not_spec.rb +0 -14
  209. data/spec/chewy/query/nodes/or_spec.rb +0 -12
  210. data/spec/chewy/query/nodes/prefix_spec.rb +0 -16
  211. data/spec/chewy/query/nodes/query_spec.rb +0 -12
  212. data/spec/chewy/query/nodes/range_spec.rb +0 -32
  213. data/spec/chewy/query/nodes/raw_spec.rb +0 -11
  214. data/spec/chewy/query/nodes/regexp_spec.rb +0 -43
  215. data/spec/chewy/query/nodes/script_spec.rb +0 -15
  216. data/spec/chewy/query/pagination/kaminari_spec.rb +0 -5
  217. data/spec/chewy/query/pagination/will_paginate_spec.rb +0 -5
  218. data/spec/chewy/query/pagination_spec.rb +0 -39
  219. data/spec/chewy/query_spec.rb +0 -637
  220. data/spec/chewy/search/pagination/will_paginate_examples.rb +0 -63
  221. data/spec/chewy/search/pagination/will_paginate_spec.rb +0 -23
  222. data/spec/chewy/search/parameters/types_spec.rb +0 -5
  223. data/spec/chewy/strategy/resque_spec.rb +0 -46
  224. data/spec/chewy/strategy/shoryuken_spec.rb +0 -66
  225. data/spec/chewy/type/actions_spec.rb +0 -50
  226. data/spec/chewy/type/adapter/mongoid_spec.rb +0 -372
  227. data/spec/chewy/type/adapter/sequel_spec.rb +0 -472
  228. data/spec/chewy/type/import/bulk_builder_spec.rb +0 -279
  229. data/spec/chewy/type/mapping_spec.rb +0 -173
  230. data/spec/chewy/type/observe_spec.rb +0 -137
  231. data/spec/chewy/type/wrapper_spec.rb +0 -98
  232. data/spec/chewy/type_spec.rb +0 -55
  233. data/spec/support/mongoid.rb +0 -93
  234. data/spec/support/sequel.rb +0 -80
@@ -1,279 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Chewy::Type::Import::BulkBuilder do
4
- before { Chewy.massacre }
5
-
6
- subject { described_class.new(type, index: index, delete: delete, fields: fields) }
7
- let(:type) { PlacesIndex::City }
8
- let(:index) { [] }
9
- let(:delete) { [] }
10
- let(:fields) { [] }
11
-
12
- describe '#bulk_body' do
13
- context 'simple bulk', :orm do
14
- before do
15
- stub_model(:city)
16
- stub_index(:places) do
17
- define_type City do
18
- field :name, :rating
19
- end
20
- end
21
- end
22
- let(:cities) { Array.new(3) { |i| City.create!(id: i + 1, name: "City#{i + 17}", rating: 42) } }
23
-
24
- specify { expect(subject.bulk_body).to eq([]) }
25
-
26
- context do
27
- let(:index) { cities }
28
- specify do
29
- expect(subject.bulk_body).to eq([
30
- {index: {_id: 1, data: {'name' => 'City17', 'rating' => 42}}},
31
- {index: {_id: 2, data: {'name' => 'City18', 'rating' => 42}}},
32
- {index: {_id: 3, data: {'name' => 'City19', 'rating' => 42}}}
33
- ])
34
- end
35
- end
36
-
37
- context do
38
- let(:delete) { cities }
39
- specify do
40
- expect(subject.bulk_body).to eq([
41
- {delete: {_id: 1}}, {delete: {_id: 2}}, {delete: {_id: 3}}
42
- ])
43
- end
44
- end
45
-
46
- context do
47
- let(:index) { cities.first(2) }
48
- let(:delete) { [cities.last] }
49
- specify do
50
- expect(subject.bulk_body).to eq([
51
- {index: {_id: 1, data: {'name' => 'City17', 'rating' => 42}}},
52
- {index: {_id: 2, data: {'name' => 'City18', 'rating' => 42}}},
53
- {delete: {_id: 3}}
54
- ])
55
- end
56
-
57
- context ':fields' do
58
- let(:fields) { %w[name] }
59
- specify do
60
- expect(subject.bulk_body).to eq([
61
- {update: {_id: 1, data: {doc: {'name' => 'City17'}}}},
62
- {update: {_id: 2, data: {doc: {'name' => 'City18'}}}},
63
- {delete: {_id: 3}}
64
- ])
65
- end
66
- end
67
- end
68
- end
69
-
70
- context 'parent-child relationship', :orm do
71
- before do
72
- stub_model(:country)
73
- stub_model(:city)
74
- adapter == :sequel ? City.many_to_one(:country) : City.belongs_to(:country)
75
- end
76
-
77
- before do
78
- stub_index(:places) do
79
- define_type Country do
80
- field :name
81
- end
82
-
83
- define_type City do
84
- root parent: 'country', parent_id: -> { country_id } do
85
- field :name
86
- field :rating
87
- end
88
- end
89
- end
90
- end
91
-
92
- before { PlacesIndex::Country.import(country) }
93
- let(:country) { Country.create!(id: 1, name: 'country') }
94
- let(:another_country) { Country.create!(id: 2, name: 'another country') }
95
- let(:city) { City.create!(id: 4, country_id: country.id, name: 'city', rating: 42) }
96
-
97
- context 'indexing' do
98
- let(:index) { [city] }
99
-
100
- specify do
101
- expect(subject.bulk_body).to eq([
102
- {index: {_id: city.id, parent: country.id, data: {'name' => 'city', 'rating' => 42}}}
103
- ])
104
- end
105
-
106
- context do
107
- let(:fields) { %w[name] }
108
-
109
- specify do
110
- expect(subject.bulk_body).to eq([
111
- {update: {_id: city.id, parent: country.id, data: {doc: {'name' => 'city'}}}}
112
- ])
113
- end
114
- end
115
- end
116
-
117
- context 'updating parent' do
118
- before do
119
- PlacesIndex::City.import(city)
120
- city.update_attributes(country_id: another_country.id)
121
- end
122
- let(:index) { [city] }
123
-
124
- specify do
125
- expect(subject.bulk_body).to eq([
126
- {delete: {_id: city.id, parent: country.id.to_s}},
127
- {index: {_id: city.id, parent: another_country.id, data: {'name' => 'city', 'rating' => 42}}}
128
- ])
129
- end
130
-
131
- context do
132
- let(:fields) { %w[name] }
133
-
134
- specify do
135
- expect(subject.bulk_body).to eq([
136
- {delete: {_id: city.id, parent: country.id.to_s}},
137
- {index: {_id: city.id, parent: another_country.id, data: {'name' => 'city', 'rating' => 42}}}
138
- ])
139
- end
140
- end
141
- end
142
-
143
- context 'destroying' do
144
- before { PlacesIndex::City.import(city) }
145
- let(:delete) { [city] }
146
-
147
- specify do
148
- expect(subject.bulk_body).to eq([
149
- {delete: {_id: city.id, parent: country.id.to_s}}
150
- ])
151
- end
152
- end
153
- end
154
-
155
- context 'custom id', :orm do
156
- before do
157
- stub_model(:city)
158
- end
159
-
160
- before do
161
- stub_index(:places) do
162
- define_type City do
163
- root id: -> { name } do
164
- field :rating
165
- end
166
- end
167
- end
168
- end
169
-
170
- let(:london) { City.create(id: 1, name: 'London', rating: 4) }
171
-
172
- specify do
173
- expect { PlacesIndex::City.import(london) }
174
- .to update_index(PlacesIndex::City).and_reindex(london.name)
175
- end
176
-
177
- context 'indexing' do
178
- let(:index) { [london] }
179
-
180
- specify do
181
- expect(subject.bulk_body).to eq([
182
- {index: {_id: london.name, data: {'rating' => 4}}}
183
- ])
184
- end
185
- end
186
-
187
- context 'destroying' do
188
- let(:delete) { [london] }
189
-
190
- specify do
191
- expect(subject.bulk_body).to eq([
192
- {delete: {_id: london.name}}
193
- ])
194
- end
195
- end
196
- end
197
-
198
- context 'crutches' do
199
- before do
200
- stub_index(:places) do
201
- define_type :city do
202
- crutch :names do |collection|
203
- collection.map { |item| [item.id, "Name#{item.id}"] }.to_h
204
- end
205
-
206
- field :name, value: ->(o, c) { c.names[o.id] }
207
- end
208
- end
209
- end
210
-
211
- let(:index) { [double(id: 42)] }
212
-
213
- specify do
214
- expect(subject.bulk_body).to eq([
215
- {index: {_id: 42, data: {'name' => 'Name42'}}}
216
- ])
217
- end
218
-
219
- context 'witchcraft' do
220
- before { PlacesIndex::City.witchcraft! }
221
- specify do
222
- expect(subject.bulk_body).to eq([
223
- {index: {_id: 42, data: {'name' => 'Name42'}}}
224
- ])
225
- end
226
- end
227
- end
228
-
229
- context 'empty ids' do
230
- before do
231
- stub_index(:places) do
232
- define_type :city do
233
- field :name
234
- end
235
- end
236
- end
237
-
238
- let(:index) { [{id: 1, name: 'Name0'}, double(id: '', name: 'Name1'), double(name: 'Name2')] }
239
- let(:delete) { [double(id: '', name: 'Name3'), {name: 'Name4'}, '', 2] }
240
-
241
- specify do
242
- expect(subject.bulk_body).to eq([
243
- {index: {_id: 1, data: {'name' => 'Name0'}}},
244
- {index: {data: {'name' => 'Name1'}}},
245
- {index: {data: {'name' => 'Name2'}}},
246
- {delete: {_id: {'name' => 'Name4'}}},
247
- {delete: {_id: 2}}
248
- ])
249
- end
250
-
251
- context do
252
- let(:fields) { %w[name] }
253
-
254
- specify do
255
- expect(subject.bulk_body).to eq([
256
- {update: {_id: 1, data: {doc: {'name' => 'Name0'}}}},
257
- {delete: {_id: {'name' => 'Name4'}}},
258
- {delete: {_id: 2}}
259
- ])
260
- end
261
- end
262
- end
263
- end
264
-
265
- describe '#index_objects_by_id' do
266
- before do
267
- stub_index(:places) do
268
- define_type :city do
269
- field :name
270
- end
271
- end
272
- end
273
-
274
- let(:index) { [double(id: 1), double(id: 2), double(id: ''), double] }
275
- let(:delete) { [double(id: 3)] }
276
-
277
- specify { expect(subject.index_objects_by_id).to eq('1' => index.first, '2' => index.second) }
278
- end
279
- end
@@ -1,173 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Chewy::Type::Mapping do
4
- let(:product) { ProductsIndex::Product }
5
- let(:review) { ProductsIndex::Review }
6
-
7
- before do
8
- stub_index(:products) do
9
- define_type :product do
10
- root do
11
- field :name, 'surname'
12
- field :title, type: 'text' do
13
- field :subfield1
14
- end
15
- field 'price', type: 'float' do
16
- field :subfield2
17
- end
18
- agg :named_agg do
19
- {avg: {field: 'title.subfield1'}}
20
- end
21
- end
22
- end
23
- define_type :review do
24
- field :title, :body
25
- field :comments do
26
- field :message
27
- field :rating, type: 'long'
28
- end
29
- agg :named_agg do
30
- {avg: {field: 'comments.rating'}}
31
- end
32
- end
33
- end
34
- end
35
-
36
- context 'no root element call' do
37
- before do
38
- stub_index(:products) do
39
- define_type :product do
40
- field :title, type: 'text' do
41
- field :subfield1
42
- end
43
- end
44
- end
45
- end
46
-
47
- specify { expect(product.root.children.map(&:name)).to eq([:title]) }
48
- specify { expect(product.root.children.map(&:parent)).to eq([product.root]) }
49
- specify { expect(product.root.children[0].children.map(&:name)).to eq([:subfield1]) }
50
- specify { expect(product.root.children[0].children.map(&:parent)).to eq([product.root.children[0]]) }
51
-
52
- context 'default root options are set' do
53
- around do |example|
54
- previous_options = Chewy.default_root_options
55
- Chewy.default_root_options = {_all: {enabled: false}}
56
- example.run
57
- Chewy.default_root_options = previous_options
58
- end
59
-
60
- specify { expect(product.mappings_hash[:product]).to include(_all: {enabled: false}) }
61
- end
62
- end
63
-
64
- describe '.agg' do
65
- specify { expect(product._agg_defs[:named_agg].call).to eq(avg: {field: 'title.subfield1'}) }
66
- specify { expect(review._agg_defs[:named_agg].call).to eq(avg: {field: 'comments.rating'}) }
67
- end
68
-
69
- describe '.field' do
70
- specify { expect(product.root.children.map(&:name)).to eq(%i[name surname title price]) }
71
- specify { expect(product.root.children.map(&:parent)).to eq([product.root] * 4) }
72
-
73
- specify { expect(product.root.children[0].children.map(&:name)).to eq([]) }
74
- specify { expect(product.root.children[1].children.map(&:name)).to eq([]) }
75
-
76
- specify { expect(product.root.children[2].children.map(&:name)).to eq([:subfield1]) }
77
- specify { expect(product.root.children[2].children.map(&:parent)).to eq([product.root.children[2]]) }
78
-
79
- specify { expect(product.root.children[3].children.map(&:name)).to eq([:subfield2]) }
80
- specify { expect(product.root.children[3].children.map(&:parent)).to eq([product.root.children[3]]) }
81
- end
82
-
83
- describe '.mappings_hash' do
84
- specify { expect(product.mappings_hash).to eq(product.root.mappings_hash) }
85
-
86
- context 'root merging' do
87
- context do
88
- before do
89
- stub_index(:products) do
90
- define_type :product do
91
- root _parent: 'project', other_option: 'nothing' do
92
- field :name do
93
- field :last_name # will be redefined in the following root flock
94
- end
95
- end
96
- root _parent: 'something_else'
97
- root other_option: 'option_value' do
98
- field :identifier
99
- field :name, type: 'integer'
100
- end
101
- end
102
- end
103
- end
104
-
105
- specify do
106
- expect(product.mappings_hash).to eq(product: {
107
- properties: {
108
- name: {type: 'integer'},
109
- identifier: {type: Chewy.default_field_type}
110
- },
111
- other_option: 'option_value',
112
- _parent: {type: 'something_else'}
113
- })
114
- end
115
- end
116
- end
117
-
118
- context 'parent-child relationship' do
119
- context do
120
- before do
121
- stub_index(:products) do
122
- define_type :product do
123
- root _parent: 'project', parent_id: -> { project_id } do
124
- field :name, 'surname'
125
- end
126
- end
127
- end
128
- end
129
-
130
- specify { expect(product.mappings_hash[:product][:_parent]).to eq(type: 'project') }
131
- end
132
-
133
- context do
134
- before do
135
- stub_index(:products) do
136
- define_type :product do
137
- root parent: {'type' => 'project'}, parent_id: -> { project_id } do
138
- field :name, 'surname'
139
- end
140
- end
141
- end
142
- end
143
-
144
- specify { expect(product.mappings_hash[:product][:_parent]).to eq('type' => 'project') }
145
- end
146
- end
147
- end
148
-
149
- describe '.supports_outdated_sync?' do
150
- def type(&block)
151
- stub_index(:cities) do
152
- define_type :city, &block
153
- end
154
- CitiesIndex::City
155
- end
156
-
157
- specify { expect(type.supports_outdated_sync?).to eq(false) }
158
- specify { expect(type { field :updated_at }.supports_outdated_sync?).to eq(true) }
159
- specify { expect(type { field :updated_at, value: -> {} }.supports_outdated_sync?).to eq(false) }
160
- specify do
161
- expect(type do
162
- self.outdated_sync_field = :version
163
- field :updated_at
164
- end.supports_outdated_sync?).to eq(false)
165
- end
166
- specify do
167
- expect(type do
168
- self.outdated_sync_field = :version
169
- field :version
170
- end.supports_outdated_sync?).to eq(true)
171
- end
172
- end
173
- end
@@ -1,137 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Chewy::Type::Observe do
4
- describe '.update_index' do
5
- before do
6
- stub_index(:dummies) do
7
- define_type :dummy
8
- end
9
- end
10
-
11
- let(:backreferenced) { Array.new(3) { |i| double(id: i) } }
12
-
13
- specify do
14
- expect { DummiesIndex::Dummy.update_index(backreferenced) }
15
- .to raise_error Chewy::UndefinedUpdateStrategy
16
- end
17
- specify do
18
- expect { DummiesIndex::Dummy.update_index([]) }
19
- .not_to update_index('dummies#dummy')
20
- end
21
- specify do
22
- expect { DummiesIndex::Dummy.update_index(nil) }
23
- .not_to update_index('dummies#dummy')
24
- end
25
- end
26
-
27
- context 'integration', :orm do
28
- let(:update_condition) { true }
29
-
30
- before do
31
- city_countries_update_proc = if adapter == :sequel
32
- ->(*) { previous_changes.try(:[], :country_id) || country }
33
- else
34
- ->(*) { changes['country_id'] || previous_changes['country_id'] || country }
35
- end
36
-
37
- stub_model(:city) do
38
- update_index(->(city) { "cities##{city.class.name.underscore}" }) { self }
39
- update_index 'countries#country', &city_countries_update_proc
40
- end
41
-
42
- stub_model(:country) do
43
- update_index('cities#city', if: -> { update_condition }) { cities }
44
- update_index(-> { "countries##{self.class.name.underscore}" }, :self)
45
- attr_accessor :update_condition
46
- end
47
-
48
- if adapter == :sequel
49
- City.many_to_one :country
50
- Country.one_to_many :cities
51
- City.plugin :dirty
52
- else
53
- if adapter == :mongoid && Mongoid::VERSION.start_with?('6')
54
- City.belongs_to :country, optional: true
55
- else
56
- City.belongs_to :country
57
- end
58
- Country.has_many :cities
59
- end
60
-
61
- stub_index(:cities) do
62
- define_type City
63
- end
64
-
65
- stub_index(:countries) do
66
- define_type Country
67
- end
68
- end
69
-
70
- context do
71
- let!(:country1) { Chewy.strategy(:atomic) { Country.create!(id: 1, update_condition: update_condition) } }
72
- let!(:country2) { Chewy.strategy(:atomic) { Country.create!(id: 2, update_condition: update_condition) } }
73
- let!(:city) { Chewy.strategy(:atomic) { City.create!(id: 1, country: country1) } }
74
-
75
- specify { expect { city.save! }.to update_index('cities#city').and_reindex(city).only }
76
- specify { expect { city.save! }.to update_index('countries#country').and_reindex(country1).only }
77
-
78
- specify { expect { city.update_attributes!(country: nil) }.to update_index('cities#city').and_reindex(city).only }
79
- specify { expect { city.update_attributes!(country: nil) }.to update_index('countries#country').and_reindex(country1).only }
80
-
81
- specify { expect { city.update_attributes!(country: country2) }.to update_index('cities#city').and_reindex(city).only }
82
- specify { expect { city.update_attributes!(country: country2) }.to update_index('countries#country').and_reindex(country1, country2).only }
83
- end
84
-
85
- context do
86
- let!(:country) do
87
- Chewy.strategy(:atomic) do
88
- cities = Array.new(2) { |i| City.create!(id: i) }
89
- if adapter == :sequel
90
- Country.create(id: 1, update_condition: update_condition).tap do |country|
91
- cities.each { |city| country.add_city(city) }
92
- end
93
- else
94
- Country.create!(id: 1, cities: cities, update_condition: update_condition)
95
- end
96
- end
97
- end
98
-
99
- specify { expect { country.save! }.to update_index('cities#city').and_reindex(country.cities).only }
100
- specify { expect { country.save! }.to update_index('countries#country').and_reindex(country).only }
101
-
102
- context 'conditional update' do
103
- let(:update_condition) { false }
104
- specify { expect { country.save! }.not_to update_index('cities#city') }
105
- end
106
- end
107
- end
108
-
109
- context 'transactions', :active_record do
110
- context do
111
- before { stub_model(:city) { update_index 'cities#city', :self } }
112
- before { stub_index(:cities) { define_type City } }
113
-
114
- specify do
115
- Chewy.strategy(:urgent) do
116
- ActiveRecord::Base.transaction do
117
- expect { City.create! }.not_to update_index('cities#city')
118
- end
119
- end
120
- end
121
- end
122
-
123
- context do
124
- before { allow(Chewy).to receive_messages(use_after_commit_callbacks: false) }
125
- before { stub_model(:city) { update_index 'cities#city', :self } }
126
- before { stub_index(:cities) { define_type City } }
127
-
128
- specify do
129
- Chewy.strategy(:urgent) do
130
- ActiveRecord::Base.transaction do
131
- expect { City.create! }.to update_index('cities#city')
132
- end
133
- end
134
- end
135
- end
136
- end
137
- end
@@ -1,98 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Chewy::Type::Wrapper do
4
- before do
5
- stub_class(:city)
6
- stub_index(:cities) do
7
- define_type City
8
- end
9
- end
10
-
11
- let(:city_type) { CitiesIndex::City }
12
-
13
- describe '.build' do
14
- specify do
15
- expect(city_type.build({}).attributes)
16
- .to eq('id' => nil, '_score' => nil, '_explanation' => nil)
17
- end
18
- specify do
19
- expect(city_type.build('_source' => {name: 'Martin'}).attributes)
20
- .to eq('id' => nil, '_score' => nil, '_explanation' => nil, 'name' => 'Martin')
21
- end
22
- specify do
23
- expect(city_type.build('_id' => 42).attributes)
24
- .to eq('id' => 42, '_score' => nil, '_explanation' => nil)
25
- end
26
- specify do
27
- expect(city_type.build('_id' => 42, '_source' => {'id' => 43}).attributes)
28
- .to eq('id' => 43, '_score' => nil, '_explanation' => nil)
29
- end
30
- specify do
31
- expect(city_type.build('_score' => 42, '_explanation' => {foo: 'bar'}).attributes)
32
- .to eq('id' => nil, '_score' => 42, '_explanation' => {foo: 'bar'})
33
- end
34
- specify do
35
- expect(city_type.build('_score' => 42, 'borogoves' => {foo: 'bar'})._data)
36
- .to eq('_score' => 42, 'borogoves' => {foo: 'bar'})
37
- end
38
- end
39
-
40
- describe '#initialize' do
41
- subject(:city) { city_type.new(name: 'Martin', age: 42) }
42
-
43
- it do
44
- is_expected.to respond_to(:name)
45
- .and respond_to(:age)
46
- .and have_attributes(
47
- name: 'Martin',
48
- age: 42
49
- )
50
- end
51
-
52
- it { expect { city.population }.to raise_error(NoMethodError) }
53
-
54
- context 'highlight' do
55
- subject(:city) do
56
- city_type.new(name: 'Martin', age: 42)
57
- .tap do |city|
58
- city._data = {
59
- 'highlight' => {'name' => ['<b>Mar</b>tin']}
60
- }
61
- end
62
- end
63
-
64
- it do
65
- is_expected.to respond_to(:name_highlight)
66
- .and have_attributes(
67
- name: 'Martin',
68
- name_highlight: '<b>Mar</b>tin'
69
- )
70
- end
71
- end
72
- end
73
-
74
- describe '#==' do
75
- specify { expect(city_type.new(id: 42)).to eq(city_type.new(id: 42)) }
76
- specify { expect(city_type.new(id: 42, age: 55)).to eq(city_type.new(id: 42, age: 54)) }
77
- specify { expect(city_type.new(id: 42)).not_to eq(city_type.new(id: 43)) }
78
- specify { expect(city_type.new(id: 42, age: 55)).not_to eq(city_type.new(id: 43, age: 55)) }
79
- specify { expect(city_type.new(age: 55)).to eq(city_type.new(age: 55)) }
80
- specify { expect(city_type.new(age: 55)).not_to eq(city_type.new(age: 54)) }
81
-
82
- specify { expect(city_type.new(id: '42')).to eq(City.new.tap { |m| allow(m).to receive_messages(id: 42) }) }
83
- specify { expect(city_type.new(id: 42)).not_to eq(City.new.tap { |m| allow(m).to receive_messages(id: 43) }) }
84
- specify { expect(city_type.new(id: 42)).not_to eq(Class.new) }
85
-
86
- context 'models', :orm do
87
- before do
88
- stub_model(:city)
89
- stub_index(:cities) do
90
- define_type City
91
- end
92
- end
93
- specify { expect(city_type.new(id: '42')).to eq(City.new.tap { |m| allow(m).to receive_messages(id: 42) }) }
94
- specify { expect(city_type.new(id: 42)).not_to eq(City.new.tap { |m| allow(m).to receive_messages(id: 43) }) }
95
- specify { expect(city_type.new(id: 42)).not_to eq(Class.new) }
96
- end
97
- end
98
- end