Skip to content

Commit 0116b2e

Browse files
Kevin Kirschekarmi
authored andcommitted
[API] Added question mark versions for predicate methods
To follow Ruby standards when returning a boolean value (eg. `Array#empty?`), added aliases for corresponding methods. Reference: * https://github.com/styleguide/ruby * http://pragmati.st/2012/03/24/the-elements-of-ruby-style-predicate-methods/ * http://raganwald.com/2013/09/12/the-predicate-module-pattern.html Closes: #134 Related: #133
1 parent a87f551 commit 0116b2e

File tree

8 files changed

+295
-0
lines changed

8 files changed

+295
-0
lines changed

elasticsearch-api/lib/elasticsearch/api/actions/exists.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ def exists(arguments={})
4848
raise e
4949
end
5050
end
51+
52+
alias_method :exists?, :exists
5153
end
5254
end
5355
end

elasticsearch-api/lib/elasticsearch/api/actions/indices/exists.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ def exists(arguments={})
5050
raise e
5151
end
5252
end
53+
54+
alias_method :exists?, :exists
5355
end
5456
end
5557
end

elasticsearch-api/lib/elasticsearch/api/actions/indices/exists_alias.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ def exists_alias(arguments={})
4848
raise e
4949
end
5050
end
51+
52+
alias_method :exists_alias?, :exists_alias
5153
end
5254
end
5355
end

elasticsearch-api/lib/elasticsearch/api/actions/indices/exists_type.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ def exists_type(arguments={})
4747
raise e
4848
end
4949
end
50+
51+
alias_method :exists_type?, :exists_type
5052
end
5153
end
5254
end
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
require 'test_helper'
2+
3+
module Elasticsearch
4+
module Test
5+
class ExistsQuestionTest < ::Test::Unit::TestCase
6+
7+
context "Exists document" do
8+
subject { FakeClient.new }
9+
10+
should "require the :index argument" do
11+
assert_raise ArgumentError do
12+
subject.exists? :type => 'bar', :id => '1'
13+
end
14+
end
15+
16+
should "NOT require the :type argument" do
17+
assert_nothing_raised do
18+
subject.exists? :index => 'foo', :id => '1'
19+
end
20+
end
21+
22+
should "require the :id argument" do
23+
assert_raise ArgumentError do
24+
subject.exists? :index => 'foo', :type => 'bar'
25+
end
26+
end
27+
28+
should "perform correct request" do
29+
subject.expects(:perform_request).with do |method, url, params, body|
30+
assert_equal 'HEAD', method
31+
assert_equal 'foo/bar/1', url
32+
assert_equal Hash.new, params
33+
assert_nil body
34+
true
35+
end.returns(FakeResponse.new)
36+
37+
subject.exists? :index => 'foo', :type => 'bar', :id => '1'
38+
end
39+
40+
should "pass the URL parameters" do
41+
subject.expects(:perform_request).with do |method, url, params, body|
42+
assert_equal 'foo/bar/1', url
43+
assert_equal 'abc123', params[:routing]
44+
true
45+
end.returns(FakeResponse.new)
46+
47+
subject.exists? :index => 'foo', :type => 'bar', :id => '1', :routing => 'abc123'
48+
end
49+
50+
should "URL-escape the parts" do
51+
subject.expects(:perform_request).with do |method, url, params, body|
52+
assert_equal 'foo/bar%2Fbam/1', url
53+
true
54+
end.returns(FakeResponse.new)
55+
56+
subject.exists? :index => 'foo', :type => 'bar/bam', :id => '1'
57+
end
58+
59+
should "return true for successful response" do
60+
subject.expects(:perform_request).returns(FakeResponse.new 200, 'OK')
61+
assert_equal true, subject.exists?(:index => 'foo', :type => 'bar', :id => '1')
62+
end
63+
64+
should "return false for 404 response" do
65+
subject.expects(:perform_request).returns(FakeResponse.new 404, 'Not Found')
66+
assert_equal false, subject.exists?(:index => 'foo', :type => 'bar', :id => '1')
67+
end
68+
69+
should "return false on 'not found' exceptions" do
70+
subject.expects(:perform_request).raises(StandardError.new '404 NotFound')
71+
assert_equal false, subject.exists?(:index => 'foo', :type => 'bar', :id => '1')
72+
end
73+
74+
should "re-raise generic exceptions" do
75+
subject.expects(:perform_request).raises(StandardError)
76+
assert_raise(StandardError) do
77+
assert_equal false, subject.exists?(:index => 'foo', :type => 'bar', :id => '1')
78+
end
79+
end
80+
81+
end
82+
83+
end
84+
end
85+
end
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
require 'test_helper'
2+
3+
module Elasticsearch
4+
module Test
5+
class IndicesexistsQuestionTest < ::Test::Unit::TestCase
6+
7+
context "Indices: exists?" do
8+
subject { FakeClient.new }
9+
10+
should "perform correct request" do
11+
subject.expects(:perform_request).with do |method, url, params, body|
12+
assert_equal 'HEAD', method
13+
assert_equal 'foo', url
14+
assert_equal Hash.new, params
15+
assert_nil body
16+
true
17+
end.returns(FakeResponse.new)
18+
19+
subject.indices.exists?(:index => 'foo')
20+
end
21+
22+
should "perform the request against multiple indices" do
23+
subject.expects(:perform_request).with do |method, url, params, body|
24+
assert_equal 'foo,bar', url
25+
true
26+
end.returns(FakeResponse.new)
27+
28+
subject.indices.exists?(:index => ['foo', 'bar'])
29+
end
30+
31+
should "URL-escape the parts" do
32+
subject.expects(:perform_request).with do |method, url, params, body|
33+
assert_equal 'foo%5Ebar,bar%2Fbam', url
34+
true
35+
end.returns(FakeResponse.new)
36+
37+
subject.indices.exists? :index => 'foo^bar,bar/bam'
38+
end
39+
40+
should "return true for successful response" do
41+
subject.expects(:perform_request).returns(FakeResponse.new 200, 'OK')
42+
assert_equal true, subject.indices.exists?(:index => 'foo')
43+
end
44+
45+
should "return false for 404 response" do
46+
subject.expects(:perform_request).returns(FakeResponse.new 404, 'Not Found')
47+
assert_equal false, subject.indices.exists?(:index => 'none')
48+
end
49+
50+
should "return false on 'not found' exceptions" do
51+
subject.expects(:perform_request).raises(StandardError.new '404 NotFound')
52+
assert_equal false, subject.indices.exists?(:index => 'none')
53+
end
54+
55+
should "re-raise generic exceptions" do
56+
subject.expects(:perform_request).raises(StandardError)
57+
assert_raise(StandardError) do
58+
assert_equal false, subject.indices.exists?(:index => 'none')
59+
end
60+
end
61+
62+
end
63+
64+
end
65+
end
66+
end
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
require 'test_helper'
2+
3+
module Elasticsearch
4+
module Test
5+
class IndicesExistsAliasQuestionTest < ::Test::Unit::TestCase
6+
7+
context "Indices: Exists alias" do
8+
subject { FakeClient.new }
9+
10+
should "perform correct request" do
11+
subject.expects(:perform_request).with do |method, url, params, body|
12+
assert_equal 'HEAD', method
13+
assert_equal '_alias/foo', url
14+
assert_equal Hash.new, params
15+
assert_nil body
16+
true
17+
end.returns(FakeResponse.new)
18+
19+
subject.indices.exists_alias? :name => 'foo'
20+
end
21+
22+
should "perform request against multiple indices" do
23+
subject.expects(:perform_request).with do |method, url, params, body|
24+
assert_equal 'foo,bar/_alias/bam', url
25+
true
26+
end.returns(FakeResponse.new)
27+
28+
subject.indices.exists_alias? :index => ['foo','bar'], :name => 'bam'
29+
end
30+
31+
should "URL-escape the parts" do
32+
subject.expects(:perform_request).with do |method, url, params, body|
33+
assert_equal 'foo%5Ebar/_alias/bar%2Fbam', url
34+
true
35+
end.returns(FakeResponse.new)
36+
37+
subject.indices.exists_alias? :index => 'foo^bar', :name => 'bar/bam'
38+
end
39+
40+
should "return true for successful response" do
41+
subject.expects(:perform_request).returns(FakeResponse.new 200, 'OK')
42+
assert_equal true, subject.indices.exists_alias?(:name => 'foo')
43+
end
44+
45+
should "return false for 404 response" do
46+
subject.expects(:perform_request).returns(FakeResponse.new 404, 'Not Found')
47+
assert_equal false, subject.indices.exists_alias?(:name => 'none')
48+
end
49+
50+
should "return false on 'not found' exceptions" do
51+
subject.expects(:perform_request).raises(StandardError.new '404 NotFound')
52+
assert_nothing_raised do
53+
assert_equal false, subject.indices.exists_alias?(:name => 'none')
54+
end
55+
end
56+
57+
should "re-raise generic exceptions" do
58+
subject.expects(:perform_request).raises(StandardError)
59+
assert_raise(StandardError) do
60+
assert_equal false, subject.indices.exists_alias?(:name => 'none')
61+
end
62+
end
63+
64+
end
65+
66+
end
67+
end
68+
end
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
require 'test_helper'
2+
3+
module Elasticsearch
4+
module Test
5+
class IndicesExistsQuestionTypeTest < ::Test::Unit::TestCase
6+
7+
context "Indices: Exists type" do
8+
subject { FakeClient.new }
9+
10+
should "perform correct request" do
11+
subject.expects(:perform_request).with do |method, url, params, body|
12+
assert_equal 'HEAD', method
13+
assert_equal 'foo/bar', url
14+
assert_equal Hash.new, params
15+
assert_nil body
16+
true
17+
end.returns(FakeResponse.new)
18+
19+
subject.indices.exists_type? :index => 'foo', :type => 'bar'
20+
end
21+
22+
should "perform request against multiple indices" do
23+
subject.expects(:perform_request).with do |method, url, params, body|
24+
assert_equal 'foo,bar/bam', url
25+
true
26+
end.returns(FakeResponse.new)
27+
28+
subject.indices.exists_type? :index => ['foo','bar'], :type => 'bam'
29+
end
30+
31+
should "URL-escape the parts" do
32+
subject.expects(:perform_request).with do |method, url, params, body|
33+
assert_equal 'foo%5Ebar/bar%2Fbam', url
34+
true
35+
end.returns(FakeResponse.new)
36+
37+
subject.indices.exists_type? :index => 'foo^bar', :type => 'bar/bam'
38+
end
39+
40+
should "return true for successful response" do
41+
subject.expects(:perform_request).returns(FakeResponse.new 200, 'OK')
42+
assert_equal true, subject.indices.exists_type?(:index => 'foo', :type => 'bar')
43+
end
44+
45+
should "return false for 404 response" do
46+
subject.expects(:perform_request).returns(FakeResponse.new 404, 'Not Found')
47+
assert_equal false, subject.indices.exists_type?(:index => 'foo', :type => 'none')
48+
end
49+
50+
should "return false on 'not found' exceptions" do
51+
subject.expects(:perform_request).raises(StandardError.new '404 NotFound')
52+
assert_nothing_raised do
53+
assert_equal false, subject.indices.exists_type?(:index => 'foo', :type => 'none')
54+
end
55+
end
56+
57+
should "re-raise generic exceptions" do
58+
subject.expects(:perform_request).raises(StandardError)
59+
assert_raise(StandardError) do
60+
assert_equal false, subject.indices.exists_type?(:index => 'foo', :type => 'none')
61+
end
62+
end
63+
64+
end
65+
66+
end
67+
end
68+
end

0 commit comments

Comments
 (0)