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