|
| 1 | +require 'test_helper' |
| 2 | + |
| 3 | +module Elasticsearch |
| 4 | + module Model |
| 5 | + class ActiveRecordImportIntegrationTest < Elasticsearch::Test::IntegrationTestCase |
| 6 | + |
| 7 | + class ::ImportArticle < ActiveRecord::Base |
| 8 | + include Elasticsearch::Model |
| 9 | + |
| 10 | + mapping do |
| 11 | + indexes :title, type: 'string' |
| 12 | + indexes :views, type: 'integer' |
| 13 | + indexes :created_at, type: 'date' |
| 14 | + end |
| 15 | + end |
| 16 | + |
| 17 | + context "ActiveRecord importing" do |
| 18 | + setup do |
| 19 | + ActiveRecord::Schema.define(:version => 1) do |
| 20 | + create_table :import_articles do |t| |
| 21 | + t.string :title |
| 22 | + t.string :views # For the sake of invalid data sent to Elasticsearch |
| 23 | + t.datetime :created_at, :default => 'NOW()' |
| 24 | + end |
| 25 | + end |
| 26 | + |
| 27 | + ImportArticle.delete_all |
| 28 | + ImportArticle.__elasticsearch__.create_index! force: true |
| 29 | + |
| 30 | + 100.times { |i| ImportArticle.create! title: "Test #{i}" } |
| 31 | + end |
| 32 | + |
| 33 | + should "import all the documents" do |
| 34 | + assert_equal 100, ImportArticle.count |
| 35 | + |
| 36 | + ImportArticle.__elasticsearch__.refresh_index! |
| 37 | + assert_equal 0, ImportArticle.search('*').results.total |
| 38 | + |
| 39 | + batches = 0 |
| 40 | + errors = ImportArticle.import(batch_size: 10) do |response| |
| 41 | + batches += 1 |
| 42 | + end |
| 43 | + |
| 44 | + assert_equal 0, errors |
| 45 | + assert_equal 10, batches |
| 46 | + |
| 47 | + ImportArticle.__elasticsearch__.refresh_index! |
| 48 | + assert_equal 100, ImportArticle.search('*').results.total |
| 49 | + end |
| 50 | + |
| 51 | + should "report and not store/index invalid documents" do |
| 52 | + ImportArticle.create! title: "Test INVALID", views: "INVALID" |
| 53 | + |
| 54 | + assert_equal 101, ImportArticle.count |
| 55 | + |
| 56 | + ImportArticle.__elasticsearch__.refresh_index! |
| 57 | + assert_equal 0, ImportArticle.search('*').results.total |
| 58 | + |
| 59 | + batches = 0 |
| 60 | + errors = ImportArticle.__elasticsearch__.import(batch_size: 10) do |response| |
| 61 | + batches += 1 |
| 62 | + end |
| 63 | + |
| 64 | + assert_equal 1, errors |
| 65 | + assert_equal 11, batches |
| 66 | + |
| 67 | + ImportArticle.__elasticsearch__.refresh_index! |
| 68 | + assert_equal 100, ImportArticle.search('*').results.total |
| 69 | + end |
| 70 | + end |
| 71 | + |
| 72 | + end |
| 73 | + end |
| 74 | +end |
0 commit comments