2018-02-07 00:35:52 +00:00
|
|
|
require 'elasticsearch/model'
|
|
|
|
require 'hashie'
|
|
|
|
|
|
|
|
RSpec.configure do |config|
|
|
|
|
config.expect_with :rspec do |expect|
|
|
|
|
expect.syntax = :expect
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class MyModel < Hashie::Mash
|
|
|
|
include Elasticsearch::Model
|
|
|
|
|
|
|
|
disable_warnings
|
|
|
|
|
|
|
|
index_name 'model'
|
|
|
|
document_type 'model'
|
|
|
|
end
|
|
|
|
|
|
|
|
RSpec.describe 'elaasticsearch-model' do
|
2019-11-17 17:35:30 +00:00
|
|
|
# See https://github.com/hashie/hashie/issues/354#issuecomment-363306114
|
2018-02-07 00:35:52 +00:00
|
|
|
# for the reason why this doesn't work as you would expect
|
|
|
|
it 'raises an error when the model does has an id' do
|
|
|
|
object = MyModel.new
|
|
|
|
stub_elasticsearch_client
|
|
|
|
|
2019-10-02 14:48:19 +00:00
|
|
|
expect { object.__elasticsearch__.index_document }.to raise_error(NameError)
|
2018-02-07 00:35:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not raise an error when the model has an id' do
|
|
|
|
object = MyModel.new(id: 123)
|
|
|
|
stub_elasticsearch_client
|
|
|
|
|
|
|
|
expect { object.__elasticsearch__.index_document }.not_to raise_error
|
|
|
|
end
|
|
|
|
|
|
|
|
def stub_elasticsearch_client
|
|
|
|
response = double('Response', body: '{}')
|
2019-10-18 03:51:50 +00:00
|
|
|
allow_any_instance_of(Elasticsearch::Transport::Client).to\
|
|
|
|
receive(:perform_request) { response }
|
2018-02-07 00:35:52 +00:00
|
|
|
end
|
|
|
|
end
|