chewy 0.5.2 → 0.6.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 (92) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +26 -4
  3. data/CHANGELOG.md +16 -0
  4. data/Gemfile +3 -1
  5. data/README.md +2 -10
  6. data/chewy.gemspec +0 -1
  7. data/gemfiles/Gemfile.rails-3.2.active_record +6 -0
  8. data/gemfiles/Gemfile.rails-3.2.active_record.kaminari +7 -0
  9. data/gemfiles/Gemfile.rails-3.2.active_record.will_paginate +7 -0
  10. data/gemfiles/Gemfile.rails-4.0.active_record +6 -0
  11. data/gemfiles/Gemfile.rails-4.0.active_record.kaminari +7 -0
  12. data/gemfiles/Gemfile.rails-4.0.active_record.will_paginate +7 -0
  13. data/gemfiles/Gemfile.rails-4.0.mongoid +6 -0
  14. data/gemfiles/Gemfile.rails-4.0.mongoid.kaminari +7 -0
  15. data/gemfiles/Gemfile.rails-4.0.mongoid.will_paginate +7 -0
  16. data/gemfiles/Gemfile.rails-4.1.active_record +6 -0
  17. data/gemfiles/Gemfile.rails-4.1.active_record.kaminari +7 -0
  18. data/gemfiles/Gemfile.rails-4.1.active_record.will_paginate +7 -0
  19. data/gemfiles/Gemfile.rails-4.1.mongoid +6 -0
  20. data/gemfiles/Gemfile.rails-4.1.mongoid.kaminari +7 -0
  21. data/gemfiles/Gemfile.rails-4.1.mongoid.will_paginate +7 -0
  22. data/gemfiles/Gemfile.rails-4.2.active_record +6 -0
  23. data/gemfiles/Gemfile.rails-4.2.active_record.kaminari +7 -0
  24. data/gemfiles/Gemfile.rails-4.2.active_record.will_paginate +7 -0
  25. data/gemfiles/Gemfile.rails-4.2.mongoid +6 -0
  26. data/gemfiles/Gemfile.rails-4.2.mongoid.kaminari +7 -0
  27. data/gemfiles/Gemfile.rails-4.2.mongoid.will_paginate +7 -0
  28. data/lib/chewy.rb +33 -5
  29. data/lib/chewy/config.rb +1 -0
  30. data/lib/chewy/index/search.rb +6 -3
  31. data/lib/chewy/query.rb +74 -1
  32. data/lib/chewy/query/compose.rb +4 -4
  33. data/lib/chewy/query/pagination.rb +5 -4
  34. data/lib/chewy/query/pagination/kaminari.rb +1 -1
  35. data/lib/chewy/query/pagination/will_paginate.rb +27 -0
  36. data/lib/chewy/type.rb +1 -0
  37. data/lib/chewy/type/adapter/active_record.rb +2 -2
  38. data/lib/chewy/type/adapter/mongoid.rb +147 -0
  39. data/lib/chewy/type/adapter/object.rb +1 -1
  40. data/lib/chewy/type/import.rb +1 -0
  41. data/lib/chewy/type/observe.rb +34 -6
  42. data/lib/chewy/version.rb +1 -1
  43. data/spec/chewy/config_spec.rb +17 -17
  44. data/spec/chewy/fields/base_spec.rb +62 -62
  45. data/spec/chewy/fields/root_spec.rb +5 -5
  46. data/spec/chewy/index/actions_spec.rb +127 -127
  47. data/spec/chewy/index/aliases_spec.rb +9 -9
  48. data/spec/chewy/index/search_spec.rb +4 -4
  49. data/spec/chewy/index/settings_spec.rb +33 -33
  50. data/spec/chewy/index_spec.rb +49 -49
  51. data/spec/chewy/query/criteria_spec.rb +173 -161
  52. data/spec/chewy/query/filters_spec.rb +76 -76
  53. data/spec/chewy/query/loading_spec.rb +54 -23
  54. data/spec/chewy/query/nodes/and_spec.rb +4 -4
  55. data/spec/chewy/query/nodes/bool_spec.rb +8 -8
  56. data/spec/chewy/query/nodes/equal_spec.rb +19 -19
  57. data/spec/chewy/query/nodes/exists_spec.rb +6 -6
  58. data/spec/chewy/query/nodes/has_child_spec.rb +25 -25
  59. data/spec/chewy/query/nodes/has_parent_spec.rb +25 -25
  60. data/spec/chewy/query/nodes/match_all_spec.rb +1 -1
  61. data/spec/chewy/query/nodes/missing_spec.rb +4 -4
  62. data/spec/chewy/query/nodes/not_spec.rb +4 -4
  63. data/spec/chewy/query/nodes/or_spec.rb +4 -4
  64. data/spec/chewy/query/nodes/prefix_spec.rb +5 -5
  65. data/spec/chewy/query/nodes/query_spec.rb +2 -2
  66. data/spec/chewy/query/nodes/range_spec.rb +18 -18
  67. data/spec/chewy/query/nodes/raw_spec.rb +1 -1
  68. data/spec/chewy/query/nodes/regexp_spec.rb +18 -18
  69. data/spec/chewy/query/nodes/script_spec.rb +4 -4
  70. data/spec/chewy/query/pagination/kaminari_spec.rb +41 -39
  71. data/spec/chewy/query/pagination/will_paginage_spec.rb +60 -0
  72. data/spec/chewy/query/pagination_spec.rb +8 -7
  73. data/spec/chewy/query_spec.rb +166 -167
  74. data/spec/chewy/rspec/update_index_spec.rb +1 -1
  75. data/spec/chewy/runtime/version_spec.rb +30 -30
  76. data/spec/chewy/runtime_spec.rb +3 -3
  77. data/spec/chewy/type/actions_spec.rb +3 -3
  78. data/spec/chewy/type/adapter/active_record_spec.rb +143 -143
  79. data/spec/chewy/type/adapter/mongoid_spec.rb +219 -0
  80. data/spec/chewy/type/adapter/object_spec.rb +39 -39
  81. data/spec/chewy/type/import_spec.rb +67 -37
  82. data/spec/chewy/type/mapping_spec.rb +12 -12
  83. data/spec/chewy/type/observe_spec.rb +5 -6
  84. data/spec/chewy/type/wrapper_spec.rb +12 -12
  85. data/spec/chewy_spec.rb +26 -28
  86. data/spec/spec_helper.rb +19 -31
  87. data/spec/support/active_record.rb +52 -0
  88. data/spec/support/class_helpers.rb +0 -4
  89. data/spec/support/mongoid.rb +87 -0
  90. metadata +33 -18
  91. data/gemfiles/Gemfile.rails-3.2 +0 -15
  92. data/gemfiles/Gemfile.rails-4.0 +0 -15
@@ -1,9 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Chewy::Query do
4
- before do
5
- Chewy.client.indices.delete index: '*'
6
- end
4
+ before { Chewy.massacre }
7
5
 
8
6
  before do
9
7
  stub_index(:products) do
@@ -18,7 +16,7 @@ describe Chewy::Query do
18
16
  subject { described_class.new(ProductsIndex) }
19
17
 
20
18
  context 'unexistent index' do
21
- specify { subject.to_a.should == [] }
19
+ specify { expect(subject.to_a).to eq([]) }
22
20
  end
23
21
 
24
22
  context 'integration' do
@@ -31,114 +29,114 @@ describe Chewy::Query do
31
29
  ProductsIndex::Country.import!(countries.map { |h| double(h) })
32
30
  end
33
31
 
34
- specify { subject.count.should == 9 }
35
- specify { subject.first._data.should be_a Hash }
36
- specify { subject.limit(6).count.should == 6 }
37
- specify { subject.offset(6).count.should == 3 }
38
- specify { subject.query(match: {name: 'name3'}).highlight(fields: {name: {}}).first.name.should == '<em>Name3</em>' }
39
- specify { subject.query(match: {name: 'name3'}).highlight(fields: {name: {}}).first._data['_source']['name'].should == 'Name3' }
40
- specify { subject.types(:product).count.should == 3 }
41
- specify { subject.types(:product, :country).count.should == 6 }
42
- specify { subject.filter(term: {age: 10}).count.should == 1 }
43
- specify { subject.query(term: {age: 10}).count.should == 1 }
32
+ specify { expect(subject.count).to eq(9) }
33
+ specify { expect(subject.first._data).to be_a Hash }
34
+ specify { expect(subject.limit(6).count).to eq(6) }
35
+ specify { expect(subject.offset(6).count).to eq(3) }
36
+ specify { expect(subject.query(match: {name: 'name3'}).highlight(fields: {name: {}}).first.name).to eq('<em>Name3</em>') }
37
+ specify { expect(subject.query(match: {name: 'name3'}).highlight(fields: {name: {}}).first._data['_source']['name']).to eq('Name3') }
38
+ specify { expect(subject.types(:product).count).to eq(3) }
39
+ specify { expect(subject.types(:product, :country).count).to eq(6) }
40
+ specify { expect(subject.filter(term: {age: 10}).count).to eq(1) }
41
+ specify { expect(subject.query(term: {age: 10}).count).to eq(1) }
44
42
  end
45
43
 
46
44
  describe '#==' do
47
45
  let(:data) { 3.times.map { |i| {id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next}.stringify_keys! } }
48
46
  before { ProductsIndex::Product.import!(data.map { |h| double(h) }) }
49
47
 
50
- specify { subject.query(match: 'hello').should == subject.query(match: 'hello') }
51
- specify { subject.query(match: 'hello').should_not == subject.query(match: 'world') }
52
- specify { subject.limit(10).should == subject.limit(10) }
53
- specify { subject.limit(10).should_not == subject.limit(11) }
54
- specify { subject.limit(2).should == subject.limit(2).to_a }
48
+ specify { expect(subject.query(match: 'hello')).to eq(subject.query(match: 'hello')) }
49
+ specify { expect(subject.query(match: 'hello')).not_to eq(subject.query(match: 'world')) }
50
+ specify { expect(subject.limit(10)).to eq(subject.limit(10)) }
51
+ specify { expect(subject.limit(10)).not_to eq(subject.limit(11)) }
52
+ specify { expect(subject.limit(2)).to eq(subject.limit(2).to_a) }
55
53
  end
56
54
 
57
55
  describe '#query_mode' do
58
- specify { subject.query_mode(:should).should be_a described_class }
59
- specify { subject.query_mode(:should).should_not == subject }
60
- specify { subject.query_mode(:should).criteria.options.should include(query_mode: :should) }
56
+ specify { expect(subject.query_mode(:should)).to be_a described_class }
57
+ specify { expect(subject.query_mode(:should)).not_to eq(subject) }
58
+ specify { expect(subject.query_mode(:should).criteria.options).to include(query_mode: :should) }
61
59
  specify { expect { subject.query_mode(:should) }.not_to change { subject.criteria.options } }
62
60
  end
63
61
 
64
62
  describe '#filter_mode' do
65
- specify { subject.filter_mode(:or).should be_a described_class }
66
- specify { subject.filter_mode(:or).should_not == subject }
67
- specify { subject.filter_mode(:or).criteria.options.should include(filter_mode: :or) }
63
+ specify { expect(subject.filter_mode(:or)).to be_a described_class }
64
+ specify { expect(subject.filter_mode(:or)).not_to eq(subject) }
65
+ specify { expect(subject.filter_mode(:or).criteria.options).to include(filter_mode: :or) }
68
66
  specify { expect { subject.filter_mode(:or) }.not_to change { subject.criteria.options } }
69
67
  end
70
68
 
71
69
  describe '#post_filter_mode' do
72
- specify { subject.post_filter_mode(:or).should be_a described_class }
73
- specify { subject.post_filter_mode(:or).should_not == subject }
74
- specify { subject.post_filter_mode(:or).criteria.options.should include(post_filter_mode: :or) }
70
+ specify { expect(subject.post_filter_mode(:or)).to be_a described_class }
71
+ specify { expect(subject.post_filter_mode(:or)).not_to eq(subject) }
72
+ specify { expect(subject.post_filter_mode(:or).criteria.options).to include(post_filter_mode: :or) }
75
73
  specify { expect { subject.post_filter_mode(:or) }.not_to change { subject.criteria.options } }
76
74
  end
77
75
 
78
76
  describe '#boost_mode' do
79
- specify { subject.boost_mode(:replace).should be_a described_class }
80
- specify { subject.boost_mode(:replace).should_not == subject }
81
- specify { subject.boost_mode(:replace).criteria.options.should include(boost_mode: :replace) }
77
+ specify { expect(subject.boost_mode(:replace)).to be_a described_class }
78
+ specify { expect(subject.boost_mode(:replace)).not_to eq(subject) }
79
+ specify { expect(subject.boost_mode(:replace).criteria.options).to include(boost_mode: :replace) }
82
80
  specify { expect { subject.boost_mode(:replace) }.not_to change { subject.criteria.options } }
83
81
  end
84
82
 
85
83
  describe '#score_mode' do
86
- specify { subject.score_mode(:first).should be_a described_class }
87
- specify { subject.score_mode(:first).should_not == subject }
88
- specify { subject.score_mode(:first).criteria.options.should include(score_mode: :first) }
84
+ specify { expect(subject.score_mode(:first)).to be_a described_class }
85
+ specify { expect(subject.score_mode(:first)).not_to eq(subject) }
86
+ specify { expect(subject.score_mode(:first).criteria.options).to include(score_mode: :first) }
89
87
  specify { expect { subject.score_mode(:first) }.not_to change { subject.criteria.options } }
90
88
  end
91
89
 
92
90
  describe '#limit' do
93
- specify { subject.limit(10).should be_a described_class }
94
- specify { subject.limit(10).should_not == subject }
95
- specify { subject.limit(10).criteria.request_options.should include(size: 10) }
91
+ specify { expect(subject.limit(10)).to be_a described_class }
92
+ specify { expect(subject.limit(10)).not_to eq(subject) }
93
+ specify { expect(subject.limit(10).criteria.request_options).to include(size: 10) }
96
94
  specify { expect { subject.limit(10) }.not_to change { subject.criteria.request_options } }
97
95
  end
98
96
 
99
97
  describe '#offset' do
100
- specify { subject.offset(10).should be_a described_class }
101
- specify { subject.offset(10).should_not == subject }
102
- specify { subject.offset(10).criteria.request_options.should include(from: 10) }
98
+ specify { expect(subject.offset(10)).to be_a described_class }
99
+ specify { expect(subject.offset(10)).not_to eq(subject) }
100
+ specify { expect(subject.offset(10).criteria.request_options).to include(from: 10) }
103
101
  specify { expect { subject.offset(10) }.not_to change { subject.criteria.request_options } }
104
102
  end
105
103
 
106
104
  describe '#script_score' do
107
- specify { subject.script_score('23').should be_a described_class }
108
- specify { subject.script_score('23').should_not == subject }
109
- specify { subject.script_score('23').criteria.scores.should == [ { script_score: { script: '23' } } ] }
105
+ specify { expect(subject.script_score('23')).to be_a described_class }
106
+ specify { expect(subject.script_score('23')).not_to eq(subject) }
107
+ specify { expect(subject.script_score('23').criteria.scores).to eq([ { script_score: { script: '23' } } ]) }
110
108
  specify { expect { subject.script_score('23') }.not_to change { subject.criteria.scores } }
111
- specify { subject.script_score('23', filter: { foo: :bar}).criteria.scores.should == [{ script_score: { script: '23' }, filter: { foo: :bar } }] }
109
+ specify { expect(subject.script_score('23', filter: { foo: :bar}).criteria.scores).to eq([{ script_score: { script: '23' }, filter: { foo: :bar } }]) }
112
110
  end
113
111
 
114
112
  describe '#boost_factor' do
115
- specify { subject.boost_factor('23').should be_a described_class }
116
- specify { subject.boost_factor('23').should_not == subject }
117
- specify { subject.boost_factor('23').criteria.scores.should == [ { boost_factor: 23 } ] }
113
+ specify { expect(subject.boost_factor('23')).to be_a described_class }
114
+ specify { expect(subject.boost_factor('23')).not_to eq(subject) }
115
+ specify { expect(subject.boost_factor('23').criteria.scores).to eq([ { boost_factor: 23 } ]) }
118
116
  specify { expect { subject.boost_factor('23') }.not_to change { subject.criteria.scores } }
119
- specify { subject.boost_factor('23', filter: { foo: :bar}).criteria.scores.should == [{ boost_factor: 23, filter: { foo: :bar } }] }
117
+ specify { expect(subject.boost_factor('23', filter: { foo: :bar}).criteria.scores).to eq([{ boost_factor: 23, filter: { foo: :bar } }]) }
120
118
  end
121
119
 
122
120
  describe '#random_score' do
123
- specify { subject.random_score('23').should be_a described_class }
124
- specify { subject.random_score('23').should_not == subject }
125
- specify { subject.random_score('23').criteria.scores.should == [ { random_score: { seed: 23 } } ] }
121
+ specify { expect(subject.random_score('23')).to be_a described_class }
122
+ specify { expect(subject.random_score('23')).not_to eq(subject) }
123
+ specify { expect(subject.random_score('23').criteria.scores).to eq([ { random_score: { seed: 23 } } ]) }
126
124
  specify { expect { subject.random_score('23') }.not_to change { subject.criteria.scores } }
127
- specify { subject.random_score('23', filter: { foo: :bar}).criteria.scores.should == [{ random_score: { seed: 23 }, filter: { foo: :bar } }] }
125
+ specify { expect(subject.random_score('23', filter: { foo: :bar}).criteria.scores).to eq([{ random_score: { seed: 23 }, filter: { foo: :bar } }]) }
128
126
  end
129
127
 
130
128
  describe '#field_value_score' do
131
- specify { subject.field_value_factor(field: :boost).should be_a described_class }
132
- specify { subject.field_value_factor(field: :boost).should_not == subject }
133
- specify { subject.field_value_factor(field: :boost).criteria.scores.should == [ { field_value_factor: { field: :boost } } ] }
129
+ specify { expect(subject.field_value_factor(field: :boost)).to be_a described_class }
130
+ specify { expect(subject.field_value_factor(field: :boost)).not_to eq(subject) }
131
+ specify { expect(subject.field_value_factor(field: :boost).criteria.scores).to eq([ { field_value_factor: { field: :boost } } ]) }
134
132
  specify { expect { subject.field_value_factor(field: :boost) }.not_to change { subject.criteria.scores } }
135
- specify { subject.field_value_factor({ field: :boost }, filter: { foo: :bar}).criteria.scores.should == [{ field_value_factor: { field: :boost }, filter: { foo: :bar } }] }
133
+ specify { expect(subject.field_value_factor({ field: :boost }, filter: { foo: :bar}).criteria.scores).to eq([{ field_value_factor: { field: :boost }, filter: { foo: :bar } }]) }
136
134
  end
137
135
 
138
136
  describe '#decay' do
139
- specify { subject.decay(:gauss, :field).should be_a described_class }
140
- specify { subject.decay(:gauss, :field).should_not == subject }
141
- specify { subject.decay(:gauss, :field).criteria.scores.should == [ {
137
+ specify { expect(subject.decay(:gauss, :field)).to be_a described_class }
138
+ specify { expect(subject.decay(:gauss, :field)).not_to eq(subject) }
139
+ specify { expect(subject.decay(:gauss, :field).criteria.scores).to eq([ {
142
140
  gauss: {
143
141
  field: {
144
142
  origin: 0,
@@ -147,15 +145,15 @@ describe Chewy::Query do
147
145
  decay: 0.1
148
146
  }
149
147
  }
150
- }] }
148
+ }]) }
151
149
  specify { expect { subject.decay(:gauss, :field) }.not_to change { subject.criteria.scores } }
152
150
  specify {
153
- subject.decay(:gauss, :field,
151
+ expect(subject.decay(:gauss, :field,
154
152
  origin: '11, 12',
155
153
  scale: '2km',
156
154
  offset: '5km',
157
155
  decay: 0.4,
158
- filter: { foo: :bar }).criteria.scores.should == [
156
+ filter: { foo: :bar }).criteria.scores).to eq([
159
157
  {
160
158
  gauss: {
161
159
  field: {
@@ -167,19 +165,19 @@ describe Chewy::Query do
167
165
  },
168
166
  filter: { foo: :bar }
169
167
  }
170
- ]
168
+ ])
171
169
  }
172
170
  end
173
171
 
174
172
  describe '#facets' do
175
- specify { subject.facets(term: {field: 'hello'}).should be_a described_class }
176
- specify { subject.facets(term: {field: 'hello'}).should_not == subject }
177
- specify { subject.facets(term: {field: 'hello'}).criteria.facets.should include(term: {field: 'hello'}) }
173
+ specify { expect(subject.facets(term: {field: 'hello'})).to be_a described_class }
174
+ specify { expect(subject.facets(term: {field: 'hello'})).not_to eq(subject) }
175
+ specify { expect(subject.facets(term: {field: 'hello'}).criteria.facets).to include(term: {field: 'hello'}) }
178
176
  specify { expect { subject.facets(term: {field: 'hello'}) }.not_to change { subject.criteria.facets } }
179
177
 
180
- context 'results' do
178
+ context 'results', :orm do
181
179
  before { stub_model(:city) }
182
- let(:cities) { 10.times.map { |i| City.create! name: "name#{i}", rating: i % 3 } }
180
+ let(:cities) { 10.times.map { |i| City.create! id: i + 1, name: "name#{i}", rating: i % 3 } }
183
181
 
184
182
  before do
185
183
  stub_index(:cities) do
@@ -191,8 +189,8 @@ describe Chewy::Query do
191
189
 
192
190
  before { CitiesIndex::City.import! cities }
193
191
 
194
- specify { CitiesIndex.facets.should == {} }
195
- specify { CitiesIndex.facets(ratings: {terms: {field: 'rating'}}).facets.should == {
192
+ specify { expect(CitiesIndex.facets).to eq({}) }
193
+ specify { expect(CitiesIndex.facets(ratings: {terms: {field: 'rating'}}).facets).to eq({
196
194
  'ratings' => {
197
195
  '_type' => 'terms', 'missing' => 0, 'total' => 10, 'other' => 0,
198
196
  'terms' => [
@@ -201,19 +199,19 @@ describe Chewy::Query do
201
199
  {'term' => 1, 'count' => 3}
202
200
  ]
203
201
  }
204
- } }
202
+ }) }
205
203
  end
206
204
  end
207
205
 
208
206
  describe '#aggregations' do
209
- specify { subject.aggregations(aggregation1: {field: 'hello'}).should be_a described_class }
210
- specify { subject.aggregations(aggregation1: {field: 'hello'}).should_not == subject }
211
- specify { subject.aggregations(aggregation1: {field: 'hello'}).criteria.aggregations.should include(aggregation1: {field: 'hello'}) }
207
+ specify { expect(subject.aggregations(aggregation1: {field: 'hello'})).to be_a described_class }
208
+ specify { expect(subject.aggregations(aggregation1: {field: 'hello'})).not_to eq(subject) }
209
+ specify { expect(subject.aggregations(aggregation1: {field: 'hello'}).criteria.aggregations).to include(aggregation1: {field: 'hello'}) }
212
210
  specify { expect { subject.aggregations(aggregation1: {field: 'hello'}) }.not_to change { subject.criteria.aggregations } }
213
211
 
214
- context 'results' do
212
+ context 'results', :orm do
215
213
  before { stub_model(:city) }
216
- let(:cities) { 10.times.map { |i| City.create! name: "name#{i}", rating: i % 3 } }
214
+ let(:cities) { 10.times.map { |i| City.create! id: i + 1, name: "name#{i}", rating: i % 3 } }
217
215
 
218
216
  context do
219
217
  before do
@@ -226,26 +224,26 @@ describe Chewy::Query do
226
224
 
227
225
  before { CitiesIndex::City.import! cities }
228
226
 
229
- specify { CitiesIndex.aggregations.should == {} }
230
- specify { CitiesIndex.aggregations(ratings: {terms: {field: 'rating'}})
231
- .aggregations['ratings']['buckets'].map { |h| h.slice('key', 'doc_count') }.should == [
227
+ specify { expect(CitiesIndex.aggregations).to eq({}) }
228
+ specify { expect(CitiesIndex.aggregations(ratings: {terms: {field: 'rating'}})
229
+ .aggregations['ratings']['buckets'].map { |h| h.slice('key', 'doc_count') }).to eq([
232
230
  { 'key' => 0, 'doc_count' => 4 },
233
231
  { 'key' => 1, 'doc_count' => 3 },
234
232
  { 'key' => 2, 'doc_count' => 3 }
235
- ] }
233
+ ]) }
236
234
  end
237
235
  end
238
236
  end
239
237
 
240
238
  describe '#suggest' do
241
239
  specify { subject.suggest(name1: {text: 'hello', term: {field: 'name'}}) }
242
- specify { subject.suggest(name1: {text: 'hello'}).should_not == subject }
240
+ specify { expect(subject.suggest(name1: {text: 'hello'})).not_to eq(subject) }
243
241
  specify { expect(subject.suggest(name1: {text: 'hello'}).criteria.suggest).to include(name1: {text: 'hello'}) }
244
242
  specify { expect { subject.suggest(name1: {text: 'hello'}) }.not_to change { subject.criteria.suggest } }
245
243
 
246
- context 'results' do
244
+ context 'results', :orm do
247
245
  before { stub_model(:city) }
248
- let(:cities) { 10.times.map { |i| City.create! name: "name#{i}" } }
246
+ let(:cities) { 10.times.map { |i| City.create! id: i + 1, name: "name#{i}" } }
249
247
 
250
248
  context do
251
249
  before do
@@ -258,8 +256,8 @@ describe Chewy::Query do
258
256
 
259
257
  before { CitiesIndex::City.import! cities }
260
258
 
261
- specify { CitiesIndex.suggest.should == {} }
262
- specify { CitiesIndex.suggest(name: {text: 'name', term: {field: 'name'}}).suggest.should == {
259
+ specify { expect(CitiesIndex.suggest).to eq({}) }
260
+ specify { expect(CitiesIndex.suggest(name: {text: 'name', term: {field: 'name'}}).suggest).to eq({
263
261
  'name' => [
264
262
  {'text' => 'name', 'offset' => 0, 'length' => 4, 'options' => [
265
263
  {'text' => 'name0', 'score' => 0.75, 'freq' => 1},
@@ -269,7 +267,7 @@ describe Chewy::Query do
269
267
  {'text' => 'name4', 'score' => 0.75, 'freq' => 1}
270
268
  ]
271
269
  }
272
- ] }
270
+ ] })
273
271
  }
274
272
  end
275
273
  end
@@ -286,140 +284,140 @@ describe Chewy::Query do
286
284
  ProductsIndex::Country.import!(countries.map { |h| double(h) })
287
285
  end
288
286
 
289
- specify { expect { subject.query(match: {name: 'name3'}).delete_all }.to change { ProductsIndex.total_count }.from(9).to(8) }
287
+ specify { expect { subject.query(match: {name: 'name3'}).delete_all }.to change { ProductsIndex.total }.from(9).to(8) }
290
288
  specify { expect { subject.filter { age == [10, 20] }.delete_all }.to change { ProductsIndex.total_count }.from(9).to(7) }
291
- specify { expect { subject.types(:product).delete_all }.to change { ProductsIndex::Product.total_count }.from(3).to(0) }
292
- specify { expect { ProductsIndex.delete_all }.to change { ProductsIndex.total_count }.from(9).to(0) }
293
- specify { expect { ProductsIndex::City.delete_all }.to change { ProductsIndex.total_count }.from(9).to(6) }
289
+ specify { expect { subject.types(:product).delete_all }.to change { ProductsIndex::Product.total_entries }.from(3).to(0) }
290
+ specify { expect { ProductsIndex.delete_all }.to change { ProductsIndex.total }.from(9).to(0) }
291
+ specify { expect { ProductsIndex::City.delete_all }.to change { ProductsIndex.total }.from(9).to(6) }
294
292
  end
295
293
 
296
294
  describe '#none' do
297
- specify { subject.none.should be_a described_class }
298
- specify { subject.none.should_not == subject }
299
- specify { subject.none.criteria.should be_none }
295
+ specify { expect(subject.none).to be_a described_class }
296
+ specify { expect(subject.none).not_to eq(subject) }
297
+ specify { expect(subject.none.criteria).to be_none }
300
298
 
301
299
  context do
302
300
  before { expect_any_instance_of(described_class).not_to receive(:_response) }
303
301
 
304
- specify { subject.none.to_a.should == [] }
305
- specify { subject.query(match: 'hello').none.to_a.should == [] }
306
- specify { subject.none.query(match: 'hello').to_a.should == [] }
302
+ specify { expect(subject.none.to_a).to eq([]) }
303
+ specify { expect(subject.query(match: 'hello').none.to_a).to eq([]) }
304
+ specify { expect(subject.none.query(match: 'hello').to_a).to eq([]) }
307
305
  end
308
306
  end
309
307
 
310
308
  describe '#strategy' do
311
- specify { subject.strategy('query_first').should be_a described_class }
312
- specify { subject.strategy('query_first').should_not == subject }
313
- specify { subject.strategy('query_first').criteria.options.should include(strategy: 'query_first') }
309
+ specify { expect(subject.strategy('query_first')).to be_a described_class }
310
+ specify { expect(subject.strategy('query_first')).not_to eq(subject) }
311
+ specify { expect(subject.strategy('query_first').criteria.options).to include(strategy: 'query_first') }
314
312
  specify { expect { subject.strategy('query_first') }.not_to change { subject.criteria.options } }
315
313
  end
316
314
 
317
315
  describe '#query' do
318
- specify { subject.query(match: 'hello').should be_a described_class }
319
- specify { subject.query(match: 'hello').should_not == subject }
320
- specify { subject.query(match: 'hello').criteria.queries.should include(match: 'hello') }
316
+ specify { expect(subject.query(match: 'hello')).to be_a described_class }
317
+ specify { expect(subject.query(match: 'hello')).not_to eq(subject) }
318
+ specify { expect(subject.query(match: 'hello').criteria.queries).to include(match: 'hello') }
321
319
  specify { expect { subject.query(match: 'hello') }.not_to change { subject.criteria.queries } }
322
320
  end
323
321
 
324
322
  describe '#filter' do
325
- specify { subject.filter(term: {field: 'hello'}).should be_a described_class }
326
- specify { subject.filter(term: {field: 'hello'}).should_not == subject }
323
+ specify { expect(subject.filter(term: {field: 'hello'})).to be_a described_class }
324
+ specify { expect(subject.filter(term: {field: 'hello'})).not_to eq(subject) }
327
325
  specify { expect { subject.filter(term: {field: 'hello'}) }.not_to change { subject.criteria.filters } }
328
- specify { subject.filter([{term: {field: 'hello'}}, {term: {field: 'world'}}]).criteria.filters
329
- .should == [{term: {field: 'hello'}}, {term: {field: 'world'}}] }
326
+ specify { expect(subject.filter([{term: {field: 'hello'}}, {term: {field: 'world'}}]).criteria.filters)
327
+ .to eq([{term: {field: 'hello'}}, {term: {field: 'world'}}]) }
330
328
 
331
329
  specify { expect { subject.filter{ name == 'John' } }.not_to change { subject.criteria.filters } }
332
- specify { subject.filter{ name == 'John' }.criteria.filters.should == [{term: {'name' => 'John'}}] }
330
+ specify { expect(subject.filter{ name == 'John' }.criteria.filters).to eq([{term: {'name' => 'John'}}]) }
333
331
  end
334
332
 
335
333
  describe '#post_filter' do
336
- specify { subject.post_filter(term: {field: 'hello'}).should be_a described_class }
337
- specify { subject.post_filter(term: {field: 'hello'}).should_not == subject }
334
+ specify { expect(subject.post_filter(term: {field: 'hello'})).to be_a described_class }
335
+ specify { expect(subject.post_filter(term: {field: 'hello'})).not_to eq(subject) }
338
336
  specify { expect { subject.post_filter(term: {field: 'hello'}) }.not_to change { subject.criteria.post_filters } }
339
- specify { subject.post_filter([{term: {field: 'hello'}}, {term: {field: 'world'}}]).criteria.post_filters
340
- .should == [{term: {field: 'hello'}}, {term: {field: 'world'}}] }
337
+ specify { expect(subject.post_filter([{term: {field: 'hello'}}, {term: {field: 'world'}}]).criteria.post_filters)
338
+ .to eq([{term: {field: 'hello'}}, {term: {field: 'world'}}]) }
341
339
 
342
340
  specify { expect { subject.post_filter{ name == 'John' } }.not_to change { subject.criteria.post_filters } }
343
- specify { subject.post_filter{ name == 'John' }.criteria.post_filters.should == [{term: {'name' => 'John'}}] }
341
+ specify { expect(subject.post_filter{ name == 'John' }.criteria.post_filters).to eq([{term: {'name' => 'John'}}]) }
344
342
  end
345
343
 
346
344
  describe '#order' do
347
- specify { subject.order(field: 'hello').should be_a described_class }
348
- specify { subject.order(field: 'hello').should_not == subject }
345
+ specify { expect(subject.order(field: 'hello')).to be_a described_class }
346
+ specify { expect(subject.order(field: 'hello')).not_to eq(subject) }
349
347
  specify { expect { subject.order(field: 'hello') }.not_to change { subject.criteria.sort } }
350
348
 
351
- specify { subject.order(:field).criteria.sort.should == [:field] }
352
- specify { subject.order([:field1, :field2]).criteria.sort.should == [:field1, :field2] }
353
- specify { subject.order(field: :asc).criteria.sort.should == [{field: :asc}] }
354
- specify { subject.order({field1: {order: :asc}, field2: :desc}).order([:field3], :field4).criteria.sort.should == [{field1: {order: :asc}}, {field2: :desc}, :field3, :field4] }
349
+ specify { expect(subject.order(:field).criteria.sort).to eq([:field]) }
350
+ specify { expect(subject.order([:field1, :field2]).criteria.sort).to eq([:field1, :field2]) }
351
+ specify { expect(subject.order(field: :asc).criteria.sort).to eq([{field: :asc}]) }
352
+ specify { expect(subject.order({field1: {order: :asc}, field2: :desc}).order([:field3], :field4).criteria.sort).to eq([{field1: {order: :asc}}, {field2: :desc}, :field3, :field4]) }
355
353
  end
356
354
 
357
355
  describe '#reorder' do
358
- specify { subject.reorder(field: 'hello').should be_a described_class }
359
- specify { subject.reorder(field: 'hello').should_not == subject }
356
+ specify { expect(subject.reorder(field: 'hello')).to be_a described_class }
357
+ specify { expect(subject.reorder(field: 'hello')).not_to eq(subject) }
360
358
  specify { expect { subject.reorder(field: 'hello') }.not_to change { subject.criteria.sort } }
361
359
 
362
- specify { subject.order(:field1).reorder(:field2).criteria.sort.should == [:field2] }
363
- specify { subject.order(:field1).reorder(:field2).order(:field3).criteria.sort.should == [:field2, :field3] }
364
- specify { subject.order(:field1).reorder(:field2).reorder(:field3).criteria.sort.should == [:field3] }
360
+ specify { expect(subject.order(:field1).reorder(:field2).criteria.sort).to eq([:field2]) }
361
+ specify { expect(subject.order(:field1).reorder(:field2).order(:field3).criteria.sort).to eq([:field2, :field3]) }
362
+ specify { expect(subject.order(:field1).reorder(:field2).reorder(:field3).criteria.sort).to eq([:field3]) }
365
363
  end
366
364
 
367
365
  describe '#only' do
368
- specify { subject.only(:field).should be_a described_class }
369
- specify { subject.only(:field).should_not == subject }
366
+ specify { expect(subject.only(:field)).to be_a described_class }
367
+ specify { expect(subject.only(:field)).not_to eq(subject) }
370
368
  specify { expect { subject.only(:field) }.not_to change { subject.criteria.fields } }
371
369
 
372
- specify { subject.only(:field1, :field2).criteria.fields.should =~ ['field1', 'field2'] }
373
- specify { subject.only([:field1, :field2]).only(:field3).criteria.fields.should =~ ['field1', 'field2', 'field3'] }
370
+ specify { expect(subject.only(:field1, :field2).criteria.fields).to match_array(['field1', 'field2']) }
371
+ specify { expect(subject.only([:field1, :field2]).only(:field3).criteria.fields).to match_array(['field1', 'field2', 'field3']) }
374
372
  end
375
373
 
376
374
  describe '#only!' do
377
- specify { subject.only!(:field).should be_a described_class }
378
- specify { subject.only!(:field).should_not == subject }
375
+ specify { expect(subject.only!(:field)).to be_a described_class }
376
+ specify { expect(subject.only!(:field)).not_to eq(subject) }
379
377
  specify { expect { subject.only!(:field) }.not_to change { subject.criteria.fields } }
380
378
 
381
- specify { subject.only!(:field1, :field2).criteria.fields.should =~ ['field1', 'field2'] }
382
- specify { subject.only!([:field1, :field2]).only!(:field3).criteria.fields.should =~ ['field3'] }
383
- specify { subject.only([:field1, :field2]).only!(:field3).criteria.fields.should =~ ['field3'] }
379
+ specify { expect(subject.only!(:field1, :field2).criteria.fields).to match_array(['field1', 'field2']) }
380
+ specify { expect(subject.only!([:field1, :field2]).only!(:field3).criteria.fields).to match_array(['field3']) }
381
+ specify { expect(subject.only([:field1, :field2]).only!(:field3).criteria.fields).to match_array(['field3']) }
384
382
  end
385
383
 
386
384
  describe '#types' do
387
- specify { subject.types(:product).should be_a described_class }
388
- specify { subject.types(:product).should_not == subject }
385
+ specify { expect(subject.types(:product)).to be_a described_class }
386
+ specify { expect(subject.types(:product)).not_to eq(subject) }
389
387
  specify { expect { subject.types(:product) }.not_to change { subject.criteria.types } }
390
388
 
391
- specify { subject.types(:user).criteria.types.should == ['user'] }
392
- specify { subject.types(:product, :city).criteria.types.should =~ ['product', 'city'] }
393
- specify { subject.types([:product, :city]).types(:country).criteria.types.should =~ ['product', 'city', 'country'] }
389
+ specify { expect(subject.types(:user).criteria.types).to eq(['user']) }
390
+ specify { expect(subject.types(:product, :city).criteria.types).to match_array(['product', 'city']) }
391
+ specify { expect(subject.types([:product, :city]).types(:country).criteria.types).to match_array(['product', 'city', 'country']) }
394
392
  end
395
393
 
396
394
  describe '#types!' do
397
- specify { subject.types!(:product).should be_a described_class }
398
- specify { subject.types!(:product).should_not == subject }
395
+ specify { expect(subject.types!(:product)).to be_a described_class }
396
+ specify { expect(subject.types!(:product)).not_to eq(subject) }
399
397
  specify { expect { subject.types!(:product) }.not_to change { subject.criteria.types } }
400
398
 
401
- specify { subject.types!(:user).criteria.types.should == ['user'] }
402
- specify { subject.types!(:product, :city).criteria.types.should =~ ['product', 'city'] }
403
- specify { subject.types!([:product, :city]).types!(:country).criteria.types.should =~ ['country'] }
404
- specify { subject.types([:product, :city]).types!(:country).criteria.types.should =~ ['country'] }
399
+ specify { expect(subject.types!(:user).criteria.types).to eq(['user']) }
400
+ specify { expect(subject.types!(:product, :city).criteria.types).to match_array(['product', 'city']) }
401
+ specify { expect(subject.types!([:product, :city]).types!(:country).criteria.types).to match_array(['country']) }
402
+ specify { expect(subject.types([:product, :city]).types!(:country).criteria.types).to match_array(['country']) }
405
403
  end
406
404
 
407
405
  describe '#aggregations' do
408
- specify { subject.aggregations(attribute: {terms: {field: 'attribute'}}).should be_a described_class }
409
- specify { subject.aggregations(attribute: {terms: {field: 'attribute'}}).should_not == subject }
410
- specify { subject.aggregations(attribute: {terms: {field: 'attribute'}}).criteria.request_body[:body].should include(aggregations: {attribute: {terms: {field: 'attribute'}}}) }
406
+ specify { expect(subject.aggregations(attribute: {terms: {field: 'attribute'}})).to be_a described_class }
407
+ specify { expect(subject.aggregations(attribute: {terms: {field: 'attribute'}})).not_to eq(subject) }
408
+ specify { expect(subject.aggregations(attribute: {terms: {field: 'attribute'}}).criteria.request_body[:body]).to include(aggregations: {attribute: {terms: {field: 'attribute'}}}) }
411
409
  end
412
410
 
413
411
  describe '#merge' do
414
412
  let(:query) { described_class.new(ProductsIndex) }
415
413
 
416
- specify { subject.filter { name == 'name' }.merge(query.filter { age == 42 }).criteria.filters
417
- .should == [{term: {'name' => 'name'}}, {term: {'age' => 42}}] }
414
+ specify { expect(subject.filter { name == 'name' }.merge(query.filter { age == 42 }).criteria.filters)
415
+ .to eq([{term: {'name' => 'name'}}, {term: {'age' => 42}}]) }
418
416
  end
419
417
 
420
- describe '#to_a' do
418
+ describe '#to_a', :orm do
421
419
  before { stub_model(:city) }
422
- let(:cities) { 3.times.map { |i| City.create! name: "name#{i}", rating: i } }
420
+ let(:cities) { 3.times.map { |i| City.create! id: i + 1, name: "name#{i}", rating: i } }
423
421
 
424
422
  context do
425
423
  before do
@@ -434,22 +432,23 @@ describe Chewy::Query do
434
432
 
435
433
  before { CitiesIndex::City.import! cities }
436
434
 
437
- specify { CitiesIndex.order(:rating).first.should be_a CitiesIndex::City }
438
- specify { CitiesIndex.order(:rating).first.name.should == 'name0' }
439
- specify { CitiesIndex.order(:rating).first.rating.should == 0 }
440
- specify { CitiesIndex.order(:rating).first.nested.should == {'name' => 'name0'} }
441
- specify { CitiesIndex.order(:rating).first.id.should == cities.first.id.to_s }
435
+ specify { expect(CitiesIndex.order(:rating).first).to be_a CitiesIndex::City }
436
+ specify { expect(CitiesIndex.order(:rating).first.name).to eq('name0') }
437
+ specify { expect(CitiesIndex.order(:rating).first.rating).to eq(0) }
438
+ specify { expect(CitiesIndex.order(:rating).first.nested).to eq({'name' => 'name0'}) }
439
+ specify { expect(CitiesIndex.order(:rating).first.id).to eq(cities.first.id.to_s) }
442
440
 
443
- specify { CitiesIndex.order(:rating).only(:name).first.name.should == 'name0' }
444
- specify { CitiesIndex.order(:rating).only(:name).first.rating.should be_nil }
445
- specify { CitiesIndex.order(:rating).only(:nested).first.nested.should == {'name' => 'name0'} }
441
+ specify { expect(CitiesIndex.order(:rating).only(:name).first.name).to eq('name0') }
442
+ specify { expect(CitiesIndex.order(:rating).only(:name).first.rating).to be_nil }
443
+ specify { expect(CitiesIndex.order(:rating).only(:nested).first.nested).to eq({'name' => 'name0'}) }
446
444
 
447
- specify { CitiesIndex.order(:rating).first._score.should be_nil }
448
- specify { CitiesIndex.all.first._score.should be > 0 }
449
- specify { CitiesIndex.query(match: {name: 'name0'}).first._score.should be > 0 }
445
+ specify { expect(CitiesIndex.order(:rating).first._score).to be_nil }
446
+ specify { expect(CitiesIndex.all.first._score).to be > 0 }
447
+ specify { expect(CitiesIndex.query(match: {name: 'name0'}).first._score).to be > 0 }
448
+ specify { expect(CitiesIndex.query(match: {name: 'name0'}).took).to be >= 0 }
450
449
 
451
- specify { CitiesIndex.order(:rating).first._data['_explanation'].should be_nil }
452
- specify { CitiesIndex.order(:rating).explain.first._data['_explanation'].should be_present }
450
+ specify { expect(CitiesIndex.order(:rating).first._explanation).to be_nil }
451
+ specify { expect(CitiesIndex.order(:rating).explain.first._explanation).to be_present }
453
452
  end
454
453
 
455
454
  context 'sourceless' do
@@ -466,10 +465,10 @@ describe Chewy::Query do
466
465
  end
467
466
  before { CitiesIndex::City.import! cities }
468
467
 
469
- specify { CitiesIndex.order(:rating).first.should be_a CitiesIndex::City }
470
- specify { CitiesIndex.order(:rating).first.name.should be_nil }
471
- specify { CitiesIndex.order(:rating).first.rating.should be_nil }
472
- specify { CitiesIndex.order(:rating).first.nested.should be_nil }
468
+ specify { expect(CitiesIndex.order(:rating).first).to be_a CitiesIndex::City }
469
+ specify { expect(CitiesIndex.order(:rating).first.name).to be_nil }
470
+ specify { expect(CitiesIndex.order(:rating).first.rating).to be_nil }
471
+ specify { expect(CitiesIndex.order(:rating).first.nested).to be_nil }
473
472
  end
474
473
  end
475
474
  end