Skip to content

[MODEL] Port all integration tests to rspec #837

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions elasticsearch-model/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,17 @@ namespace :test do

desc "Run unit tests against ActiveModel 3, 4 and 5"
task :unit do
end

desc "Run integration tests against latest stable ActiveModel (5)"
task :integration do
['3.0.gemfile', '4.0.gemfile', '5.0.gemfile'].each do |gemfile|
['bundle exec rake test:run_unit', 'bundle exec rspec'].each do |cmd|
sh "BUNDLE_GEMFILE='#{File.expand_path('../gemfiles/'+gemfile, __FILE__)}' #{cmd}"
end
end
end

desc "Run integration tests against latest stable ActiveModel (5)"
task :integration do
#sh "BUNDLE_GEMFILE='#{File.expand_path('../gemfiles/3.0.gemfile', __FILE__)}' bundle exec rake test:run_integration"
#sh "BUNDLE_GEMFILE='#{File.expand_path('../gemfiles/4.0.gemfile', __FILE__)}' bundle exec rake test:run_integration"
sh "BUNDLE_GEMFILE='#{File.expand_path('../gemfiles/5.0.gemfile', __FILE__)}' bundle exec rake test:run_integration"
end

desc "Run unit and integration tests"
task :all do
Rake::Task['test:unit'].invoke
Expand Down
1 change: 1 addition & 0 deletions elasticsearch-model/gemfiles/4.0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ gemspec path: '../'
gem 'activemodel', '~> 4'
gem 'activerecord', '~> 4'
gem 'sqlite3' unless defined?(JRUBY_VERSION)
gem 'mongoid', '~> 5'

group :development, :testing do
gem 'rspec'
Expand Down
1 change: 1 addition & 0 deletions elasticsearch-model/gemfiles/5.0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ gemspec path: '../'
gem 'activemodel', '~> 5'
gem 'activerecord', '~> 5'
gem 'sqlite3' unless defined?(JRUBY_VERSION)
gem 'mongoid', '~> 6'

group :development, :testing do
gem 'rspec'
Expand Down
10 changes: 4 additions & 6 deletions elasticsearch-model/spec/elasticsearch/model/adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ class ::DummyAdapter
end

after(:all) do
Elasticsearch::Model::Adapter::Adapter.adapters.delete(DummyAdapterClassWithAdapter)
Elasticsearch::Model::Adapter::Adapter.adapters.delete(DummyAdapterClass)
Elasticsearch::Model::Adapter::Adapter.adapters.delete(DummyAdapter)
Object.send(:remove_const, :DummyAdapterClass) if defined?(DummyAdapterClass)
Object.send(:remove_const, :DummyAdapterClassWithAdapter) if defined?(DummyAdapterClassWithAdapter)
Object.send(:remove_const, :DummyAdapter) if defined?(DummyAdapter)
[DummyAdapterClassWithAdapter, DummyAdapterClass, DummyAdapter].each do |adapter|
Elasticsearch::Model::Adapter::Adapter.adapters.delete(adapter)
end
remove_classes(DummyAdapterClass, DummyAdapterClassWithAdapter, DummyAdapter)
end

describe '#from_class' do
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,334 @@
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
Loading