Skip to content

Commit fa2b525

Browse files
committed
[MODEL] Reorganized the class definitions in the integration tests
With ActiveRecord 4 and 5, the class definitions within Shoulda's `setup` method don't work correctly.
1 parent 66bb012 commit fa2b525

5 files changed

+126
-123
lines changed

Diff for: elasticsearch-model/test/integration/active_record_associations_test.rb

+55-53
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,58 @@ module Elasticsearch
88
module Model
99
class ActiveRecordAssociationsIntegrationTest < Elasticsearch::Test::IntegrationTestCase
1010

11+
# ----- Search integration via Concern module -----------------------------------------------------
12+
13+
module Searchable
14+
extend ActiveSupport::Concern
15+
16+
included do
17+
include Elasticsearch::Model
18+
include Elasticsearch::Model::Callbacks
19+
20+
# Set up the mapping
21+
#
22+
settings index: { number_of_shards: 1, number_of_replicas: 0 } do
23+
mapping do
24+
indexes :title, analyzer: 'snowball'
25+
indexes :created_at, type: 'date'
26+
27+
indexes :authors do
28+
indexes :first_name
29+
indexes :last_name
30+
indexes :full_name, type: 'multi_field' do
31+
indexes :full_name
32+
indexes :raw, analyzer: 'keyword'
33+
end
34+
end
35+
36+
indexes :categories, analyzer: 'keyword'
37+
38+
indexes :comments, type: 'nested' do
39+
indexes :text
40+
indexes :author
41+
end
42+
end
43+
end
44+
45+
# Customize the JSON serialization for Elasticsearch
46+
#
47+
def as_indexed_json(options={})
48+
{
49+
title: title,
50+
text: text,
51+
categories: categories.map(&:title),
52+
authors: authors.as_json(methods: [:full_name], only: [:full_name, :first_name, :last_name]),
53+
comments: comments.as_json(only: [:text, :author])
54+
}
55+
end
56+
57+
# Update document in the index after touch
58+
#
59+
after_touch() { __elasticsearch__.index_document }
60+
end
61+
end
62+
1163
context "ActiveRecord associations" do
1264
setup do
1365

@@ -40,7 +92,9 @@ class ActiveRecordAssociationsIntegrationTest < Elasticsearch::Test::Integration
4092
t.string :author
4193
t.references :post
4294
t.timestamps
43-
end and add_index(:comments, :post_id)
95+
end
96+
97+
add_index(:comments, :post_id) unless index_exists?(:comments, :post_id)
4498

4599
create_table :posts do |t|
46100
t.string :title
@@ -81,58 +135,6 @@ class Post < ActiveRecord::Base
81135
has_many :comments
82136
end
83137

84-
# ----- Search integration via Concern module -----------------------------------------------------
85-
86-
module Searchable
87-
extend ActiveSupport::Concern
88-
89-
included do
90-
include Elasticsearch::Model
91-
include Elasticsearch::Model::Callbacks
92-
93-
# Set up the mapping
94-
#
95-
settings index: { number_of_shards: 1, number_of_replicas: 0 } do
96-
mapping do
97-
indexes :title, analyzer: 'snowball'
98-
indexes :created_at, type: 'date'
99-
100-
indexes :authors do
101-
indexes :first_name
102-
indexes :last_name
103-
indexes :full_name, type: 'multi_field' do
104-
indexes :full_name
105-
indexes :raw, analyzer: 'keyword'
106-
end
107-
end
108-
109-
indexes :categories, analyzer: 'keyword'
110-
111-
indexes :comments, type: 'nested' do
112-
indexes :text
113-
indexes :author
114-
end
115-
end
116-
end
117-
118-
# Customize the JSON serialization for Elasticsearch
119-
#
120-
def as_indexed_json(options={})
121-
{
122-
title: title,
123-
text: text,
124-
categories: categories.map(&:title),
125-
authors: authors.as_json(methods: [:full_name], only: [:full_name, :first_name, :last_name]),
126-
comments: comments.as_json(only: [:text, :author])
127-
}
128-
end
129-
130-
# Update document in the index after touch
131-
#
132-
after_touch() { __elasticsearch__.index_document }
133-
end
134-
end
135-
136138
# Include the search integration
137139
#
138140
Post.__send__ :include, Searchable

Diff for: elasticsearch-model/test/integration/active_record_basic_test.rb

+22-21
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,28 @@
99
module Elasticsearch
1010
module Model
1111
class ActiveRecordBasicIntegrationTest < Elasticsearch::Test::IntegrationTestCase
12+
13+
class ::Article < ActiveRecord::Base
14+
include Elasticsearch::Model
15+
include Elasticsearch::Model::Callbacks
16+
17+
settings index: { number_of_shards: 1, number_of_replicas: 0 } do
18+
mapping do
19+
indexes :title, type: 'string', analyzer: 'snowball'
20+
indexes :body, type: 'string'
21+
indexes :clicks, type: 'integer'
22+
indexes :created_at, type: 'date'
23+
end
24+
end
25+
26+
def as_indexed_json(options = {})
27+
attributes
28+
.symbolize_keys
29+
.slice(:title, :body, :clicks, :created_at)
30+
.merge(suggest_title: title)
31+
end
32+
end
33+
1234
context "ActiveRecord basic integration" do
1335
setup do
1436
ActiveRecord::Schema.define(:version => 1) do
@@ -20,27 +42,6 @@ class ActiveRecordBasicIntegrationTest < Elasticsearch::Test::IntegrationTestCas
2042
end
2143
end
2244

23-
class ::Article < ActiveRecord::Base
24-
include Elasticsearch::Model
25-
include Elasticsearch::Model::Callbacks
26-
27-
settings index: { number_of_shards: 1, number_of_replicas: 0 } do
28-
mapping do
29-
indexes :title, type: 'string', analyzer: 'snowball'
30-
indexes :body, type: 'string'
31-
indexes :clicks, type: 'integer'
32-
indexes :created_at, type: 'date'
33-
end
34-
end
35-
36-
def as_indexed_json(options = {})
37-
attributes
38-
.symbolize_keys
39-
.slice(:title, :body, :clicks, :created_at)
40-
.merge(suggest_title: title)
41-
end
42-
end
43-
4445
Article.delete_all
4546
Article.__elasticsearch__.create_index! force: true
4647

Diff for: elasticsearch-model/test/integration/active_record_import_test.rb

+14-13
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@
77
module Elasticsearch
88
module Model
99
class ActiveRecordImportIntegrationTest < Elasticsearch::Test::IntegrationTestCase
10+
11+
class ::ImportArticle < ActiveRecord::Base
12+
include Elasticsearch::Model
13+
14+
scope :popular, -> { where('views >= 50') }
15+
16+
mapping do
17+
indexes :title, type: 'string'
18+
indexes :views, type: 'integer'
19+
indexes :numeric, type: 'integer'
20+
indexes :created_at, type: 'date'
21+
end
22+
end
23+
1024
context "ActiveRecord importing" do
1125
setup do
1226
ActiveRecord::Schema.define(:version => 1) do
@@ -18,19 +32,6 @@ class ActiveRecordImportIntegrationTest < Elasticsearch::Test::IntegrationTestCa
1832
end
1933
end
2034

21-
class ::ImportArticle < ActiveRecord::Base
22-
include Elasticsearch::Model
23-
24-
scope :popular, -> { where('views >= 50') }
25-
26-
mapping do
27-
indexes :title, type: 'string'
28-
indexes :views, type: 'integer'
29-
indexes :numeric, type: 'integer'
30-
indexes :created_at, type: 'date'
31-
end
32-
end
33-
3435
ImportArticle.delete_all
3536
ImportArticle.__elasticsearch__.create_index! force: true
3637
ImportArticle.__elasticsearch__.client.cluster.health wait_for_status: 'yellow'

Diff for: elasticsearch-model/test/integration/active_record_pagination_test.rb

+11-11
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@
77
module Elasticsearch
88
module Model
99
class ActiveRecordPaginationTest < Elasticsearch::Test::IntegrationTestCase
10-
context "ActiveRecord pagination" do
11-
setup do
12-
class ::ArticleForPagination < ActiveRecord::Base
13-
include Elasticsearch::Model
10+
class ::ArticleForPagination < ActiveRecord::Base
11+
include Elasticsearch::Model
1412

15-
scope :published, -> { where(published: true) }
13+
scope :published, -> { where(published: true) }
1614

17-
settings index: { number_of_shards: 1, number_of_replicas: 0 } do
18-
mapping do
19-
indexes :title, type: 'string', analyzer: 'snowball'
20-
indexes :created_at, type: 'date'
21-
end
22-
end
15+
settings index: { number_of_shards: 1, number_of_replicas: 0 } do
16+
mapping do
17+
indexes :title, type: 'string', analyzer: 'snowball'
18+
indexes :created_at, type: 'date'
2319
end
20+
end
21+
end
2422

23+
context "ActiveRecord pagination" do
24+
setup do
2525
ActiveRecord::Schema.define(:version => 1) do
2626
create_table ::ArticleForPagination.table_name do |t|
2727
t.string :title

Diff for: elasticsearch-model/test/integration/multiple_models_test.rb

+24-25
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,30 @@
99
module Elasticsearch
1010
module Model
1111
class MultipleModelsIntegration < Elasticsearch::Test::IntegrationTestCase
12+
module ::NameSearch
13+
extend ActiveSupport::Concern
14+
15+
included do
16+
include Elasticsearch::Model
17+
include Elasticsearch::Model::Callbacks
18+
19+
settings index: {number_of_shards: 1, number_of_replicas: 0} do
20+
mapping do
21+
indexes :name, type: 'string', analyzer: 'snowball'
22+
indexes :created_at, type: 'date'
23+
end
24+
end
25+
end
26+
end
27+
28+
class ::Episode < ActiveRecord::Base
29+
include NameSearch
30+
end
31+
32+
class ::Series < ActiveRecord::Base
33+
include NameSearch
34+
end
35+
1236
context "Multiple models" do
1337
setup do
1438
ActiveRecord::Schema.define(:version => 1) do
@@ -23,30 +47,6 @@ class MultipleModelsIntegration < Elasticsearch::Test::IntegrationTestCase
2347
end
2448
end
2549

26-
module ::NameSearch
27-
extend ActiveSupport::Concern
28-
29-
included do
30-
include Elasticsearch::Model
31-
include Elasticsearch::Model::Callbacks
32-
33-
settings index: {number_of_shards: 1, number_of_replicas: 0} do
34-
mapping do
35-
indexes :name, type: 'string', analyzer: 'snowball'
36-
indexes :created_at, type: 'date'
37-
end
38-
end
39-
end
40-
end
41-
42-
class ::Episode < ActiveRecord::Base
43-
include NameSearch
44-
end
45-
46-
class ::Series < ActiveRecord::Base
47-
include NameSearch
48-
end
49-
5050
[::Episode, ::Series].each do |model|
5151
model.delete_all
5252
model.__elasticsearch__.create_index! force: true
@@ -55,7 +55,6 @@ class ::Series < ActiveRecord::Base
5555
model.create name: "The greatest #{model.name}"
5656
model.__elasticsearch__.refresh_index!
5757
end
58-
5958
end
6059

6160
should "find matching documents across multiple models" do

0 commit comments

Comments
 (0)