freelancing-god-thinking-sphinx 1.1.12 → 1.1.14

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,23 +1,28 @@
1
1
  require 'spec/spec_helper'
2
2
 
3
3
  describe ThinkingSphinx::Field do
4
+ before :each do
5
+ @index = ThinkingSphinx::Index.new(Alpha)
6
+ @source = ThinkingSphinx::Source.new(@index)
7
+ end
8
+
4
9
  describe '#initialize' do
5
10
  it 'raises if no columns are provided so that configuration errors are easier to track down' do
6
11
  lambda {
7
- ThinkingSphinx::Field.new([])
12
+ ThinkingSphinx::Field.new(@source, [])
8
13
  }.should raise_error(RuntimeError)
9
14
  end
10
15
 
11
16
  it 'raises if an element of the columns param is an integer - as happens when you use id instead of :id - so that configuration errors are easier to track down' do
12
17
  lambda {
13
- ThinkingSphinx::Field.new([1234])
18
+ ThinkingSphinx::Field.new(@source, [1234])
14
19
  }.should raise_error(RuntimeError)
15
20
  end
16
21
  end
17
22
 
18
23
  describe "unique_name method" do
19
24
  before :each do
20
- @field = ThinkingSphinx::Field.new [
25
+ @field = ThinkingSphinx::Field.new @source, [
21
26
  Object.stub_instance(:__stack => [], :__name => "col_name")
22
27
  ]
23
28
  end
@@ -42,13 +47,15 @@ describe ThinkingSphinx::Field do
42
47
 
43
48
  describe "prefixes method" do
44
49
  it "should default to false" do
45
- @field = ThinkingSphinx::Field.new([Object.stub_instance(:__stack => [])])
50
+ @field = ThinkingSphinx::Field.new(
51
+ @source, [Object.stub_instance(:__stack => [])]
52
+ )
46
53
  @field.prefixes.should be_false
47
54
  end
48
55
 
49
56
  it "should be true if the corresponding option is set" do
50
57
  @field = ThinkingSphinx::Field.new(
51
- [Object.stub_instance(:__stack => [])], :prefixes => true
58
+ @source, [Object.stub_instance(:__stack => [])], :prefixes => true
52
59
  )
53
60
  @field.prefixes.should be_true
54
61
  end
@@ -56,13 +63,15 @@ describe ThinkingSphinx::Field do
56
63
 
57
64
  describe "infixes method" do
58
65
  it "should default to false" do
59
- @field = ThinkingSphinx::Field.new([Object.stub_instance(:__stack => [])])
66
+ @field = ThinkingSphinx::Field.new(
67
+ @source, [Object.stub_instance(:__stack => [])]
68
+ )
60
69
  @field.infixes.should be_false
61
70
  end
62
71
 
63
72
  it "should be true if the corresponding option is set" do
64
73
  @field = ThinkingSphinx::Field.new(
65
- [Object.stub_instance(:__stack => [])], :infixes => true
74
+ @source, [Object.stub_instance(:__stack => [])], :infixes => true
66
75
  )
67
76
  @field.infixes.should be_true
68
77
  end
@@ -70,7 +79,7 @@ describe ThinkingSphinx::Field do
70
79
 
71
80
  describe "column_with_prefix method" do
72
81
  before :each do
73
- @field = ThinkingSphinx::Field.new [
82
+ @field = ThinkingSphinx::Field.new @source, [
74
83
  ThinkingSphinx::Index::FauxColumn.new(:col_name)
75
84
  ]
76
85
  @field.columns.each { |col| @field.associations[col] = [] }
@@ -116,7 +125,7 @@ describe ThinkingSphinx::Field do
116
125
  @assoc_c = Object.stub_instance(:is_many? => true)
117
126
 
118
127
  @field = ThinkingSphinx::Field.new(
119
- [ThinkingSphinx::Index::FauxColumn.new(:col_name)]
128
+ @source, [ThinkingSphinx::Index::FauxColumn.new(:col_name)]
120
129
  )
121
130
  @field.associations = {
122
131
  :a => @assoc_a, :b => @assoc_b, :c => @assoc_c
@@ -1,5 +1,351 @@
1
1
  require 'spec/spec_helper'
2
2
 
3
3
  describe ThinkingSphinx::Index::Builder do
4
- #
4
+ describe ".generate without source scope" do
5
+ before :each do
6
+ @index = ThinkingSphinx::Index::Builder.generate(Person) do
7
+ indexes first_name, last_name
8
+ has birthday
9
+ has id, :as => :internal_id
10
+
11
+ set_property :sql_range_step => 1000
12
+
13
+ where "birthday <= NOW()"
14
+ group_by "first_name"
15
+ end
16
+
17
+ @source = @index.sources.first
18
+ end
19
+
20
+ it "should return an index" do
21
+ @index.should be_a_kind_of(ThinkingSphinx::Index)
22
+ end
23
+
24
+ it "should have one source for the index" do
25
+ @index.sources.length.should == 1
26
+ end
27
+
28
+ it "should have two fields" do
29
+ @source.fields.length.should == 2
30
+ @source.fields[0].unique_name.should == :first_name
31
+ @source.fields[1].unique_name.should == :last_name
32
+ end
33
+
34
+ it "should have two attributes alongside the four internal ones" do
35
+ @source.attributes.length.should == 6
36
+ @source.attributes[4].unique_name.should == :birthday
37
+ @source.attributes[5].unique_name.should == :internal_id
38
+ end
39
+
40
+ it "should have one condition" do
41
+ @source.conditions.length.should == 1
42
+ @source.conditions.first.should == "birthday <= NOW()"
43
+ end
44
+
45
+ it "should have one grouping" do
46
+ @source.groupings.length.should == 1
47
+ @source.groupings.first.should == "first_name"
48
+ end
49
+
50
+ it "should have one option" do
51
+ @source.options.length.should == 1
52
+ @source.options[:sql_range_step].should == 1000
53
+ end
54
+ end
55
+
56
+ describe "sortable field" do
57
+ before :each do
58
+ @index = ThinkingSphinx::Index::Builder.generate(Person) do
59
+ indexes first_name, :sortable => true
60
+ end
61
+
62
+ @source = @index.sources.first
63
+ end
64
+
65
+ it "should have one field" do
66
+ @source.fields.length.should == 1
67
+ end
68
+
69
+ it "should have one attribute alongside the four internal ones" do
70
+ @source.attributes.length.should == 5
71
+ end
72
+
73
+ it "should set the attribute name to have the _sort suffix" do
74
+ @source.attributes.last.unique_name.should == :first_name_sort
75
+ end
76
+
77
+ it "should set the attribute column to be the same as the field" do
78
+ @source.attributes.last.columns.length.should == 1
79
+ @source.attributes.last.columns.first.__name.should == :first_name
80
+ end
81
+ end
82
+
83
+ describe "faceted field" do
84
+ before :each do
85
+ @index = ThinkingSphinx::Index::Builder.generate(Person) do
86
+ indexes first_name, :facet => true
87
+ end
88
+
89
+ @source = @index.sources.first
90
+ end
91
+
92
+ after :each do
93
+ Person.sphinx_facets.delete_at(-1)
94
+ end
95
+
96
+ it "should have one field" do
97
+ @source.fields.length.should == 1
98
+ end
99
+
100
+ it "should have one attribute alongside the four internal ones" do
101
+ @source.attributes.length.should == 5
102
+ end
103
+
104
+ it "should set the attribute name to have the _facet suffix" do
105
+ @source.attributes.last.unique_name.should == :first_name_facet
106
+ end
107
+
108
+ it "should set the attribute type to integer" do
109
+ @source.attributes.last.type.should == :integer
110
+ end
111
+
112
+ it "should set the attribute column to be the same as the field" do
113
+ @source.attributes.last.columns.length.should == 1
114
+ @source.attributes.last.columns.first.__name.should == :first_name
115
+ end
116
+ end
117
+
118
+ describe "faceted integer attribute" do
119
+ before :each do
120
+ @index = ThinkingSphinx::Index::Builder.generate(Alpha) do
121
+ indexes :name
122
+ has value, :facet => true
123
+ end
124
+
125
+ @source = @index.sources.first
126
+ end
127
+
128
+ after :each do
129
+ Alpha.sphinx_facets.delete_at(-1)
130
+ end
131
+
132
+ it "should have just one attribute alongside the four internal ones" do
133
+ @source.attributes.length.should == 5
134
+ end
135
+ end
136
+
137
+ describe "faceted timestamp attribute" do
138
+ before :each do
139
+ @index = ThinkingSphinx::Index::Builder.generate(Person) do
140
+ indexes first_name
141
+ has birthday, :facet => true
142
+ end
143
+
144
+ @source = @index.sources.first
145
+ end
146
+
147
+ after :each do
148
+ Person.sphinx_facets.delete_at(-1)
149
+ end
150
+
151
+ it "should have just one attribute alongside the four internal ones" do
152
+ @source.attributes.length.should == 5
153
+ end
154
+ end
155
+
156
+ describe "faceted boolean attribute" do
157
+ before :each do
158
+ @index = ThinkingSphinx::Index::Builder.generate(Beta) do
159
+ indexes :name
160
+ has delta, :facet => true
161
+ end
162
+
163
+ @source = @index.sources.first
164
+ end
165
+
166
+ after :each do
167
+ Beta.sphinx_facets.delete_at(-1)
168
+ end
169
+
170
+ it "should have just one attribute alongside the four internal ones" do
171
+ @source.attributes.length.should == 5
172
+ end
173
+ end
174
+
175
+ describe "faceted float attribute" do
176
+ before :each do
177
+ @index = ThinkingSphinx::Index::Builder.generate(Alpha) do
178
+ indexes :name
179
+ has cost, :facet => true
180
+ end
181
+
182
+ @source = @index.sources.first
183
+ end
184
+
185
+ after :each do
186
+ Alpha.sphinx_facets.delete_at(-1)
187
+ end
188
+
189
+ it "should have just one attribute alongside the four internal ones" do
190
+ @source.attributes.length.should == 5
191
+ end
192
+ end
193
+
194
+ describe "faceted string attribute" do
195
+ before :each do
196
+ @index = ThinkingSphinx::Index::Builder.generate(Person) do
197
+ indexes first_name
198
+ has last_name, :facet => true
199
+ end
200
+
201
+ @source = @index.sources.first
202
+ end
203
+
204
+ after :each do
205
+ Person.sphinx_facets.delete_at(-1)
206
+ end
207
+
208
+ it "should have two attributes alongside the four internal ones" do
209
+ @source.attributes.length.should == 6
210
+ end
211
+
212
+ it "should set the facet attribute name to have the _facet suffix" do
213
+ @source.attributes.last.unique_name.should == :last_name_facet
214
+ end
215
+
216
+ it "should set the attribute type to integer" do
217
+ @source.attributes.last.type.should == :integer
218
+ end
219
+
220
+ it "should set the attribute column to be the same as the field" do
221
+ @source.attributes.last.columns.length.should == 1
222
+ @source.attributes.last.columns.first.__name.should == :last_name
223
+ end
224
+ end
225
+
226
+ describe "no fields" do
227
+ it "should raise an exception" do
228
+ lambda {
229
+ ThinkingSphinx::Index::Builder.generate(Person) do
230
+ #
231
+ end
232
+ }.should raise_error
233
+ end
234
+ end
235
+
236
+ describe "explicit source" do
237
+ before :each do
238
+ @index = ThinkingSphinx::Index::Builder.generate(Person) do
239
+ define_source do
240
+ indexes first_name, last_name
241
+ has birthday
242
+ has id, :as => :internal_id
243
+
244
+ set_property :delta => true
245
+
246
+ where "birthday <= NOW()"
247
+ group_by "first_name"
248
+ end
249
+ end
250
+
251
+ @source = @index.sources.first
252
+ end
253
+
254
+ it "should return an index" do
255
+ @index.should be_a_kind_of(ThinkingSphinx::Index)
256
+ end
257
+
258
+ it "should have one source for the index" do
259
+ @index.sources.length.should == 1
260
+ end
261
+
262
+ it "should have two fields" do
263
+ @source.fields.length.should == 2
264
+ @source.fields[0].unique_name.should == :first_name
265
+ @source.fields[1].unique_name.should == :last_name
266
+ end
267
+
268
+ it "should have two attributes alongside the four internal ones" do
269
+ @source.attributes.length.should == 6
270
+ @source.attributes[4].unique_name.should == :birthday
271
+ @source.attributes[5].unique_name.should == :internal_id
272
+ end
273
+ end
274
+
275
+ describe "multiple sources" do
276
+ before :each do
277
+ @index = ThinkingSphinx::Index::Builder.generate(Person) do
278
+ define_source do
279
+ indexes first_name
280
+ has birthday
281
+ end
282
+
283
+ define_source do
284
+ indexes last_name
285
+ has :id, :as => :internal_id
286
+ end
287
+ end
288
+ end
289
+
290
+ it "should have two sources" do
291
+ @index.sources.length.should == 2
292
+ end
293
+
294
+ it "should have two fields" do
295
+ @index.fields.length.should == 2
296
+ end
297
+
298
+ it "should have one field in each source" do
299
+ @index.sources.each do |source|
300
+ source.fields.length.should == 1
301
+ end
302
+ end
303
+
304
+ it "should have two attributes alongside the eight internal ones" do
305
+ @index.attributes.length.should == 10
306
+ end
307
+
308
+ it "should have one attribute in each source alongside the four internal ones" do
309
+ @index.sources.each do |source|
310
+ source.attributes.length.should == 5
311
+ end
312
+ end
313
+ end
314
+
315
+ describe "index options" do
316
+ before :each do
317
+ @index = ThinkingSphinx::Index::Builder.generate(Person) do
318
+ indexes first_name
319
+
320
+ set_property :charset_type => "utf16"
321
+ end
322
+ end
323
+
324
+ it "should store the setting for the index" do
325
+ @index.local_options.length.should == 1
326
+ @index.local_options[:charset_type].should == "utf16"
327
+ end
328
+ end
329
+
330
+ describe "delta options" do
331
+ before :each do
332
+ @index = ThinkingSphinx::Index::Builder.generate(Person) do
333
+ indexes first_name
334
+
335
+ set_property :delta => true
336
+ end
337
+ end
338
+
339
+ it "should not keep the delta setting in source options" do
340
+ @index.sources.first.options.should be_empty
341
+ end
342
+
343
+ it "should not keep the delta setting in index options" do
344
+ @index.local_options.should be_empty
345
+ end
346
+
347
+ it "should set the index delta object set" do
348
+ @index.delta_object.should be_a_kind_of(ThinkingSphinx::Deltas::DefaultDelta)
349
+ end
350
+ end
5
351
  end
@@ -1,15 +1,6 @@
1
1
  require 'spec/spec_helper'
2
2
 
3
3
  describe ThinkingSphinx::Index do
4
- describe "generated sql_query" do
5
- it "should include explicit groupings if requested" do
6
- @index = ThinkingSphinx::Index.new(Person)
7
-
8
- @index.groupings << "custom_sql"
9
- @index.to_riddle_for_core(0, 0).sql_query.should match(/GROUP BY.+custom_sql/)
10
- end
11
- end
12
-
13
4
  describe "prefix_fields method" do
14
5
  before :each do
15
6
  @index = ThinkingSphinx::Index.new(Person)
@@ -18,7 +9,7 @@ describe ThinkingSphinx::Index do
18
9
  @field_b = ThinkingSphinx::Field.stub_instance(:prefixes => false)
19
10
  @field_c = ThinkingSphinx::Field.stub_instance(:prefixes => true)
20
11
 
21
- @index.fields = [@field_a, @field_b, @field_c]
12
+ @index.stub_method(:fields => [@field_a, @field_b, @field_c])
22
13
  end
23
14
 
24
15
  it "should return fields that are flagged as prefixed" do
@@ -39,7 +30,7 @@ describe ThinkingSphinx::Index do
39
30
  @field_b = ThinkingSphinx::Field.stub_instance(:infixes => false)
40
31
  @field_c = ThinkingSphinx::Field.stub_instance(:infixes => true)
41
32
 
42
- @index.fields = [@field_a, @field_b, @field_c]
33
+ @index.stub_method(:fields => [@field_a, @field_b, @field_c])
43
34
  end
44
35
 
45
36
  it "should return fields that are flagged as infixed" do
@@ -54,12 +45,12 @@ describe ThinkingSphinx::Index do
54
45
 
55
46
  describe "multi-value attribute as ranged-query with has-many association" do
56
47
  before :each do
57
- @index = ThinkingSphinx::Index.new(Person) do
48
+ @index = ThinkingSphinx::Index::Builder.generate(Person) do
49
+ indexes first_name
58
50
  has tags(:id), :as => :tag_ids, :source => :ranged_query
59
51
  end
60
- @index.link!
61
52
 
62
- @sql = @index.to_riddle_for_core(0, 0).sql_query
53
+ @sql = @index.sources.first.to_riddle_for_core(0, 0).sql_query
63
54
  end
64
55
 
65
56
  it "should not include attribute in select-clause sql_query" do
@@ -72,8 +63,8 @@ describe ThinkingSphinx::Index do
72
63
  end
73
64
 
74
65
  it "should include sql_attr_multi as ranged-query" do
75
- attribute = @index.send(:attributes).first
76
- attribute.send(:type_to_config).to_s.should == "sql_attr_multi"
66
+ attribute = @index.attributes.detect { |attrib| attrib.unique_name == :tag_ids }
67
+ attribute.type_to_config.should == :sql_attr_multi
77
68
 
78
69
  declaration, query, range_query = attribute.send(:config_value).split('; ')
79
70
  declaration.should == "uint tag_ids from ranged-query"
@@ -84,12 +75,12 @@ describe ThinkingSphinx::Index do
84
75
 
85
76
  describe "multi-value attribute as ranged-query with has-many-through association" do
86
77
  before :each do
87
- @index = ThinkingSphinx::Index.new(Person) do
78
+ @index = ThinkingSphinx::Index::Builder.generate(Person) do
79
+ indexes first_name
88
80
  has football_teams(:id), :as => :football_teams_ids, :source => :ranged_query
89
81
  end
90
- @index.link!
91
82
 
92
- @sql = @index.to_riddle_for_core(0, 0).sql_query
83
+ @sql = @index.sources.first.to_riddle_for_core(0, 0).sql_query
93
84
  end
94
85
 
95
86
  it "should not include attribute in select-clause sql_query" do
@@ -102,8 +93,10 @@ describe ThinkingSphinx::Index do
102
93
  end
103
94
 
104
95
  it "should include sql_attr_multi as ranged-query" do
105
- attribute = @index.send(:attributes).first
106
- attribute.send(:type_to_config).to_s.should == "sql_attr_multi"
96
+ attribute = @index.attributes.detect { |attrib|
97
+ attrib.unique_name == :football_teams_ids
98
+ }
99
+ attribute.type_to_config.should == :sql_attr_multi
107
100
 
108
101
  declaration, query, range_query = attribute.send(:config_value).split('; ')
109
102
  declaration.should == "uint football_teams_ids from ranged-query"
@@ -114,12 +107,12 @@ describe ThinkingSphinx::Index do
114
107
 
115
108
  describe "multi-value attribute as ranged-query with has-many-through association and foreign_key" do
116
109
  before :each do
117
- @index = ThinkingSphinx::Index.new(Person) do
110
+ @index = ThinkingSphinx::Index::Builder.generate(Person) do
111
+ indexes first_name
118
112
  has friends(:id), :as => :friend_ids, :source => :ranged_query
119
113
  end
120
- @index.link!
121
114
 
122
- @sql = @index.to_riddle_for_core(0, 0).sql_query
115
+ @sql = @index.sources.first.to_riddle_for_core(0, 0).sql_query
123
116
  end
124
117
 
125
118
  it "should not include attribute in select-clause sql_query" do
@@ -132,8 +125,10 @@ describe ThinkingSphinx::Index do
132
125
  end
133
126
 
134
127
  it "should include sql_attr_multi as ranged-query" do
135
- attribute = @index.send(:attributes).first
136
- attribute.send(:type_to_config).to_s.should == "sql_attr_multi"
128
+ attribute = @index.attributes.detect { |attrib|
129
+ attrib.unique_name == :friend_ids
130
+ }
131
+ attribute.type_to_config.should == :sql_attr_multi
137
132
 
138
133
  declaration, query, range_query = attribute.send(:config_value).split('; ')
139
134
  declaration.should == "uint friend_ids from ranged-query"
@@ -141,4 +136,4 @@ describe ThinkingSphinx::Index do
141
136
  range_query.should == "SELECT MIN(`friendships`.`person_id`), MAX(`friendships`.`person_id`) FROM `friendships`"
142
137
  end
143
138
  end
144
- end
139
+ end