module Elastictastic::TestHelpers

Constants

ALPHANUM

Public Class Methods

match_es_path(path) click to toggle source
# File lib/elastictastic/test_helpers.rb, line 173
def self.match_es_path(path)
  /^#{Regexp.escape(Elastictastic.config.hosts.first)}#{Regexp.escape(path)}(\?.*)?$/
end
match_es_resource(*components) click to toggle source
# File lib/elastictastic/test_helpers.rb, line 177
def self.match_es_resource(*components)
  match_es_path("/#{components.flatten.join('/')}")
end

Public Instance Methods

generate_es_hit(type, options = {}) click to toggle source
# File lib/elastictastic/test_helpers.rb, line 217
def generate_es_hit(type, options = {})
  {
    '_id' => options[:id] || generate_es_id,
    '_type' => type,
    '_index' => options[:index] || Elastictastic.config.default_index,
    '_version' => options[:version] || 1,
    '_source' => options.key?(:source) ? options[:source] : {}
  }
end
generate_es_id() click to toggle source
# File lib/elastictastic/test_helpers.rb, line 211
def generate_es_id
  ''.tap do |id|
    22.times { id << ALPHANUM[rand(ALPHANUM.length)] }
  end
end
last_request() click to toggle source
# File lib/elastictastic/test_helpers.rb, line 199
def last_request
  FakeWeb.last_request
end
last_request_json() click to toggle source
# File lib/elastictastic/test_helpers.rb, line 203
def last_request_json
  Elastictastic.json_decode(last_request.body)
end
last_request_uri() click to toggle source
# File lib/elastictastic/test_helpers.rb, line 207
def last_request_uri
  URI.parse(last_request.path)
end
match_es_path(path) click to toggle source
# File lib/elastictastic/test_helpers.rb, line 181
def match_es_path(path)
  TestHelpers.match_es_path(path)
end
match_es_resource(*components) click to toggle source
# File lib/elastictastic/test_helpers.rb, line 185
def match_es_resource(*components)
  TestHelpers.match_es_resource(*components)
end
stub_es_bulk(*responses) click to toggle source
# File lib/elastictastic/test_helpers.rb, line 98
def stub_es_bulk(*responses)
  stub_request_json(
    :post,
    match_es_path('/_bulk'),
    'took' => 1, 'items' => responses
  )
end
stub_es_create(index, type, id = nil) click to toggle source
# File lib/elastictastic/test_helpers.rb, line 11
def stub_es_create(index, type, id = nil)
  if id.nil?
    id = generate_es_id
    components = [index, type]
    method = :post
  else
    components = [index, type, id, '_create']
    method = :put
  end

  stub_request_json(
    method,
    match_es_resource(components),
    generate_es_hit(type, :id => id, :index => index).merge('ok' => 'true')
  )
  id
end
stub_es_destroy(index, type, id, options = {}) click to toggle source
# File lib/elastictastic/test_helpers.rb, line 80
def stub_es_destroy(index, type, id, options = {})
  stub_request_json(
    :delete,
    match_es_resource(index, type, id),
    generate_es_hit(
      type, :index => index, :id => id
    ).merge('ok' => true, 'found' => true).merge(options)
  )
end
stub_es_destroy_all(index, type) click to toggle source
# File lib/elastictastic/test_helpers.rb, line 90
def stub_es_destroy_all(index, type)
  stub_request_json(
    :delete,
    match_es_resource(index, type),
    'ok' => true
  )
end
stub_es_get(index, type, id, doc = {}, version = 1) click to toggle source
# File lib/elastictastic/test_helpers.rb, line 46
def stub_es_get(index, type, id, doc = {}, version = 1)
  stub_request_json(
    :get,
    match_es_resource(index, type, id),
    generate_es_hit(
      type,
      :index => index,
      :id => id,
      :version => version,
      :source => doc
    ).merge('exists' => !doc.nil?)
  )
end
stub_es_head(index, type, id, exists) click to toggle source
# File lib/elastictastic/test_helpers.rb, line 37
def stub_es_head(index, type, id, exists)
  stub_request(
    :head,
    match_es_resource(index, type, id),
    :status => (exists ? 200 : 404),
    :body => nil
  )
end
stub_es_mget(index, type, *ids) click to toggle source
# File lib/elastictastic/test_helpers.rb, line 60
def stub_es_mget(index, type, *ids)
  given_ids_with_docs = ids.extract_options!
  ids_with_docs = {}
  ids.each { |id| ids_with_docs[id] = {} }
  ids_with_docs.merge!(given_ids_with_docs)
  path = index ? "/#{index}/#{type}/_mget" : "/_mget"
  docs = ids_with_docs.each_pair.map do |id, doc|
    id, type, index = *id if Array === id
    generate_es_hit(
      type, :index => index, :id => id, :source => doc
    ).merge('exists' => !!doc)
  end

  stub_request_json(
    :post,
    match_es_path(path),
    'docs' => docs
  )
end
stub_es_msearch(*hits_collections) click to toggle source
# File lib/elastictastic/test_helpers.rb, line 130
def stub_es_msearch(*hits_collections)
  responses = hits_collections.map do |collection|
    { 'hits' => { 'hits' => collection, 'total' => collection.length }}
  end
  stub_request_json(
    :post,
    match_es_path('/_msearch'),
    'responses' => responses
  )
end
stub_es_msearch_count(*counts) click to toggle source
# File lib/elastictastic/test_helpers.rb, line 141
def stub_es_msearch_count(*counts)
  responses = counts.map do |count|
    { 'hits' => { 'hits' => [], 'total' => count }}
  end
  stub_request_json(
    :post,
    match_es_path('/_msearch'),
    'responses' => responses
  )
end
stub_es_put_mapping(index, type) click to toggle source
# File lib/elastictastic/test_helpers.rb, line 106
def stub_es_put_mapping(index, type)
  stub_request_json(
    :put,
    match_es_resource(index, type, '_mapping'),
    'ok' => true, 'acknowledged' => true
  )
end
stub_es_scan(index, type, batch_size, *hits) click to toggle source
# File lib/elastictastic/test_helpers.rb, line 152
def stub_es_scan(index, type, batch_size, *hits)
  scroll_ids = Array.new(batch_size + 1) { rand(10**100).to_s(36) }
  stub_request_json(
    :post,
    match_es_resource(index, type, '_search'),
    '_scroll_id' => scroll_ids.first,
    'hits' => { 'total' => hits.length, 'hits' => [] }
  )

  batches = hits.each_slice(batch_size).each_with_index.map do |hit_batch, i|
    {
      :body => Elastictastic.json_encode(
        '_scroll_id' => scroll_ids[i+1], 'hits' => { 'hits' => hit_batch }
      )
    }
  end
  batches << { :body => Elastictastic.json_encode('hits' => { 'hits' => [] }) }
  stub_request(:post, match_es_path('/_search/scroll'), batches)
  scroll_ids
end
stub_es_update(index, type, id, version = 2) click to toggle source
# File lib/elastictastic/test_helpers.rb, line 29
def stub_es_update(index, type, id, version = 2)
  stub_request_json(
    :put,
    match_es_resource(index, type, id),
    generate_es_hit(type, :index => index, :id => id, :version => version)
  )
end
stub_request(method, url, options = {}) click to toggle source
# File lib/elastictastic/test_helpers.rb, line 195
def stub_request(method, url, options = {})
  FakeWeb.register_uri(method, url, options)
end
stub_request_json(method, uri, *responses) click to toggle source
# File lib/elastictastic/test_helpers.rb, line 189
def stub_request_json(method, uri, *responses)
  json_responses = responses.map { |response| { :body => Elastictastic.json_encode(response) }}
  json_responses = json_responses.first if json_responses.length == 1
  stub_request(method, uri, json_responses)
end