1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
|
# Licensed to Elasticsearch B.V. under one or more contributor
# license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright
# ownership. Elasticsearch B.V. licenses this file to you under
# the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
require 'spec_helper'
describe 'Elasticsearch::Model::Adapter::ActiveRecord Associations' do
before(:all) do
ActiveRecord::Schema.define(version: 1) do
create_table :categories do |t|
t.string :title
t.timestamps null: false
end
create_table :categories_posts do |t|
t.references :post, :category
end
create_table :authors do |t|
t.string :first_name, :last_name
t.timestamps null: false
end
create_table :authorships do |t|
t.string :first_name, :last_name
t.references :post
t.references :author
t.timestamps null: false
end
create_table :comments do |t|
t.string :text
t.string :author
t.references :post
t.timestamps null: false
end
add_index(:comments, :post_id) unless index_exists?(:comments, :post_id)
create_table :posts do |t|
t.string :title
t.text :text
t.boolean :published
t.timestamps null: false
end
end
Comment.__send__ :include, Elasticsearch::Model
Comment.__send__ :include, Elasticsearch::Model::Callbacks
end
before do
clear_tables(:categories, :categories_posts, :authors, :authorships, :comments, :posts)
clear_indices(Post)
Post.__elasticsearch__.create_index!(force: true)
Comment.__elasticsearch__.create_index!(force: true)
end
after do
clear_tables(Post, Category)
clear_indices(Post)
end
context 'when a document is created' do
before do
Post.create!(title: 'Test')
Post.create!(title: 'Testing Coding')
Post.create!(title: 'Coding')
Post.__elasticsearch__.refresh_index!
end
let(:search_result) do
Post.search('title:test')
end
it 'indexes the document' do
expect(search_result.results.size).to eq(2)
expect(search_result.results.first.title).to eq('Test')
expect(search_result.records.size).to eq(2)
expect(search_result.records.first.title).to eq('Test')
end
end
describe 'has_many_and_belongs_to association' do
context 'when an association is updated' do
before do
post.categories = [category_a, category_b]
Post.__elasticsearch__.refresh_index!
end
let(:category_a) do
Category.where(title: "One").first_or_create!
end
let(:category_b) do
Category.where(title: "Two").first_or_create!
end
let(:post) do
Post.create! title: "First Post", text: "This is the first post..."
end
let(:search_result) do
Post.search(query: {
bool: {
must: {
multi_match: {
fields: ['title'],
query: 'first'
}
},
filter: {
terms: {
categories: ['One']
}
}
}
} )
end
it 'applies the update with' do
expect(search_result.results.size).to eq(1)
expect(search_result.results.first.title).to eq('First Post')
expect(search_result.records.size).to eq(1)
expect(search_result.records.first.title).to eq('First Post')
end
end
context 'when an association is deleted' do
before do
post.categories = [category_a, category_b]
post.categories = [category_b]
Post.__elasticsearch__.refresh_index!
end
let(:category_a) do
Category.where(title: "One").first_or_create!
end
let(:category_b) do
Category.where(title: "Two").first_or_create!
end
let(:post) do
Post.create! title: "First Post", text: "This is the first post..."
end
let(:search_result) do
Post.search(query: {
bool: {
must: {
multi_match: {
fields: ['title'],
query: 'first'
}
},
filter: {
terms: {
categories: ['One']
}
}
}
} )
end
it 'applies the update with a reindex' do
expect(search_result.results.size).to eq(0)
expect(search_result.records.size).to eq(0)
end
end
end
describe 'has_many through association' do
context 'when the association is updated' do
before do
author_a = Author.where(first_name: "John", last_name: "Smith").first_or_create!
author_b = Author.where(first_name: "Mary", last_name: "Smith").first_or_create!
author_c = Author.where(first_name: "Kobe", last_name: "Griss").first_or_create!
# Create posts
post_1 = Post.create!(title: "First Post", text: "This is the first post...")
post_2 = Post.create!(title: "Second Post", text: "This is the second post...")
post_3 = Post.create!(title: "Third Post", text: "This is the third post...")
# Assign authors
post_1.authors = [author_a, author_b]
post_2.authors = [author_a]
post_3.authors = [author_c]
Post.__elasticsearch__.refresh_index!
end
context 'if active record is at least 4' do
let(:search_result) do
Post.search('authors.full_name:john')
end
it 'applies the update', if: active_record_at_least_4? do
expect(search_result.results.size).to eq(2)
expect(search_result.records.size).to eq(2)
end
end
context 'if active record is less than 4' do
let(:search_result) do
Post.search('authors.author.full_name:john')
end
it 'applies the update', if: !active_record_at_least_4? do
expect(search_result.results.size).to eq(2)
expect(search_result.records.size).to eq(2)
end
end
end
context 'when an association is added', if: active_record_at_least_4? do
before do
author_a = Author.where(first_name: "John", last_name: "Smith").first_or_create!
author_b = Author.where(first_name: "Mary", last_name: "Smith").first_or_create!
# Create posts
post_1 = Post.create!(title: "First Post", text: "This is the first post...")
# Assign authors
post_1.authors = [author_a]
post_1.authors << author_b
Post.__elasticsearch__.refresh_index!
end
let(:search_result) do
Post.search('authors.full_name:john')
end
it 'adds the association' do
expect(search_result.results.size).to eq(1)
expect(search_result.records.size).to eq(1)
end
end
end
describe 'has_many association' do
context 'when an association is added', if: active_record_at_least_4? do
before do
# Create posts
post_1 = Post.create!(title: "First Post", text: "This is the first post...")
post_2 = Post.create!(title: "Second Post", text: "This is the second post...")
# Add comments
post_1.comments.create!(author: 'John', text: 'Excellent')
post_1.comments.create!(author: 'Abby', text: 'Good')
post_2.comments.create!(author: 'John', text: 'Terrible')
post_1.comments.create!(author: 'John', text: 'Or rather just good...')
Post.__elasticsearch__.refresh_index!
end
let(:search_result) do
Post.search(query: {
nested: {
path: 'comments',
query: {
bool: {
must: [
{ match: { 'comments.author' => 'john' } },
{ match: { 'comments.text' => 'good' } }
]
}
}
}
})
end
it 'adds the association' do
expect(search_result.results.size).to eq(1)
end
end
end
describe '#touch' do
context 'when a touch callback is defined on the model' do
before do
# Create categories
category_a = Category.where(title: "One").first_or_create!
# Create post
post = Post.create!(title: "First Post", text: "This is the first post...")
# Assign category
post.categories << category_a
category_a.update_attribute(:title, "Updated")
category_a.posts.each { |p| p.touch }
Post.__elasticsearch__.refresh_index!
end
it 'executes the callback after #touch' do
expect(Post.search('categories:One').size).to eq(0)
expect(Post.search('categories:Updated').size).to eq(1)
end
end
end
describe '#includes' do
before do
post_1 = Post.create(title: 'One')
post_2 = Post.create(title: 'Two')
post_1.comments.create(text: 'First comment')
post_2.comments.create(text: 'Second comment')
Comment.__elasticsearch__.refresh_index!
end
let(:search_result) do
Comment.search('first').records(includes: :post)
end
it 'eager loads associations' do
expect(search_result.first.association(:post)).to be_loaded
expect(search_result.first.post.title).to eq('One')
end
end
end
|