forked from elastic/elasticsearch-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearching_search_request_test.rb
78 lines (63 loc) · 2.13 KB
/
searching_search_request_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
require 'test_helper'
class Elasticsearch::Model::SearchRequestTest < Test::Unit::TestCase
context "SearchRequest class" do
class ::DummySearchingModel
extend Elasticsearch::Model::Searching::ClassMethods
def self.index_name; 'foo'; end
def self.document_type; 'bar'; end
end
setup do
@client = mock('client')
DummySearchingModel.stubs(:client).returns(@client)
end
should "pass the search definition as a simple query" do
@client.expects(:search).with do |params|
assert_equal 'foo', params[:q]
true
end
.returns({})
s = Elasticsearch::Model::Searching::SearchRequest.new ::DummySearchingModel, 'foo'
s.execute!
end
should "pass the search definition as a Hash" do
@client.expects(:search).with do |params|
assert_equal( {foo: 'bar'}, params[:body] )
true
end
.returns({})
s = Elasticsearch::Model::Searching::SearchRequest.new ::DummySearchingModel, foo: 'bar'
s.execute!
end
should "pass the search definition as a JSON string" do
@client.expects(:search).with do |params|
assert_equal( '{"foo":"bar"}', params[:body] )
true
end
.returns({})
s = Elasticsearch::Model::Searching::SearchRequest.new ::DummySearchingModel, '{"foo":"bar"}'
s.execute!
end
should "pass the search definition as an object which responds to to_hash" do
class MySpecialQueryBuilder
def to_hash; {foo: 'bar'}; end
end
@client.expects(:search).with do |params|
assert_equal( {foo: 'bar'}, params[:body] )
true
end
.returns({})
s = Elasticsearch::Model::Searching::SearchRequest.new ::DummySearchingModel, MySpecialQueryBuilder.new
s.execute!
end
should "pass the options to the client" do
@client.expects(:search).with do |params|
assert_equal 'foo', params[:q]
assert_equal 15, params[:size]
true
end
.returns({})
s = Elasticsearch::Model::Searching::SearchRequest.new ::DummySearchingModel, 'foo', size: 15
s.execute!
end
end
end