Skip to content

[MODEL] Use default scope on ActiveRecord model when importing #827

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 5, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 65 additions & 12 deletions elasticsearch-model/test/integration/active_record_import_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,22 @@ module Elasticsearch
module Model
class ActiveRecordImportIntegrationTest < Elasticsearch::Test::IntegrationTestCase

class ::ImportArticle < ActiveRecord::Base
include Elasticsearch::Model
context "ActiveRecord importing" do
setup do
Object.send(:remove_const, :ImportArticle) if defined?(ImportArticle)
class ::ImportArticle < ActiveRecord::Base
include Elasticsearch::Model

scope :popular, -> { where('views >= 50') }
scope :popular, -> { where('views >= 50') }

mapping do
indexes :title, type: 'text'
indexes :views, type: 'integer'
indexes :numeric, type: 'integer'
indexes :created_at, type: 'date'
end
end
mapping do
indexes :title, type: 'text'
indexes :views, type: 'integer'
indexes :numeric, type: 'integer'
indexes :created_at, type: 'date'
end
end

context "ActiveRecord importing" do
setup do
ActiveRecord::Schema.define(:version => 1) do
create_table :import_articles do |t|
t.string :title
Expand Down Expand Up @@ -110,6 +111,58 @@ class ::ImportArticle < ActiveRecord::Base
end
end

context "ActiveRecord importing when the model has a default scope" do

setup do
Object.send(:remove_const, :ImportArticle) if defined?(ImportArticle)
class ::ImportArticle < ActiveRecord::Base
include Elasticsearch::Model

default_scope { where('views >= 8') }

mapping do
indexes :title, type: 'text'
indexes :views, type: 'integer'
indexes :numeric, type: 'integer'
indexes :created_at, type: 'date'
end
end

ActiveRecord::Schema.define(:version => 1) do
create_table :import_articles do |t|
t.string :title
t.integer :views
t.string :numeric # For the sake of invalid data sent to Elasticsearch
t.datetime :created_at, :default => 'NOW()'
end
end

ImportArticle.delete_all
ImportArticle.__elasticsearch__.delete_index! force: true
ImportArticle.__elasticsearch__.create_index! force: true
ImportArticle.__elasticsearch__.client.cluster.health wait_for_status: 'yellow'

10.times { |i| ImportArticle.create! title: "Test #{i}", views: i }
end

should "import only documents from the default scope" do
assert_equal 2, ImportArticle.count

assert_equal 0, ImportArticle.import

ImportArticle.__elasticsearch__.refresh_index!
assert_equal 2, ImportArticle.search('*').results.total
end

should "import only documents from a specific query combined with the default scope" do
assert_equal 2, ImportArticle.count

assert_equal 0, ImportArticle.import(query: -> { where("title = 'Test 9'") })

ImportArticle.__elasticsearch__.refresh_index!
assert_equal 1, ImportArticle.search('*').results.total
end
end
end
end
end