@@ -7,9 +7,12 @@ class ActiveRecordImportIntegrationTest < Elasticsearch::Test::IntegrationTestCa
7
7
class ::ImportArticle < ActiveRecord ::Base
8
8
include Elasticsearch ::Model
9
9
10
+ scope :popular , -> { where ( 'views >= 50' ) }
11
+
10
12
mapping do
11
13
indexes :title , type : 'string'
12
14
indexes :views , type : 'integer'
15
+ indexes :numeric , type : 'integer'
13
16
indexes :created_at , type : 'date'
14
17
end
15
18
end
@@ -19,7 +22,8 @@ class ::ImportArticle < ActiveRecord::Base
19
22
ActiveRecord ::Schema . define ( :version => 1 ) do
20
23
create_table :import_articles do |t |
21
24
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
23
27
t . datetime :created_at , :default => 'NOW()'
24
28
end
25
29
end
@@ -28,7 +32,7 @@ class ::ImportArticle < ActiveRecord::Base
28
32
ImportArticle . __elasticsearch__ . create_index! force : true
29
33
ImportArticle . __elasticsearch__ . client . cluster . health wait_for_status : 'yellow'
30
34
31
- 100 . times { |i | ImportArticle . create! title : "Test #{ i } " }
35
+ 100 . times { |i | ImportArticle . create! title : "Test #{ i } " , views : i }
32
36
end
33
37
34
38
should "import all the documents" do
@@ -49,8 +53,17 @@ class ::ImportArticle < ActiveRecord::Base
49
53
assert_equal 100 , ImportArticle . search ( '*' ) . results . total
50
54
end
51
55
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
+
52
65
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"
54
67
55
68
assert_equal 101 , ImportArticle . count
56
69
0 commit comments