forked from pat/thinking-sphinx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_spec.rb
183 lines (142 loc) · 5.37 KB
/
index_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
require 'spec_helper'
describe ThinkingSphinx::Index do
describe "prefix_fields method" do
before :each do
@index = ThinkingSphinx::Index.new(Person)
@field_a = stub('field', :prefixes => true)
@field_b = stub('field', :prefixes => false)
@field_c = stub('field', :prefixes => true)
@index.stub!(:fields => [@field_a, @field_b, @field_c])
end
it "should return fields that are flagged as prefixed" do
@index.prefix_fields.should include(@field_a)
@index.prefix_fields.should include(@field_c)
end
it "should not return fields that aren't flagged as prefixed" do
@index.prefix_fields.should_not include(@field_b)
end
end
describe "infix_fields method" do
before :each do
@index = ThinkingSphinx::Index.new(Person)
@field_a = stub('field', :infixes => true)
@field_b = stub('field', :infixes => false)
@field_c = stub('field', :infixes => true)
@index.stub!(:fields => [@field_a, @field_b, @field_c])
end
it "should return fields that are flagged as infixed" do
@index.infix_fields.should include(@field_a)
@index.infix_fields.should include(@field_c)
end
it "should not return fields that aren't flagged as infixed" do
@index.infix_fields.should_not include(@field_b)
end
end
describe '.name_for' do
it "should return the model's name downcased" do
ThinkingSphinx::Index.name_for(Alpha).should == 'alpha'
end
it "should separate words by underscores" do
ThinkingSphinx::Index.name_for(ActiveRecord).should == 'active_record'
end
it "should separate namespaces by underscores" do
ThinkingSphinx::Index.name_for(ActiveRecord::Base).
should == 'active_record_base'
end
end
describe '#name' do
it "should return the downcased name of the index's model" do
ThinkingSphinx::Index.new(Alpha).name.should == 'alpha'
end
it "should return a custom name if one is set" do
index = ThinkingSphinx::Index.new(Alpha)
index.name = 'custom'
index.name.should == 'custom'
end
end
describe '#core_name' do
it "should take the index's name and append _core" do
ThinkingSphinx::Index.new(Alpha).core_name.should == 'alpha_core'
end
end
describe '#delta_name' do
it "should take the index's name and append _delta" do
ThinkingSphinx::Index.new(Alpha).delta_name.should == 'alpha_delta'
end
end
describe '#all_names' do
it "should return the core index name by default" do
ThinkingSphinx::Index.new(Alpha).all_names.should == ['alpha_core']
end
it "should return both core and delta names if deltas are enabled" do
index = ThinkingSphinx::Index.new(Alpha)
index.delta_object = stub('delta')
index.all_names.should == ['alpha_core', 'alpha_delta']
end
it "should respect custom names" do
index = ThinkingSphinx::Index.new(Alpha)
index.name = 'custom'
index.all_names.should == ['custom_core']
end
it "should respect custom names when deltas are enabled" do
index = ThinkingSphinx::Index.new(Alpha)
index.name = 'custom'
index.delta_object = stub('delta')
index.all_names.should == ['custom_core', 'custom_delta']
end
end
describe '#to_riddle' do
it "should return two Riddle indexes if deltas are disabled" do
index = ThinkingSphinx::Index.new(Alpha)
index.to_riddle(0).length.should == 2
end
it "should return three Riddle indexes if deltas are enabled" do
index = ThinkingSphinx::Index.new(Beta)
index.delta_object = stub('delta')
index.to_riddle(0).length.should == 3
end
it "should include a distributed index" do
index = ThinkingSphinx::Index.new(Alpha)
index.to_riddle(0).last.
should be_a(Riddle::Configuration::DistributedIndex)
end
context 'core index' do
it "should use the core name" do
@index = ThinkingSphinx::Index.new(Alpha).to_riddle(0).first
@index.name.should == 'alpha_core'
end
it "should not try to set disable_range on the index" do
ThinkingSphinx::Configuration.instance.
index_options[:disable_range] = true
lambda {
@index = ThinkingSphinx::Index.new(Alpha).to_riddle(0).first
}.should_not raise_error(NoMethodError)
end
end
context 'delta index' do
before :each do
index = ThinkingSphinx::Index.new(Beta)
index.delta_object = stub('delta')
@index = index.to_riddle(0)[1]
end
it "should use the delta name" do
@index.name.should == 'beta_delta'
end
end
context 'distributed index' do
it "should use the index's name" do
index = ThinkingSphinx::Index.new(Alpha)
index.to_riddle(0).last.name.should == 'alpha'
end
it "should add the core index" do
index = ThinkingSphinx::Index.new(Alpha)
index.to_riddle(0).last.local_indexes.should include('alpha_core')
end
it "should add the delta index if there is one" do
index = ThinkingSphinx::Index.new(Beta)
index.delta_object = stub('delta')
index.to_riddle(0).last.local_indexes.should include('beta_delta')
end
end
end
end