/
category_indexed_spec.rb
172 lines (152 loc) · 5.03 KB
/
category_indexed_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
require 'spec_helper'
describe Picky::Category do
before(:each) do
@index = Picky::Index.new :some_index do
source []
end
@partial_strategy = stub :partial, :each_partial => nil, :use_exact_for_partial? => false
@weight_strategy = stub :weights, :saved? => true
@similarity_strategy = stub :similarity, :encode => nil, :prioritize => nil
@exact = stub :exact, :dump => nil
@partial = stub :partial, :dump => nil
@category = described_class.new :some_name, @index, :partial => @partial_strategy,
:weights => @weight_strategy,
:similarity => @similarity_strategy,
:qualifiers => [:q, :qualifier]
@category.stub! :exclaim
end
describe 'partial' do
context 'default' do
before(:each) do
@category = described_class.new :some_name, @index
end
context 'with a partial strategy that uses the exact index' do
before(:each) do
@partial_strategy.stub! :use_exact_for_partial? => true
end
it 'returns the partial index' do
@category.partial.should be_kind_of(Picky::Bundle)
end
end
context 'with a partial strategy that uses the partial index (default)' do
it 'returns the partial index' do
@category.partial.should be_kind_of(Picky::Bundle)
end
end
end
context 'indexed_bundle_class defined differently' do
before(:each) do
index = Picky::Index.new(:some_index_name) do
source []
end
@category = described_class.new :some_name, index
end
context 'with a partial strategy that uses the exact index' do
before(:each) do
@partial_strategy.stub! :use_exact_for_partial? => true
end
it 'returns the partial index' do
@category.partial.should be_kind_of(Picky::Bundle)
end
end
context 'with a partial strategy that uses the partial index (default)' do
it 'returns the partial index' do
@category.partial.should be_kind_of(Picky::Bundle)
end
end
end
end
describe 'weight' do
before(:each) do
@token = stub :token, :text => :some_text
end
context 'partial bundle' do
before(:each) do
@category.stub! :bundle_for => @partial
end
it 'should receive weight with the token text' do
@partial.should_receive(:weight).once.with :some_text
@category.weight @token
end
end
context 'exact bundle' do
before(:each) do
@category.stub! :bundle_for => @exact
end
it 'should receive weight with the token text' do
@exact.should_receive(:weight).once.with :some_text
@category.weight @token
end
end
end
describe 'ids' do
before(:each) do
@token = stub :token, :text => :some_text
end
context 'partial bundle' do
before(:each) do
@category.stub! :bundle_for => @partial
end
it 'should receive ids with the token text' do
@partial.should_receive(:ids).once.with :some_text
@category.ids @token
end
end
context 'exact bundle' do
before(:each) do
@category.stub! :bundle_for => @exact
end
it 'should receive ids with the token text' do
@exact.should_receive(:ids).once.with :some_text
@category.ids @token
end
end
end
describe 'combination_for' do
context 'no weight for token' do
before(:each) do
@category.stub! :weight => nil
end
it 'should return nil' do
@category.combination_for(:anything).should == nil
end
end
context 'weight for token' do
before(:each) do
@token = stub :token, :text => :some_text
@category.stub! :weight => :some_weight, :bundle_for => :bundle
end
it 'should return a new combination' do
@category.combination_for(@token).should be_kind_of(Picky::Query::Combination)
end
it 'should create the combination correctly' do
Picky::Query::Combination.should_receive(:new).once.with @token, @category
@category.combination_for @token
end
end
end
context 'stubbed exact/partial' do
before(:each) do
@category.stub! :exact => (@exact = stub :exact)
@category.stub! :partial => (@partial = stub :partial)
end
describe 'bundle_for' do
it 'should return the right bundle' do
token = stub :token, :partial? => false
@category.bundle_for(token).should == @exact
end
it 'should return the right bundle' do
token = stub :token, :partial? => true
@category.bundle_for(token).should == @partial
end
end
describe 'load' do
it 'should call two methods' do
@category.should_receive(:clear_realtime).once
@exact.should_receive(:load).once
@partial.should_receive(:load).once
@category.load
end
end
end
end