Skip to content

Commit aa97dc0

Browse files
committed
[MODEL] Use default scope on ActiveRecord model when importing (elastic#827)
1 parent 0143aa7 commit aa97dc0

File tree

1 file changed

+65
-12
lines changed

1 file changed

+65
-12
lines changed

elasticsearch-model/test/integration/active_record_import_test.rb

+65-12
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,22 @@ module Elasticsearch
1010
module Model
1111
class ActiveRecordImportIntegrationTest < Elasticsearch::Test::IntegrationTestCase
1212

13-
class ::ImportArticle < ActiveRecord::Base
14-
include Elasticsearch::Model
13+
context "ActiveRecord importing" do
14+
setup do
15+
Object.send(:remove_const, :ImportArticle) if defined?(ImportArticle)
16+
class ::ImportArticle < ActiveRecord::Base
17+
include Elasticsearch::Model
1518

16-
scope :popular, -> { where('views >= 50') }
19+
scope :popular, -> { where('views >= 50') }
1720

18-
mapping do
19-
indexes :title, type: 'text'
20-
indexes :views, type: 'integer'
21-
indexes :numeric, type: 'integer'
22-
indexes :created_at, type: 'date'
23-
end
24-
end
21+
mapping do
22+
indexes :title, type: 'text'
23+
indexes :views, type: 'integer'
24+
indexes :numeric, type: 'integer'
25+
indexes :created_at, type: 'date'
26+
end
27+
end
2528

26-
context "ActiveRecord importing" do
27-
setup do
2829
ActiveRecord::Schema.define(:version => 1) do
2930
create_table :import_articles do |t|
3031
t.string :title
@@ -110,6 +111,58 @@ class ::ImportArticle < ActiveRecord::Base
110111
end
111112
end
112113

114+
context "ActiveRecord importing when the model has a default scope" do
115+
116+
setup do
117+
Object.send(:remove_const, :ImportArticle) if defined?(ImportArticle)
118+
class ::ImportArticle < ActiveRecord::Base
119+
include Elasticsearch::Model
120+
121+
default_scope { where('views >= 8') }
122+
123+
mapping do
124+
indexes :title, type: 'text'
125+
indexes :views, type: 'integer'
126+
indexes :numeric, type: 'integer'
127+
indexes :created_at, type: 'date'
128+
end
129+
end
130+
131+
ActiveRecord::Schema.define(:version => 1) do
132+
create_table :import_articles do |t|
133+
t.string :title
134+
t.integer :views
135+
t.string :numeric # For the sake of invalid data sent to Elasticsearch
136+
t.datetime :created_at, :default => 'NOW()'
137+
end
138+
end
139+
140+
ImportArticle.delete_all
141+
ImportArticle.__elasticsearch__.delete_index! force: true
142+
ImportArticle.__elasticsearch__.create_index! force: true
143+
ImportArticle.__elasticsearch__.client.cluster.health wait_for_status: 'yellow'
144+
145+
10.times { |i| ImportArticle.create! title: "Test #{i}", views: i }
146+
end
147+
148+
should "import only documents from the default scope" do
149+
assert_equal 2, ImportArticle.count
150+
151+
assert_equal 0, ImportArticle.import
152+
153+
ImportArticle.__elasticsearch__.refresh_index!
154+
assert_equal 2, ImportArticle.search('*').results.total
155+
end
156+
157+
should "import only documents from a specific query combined with the default scope" do
158+
assert_equal 2, ImportArticle.count
159+
160+
assert_equal 0, ImportArticle.import(query: -> { where("title = 'Test 9'") })
161+
162+
ImportArticle.__elasticsearch__.refresh_index!
163+
assert_equal 1, ImportArticle.search('*').results.total
164+
end
165+
end
113166
end
114167
end
115168
end

0 commit comments

Comments
 (0)