Last active
February 11, 2021 16:57
-
-
Save Mifrill/de75ecc68b6c50e013f34884a84f2140 to your computer and use it in GitHub Desktop.
elasticsearch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Rspec settings | |
# | |
# | |
# Cluster | |
# Gemfile | |
# ElasticSearch test cluster | |
gem 'elasticsearch-extensions', git: 'git://github.com/elasticsearch/elasticsearch-ruby.git' | |
# .ENV | |
# | |
# TEST_CLUSTER_NAME=testing-cluster | |
# TEST_CLUSTER_COMMAND=~/elasticsearch-6.5.2/bin/elasticsearch | |
# TEST_CLUSTER_PORT=9350 | |
# TEST_CLUSTER_NODES=1 | |
require 'elasticsearch/extensions/test/cluster' | |
# Start an in-memory cluster for Elasticsearch as needed | |
config.before :all do | |
unless Elasticsearch::Extensions::Test::Cluster.running?(on: ENV['TEST_CLUSTER_PORT']) | |
Elasticsearch::Extensions::Test::Cluster.start | |
end | |
end | |
# Stop elasticsearch cluster after test run | |
config.after :suite do | |
if Elasticsearch::Extensions::Test::Cluster.running?(on: ENV['TEST_CLUSTER_PORT']) | |
Elasticsearch::Extensions::Test::Cluster.stop | |
end | |
end | |
config.before :each do | |
ActiveRecord::Base.descendants.each do |model| | |
next unless model.respond_to?(:__elasticsearch__) | |
begin | |
model.__elasticsearch__.create_index! | |
model.__elasticsearch__.refresh_index! | |
rescue StandardError => e | |
STDERR.puts "#{model.name}: #{e.inspect}" | |
end | |
end | |
end | |
# Delete indexes for all elastic searchable models to ensure clean state between tests | |
config.after :each do | |
ActiveRecord::Base.descendants.each do |model| | |
next unless model.respond_to?(:__elasticsearch__) | |
begin | |
model.__elasticsearch__.delete_index! | |
rescue StandardError => e | |
STDERR.puts "#{model.name}: #{e.inspect}" | |
end | |
end | |
end |
Author
Mifrill
commented
Feb 11, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment