Module: Elastictastic::TestHelpers

Defined in:
lib/elastictastic/test_helpers.rb

Constant Summary collapse

ALPHANUM =
('0'..'9').to_a + ('A'..'Z').to_a + ('a'..'z').to_a

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.match_es_path(path) ⇒ Object



173
174
175
# 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) ⇒ Object



177
178
179
# File 'lib/elastictastic/test_helpers.rb', line 177

def self.match_es_resource(*components)
  match_es_path("/#{components.flatten.join('/')}")
end

Instance Method Details

#generate_es_hit(type, options = {}) ⇒ Object



217
218
219
220
221
222
223
224
225
# 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_idObject



211
212
213
214
215
# 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_requestObject



199
200
201
# File 'lib/elastictastic/test_helpers.rb', line 199

def last_request
  FakeWeb.last_request
end

#last_request_jsonObject



203
204
205
# File 'lib/elastictastic/test_helpers.rb', line 203

def last_request_json
  Elastictastic.json_decode(last_request.body)
end

#last_request_uriObject



207
208
209
# File 'lib/elastictastic/test_helpers.rb', line 207

def last_request_uri
  URI.parse(last_request.path)
end

#match_es_path(path) ⇒ Object



181
182
183
# File 'lib/elastictastic/test_helpers.rb', line 181

def match_es_path(path)
  TestHelpers.match_es_path(path)
end

#match_es_resource(*components) ⇒ Object



185
186
187
# File 'lib/elastictastic/test_helpers.rb', line 185

def match_es_resource(*components)
  TestHelpers.match_es_resource(*components)
end

#stub_es_bulk(*responses) ⇒ Object



98
99
100
101
102
103
104
# 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) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 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 = {}) ⇒ Object



80
81
82
83
84
85
86
87
88
# 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) ⇒ Object



90
91
92
93
94
95
96
# 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) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# 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) ⇒ Object



37
38
39
40
41
42
43
44
# 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) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 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) ⇒ Object



130
131
132
133
134
135
136
137
138
139
# 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) ⇒ Object



141
142
143
144
145
146
147
148
149
150
# 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) ⇒ Object



106
107
108
109
110
111
112
# 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) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# 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_search(index, type, data) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/elastictastic/test_helpers.rb', line 114

def stub_es_search(index, type, data)
  if Array === data
    response = data.map do |datum|
      { :body => Elastictastic.json_encode(datum) }
    end
  else
    response = { :body =>  Elastictastic.json_encode(data) }
  end

  stub_request(
    :post,
    match_es_resource(index, type, '_search'),
    response
  )
end

#stub_es_update(index, type, id, version = 2) ⇒ Object



29
30
31
32
33
34
35
# 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 = {}) ⇒ Object



195
196
197
# 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) ⇒ Object



189
190
191
192
193
# 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