|
18 | 18 | require 'spec_helper' |
19 | 19 |
|
20 | 20 | describe 'Elasticsearch::Model::Adapter::ActiveRecord Associations' do |
21 | | - |
22 | 21 | before(:all) do |
23 | 22 | ActiveRecord::Schema.define(version: 1) do |
24 | 23 | create_table :categories do |t| |
|
76 | 75 | end |
77 | 76 |
|
78 | 77 | context 'when a document is created' do |
79 | | - |
80 | 78 | before do |
81 | 79 | Post.create!(title: 'Test') |
82 | 80 | Post.create!(title: 'Testing Coding') |
|
97 | 95 | end |
98 | 96 |
|
99 | 97 | describe 'has_many_and_belongs_to association' do |
100 | | - |
101 | | - context 'when an association is updated' do |
102 | | - |
| 98 | + context 'when an association is updated' do |
103 | 99 | before do |
104 | 100 | post.categories = [category_a, category_b] |
105 | 101 | Post.__elasticsearch__.refresh_index! |
|
119 | 115 |
|
120 | 116 | let(:search_result) do |
121 | 117 | Post.search(query: { |
122 | | - bool: { |
123 | | - must: { |
124 | | - multi_match: { |
125 | | - fields: ['title'], |
126 | | - query: 'first' |
127 | | - } |
128 | | - }, |
129 | | - filter: { |
130 | | - terms: { |
131 | | - categories: ['One'] |
132 | | - } |
133 | | - } |
134 | | - } |
135 | | - } ) |
| 118 | + bool: { |
| 119 | + must: { |
| 120 | + multi_match: { |
| 121 | + fields: ['title'], |
| 122 | + query: 'first' |
| 123 | + } |
| 124 | + }, |
| 125 | + filter: { |
| 126 | + terms: { |
| 127 | + categories: ['One'] |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + } ) |
136 | 132 | end |
137 | 133 |
|
138 | 134 | it 'applies the update with' do |
|
144 | 140 | end |
145 | 141 |
|
146 | 142 | context 'when an association is deleted' do |
147 | | - |
148 | 143 | before do |
149 | 144 | post.categories = [category_a, category_b] |
150 | 145 | post.categories = [category_b] |
|
165 | 160 |
|
166 | 161 | let(:search_result) do |
167 | 162 | Post.search(query: { |
168 | | - bool: { |
169 | | - must: { |
170 | | - multi_match: { |
171 | | - fields: ['title'], |
172 | | - query: 'first' |
173 | | - } |
174 | | - }, |
175 | | - filter: { |
176 | | - terms: { |
177 | | - categories: ['One'] |
178 | | - } |
179 | | - } |
180 | | - } |
181 | | - } ) |
| 163 | + bool: { |
| 164 | + must: { |
| 165 | + multi_match: { |
| 166 | + fields: ['title'], |
| 167 | + query: 'first' |
| 168 | + } |
| 169 | + }, |
| 170 | + filter: { |
| 171 | + terms: { |
| 172 | + categories: ['One'] |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + } ) |
182 | 177 | end |
183 | 178 |
|
184 | 179 | it 'applies the update with a reindex' do |
|
189 | 184 | end |
190 | 185 |
|
191 | 186 | describe 'has_many through association' do |
192 | | - |
193 | 187 | context 'when the association is updated' do |
194 | | - |
195 | 188 | before do |
196 | 189 | author_a = Author.where(first_name: "John", last_name: "Smith").first_or_create! |
197 | 190 | author_b = Author.where(first_name: "Mary", last_name: "Smith").first_or_create! |
|
210 | 203 | Post.__elasticsearch__.refresh_index! |
211 | 204 | end |
212 | 205 |
|
213 | | - context 'if active record is at least 4' do |
214 | | - |
215 | | - let(:search_result) do |
216 | | - Post.search('authors.full_name:john') |
217 | | - end |
218 | | - |
219 | | - it 'applies the update', if: active_record_at_least_4? do |
220 | | - expect(search_result.results.size).to eq(2) |
221 | | - expect(search_result.records.size).to eq(2) |
222 | | - end |
| 206 | + let(:search_result) do |
| 207 | + Post.search('authors.full_name:john') |
223 | 208 | end |
224 | 209 |
|
225 | | - context 'if active record is less than 4' do |
226 | | - |
227 | | - let(:search_result) do |
228 | | - Post.search('authors.author.full_name:john') |
229 | | - end |
230 | | - |
231 | | - it 'applies the update', if: !active_record_at_least_4? do |
232 | | - expect(search_result.results.size).to eq(2) |
233 | | - expect(search_result.records.size).to eq(2) |
234 | | - end |
| 210 | + it 'applies the update' do |
| 211 | + expect(search_result.results.size).to eq(2) |
| 212 | + expect(search_result.records.size).to eq(2) |
235 | 213 | end |
236 | 214 | end |
237 | 215 |
|
238 | | - context 'when an association is added', if: active_record_at_least_4? do |
239 | | - |
| 216 | + context 'when an association is added' do |
240 | 217 | before do |
241 | 218 | author_a = Author.where(first_name: "John", last_name: "Smith").first_or_create! |
242 | 219 | author_b = Author.where(first_name: "Mary", last_name: "Smith").first_or_create! |
|
262 | 239 | end |
263 | 240 |
|
264 | 241 | describe 'has_many association' do |
265 | | - |
266 | | - context 'when an association is added', if: active_record_at_least_4? do |
267 | | - |
| 242 | + context 'when an association is added' do |
268 | 243 | before do |
269 | 244 | # Create posts |
270 | 245 | post_1 = Post.create!(title: "First Post", text: "This is the first post...") |
|
282 | 257 |
|
283 | 258 | let(:search_result) do |
284 | 259 | Post.search(query: { |
285 | | - nested: { |
286 | | - path: 'comments', |
287 | | - query: { |
288 | | - bool: { |
289 | | - must: [ |
290 | | - { match: { 'comments.author' => 'john' } }, |
291 | | - { match: { 'comments.text' => 'good' } } |
292 | | - ] |
293 | | - } |
294 | | - } |
295 | | - } |
296 | | - }) |
| 260 | + nested: { |
| 261 | + path: 'comments', |
| 262 | + query: { |
| 263 | + bool: { |
| 264 | + must: [ |
| 265 | + { match: { 'comments.author' => 'john' } }, |
| 266 | + { match: { 'comments.text' => 'good' } } |
| 267 | + ] |
| 268 | + } |
| 269 | + } |
| 270 | + } |
| 271 | + }) |
297 | 272 | end |
298 | 273 |
|
299 | 274 | it 'adds the association' do |
|
303 | 278 | end |
304 | 279 |
|
305 | 280 | describe '#touch' do |
306 | | - |
307 | 281 | context 'when a touch callback is defined on the model' do |
308 | | - |
309 | 282 | before do |
310 | 283 | # Create categories |
311 | 284 | category_a = Category.where(title: "One").first_or_create! |
|
329 | 302 | end |
330 | 303 |
|
331 | 304 | describe '#includes' do |
332 | | - |
333 | 305 | before do |
334 | 306 | post_1 = Post.create(title: 'One') |
335 | 307 | post_2 = Post.create(title: 'Two') |
|
0 commit comments