elasticsearch-api 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (145) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +13 -0
  5. data/README.md +160 -0
  6. data/Rakefile +62 -0
  7. data/elasticsearch-api.gemspec +51 -0
  8. data/lib/elasticsearch-api +1 -0
  9. data/lib/elasticsearch/api.rb +23 -0
  10. data/lib/elasticsearch/api/actions/bulk.rb +71 -0
  11. data/lib/elasticsearch/api/actions/cluster/get_settings.rb +21 -0
  12. data/lib/elasticsearch/api/actions/cluster/health.rb +50 -0
  13. data/lib/elasticsearch/api/actions/cluster/node_hot_threads.rb +46 -0
  14. data/lib/elasticsearch/api/actions/cluster/node_info.rb +59 -0
  15. data/lib/elasticsearch/api/actions/cluster/node_shutdown.rb +36 -0
  16. data/lib/elasticsearch/api/actions/cluster/node_stats.rb +77 -0
  17. data/lib/elasticsearch/api/actions/cluster/put_settings.rb +28 -0
  18. data/lib/elasticsearch/api/actions/cluster/reroute.rb +44 -0
  19. data/lib/elasticsearch/api/actions/cluster/state.rb +47 -0
  20. data/lib/elasticsearch/api/actions/count.rb +46 -0
  21. data/lib/elasticsearch/api/actions/create.rb +34 -0
  22. data/lib/elasticsearch/api/actions/delete.rb +61 -0
  23. data/lib/elasticsearch/api/actions/delete_by_query.rb +62 -0
  24. data/lib/elasticsearch/api/actions/exists.rb +51 -0
  25. data/lib/elasticsearch/api/actions/explain.rb +71 -0
  26. data/lib/elasticsearch/api/actions/get.rb +59 -0
  27. data/lib/elasticsearch/api/actions/get_source.rb +59 -0
  28. data/lib/elasticsearch/api/actions/index.rb +81 -0
  29. data/lib/elasticsearch/api/actions/indices/analyze.rb +63 -0
  30. data/lib/elasticsearch/api/actions/indices/clear_cache.rb +69 -0
  31. data/lib/elasticsearch/api/actions/indices/close.rb +35 -0
  32. data/lib/elasticsearch/api/actions/indices/create.rb +83 -0
  33. data/lib/elasticsearch/api/actions/indices/delete.rb +48 -0
  34. data/lib/elasticsearch/api/actions/indices/delete_alias.rb +37 -0
  35. data/lib/elasticsearch/api/actions/indices/delete_mapping.rb +26 -0
  36. data/lib/elasticsearch/api/actions/indices/delete_template.rb +33 -0
  37. data/lib/elasticsearch/api/actions/indices/delete_warmer.rb +32 -0
  38. data/lib/elasticsearch/api/actions/indices/exists.rb +35 -0
  39. data/lib/elasticsearch/api/actions/indices/exists_alias.rb +41 -0
  40. data/lib/elasticsearch/api/actions/indices/exists_type.rb +39 -0
  41. data/lib/elasticsearch/api/actions/indices/flush.rb +40 -0
  42. data/lib/elasticsearch/api/actions/indices/get_alias.rb +41 -0
  43. data/lib/elasticsearch/api/actions/indices/get_aliases.rb +32 -0
  44. data/lib/elasticsearch/api/actions/indices/get_mapping.rb +36 -0
  45. data/lib/elasticsearch/api/actions/indices/get_settings.rb +32 -0
  46. data/lib/elasticsearch/api/actions/indices/get_template.rb +30 -0
  47. data/lib/elasticsearch/api/actions/indices/get_warmer.rb +44 -0
  48. data/lib/elasticsearch/api/actions/indices/open.rb +33 -0
  49. data/lib/elasticsearch/api/actions/indices/optimize.rb +57 -0
  50. data/lib/elasticsearch/api/actions/indices/put_alias.rb +43 -0
  51. data/lib/elasticsearch/api/actions/indices/put_mapping.rb +49 -0
  52. data/lib/elasticsearch/api/actions/indices/put_settings.rb +45 -0
  53. data/lib/elasticsearch/api/actions/indices/put_template.rb +40 -0
  54. data/lib/elasticsearch/api/actions/indices/put_warmer.rb +48 -0
  55. data/lib/elasticsearch/api/actions/indices/refresh.rb +43 -0
  56. data/lib/elasticsearch/api/actions/indices/segments.rb +33 -0
  57. data/lib/elasticsearch/api/actions/indices/snapshot_index.rb +32 -0
  58. data/lib/elasticsearch/api/actions/indices/stats.rb +96 -0
  59. data/lib/elasticsearch/api/actions/indices/status.rb +46 -0
  60. data/lib/elasticsearch/api/actions/indices/update_aliases.rb +49 -0
  61. data/lib/elasticsearch/api/actions/indices/validate_query.rb +68 -0
  62. data/lib/elasticsearch/api/actions/info.rb +19 -0
  63. data/lib/elasticsearch/api/actions/mget.rb +64 -0
  64. data/lib/elasticsearch/api/actions/mlt.rb +86 -0
  65. data/lib/elasticsearch/api/actions/msearch.rb +75 -0
  66. data/lib/elasticsearch/api/actions/percolate.rb +57 -0
  67. data/lib/elasticsearch/api/actions/ping.rb +29 -0
  68. data/lib/elasticsearch/api/actions/scroll.rb +44 -0
  69. data/lib/elasticsearch/api/actions/search.rb +145 -0
  70. data/lib/elasticsearch/api/actions/suggest.rb +46 -0
  71. data/lib/elasticsearch/api/actions/update.rb +103 -0
  72. data/lib/elasticsearch/api/namespace/cluster.rb +20 -0
  73. data/lib/elasticsearch/api/namespace/common.rb +27 -0
  74. data/lib/elasticsearch/api/namespace/indices.rb +20 -0
  75. data/lib/elasticsearch/api/utils.rb +97 -0
  76. data/lib/elasticsearch/api/version.rb +5 -0
  77. data/test/integration/yaml_test_runner.rb +330 -0
  78. data/test/test_helper.rb +52 -0
  79. data/test/unit/bulk_test.rb +85 -0
  80. data/test/unit/client_test.rb +31 -0
  81. data/test/unit/cluster/get_settings_test.rb +26 -0
  82. data/test/unit/cluster/health_test.rb +38 -0
  83. data/test/unit/cluster/node_hot_threads_test.rb +35 -0
  84. data/test/unit/cluster/node_info_test.rb +45 -0
  85. data/test/unit/cluster/node_shutdown_test.rb +45 -0
  86. data/test/unit/cluster/node_stats_test.rb +65 -0
  87. data/test/unit/cluster/put_settings_test.rb +26 -0
  88. data/test/unit/cluster/reroute_test.rb +38 -0
  89. data/test/unit/cluster/state_test.rb +37 -0
  90. data/test/unit/count_test.rb +46 -0
  91. data/test/unit/create_document_test.rb +38 -0
  92. data/test/unit/delete_by_query_test.rb +42 -0
  93. data/test/unit/delete_document_test.rb +62 -0
  94. data/test/unit/exists_document_test.rb +76 -0
  95. data/test/unit/explain_document_test.rb +64 -0
  96. data/test/unit/get_document_source_test.rb +62 -0
  97. data/test/unit/get_document_test.rb +62 -0
  98. data/test/unit/hashie_test.rb +78 -0
  99. data/test/unit/index_document_test.rb +77 -0
  100. data/test/unit/indices/analyze_test.rb +67 -0
  101. data/test/unit/indices/clear_cache_test.rb +45 -0
  102. data/test/unit/indices/close_test.rb +42 -0
  103. data/test/unit/indices/create_test.rb +42 -0
  104. data/test/unit/indices/delete_alias_test.rb +38 -0
  105. data/test/unit/indices/delete_mapping_test.rb +47 -0
  106. data/test/unit/indices/delete_template_test.rb +26 -0
  107. data/test/unit/indices/delete_test.rb +45 -0
  108. data/test/unit/indices/delete_warmer_test.rb +59 -0
  109. data/test/unit/indices/exists_alias_test.rb +65 -0
  110. data/test/unit/indices/exists_test.rb +57 -0
  111. data/test/unit/indices/exists_type_test.rb +59 -0
  112. data/test/unit/indices/flush_test.rb +45 -0
  113. data/test/unit/indices/get_alias_test.rb +41 -0
  114. data/test/unit/indices/get_aliases_test.rb +35 -0
  115. data/test/unit/indices/get_mapping_test.rb +53 -0
  116. data/test/unit/indices/get_settings_test.rb +35 -0
  117. data/test/unit/indices/get_template_test.rb +32 -0
  118. data/test/unit/indices/get_warmer_test.rb +41 -0
  119. data/test/unit/indices/open_test.rb +42 -0
  120. data/test/unit/indices/optimize_test.rb +45 -0
  121. data/test/unit/indices/put_alias_test.rb +47 -0
  122. data/test/unit/indices/put_mapping_test.rb +57 -0
  123. data/test/unit/indices/put_settings_test.rb +50 -0
  124. data/test/unit/indices/put_template_test.rb +48 -0
  125. data/test/unit/indices/put_warmer_test.rb +62 -0
  126. data/test/unit/indices/refresh_test.rb +55 -0
  127. data/test/unit/indices/segments_test.rb +55 -0
  128. data/test/unit/indices/snapshot_index_test.rb +55 -0
  129. data/test/unit/indices/stats_test.rb +76 -0
  130. data/test/unit/indices/status_test.rb +55 -0
  131. data/test/unit/indices/update_aliases_test.rb +42 -0
  132. data/test/unit/indices/validate_query_test.rb +75 -0
  133. data/test/unit/info_test.rb +26 -0
  134. data/test/unit/json_builders_test.rb +64 -0
  135. data/test/unit/mget_test.rb +70 -0
  136. data/test/unit/mlt_test.rb +80 -0
  137. data/test/unit/msearch_test.rb +120 -0
  138. data/test/unit/percolate_test.rb +49 -0
  139. data/test/unit/ping_test.rb +48 -0
  140. data/test/unit/scroll_test.rb +26 -0
  141. data/test/unit/search_test.rb +93 -0
  142. data/test/unit/suggest_test.rb +55 -0
  143. data/test/unit/update_document_test.rb +62 -0
  144. data/test/unit/utils_test.rb +118 -0
  145. metadata +498 -0
@@ -0,0 +1,5 @@
1
+ module Elasticsearch
2
+ module API
3
+ VERSION = "0.0.2"
4
+ end
5
+ end
@@ -0,0 +1,330 @@
1
+ require 'pathname'
2
+ require 'active_support/inflector'
3
+ require 'yaml'
4
+ require 'pry'
5
+
6
+ require 'elasticsearch'
7
+ require 'elasticsearch/transport/extensions/test_cluster'
8
+
9
+ # Launch test cluster
10
+ #
11
+ Elasticsearch::TestCluster.start if ENV['SERVER']
12
+
13
+ # Register `at_exit` handler for server shutdown.
14
+ # MUST be called before requiring `test/unit`.
15
+ #
16
+ at_exit { Elasticsearch::TestCluster.stop if ENV['SERVER'] }
17
+
18
+ require 'logger'
19
+ require 'ansi'
20
+
21
+ logger = Logger.new(STDERR)
22
+ logger.formatter = proc do |severity, datetime, progname, msg|
23
+ color = case severity
24
+ when /INFO/ then :green
25
+ when /ERROR|WARN|FATAL/ then :red
26
+ when /DEBUG/ then :cyan
27
+ else :white
28
+ end
29
+ ANSI.ansi(severity[0] + ' ', color, :faint) + ANSI.ansi(msg, :white, :faint) + "\n"
30
+ end
31
+
32
+ $client = Elasticsearch::Client.new host: 'localhost:9250', logger: (ENV['QUIET'] ? nil : logger)
33
+ $es_version = $client.info['version']['number']
34
+
35
+ puts '-'*80, "Elasticsearch #{ANSI.ansi($es_version, :bold)}", '-'*80
36
+
37
+ require 'test_helper'
38
+ require 'test/unit'
39
+ require 'shoulda/context'
40
+
41
+ # Monkeypatch shoulda to remove "should" from test name
42
+ #
43
+ module Shoulda
44
+ module Context
45
+ class Context
46
+ def create_test_from_should_hash(should)
47
+ test_name = ["test:", full_name, "--", "#{should[:name]}. "].flatten.join(' ').to_sym
48
+
49
+ if test_methods[test_unit_class][test_name.to_s] then
50
+ raise DuplicateTestError, "'#{test_name}' is defined more than once."
51
+ end
52
+
53
+ test_methods[test_unit_class][test_name.to_s] = true
54
+
55
+ context = self
56
+ test_unit_class.send(:define_method, test_name) do
57
+ @shoulda_context = context
58
+ begin
59
+ context.run_parent_setup_blocks(self)
60
+ should[:before].bind(self).call if should[:before]
61
+ context.run_current_setup_blocks(self)
62
+ should[:block].bind(self).call
63
+ ensure
64
+ context.run_all_teardown_blocks(self)
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
71
+
72
+ module Elasticsearch
73
+ module YamlTestSuite
74
+ $results = {}
75
+ $stash = {}
76
+
77
+ module Utils
78
+ def titleize(word)
79
+ word.to_s.gsub(/[^\w]+/, ' ').gsub(/\b('?[a-z])/) { $1.capitalize }
80
+ end
81
+
82
+ def symbolize_keys(object)
83
+ if object.is_a? Hash
84
+ object.reduce({}) { |memo,(k,v)| memo[k.to_sym] = symbolize_keys(v); memo }
85
+ else
86
+ object
87
+ end
88
+ end
89
+
90
+ extend self
91
+ end
92
+
93
+ module Runner
94
+ def perform_api_call(test, api, arguments=nil)
95
+ namespace = api.split('.')
96
+
97
+ replacer = lambda do |value|
98
+ case value
99
+ when Array
100
+ value.map { |v| replacer.call(v) }
101
+ when Hash
102
+ Hash[ value.map { |v| replacer.call(v) } ]
103
+ else
104
+ fetch_or_return value
105
+ end
106
+ end
107
+
108
+ timefixer = lambda do |value|
109
+ if value.is_a?(Time)
110
+ value.iso8601
111
+ else
112
+ value
113
+ end
114
+ end
115
+
116
+ arguments = Hash[
117
+ arguments.map do |key, value|
118
+ replacement = replacer.call(value)
119
+ replacement = timefixer.call(replacement)
120
+ [key, replacement]
121
+ end
122
+ ]
123
+
124
+ STDERR.puts "ARGUMENTS: #{arguments.inspect}" if ENV['DEBUG']
125
+
126
+ $results[test.hash] = namespace.reduce($client) do |memo, current|
127
+ unless current == namespace.last
128
+ memo = memo.send(current)
129
+ else
130
+ arguments ? memo = memo.send(current, arguments) : memo = memo.send(current)
131
+ end
132
+ memo
133
+ end
134
+ end
135
+
136
+ def evaluate(test, property)
137
+ property.gsub(/\\\./, '_____').split('.').reduce($results[test.hash]) do |memo, attr|
138
+ if memo
139
+ attr = attr.gsub(/_____/, '.') if attr
140
+ memo = memo.is_a?(Hash) ? memo[attr] : memo[attr.to_i]
141
+ end
142
+ memo
143
+ end
144
+ end
145
+
146
+ def in_context(name, &block)
147
+ klass = Class.new(YamlTestCase)
148
+ Object::const_set "%sTest" % name.split(/\s/).map { |d| d.capitalize }.join('').gsub(/[^\w]+/, ''), klass
149
+ klass.context name, &block
150
+ end
151
+
152
+ def fetch_or_return(var)
153
+ if var.is_a?(String) && var =~ /^\$(.+)/
154
+ $stash[var]
155
+ else
156
+ var
157
+ end
158
+ end
159
+
160
+ def set(var, val)
161
+ $stash["$#{var}"] = val
162
+ end
163
+
164
+ def skip?(actions)
165
+ skip = actions.select { |a| a['skip'] }.first
166
+ if skip
167
+ min, max = skip['skip']['version'].split('-').map(&:strip)
168
+ if min <= $es_version && max >= $es_version
169
+ return skip['skip']['reason'] ? skip['skip']['reason'] : true
170
+ end
171
+ end
172
+ return false
173
+ end
174
+
175
+ extend self
176
+ end
177
+
178
+ class YamlTestCase < ::Test::Unit::TestCase; end
179
+ end
180
+ end
181
+
182
+ include Elasticsearch::YamlTestSuite
183
+
184
+ PATH = Pathname(ENV['SPEC'] || File.expand_path('../../../spec/test', __FILE__))
185
+ suites = Dir.glob(PATH.join('*')).map { |d| Pathname(d) }
186
+ suites = suites.select { |s| s.to_s =~ Regexp.new(ENV['FILTER']) } if ENV['FILTER']
187
+
188
+ suites.each do |suite|
189
+ name = Elasticsearch::YamlTestSuite::Utils.titleize(suite.basename)
190
+
191
+ Elasticsearch::YamlTestSuite::Runner.in_context name do
192
+
193
+ # --- Register context setup -------------------------------------------
194
+ #
195
+ setup do
196
+ $client.indices.delete index: '_all'
197
+ $results = {}
198
+ $stash = {}
199
+ end
200
+
201
+ # --- Register context teardown ----------------------------------------
202
+ #
203
+ teardown do
204
+ $client.indices.delete index: '_all'
205
+ end
206
+
207
+ files = Dir[suite.join('*.{yml,yaml}')]
208
+ files.each do |file|
209
+
210
+ tests = YAML.load_documents File.new(file)
211
+
212
+ # Extract setup actions
213
+ setup_actions = tests.select { |t| t['setup'] }.first['setup'] rescue []
214
+
215
+ # Remove setup actions from tests
216
+ tests = tests.reject { |t| t['setup'] }
217
+
218
+ # Add setup actions to each individual test
219
+ tests.each { |t| t[t.keys.first] << { 'setup' => setup_actions } }
220
+
221
+ tests.each do |test|
222
+ context '' do
223
+ test_name = test.keys.first
224
+ actions = test.values.first
225
+
226
+ if reason = Runner.skip?(actions)
227
+ STDOUT.puts "#{ANSI.ansi('SKIP', :yellow)}: [#{name}] #{test_name} (Reason: #{reason})"
228
+ next
229
+ end
230
+
231
+ # --- Register test setup -------------------------------------------
232
+ setup do
233
+ actions.select { |a| a['setup'] }.first['setup'].each do |action|
234
+ api, arguments = action['do'].to_a.first
235
+ arguments = Utils.symbolize_keys(arguments)
236
+ Runner.perform_api_call((test.to_s + '___setup'), api, arguments)
237
+ end
238
+ end
239
+
240
+ # --- Register test method ------------------------------------------
241
+ should test_name do
242
+ actions.each do |action|
243
+ STDERR.puts "ACTION: #{action.inspect}" if ENV['DEBUG']
244
+
245
+ case
246
+
247
+ # --- Perform action ------------------------------------------
248
+ #
249
+ when action['do']
250
+ catch_exception = action['do'].delete('catch') if action['do']
251
+ api, arguments = action['do'].to_a.first
252
+ arguments = Utils.symbolize_keys(arguments)
253
+
254
+ begin
255
+ $results[test.hash] = Runner.perform_api_call(test, api, arguments)
256
+ rescue Exception => e
257
+ if catch_exception
258
+ STDERR.puts "CATCH '#{catch_exception}': #{e.inspect}" if ENV['DEBUG']
259
+ case e
260
+ when 'missing'
261
+ assert_match /\[404\]/, e.message
262
+ when 'conflict'
263
+ assert_match /\[409\]/, e.message
264
+ when 'request'
265
+ assert_match /\[500\]/, e.message
266
+ when 'param'
267
+ raise ArgumentError, "NOT IMPLEMENTED"
268
+ when /\/.+\//
269
+ assert_match Regexp.new(catch_exception.tr('/', '')), e.message
270
+ end
271
+ else
272
+ raise e
273
+ end
274
+ end
275
+
276
+ # --- Evaluate predicates -------------------------------------
277
+ #
278
+ when property = action['is_true']
279
+ result = Runner.evaluate(test, property)
280
+ STDERR.puts "CHECK: Expected '#{property}' to be true, is: #{result.inspect}" if ENV['DEBUG']
281
+ assert(result, "Property '#{property}' should be true, is: #{result.inspect}")
282
+
283
+ when property = action['is_false']
284
+ result = Runner.evaluate(test, property)
285
+ STDERR.puts "CHECK: Expected '#{property}' to be false, is: #{result.inspect}" if ENV['DEBUG']
286
+ assert( !!! result, "Property '#{property}' should be false, is: #{result.inspect}")
287
+
288
+ when a = action['match']
289
+ property, value = a.to_a.first
290
+
291
+ value = Runner.fetch_or_return(value)
292
+ result = Runner.evaluate(test, property)
293
+ STDERR.puts "CHECK: Expected '#{property}' to be '#{value}', is: #{result.inspect}" if ENV['DEBUG']
294
+ assert_equal(value, result)
295
+
296
+ when a = action['length']
297
+ property, value = a.to_a.first
298
+
299
+ result = Runner.evaluate(test, property)
300
+ length = result.size
301
+ STDERR.puts "CHECK: Expected '#{property}' to be #{value}, is: #{length.inspect}" if ENV['DEBUG']
302
+ assert_equal(value, length)
303
+
304
+ when a = action['lt'] || action['gt']
305
+ next
306
+ property, value = a.to_a.first
307
+ operator = action['lt'] ? '<' : '>'
308
+
309
+ result = Runner.evaluate(test, property)
310
+ message = "Expected '#{property}' to be #{operator} #{value}, is: #{result.inspect}"
311
+
312
+ STDERR.puts "CHECK: #{message}" if ENV['DEBUG']
313
+ # operator == 'less than' ? assert(value.to_f < result.to_f, message) : assert(value.to_f > result.to_f, message)
314
+ assert_operator result, operator.to_sym, value.to_i
315
+
316
+ when stash = action['set']
317
+ property, variable = stash.to_a.first
318
+ result = Runner.evaluate(test, property)
319
+ STDERR.puts "STASH: '$#{variable}' => #{result.inspect}" if ENV['DEBUG']
320
+ Runner.set variable, result
321
+ end
322
+ end
323
+ end
324
+ end
325
+ end
326
+ end
327
+
328
+ end
329
+
330
+ end
@@ -0,0 +1,52 @@
1
+ RUBY_1_8 = defined?(RUBY_VERSION) && RUBY_VERSION < '1.9'
2
+
3
+ if RUBY_1_8
4
+ require 'rubygems'
5
+ gem 'test-unit'
6
+ end
7
+
8
+ require 'simplecov' and SimpleCov.start { add_filter "/test|test_/" } if ENV["COVERAGE"]
9
+
10
+ require 'test/unit'
11
+ require 'shoulda-context'
12
+ require 'mocha/setup'
13
+
14
+ unless ENV["TM_FILEPATH"] || ENV["NOTURN"] || RUBY_1_8
15
+ require 'turn'
16
+
17
+ if ENV['QUIET']
18
+ Turn.config.format = :outline
19
+ Turn.config.trace = 1
20
+ end
21
+ end
22
+
23
+ require 'require-prof' if ENV["REQUIRE_PROF"]
24
+ require 'elasticsearch/api'
25
+ RequireProf.print_timing_infos if ENV["REQUIRE_PROF"]
26
+
27
+ module Elasticsearch
28
+ module Test
29
+ class FakeClient
30
+ include Elasticsearch::API
31
+
32
+ def perform_request(method, path, params, body)
33
+ puts "PERFORMING REQUEST:", "--> #{method.to_s.upcase} #{path} #{params} #{body}"
34
+ FakeResponse.new(200, 'FAKE', {})
35
+ end
36
+ end
37
+
38
+ FakeResponse = Struct.new(:status, :body, :headers) do
39
+ def status
40
+ values[0] || 200
41
+ end
42
+ def body
43
+ values[1] || '{}'
44
+ end
45
+ def headers
46
+ values[2] || {}
47
+ end
48
+ end
49
+
50
+ class NotFound < StandardError; end
51
+ end
52
+ end
@@ -0,0 +1,85 @@
1
+ require 'test_helper'
2
+
3
+ module Elasticsearch
4
+ module Test
5
+ class BulkTest < ::Test::Unit::TestCase
6
+
7
+ context "Bulk" do
8
+ subject { FakeClient.new }
9
+
10
+ should "post correct payload to the endpoint" do
11
+ subject.expects(:perform_request).with do |method, url, params, body|
12
+ assert_equal 'POST', method
13
+ assert_equal '_bulk', url
14
+ assert_equal Hash.new, params
15
+
16
+ if RUBY_1_8
17
+ lines = body.split("\n")
18
+
19
+ assert_equal 5, lines.size
20
+ assert_match /\{"index"\:\{/, lines[0]
21
+ assert_match /\{"title"\:"Test"/, lines[1]
22
+ assert_match /\{"update"\:\{/, lines[2]
23
+ assert_match /\{"doc"\:\{"title"/, lines[3]
24
+ else
25
+ assert_equal <<-PAYLOAD.gsub(/^\s+/, ''), body
26
+ {"index":{"_index":"myindexA","_type":"mytype","_id":"1"}}
27
+ {"title":"Test"}
28
+ {"update":{"_index":"myindexB","_type":"mytype","_id":"2"}}
29
+ {"doc":{"title":"Update"}}
30
+ {"delete":{"_index":"myindexC","_type":"mytypeC","_id":"3"}}
31
+ PAYLOAD
32
+ end
33
+ true
34
+ end.returns(FakeResponse.new)
35
+
36
+ subject.bulk :body => [
37
+ { :index => { :_index => 'myindexA', :_type => 'mytype', :_id => '1', :data => { :title => 'Test' } } },
38
+ { :update => { :_index => 'myindexB', :_type => 'mytype', :_id => '2', :data => { :doc => { :title => 'Update' } } } },
39
+ { :delete => { :_index => 'myindexC', :_type => 'mytypeC', :_id => '3' } }
40
+ ]
41
+ end
42
+
43
+ should "post payload to the correct endpoint" do
44
+ subject.expects(:perform_request).with do |method, url, params, body|
45
+ assert_equal 'POST', method
46
+ assert_equal 'myindex/_bulk', url
47
+ true
48
+ end.returns(FakeResponse.new)
49
+
50
+ subject.bulk :index => 'myindex', :body => []
51
+ end
52
+
53
+ should "post a string payload" do
54
+ subject.expects(:perform_request).with do |method, url, params, body|
55
+ assert_equal "foo\nbar", body
56
+ true
57
+ end.returns(FakeResponse.new)
58
+
59
+ subject.bulk :body => "foo\nbar"
60
+ end
61
+
62
+ should "post an array of strings payload" do
63
+ subject.expects(:perform_request).with do |method, url, params, body|
64
+ assert_equal "foo\nbar\n", body
65
+ true
66
+ end.returns(FakeResponse.new)
67
+
68
+ subject.bulk :body => ["foo", "bar"]
69
+ end
70
+
71
+ should "encode URL parameters" do
72
+ subject.expects(:perform_request).with do |method, url, params, body|
73
+ assert_equal '_bulk', url
74
+ assert_equal({:refresh => true}, params)
75
+ true
76
+ end.returns(FakeResponse.new)
77
+
78
+ subject.bulk :refresh => true, :body => []
79
+ end
80
+
81
+ end
82
+
83
+ end
84
+ end
85
+ end