Skip to content

Commit 90d0055

Browse files
committed
[MODEL] Added an integration test for the ActiveRecord scope support for import
Related: elastic#104
1 parent 9b0e0d0 commit 90d0055

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

elasticsearch-model/test/integration/active_record_import_test.rb

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ class ActiveRecordImportIntegrationTest < Elasticsearch::Test::IntegrationTestCa
77
class ::ImportArticle < ActiveRecord::Base
88
include Elasticsearch::Model
99

10+
scope :popular, -> { where('views >= 50') }
11+
1012
mapping do
1113
indexes :title, type: 'string'
1214
indexes :views, type: 'integer'
15+
indexes :numeric, type: 'integer'
1316
indexes :created_at, type: 'date'
1417
end
1518
end
@@ -19,7 +22,8 @@ class ::ImportArticle < ActiveRecord::Base
1922
ActiveRecord::Schema.define(:version => 1) do
2023
create_table :import_articles do |t|
2124
t.string :title
22-
t.string :views # For the sake of invalid data sent to Elasticsearch
25+
t.integer :views
26+
t.string :numeric # For the sake of invalid data sent to Elasticsearch
2327
t.datetime :created_at, :default => 'NOW()'
2428
end
2529
end
@@ -28,7 +32,7 @@ class ::ImportArticle < ActiveRecord::Base
2832
ImportArticle.__elasticsearch__.create_index! force: true
2933
ImportArticle.__elasticsearch__.client.cluster.health wait_for_status: 'yellow'
3034

31-
100.times { |i| ImportArticle.create! title: "Test #{i}" }
35+
100.times { |i| ImportArticle.create! title: "Test #{i}", views: i }
3236
end
3337

3438
should "import all the documents" do
@@ -49,8 +53,17 @@ class ::ImportArticle < ActiveRecord::Base
4953
assert_equal 100, ImportArticle.search('*').results.total
5054
end
5155

56+
should "import only documents from a specific scope" do
57+
assert_equal 100, ImportArticle.count
58+
59+
assert_equal 0, ImportArticle.import(scope: 'popular')
60+
61+
ImportArticle.__elasticsearch__.refresh_index!
62+
assert_equal 50, ImportArticle.search('*').results.total
63+
end
64+
5265
should "report and not store/index invalid documents" do
53-
ImportArticle.create! title: "Test INVALID", views: "INVALID"
66+
ImportArticle.create! title: "Test INVALID", numeric: "INVALID"
5467

5568
assert_equal 101, ImportArticle.count
5669

0 commit comments

Comments
 (0)