Skip to content

Commit 87a9f83

Browse files
committed
[MODEL] Port all integration tests to rspec
1 parent bdb51da commit 87a9f83

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+2391
-1913
lines changed

elasticsearch-model/Rakefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ namespace :test do
4545
task :integration do
4646
#sh "BUNDLE_GEMFILE='#{File.expand_path('../gemfiles/3.0.gemfile', __FILE__)}' bundle exec rake test:run_integration"
4747
#sh "BUNDLE_GEMFILE='#{File.expand_path('../gemfiles/4.0.gemfile', __FILE__)}' bundle exec rake test:run_integration"
48-
sh "BUNDLE_GEMFILE='#{File.expand_path('../gemfiles/5.0.gemfile', __FILE__)}' bundle exec rake test:run_integration"
48+
#sh "BUNDLE_GEMFILE='#{File.expand_path('../gemfiles/5.0.gemfile', __FILE__)}' bundle exec rake test:run_integration"
4949
end
5050

5151
desc "Run unit and integration tests"

elasticsearch-model/gemfiles/4.0.gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ gemspec path: '../'
1010
gem 'activemodel', '~> 4'
1111
gem 'activerecord', '~> 4'
1212
gem 'sqlite3' unless defined?(JRUBY_VERSION)
13+
gem 'mongoid', '~> 5'
1314

1415
group :development, :testing do
1516
gem 'rspec'

elasticsearch-model/gemfiles/5.0.gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ gemspec path: '../'
1010
gem 'activemodel', '~> 5'
1111
gem 'activerecord', '~> 5'
1212
gem 'sqlite3' unless defined?(JRUBY_VERSION)
13+
gem 'mongoid', '~> 6'
1314

1415
group :development, :testing do
1516
gem 'rspec'

elasticsearch-model/spec/elasticsearch/model/adapter_spec.rb

+4-6
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,10 @@ class ::DummyAdapter
1313
end
1414

1515
after(:all) do
16-
Elasticsearch::Model::Adapter::Adapter.adapters.delete(DummyAdapterClassWithAdapter)
17-
Elasticsearch::Model::Adapter::Adapter.adapters.delete(DummyAdapterClass)
18-
Elasticsearch::Model::Adapter::Adapter.adapters.delete(DummyAdapter)
19-
Object.send(:remove_const, :DummyAdapterClass) if defined?(DummyAdapterClass)
20-
Object.send(:remove_const, :DummyAdapterClassWithAdapter) if defined?(DummyAdapterClassWithAdapter)
21-
Object.send(:remove_const, :DummyAdapter) if defined?(DummyAdapter)
16+
[DummyAdapterClassWithAdapter, DummyAdapterClass, DummyAdapter].each do |adapter|
17+
Elasticsearch::Model::Adapter::Adapter.adapters.delete(adapter)
18+
end
19+
remove_classes(DummyAdapterClass, DummyAdapterClassWithAdapter, DummyAdapter)
2220
end
2321

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

0 commit comments

Comments
 (0)