/
hits_spec.rb
75 lines (58 loc) · 2.51 KB
/
hits_spec.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
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe ElasticSearch::Api::Hit do
let(:response) { {"_source" => {"foo" => "bar"}, "_id" => "1"} }
subject { described_class.new(response) }
its(:id) { should == response["_id"] }
it "should set hit attributes" do
subject.foo.should == response["_source"]["foo"]
end
end
describe ElasticSearch::Api::Hits do
context "unpaginated" do
let(:response) do
{
"_shards"=>{"total"=>30, "successful"=>30, "failed"=>0},
"hits"=>{"total"=>2, "max_score"=>1.0, "hits"=>[
{"_index"=>"test_index", "_type"=>"test_type", "_id"=>"1", "_score"=>1.0, "_source"=>{"foo" => "bar"}},
{"_index"=>"test_index", "_type"=>"test_type", "_id"=>"2", "_score"=>1.0, "_source"=>{"foo" => "baz"}}
]}}
end
subject { described_class.new(response) }
it { should respond_to(:response) }
its(:total_entries) { should == response["hits"]["hits"].size } # will_paginate
its(:total_count) { should == response["hits"]["hits"].size } # kaminari
it "should instantiate hits in order" do
response["hits"]["hits"].each_with_index do |hit, i|
subject.hits[i].should == ElasticSearch::Api::Hit.new(hit)
end
end
it { should respond_to(:to_a) }
it "should delegate array methods" do
subject.collect { |h| h.id }.should == response["hits"]["hits"].collect { |h| h["_id"] }
end
end
context "paginated" do
let(:response) do
{
"_shards"=>{"total"=>30, "successful"=>30, "failed"=>0},
"hits"=>{"total"=>6, "max_score"=>1.0, "hits"=>[
{"_index"=>"test_index", "_type"=>"test_type", "_id"=>"3", "_score"=>1.0, "_source"=>{"foo" => "bar"}},
{"_index"=>"test_index", "_type"=>"test_type", "_id"=>"4", "_score"=>1.0, "_source"=>{"foo" => "baz"}}
]}}
end
let(:per_page) { 2 }
let(:page) { 2 }
subject { described_class.new(response, {:page => page, :per_page => per_page}) }
# will_paginate
its(:total_pages) { should == (response["hits"]["total"] / per_page.to_f).ceil }
its(:next_page) { should == (page + 1) }
its(:previous_page) { should == (page - 1) }
its(:current_page) { should == page }
its(:per_page) { should == per_page }
its(:offset) { should == per_page * (page - 1) }
# kaminari
its(:limit_value) { should == per_page }
its(:num_pages) { should == (response["hits"]["total"] / per_page.to_f).ceil }
its(:offset_value) { should == per_page * (page - 1) }
end
end