forked from pat/thinking-sphinx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
facet_search_spec.rb
176 lines (146 loc) · 5.11 KB
/
facet_search_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
require 'spec_helper'
describe ThinkingSphinx::FacetSearch do
describe 'populate' do
it "should make separate Sphinx queries for each facet" do
ThinkingSphinx.should_receive(:search).with(
hash_including(:group_by => 'city_facet')
).and_return([])
ThinkingSphinx.should_receive(:search).with(
hash_including(:group_by => 'state_facet')
).and_return([])
ThinkingSphinx.should_receive(:search).with(
hash_including(:group_by => 'birthday')
).and_return([])
ThinkingSphinx::FacetSearch.new(:classes => [Person])
end
it "should request all shared facets in a multi-model request by default" do
ThinkingSphinx.stub!(:search => [])
ThinkingSphinx::FacetSearch.new.facet_names.should == ['class_crc']
end
it "should request all facets in a multi-model request if specified" do
ThinkingSphinx.stub!(:search => [])
ThinkingSphinx::FacetSearch.new(
:all_facets => true
).facet_names.should == [
'class_crc', 'city_facet', 'state_facet', 'birthday'
]
end
describe ':facets option' do
it "should limit facets to the requested set" do
ThinkingSphinx.should_receive(:search).once.and_return([])
ThinkingSphinx::FacetSearch.new(
:classes => [Person], :facets => :state
)
end
end
describe "empty result set for attributes" do
before :each do
ThinkingSphinx.stub!(:search => [])
@facets = ThinkingSphinx::FacetSearch.new(
:classes => [Person], :facets => :state
)
end
it "should add key as attribute" do
@facets.should have_key(:state)
end
it "should return an empty hash for the facet results" do
@facets[:state].should be_empty
end
end
describe "non-empty result set" do
before :each do
@person = Person.find(:first)
@people = [@person]
@people.stub!(:each_with_groupby_and_count).
and_yield(@person, @person.city.to_crc32, 1)
ThinkingSphinx.stub!(:search => @people)
@facets = ThinkingSphinx::FacetSearch.new(
:classes => [Person], :facets => :city
)
end
it "should return a hash" do
@facets.should be_a_kind_of(Hash)
end
it "should add key as attribute" do
@facets.keys.should include(:city)
end
it "should return a hash" do
@facets[:city].should == {@person.city => 1}
end
end
before :each do
@config = ThinkingSphinx::Configuration.instance
@config.configuration.searchd.max_matches = 10_000
end
it "should use the system-set max_matches for limit on facet calls" do
ThinkingSphinx.should_receive(:search) do |options|
options[:max_matches].should == 10_000
options[:limit].should == 10_000
[]
end
ThinkingSphinx::FacetSearch.new
end
it "should use the default max-matches if there is no explicit setting" do
@config.configuration.searchd.max_matches = nil
ThinkingSphinx.should_receive(:search) do |options|
options[:max_matches].should == 1000
options[:limit].should == 1000
[]
end
ThinkingSphinx::FacetSearch.new
end
it "should ignore user-provided max_matches and limit on facet calls" do
ThinkingSphinx.should_receive(:search) do |options|
options[:max_matches].should == 10_000
options[:limit].should == 10_000
[]
end
ThinkingSphinx::FacetSearch.new(
:max_matches => 500,
:limit => 200
)
end
it "should not use an explicit :page" do
ThinkingSphinx.should_receive(:search) do |options|
options[:page].should == 1
[]
end
ThinkingSphinx::FacetSearch.new(:page => 3)
end
describe "conflicting facets" do
before :each do
@index = ThinkingSphinx::Index::Builder.generate(Alpha) do
indexes :name
has :value, :as => :city, :facet => true
end
end
after :each do
Alpha.sphinx_facets.delete_at(-1)
end
it "should raise an error if searching with facets of same name but different type" do
lambda {
facets = ThinkingSphinx.facets :all_facets => true
}.should raise_error
end
end
end
describe "#for" do
before do
@person = Person.find(:first)
@people = [@person]
@people.stub!(:each_with_groupby_and_count).
and_yield(@person, @person.city.to_crc32, 1)
ThinkingSphinx.stub!(:search => @people)
@facets = ThinkingSphinx::FacetSearch.new(
:classes => [Person], :facets => :city
)
end
it "should return the search results for the attribute and key pair" do
ThinkingSphinx.should_receive(:search) do |options|
options[:with].should have_key('city_facet')
options[:with]['city_facet'].should == @person.city.to_crc32
end
@facets.for(:city => @person.city)
end
end
end