/
resultset_spec.rb
190 lines (152 loc) · 7.23 KB
/
resultset_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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# encoding: utf-8
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
describe XapianDb::Resultset do
before :each do
# Setup a blueprint
XapianDb::DocumentBlueprint.setup(:IndexedObject) do |blueprint|
blueprint.attribute :name
end
@name = "Gernot"
# Mock a Xapian search result (Xapian::Enquire)
@enquiry = double(Xapian::Enquire)
@mset = double(Xapian::MSet)
# Create 3 matches and docs to play with
@matches = []
3.times do |i|
match = double(Xapian::Match)
doc = Xapian::Document.new
doc.add_value(0, IndexedObject.name)
doc.add_value(XapianDb::DocumentBlueprint.value_number_for(:name), "#{@name}#{i}")
allow(match).to receive(:document).and_return(doc)
allow(match).to receive(:percent).and_return(90)
@matches << match
end
allow(@mset).to receive(:matches_estimated).and_return(@matches.size)
allow(@mset).to receive(:matches).and_return(@matches)
allow(@enquiry).to receive(:mset).and_return(@mset)
end
it "behaves like an array" do
resultset = XapianDb::Resultset.new(nil, {})
%w(size [] each).each do |method|
expect(resultset).to respond_to(method)
end
end
it "is compatible with kaminari pagination" do
resultset = XapianDb::Resultset.new(nil, {})
%w(total_count num_pages limit_value current_page).each do |method|
expect(resultset).to respond_to(method)
end
end
it "is compatible with will_paginate pagination" do
resultset = XapianDb::Resultset.new(nil, {})
expect(resultset).to respond_to(:total_entries)
end
describe ".initialize(enquiry, options)" do
it "creates a valid, empty result set if we pass nil for the enquiry" do
resultset = XapianDb::Resultset.new(nil, {})
expect(resultset.hits).to eq(0)
expect(resultset.size).to eq(0)
expect(resultset.current_page).to eq(0)
expect(resultset.total_pages).to eq(0)
expect(resultset.limit_value).to eq(0)
end
it "raises an exception if an unsupported option is passed" do
expect{XapianDb::Resultset.new(@enquiry, :unsupported => "?")}.to raise_error 'unsupported options for resultset: {:unsupported=>"?"}'
end
it "accepts a limit option (as a string or an integer)" do
allow(@mset).to receive(:matches).and_return(@matches[0..1])
resultset = XapianDb::Resultset.new(@enquiry, :db_size => @matches.size, :limit => "2")
expect(resultset.hits).to eq(3)
expect(resultset.size).to eq(2)
expect(resultset.current_page).to eq(1)
expect(resultset.total_pages).to eq(2)
expect(resultset.limit_value).to eq(2)
expect(resultset.total_count).to eq(@matches.size)
expect(resultset.total_entries).to eq(@matches.size)
end
it "accepts an offset option (as a string or an integer)" do
allow(@mset).to receive(:matches).and_return(@matches[1..2])
resultset = XapianDb::Resultset.new(@enquiry, :db_size => @matches.size, :limit => "2", offset: "1")
expect(resultset.hits).to eq(3)
expect(resultset.size).to eq(2)
expect(resultset.current_page).to eq(1.5)
expect(resultset.total_pages).to eq(2)
expect(resultset.total_count).to eq(@matches.size)
expect(resultset.total_entries).to eq(@matches.size)
end
it "accepts a per_page option (as a string or an integer)" do
allow(@mset).to receive(:matches).and_return(@matches[0..1])
resultset = XapianDb::Resultset.new(@enquiry, :db_size => @matches.size, :per_page => "2")
expect(resultset.hits).to eq(3)
expect(resultset.size).to eq(2)
expect(resultset.current_page).to eq(1)
expect(resultset.total_pages).to eq(2)
end
it "accepts a page number (as a string or an integer)" do
allow(@mset).to receive(:matches).and_return(@matches[2..2])
resultset = XapianDb::Resultset.new(@enquiry, :db_size => @matches.size, :per_page => "2", :page => "2")
expect(resultset.hits).to eq(3)
expect(resultset.size).to eq(1)
expect(resultset.current_page).to eq(2)
expect(resultset.total_pages).to eq(2)
end
it "accepts nil as a page number" do
allow(@mset).to receive(:matches).and_return(@matches[0..1])
resultset = XapianDb::Resultset.new(@enquiry, :db_size => @matches.size, :per_page => 2, :page => nil)
expect(resultset.hits).to eq(3)
expect(resultset.size).to eq(2)
expect(resultset.current_page).to eq(1)
expect(resultset.total_pages).to eq(2)
end
it "returns an empty result set if page is requested that does not exist" do
expect(XapianDb::Resultset.new(@enquiry, db_size: @matches.size, per_page: 2, :page => 3).count).to eq(0)
end
it "raises an exception if elements of both page / per_page and offset / limit are given" do
expected_error = "Impossible combination of parameters. Either pass page and/or per_page, or limit and/or offset."
expect{XapianDb::Resultset.new(@enquiry, db_size: @matches.size, per_page: 2, page: 2, offset: 1, limit: 2)}.to raise_error ArgumentError, expected_error
end
it "should populate itself with found xapian documents" do
resultset = XapianDb::Resultset.new(@enquiry, :db_size => @matches.size, :per_page => 2, :page => 2)
expect(resultset.first).to be_a_kind_of(Xapian::Document)
end
it "should decorate the Xapian documents with attribute accessors" do
resultset = XapianDb::Resultset.new(@enquiry, :db_size => @matches.size, :per_page => 2, :page => 2)
doc = resultset.first
expect(doc.respond_to?(:name)).to be_truthy
expect(doc.name).to eq("#{@name}0")
end
it "should add the score of a match to the document" do
resultset = XapianDb::Resultset.new(@enquiry, :db_size => @matches.size, :per_page => 2, :page => 2)
doc = resultset.first
expect(doc.score).to eq(90)
end
it "can handle a page option as a string" do
allow(@mset).to receive(:matches).and_return(@matches[2..2])
resultset = XapianDb::Resultset.new(@enquiry, :db_size => @matches.size, :per_page => 2, :page => "2")
expect(resultset.hits).to eq(3)
expect(resultset.size).to eq(1)
expect(resultset.current_page).to eq(2)
expect(resultset.total_pages).to eq(2)
end
end
describe ".previous_page" do
it "should return nil if we are at page 1" do
resultset = XapianDb::Resultset.new(@enquiry, :db_size => @matches.size, :per_page => 2, :page => 1)
expect(resultset.previous_page).not_to be
end
it "should return 1 if we are at page 2" do
resultset = XapianDb::Resultset.new(@enquiry, :db_size => @matches.size, :per_page => 2, :page => 2)
expect(resultset.previous_page).to eq(1)
end
end
describe ".next_page" do
it "should return 2 if we are at page 1" do
resultset = XapianDb::Resultset.new(@enquiry, :db_size => @matches.size, :per_page => 2, :page => 1)
expect(resultset.next_page).to eq(2)
end
it "should return nil if we are on the last page" do
resultset = XapianDb::Resultset.new(@enquiry, :db_size => @matches.size, :per_page => 2, :page => 2)
expect(resultset.next_page).not_to be
end
end
end