jaikoo-thinking-sphinx 0.9.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. data/LICENCE +20 -0
  2. data/README +76 -0
  3. data/lib/thinking_sphinx.rb +112 -0
  4. data/lib/thinking_sphinx/active_record.rb +153 -0
  5. data/lib/thinking_sphinx/active_record/delta.rb +80 -0
  6. data/lib/thinking_sphinx/active_record/has_many_association.rb +29 -0
  7. data/lib/thinking_sphinx/active_record/search.rb +50 -0
  8. data/lib/thinking_sphinx/adapters/abstract_adapter.rb +27 -0
  9. data/lib/thinking_sphinx/adapters/mysql_adapter.rb +9 -0
  10. data/lib/thinking_sphinx/adapters/postgresql_adapter.rb +84 -0
  11. data/lib/thinking_sphinx/association.rb +144 -0
  12. data/lib/thinking_sphinx/attribute.rb +284 -0
  13. data/lib/thinking_sphinx/collection.rb +105 -0
  14. data/lib/thinking_sphinx/configuration.rb +314 -0
  15. data/lib/thinking_sphinx/field.rb +206 -0
  16. data/lib/thinking_sphinx/index.rb +432 -0
  17. data/lib/thinking_sphinx/index/builder.rb +220 -0
  18. data/lib/thinking_sphinx/index/faux_column.rb +110 -0
  19. data/lib/thinking_sphinx/rails_additions.rb +68 -0
  20. data/lib/thinking_sphinx/search.rb +436 -0
  21. data/spec/unit/thinking_sphinx/active_record/delta_spec.rb +132 -0
  22. data/spec/unit/thinking_sphinx/active_record/has_many_association_spec.rb +53 -0
  23. data/spec/unit/thinking_sphinx/active_record/search_spec.rb +107 -0
  24. data/spec/unit/thinking_sphinx/active_record_spec.rb +295 -0
  25. data/spec/unit/thinking_sphinx/association_spec.rb +247 -0
  26. data/spec/unit/thinking_sphinx/attribute_spec.rb +360 -0
  27. data/spec/unit/thinking_sphinx/collection_spec.rb +71 -0
  28. data/spec/unit/thinking_sphinx/configuration_spec.rb +512 -0
  29. data/spec/unit/thinking_sphinx/field_spec.rb +224 -0
  30. data/spec/unit/thinking_sphinx/index/builder_spec.rb +34 -0
  31. data/spec/unit/thinking_sphinx/index/faux_column_spec.rb +68 -0
  32. data/spec/unit/thinking_sphinx/index_spec.rb +317 -0
  33. data/spec/unit/thinking_sphinx/search_spec.rb +203 -0
  34. data/spec/unit/thinking_sphinx_spec.rb +129 -0
  35. data/tasks/thinking_sphinx_tasks.rake +1 -0
  36. data/tasks/thinking_sphinx_tasks.rb +100 -0
  37. metadata +103 -0
@@ -0,0 +1,247 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe ThinkingSphinx::Association do
4
+ describe "class-level children method" do
5
+ before :each do
6
+ @normal_reflection = ::ActiveRecord::Reflection::AssociationReflection.stub_instance(
7
+ :options => {:polymorphic => false}
8
+ )
9
+ @normal_association = ThinkingSphinx::Association.stub_instance
10
+ @poly_reflection = ::ActiveRecord::Reflection::AssociationReflection.stub_instance(
11
+ :options => {:polymorphic => true},
12
+ :macro => :has_many,
13
+ :name => "polly",
14
+ :active_record => "AR"
15
+ )
16
+ @non_poly_reflection = ::ActiveRecord::Reflection::AssociationReflection.stub_instance
17
+
18
+ Person.stub_method(:reflect_on_association => @normal_reflection)
19
+ ThinkingSphinx::Association.stub_methods(
20
+ :new => @normal_association,
21
+ :polymorphic_classes => [Person, Person],
22
+ :casted_options => {:casted => :options}
23
+ )
24
+ ::ActiveRecord::Reflection::AssociationReflection.stub_method(
25
+ :new => @non_poly_reflection
26
+ )
27
+ end
28
+
29
+ it "should return an empty array if no association exists" do
30
+ Person.stub_method(:reflect_on_association => nil)
31
+
32
+ ThinkingSphinx::Association.children(Person, :assoc).should == []
33
+ end
34
+
35
+ it "should return a single association instance in an array if assocation isn't polymorphic" do
36
+ ThinkingSphinx::Association.children(Person, :assoc).should == [@normal_association]
37
+ end
38
+
39
+ it "should return multiple association instances for polymorphic associations" do
40
+ Person.stub_method(:reflect_on_association => @poly_reflection)
41
+
42
+ ThinkingSphinx::Association.children(Person, :assoc).should ==
43
+ [@normal_association, @normal_association]
44
+ end
45
+
46
+ it "should generate non-polymorphic 'casted' associations for each polymorphic possibility" do
47
+ Person.stub_method(:reflect_on_association => @poly_reflection)
48
+
49
+ ThinkingSphinx::Association.children(Person, :assoc)
50
+
51
+ ThinkingSphinx::Association.should have_received(:casted_options).with(
52
+ Person, @poly_reflection
53
+ ).twice
54
+
55
+ ::ActiveRecord::Reflection::AssociationReflection.should have_received(:new).with(
56
+ :has_many, :polly_Person, {:casted => :options}, "AR"
57
+ ).twice
58
+
59
+ ThinkingSphinx::Association.should have_received(:new).with(
60
+ nil, @non_poly_reflection
61
+ ).twice
62
+ end
63
+ end
64
+
65
+ describe "instance-level children method" do
66
+ it "should return the children associations for the given association" do
67
+ @reflection = ::ActiveRecord::Reflection::AssociationReflection.stub_instance(
68
+ :klass => :klass
69
+ )
70
+ @association = ThinkingSphinx::Association.new(nil, @reflection)
71
+ ThinkingSphinx::Association.stub_method(:children => :result)
72
+
73
+ @association.children(:assoc).should == :result
74
+ ThinkingSphinx::Association.should have_received(:children).with(:klass, :assoc, @association)
75
+ end
76
+ end
77
+
78
+ describe "join_to method" do
79
+ before :each do
80
+ @parent_join = ::ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation.stub_instance
81
+ @join = ::ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation.stub_instance
82
+ @parent = ThinkingSphinx::Association.stub_instance(:join_to => true, :join => nil)
83
+ @base_join = Object.stub_instance(:joins => [:a, :b, :c])
84
+
85
+ ::ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation.stub_method(:new => @join)
86
+ end
87
+
88
+ it "should call the parent's join_to if parent has no join" do
89
+ @assoc = ThinkingSphinx::Association.new(@parent, :ref)
90
+
91
+ @assoc.join_to(@base_join)
92
+
93
+ @parent.should have_received(:join_to).with(@base_join)
94
+ end
95
+
96
+ it "should not call the parent's join_to if it already has a join" do
97
+ @assoc = ThinkingSphinx::Association.new(@parent, :ref)
98
+ @parent.stub_method(:join => @parent_join)
99
+
100
+ @assoc.join_to(@base_join)
101
+
102
+ @parent.should_not have_received(:join_to)
103
+ end
104
+
105
+ it "should define the join association with a JoinAssociation instance" do
106
+ @assoc = ThinkingSphinx::Association.new(@parent, :ref)
107
+
108
+ @assoc.join_to(@base_join).should == @join
109
+ @assoc.join.should == @join
110
+ end
111
+ end
112
+
113
+ describe "to_sql method" do
114
+ before :each do
115
+ @reflection = ::ActiveRecord::Reflection::AssociationReflection.stub_instance(
116
+ :klass => Person
117
+ )
118
+ @association = ThinkingSphinx::Association.new(nil, @reflection)
119
+ @parent = Object.stub_instance(:aliased_table_name => "ALIAS TABLE NAME")
120
+ @join = ::ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation.stub_instance(
121
+ :association_join => "full association join SQL",
122
+ :parent => @parent
123
+ )
124
+ @association.join = @join
125
+ end
126
+
127
+ it "should return the join's association join value" do
128
+ @association.to_sql.should == "full association join SQL"
129
+ end
130
+
131
+ it "should replace ::ts_join_alias:: with the aliased table name" do
132
+ @join.stub_method(:association_join => "text with ::ts_join_alias:: gone")
133
+
134
+ @association.to_sql.should == "text with `ALIAS TABLE NAME` gone"
135
+ end
136
+ end
137
+
138
+ describe "is_many? method" do
139
+ before :each do
140
+ @parent = ThinkingSphinx::Association.stub_instance(
141
+ :is_many? => :parent_is_many
142
+ )
143
+ @reflection = ::ActiveRecord::Reflection::AssociationReflection.stub_instance(
144
+ :macro => :has_many
145
+ )
146
+ end
147
+
148
+ it "should return true if association is either a has_many or a habtm" do
149
+ association = ThinkingSphinx::Association.new(@parent, @reflection)
150
+ association.is_many?.should be_true
151
+
152
+ @reflection.stub_method(:macro => :has_and_belongs_to_many)
153
+ association.is_many?.should be_true
154
+ end
155
+
156
+ it "should return the parent value if not a has many or habtm and there is a parent" do
157
+ association = ThinkingSphinx::Association.new(@parent, @reflection)
158
+ @reflection.stub_method(:macro => :belongs_to)
159
+ association.is_many?.should == :parent_is_many
160
+ end
161
+
162
+ it "should return false if no parent and not a has many or habtm" do
163
+ association = ThinkingSphinx::Association.new(nil, @reflection)
164
+ @reflection.stub_method(:macro => :belongs_to)
165
+ association.is_many?.should be_false
166
+ end
167
+ end
168
+
169
+ describe "ancestors method" do
170
+ it "should return an array of associations - including all parents" do
171
+ parent = ThinkingSphinx::Association.stub_instance(:ancestors => [:all, :ancestors])
172
+ association = ThinkingSphinx::Association.new(parent, @reflection)
173
+ association.ancestors.should == [:all, :ancestors, association]
174
+ end
175
+ end
176
+
177
+ describe "polymorphic_classes method" do
178
+ it "should return all the polymorphic result types as classes" do
179
+ Person.connection.stub_method(:select_all => [
180
+ {"person_type" => "Person"},
181
+ {"person_type" => "Friendship"}
182
+ ])
183
+ ref = Object.stub_instance(
184
+ :active_record => Person,
185
+ :options => {:foreign_type => "person_type"}
186
+ )
187
+
188
+ ThinkingSphinx::Association.send(:polymorphic_classes, ref).should == [Person, Friendship]
189
+ end
190
+ end
191
+
192
+ describe "casted_options method" do
193
+ before :each do
194
+ @options = {
195
+ :foreign_key => "thing_id",
196
+ :foreign_type => "thing_type",
197
+ :polymorphic => true
198
+ }
199
+ @reflection = ::ActiveRecord::Reflection::AssociationReflection.stub_instance(
200
+ :options => @options
201
+ )
202
+ end
203
+
204
+ it "should return a new options set for a specific class" do
205
+ ThinkingSphinx::Association.send(:casted_options, Person, @reflection).should == {
206
+ :polymorphic => nil,
207
+ :class_name => "Person",
208
+ :foreign_key => "thing_id",
209
+ :foreign_type => "thing_type",
210
+ :conditions => "::ts_join_alias::.`thing_type` = 'Person'"
211
+ }
212
+ end
213
+
214
+ it "should append to existing Array of conditions" do
215
+ @options[:conditions] = ["first condition"]
216
+ ThinkingSphinx::Association.send(:casted_options, Person, @reflection).should == {
217
+ :polymorphic => nil,
218
+ :class_name => "Person",
219
+ :foreign_key => "thing_id",
220
+ :foreign_type => "thing_type",
221
+ :conditions => ["first condition", "::ts_join_alias::.`thing_type` = 'Person'"]
222
+ }
223
+ end
224
+
225
+ it "should merge to an existing Hash of conditions" do
226
+ @options[:conditions] = {"field" => "value"}
227
+ ThinkingSphinx::Association.send(:casted_options, Person, @reflection).should == {
228
+ :polymorphic => nil,
229
+ :class_name => "Person",
230
+ :foreign_key => "thing_id",
231
+ :foreign_type => "thing_type",
232
+ :conditions => {"field" => "value", "thing_type" => "Person"}
233
+ }
234
+ end
235
+
236
+ it "should append to an existing String of conditions" do
237
+ @options[:conditions] = "first condition"
238
+ ThinkingSphinx::Association.send(:casted_options, Person, @reflection).should == {
239
+ :polymorphic => nil,
240
+ :class_name => "Person",
241
+ :foreign_key => "thing_id",
242
+ :foreign_type => "thing_type",
243
+ :conditions => "first condition AND ::ts_join_alias::.`thing_type` = 'Person'"
244
+ }
245
+ end
246
+ end
247
+ end
@@ -0,0 +1,360 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe ThinkingSphinx::Attribute do
4
+ describe '#initialize' do
5
+ it 'raises if no columns are provided so that configuration errors are easier to track down' do
6
+ lambda {
7
+ ThinkingSphinx::Attribute.new([])
8
+ }.should raise_error(RuntimeError)
9
+ end
10
+
11
+ 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
+ lambda {
13
+ ThinkingSphinx::Attribute.new([1234])
14
+ }.should raise_error(RuntimeError)
15
+ end
16
+ end
17
+
18
+ describe "to_select_sql method with MySQL" do
19
+ before :each do
20
+ @index = Person.sphinx_indexes.first
21
+ @index.link!
22
+ end
23
+
24
+ it "should concat with spaces if there's more than one non-integer column" do
25
+ @index.attributes[0].to_select_sql.should match(/CONCAT_WS\(' ', /)
26
+ end
27
+
28
+ it "should concat with spaces if there's more than one association for a non-integer column" do
29
+ @index.attributes[1].to_select_sql.should match(/CONCAT_WS\(' ', /)
30
+ end
31
+
32
+ it "should concat with commas if there's multiple integer columns" do
33
+ @index.attributes[2].to_select_sql.should match(/CONCAT_WS\(',', /)
34
+ end
35
+
36
+ it "should concat with commas if there's more than one association for an integer column" do
37
+ @index.attributes[3].to_select_sql.should match(/CONCAT_WS\(',', /)
38
+ end
39
+
40
+ it "should group with spaces if there's string columns from a has_many or has_and_belongs_to_many association" do
41
+ @index.attributes[4].to_select_sql.should match(/GROUP_CONCAT\(.+ SEPARATOR ' '\)/)
42
+ end
43
+
44
+ it "should group with commas if there's integer columns from a has_many or has_and_belongs_to_many association" do
45
+ @index.attributes[5].to_select_sql.should match(/GROUP_CONCAT\(.+ SEPARATOR ','\)/)
46
+ end
47
+
48
+ it "should convert datetime values to timestamps" do
49
+ @index.attributes[6].to_select_sql.should match(/UNIX_TIMESTAMP/)
50
+ end
51
+ end
52
+
53
+ describe "to_select_sql method with PostgreSQL" do
54
+ before :each do
55
+ @index = Person.sphinx_indexes.first
56
+ Person.connection.class.stub_method(
57
+ :name => "ActiveRecord::ConnectionAdapters::PostgreSQLAdapter"
58
+ )
59
+ @index.link!
60
+ end
61
+
62
+ it "should concat with spaces if there's more than one non-integer column" do
63
+ @index.attributes[0].to_select_sql.should match(/|| ' ' ||/)
64
+ end
65
+
66
+ it "should concat with spaces if there's more than one association for a non-integer column" do
67
+ @index.attributes[1].to_select_sql.should match(/|| ' ' ||/)
68
+ end
69
+
70
+ it "should concat with commas if there's multiple integer columns" do
71
+ @index.attributes[2].to_select_sql.should match(/|| ',' ||/)
72
+ end
73
+
74
+ it "should concat with commas if there's more than one association for an integer column" do
75
+ @index.attributes[3].to_select_sql.should match(/|| ',' ||/)
76
+ end
77
+
78
+ it "should group with spaces if there's string columns from a has_many or has_and_belongs_to_many association" do
79
+ @index.attributes[4].to_select_sql.should match(/array_to_string\(array_accum\(.+, ' '\)/)
80
+ end
81
+
82
+ it "should group with commas if there's integer columns from a has_many or has_and_belongs_to_many association" do
83
+ @index.attributes[5].to_select_sql.should match(/array_to_string\(array_accum\(.+, ','\)/)
84
+ end
85
+ end
86
+
87
+ describe "to_group_sql method" do
88
+ before :each do
89
+ @attribute = ThinkingSphinx::Attribute.new([Object.stub_instance(:__stack => [])])
90
+ @attribute.stub_method(:is_many? => false, :is_string? => false)
91
+
92
+ ThinkingSphinx.stub_method(:use_group_by_shortcut? => false)
93
+ end
94
+
95
+ it "should return nil if is_many?" do
96
+ @attribute.stub_method(:is_many? => true)
97
+
98
+ @attribute.to_group_sql.should be_nil
99
+ end
100
+
101
+ it "should return nil if is_string?" do
102
+ @attribute.stub_method(:is_string? => true)
103
+
104
+ @attribute.to_group_sql.should be_nil
105
+ end
106
+
107
+ it "should return nil if group_by shortcut is allowed" do
108
+ ThinkingSphinx.stub_method(:use_group_by_shortcut? => true)
109
+
110
+ @attribute.to_group_sql.should be_nil
111
+ end
112
+
113
+ it "should return an array if neither is_many? or shortcut allowed" do
114
+ @attribute.stub_method(:column_with_prefix => 'hello')
115
+ @attribute.to_group_sql.should be_a_kind_of(Array)
116
+ end
117
+
118
+ after :each do
119
+ ThinkingSphinx.unstub_method(:use_group_by_shortcut?)
120
+ end
121
+ end
122
+
123
+ describe "to_sphinx_clause method" do
124
+ before :each do
125
+ @attribute = ThinkingSphinx::Attribute.new [Object.stub_instance(:__stack => [])]
126
+ @attribute.stub_method(:unique_name => "unique name")
127
+ end
128
+
129
+ it "should use sql_attr_multi syntax for MVA attributes" do
130
+ @attribute.stub_method(:type => :multi)
131
+ @attribute.to_sphinx_clause.should match(/^sql_attr_multi\s+= uint unique name from field$/)
132
+ end
133
+
134
+ it "should use sql_attr_timestamp syntax for datetime values" do
135
+ @attribute.stub_method(:type => :datetime)
136
+ @attribute.to_sphinx_clause.should match(/^sql_attr_timestamp\s+= unique name$/)
137
+ end
138
+
139
+ it "should use sql_attr_str2ordinal for string values" do
140
+ @attribute.stub_method(:type => :string)
141
+ @attribute.to_sphinx_clause.should match(/^sql_attr_str2ordinal\s+= unique name$/)
142
+ end
143
+
144
+ it "should use sql_attr_float for float values" do
145
+ @attribute.stub_method(:type => :float)
146
+ @attribute.to_sphinx_clause.should match(/^sql_attr_float\s+= unique name$/)
147
+ end
148
+
149
+ it "should use sql_attr_bool for boolean values" do
150
+ @attribute.stub_method(:type => :boolean)
151
+ @attribute.to_sphinx_clause.should match(/^sql_attr_bool\s+= unique name$/)
152
+ end
153
+
154
+ it "should use sql_attr_uint for integer values" do
155
+ @attribute.stub_method(:type => :integer)
156
+ @attribute.to_sphinx_clause.should match(/^sql_attr_uint\s+= unique name$/)
157
+ end
158
+
159
+ it "should assume integer for any other types" do
160
+ @attribute.stub_method(:type => :unknown)
161
+ @attribute.to_sphinx_clause.should match(/^sql_attr_uint\s+= unique name$/)
162
+ end
163
+
164
+ end
165
+
166
+ describe "unique_name method" do
167
+ before :each do
168
+ @attribute = ThinkingSphinx::Attribute.new [
169
+ Object.stub_instance(:__stack => [], :__name => "col_name")
170
+ ]
171
+ end
172
+
173
+ it "should use the alias if there is one" do
174
+ @attribute.alias = "alias"
175
+ @attribute.unique_name.should == "alias"
176
+ end
177
+
178
+ it "should use the alias if there's multiple columns" do
179
+ @attribute.columns << Object.stub_instance(:__stack => [], :__name => "col_name")
180
+ @attribute.unique_name.should be_nil
181
+
182
+ @attribute.alias = "alias"
183
+ @attribute.unique_name.should == "alias"
184
+ end
185
+
186
+ it "should use the column name if there's no alias and just one column" do
187
+ @attribute.unique_name.should == "col_name"
188
+ end
189
+ end
190
+
191
+ describe "column_with_prefix method" do
192
+ before :each do
193
+ @attribute = ThinkingSphinx::Attribute.new [
194
+ ThinkingSphinx::Index::FauxColumn.new(:col_name)
195
+ ]
196
+ @attribute.columns.each { |col| @attribute.associations[col] = [] }
197
+ @attribute.model = Person
198
+
199
+ @first_join = Object.stub_instance(:aliased_table_name => "tabular")
200
+ @second_join = Object.stub_instance(:aliased_table_name => "data")
201
+
202
+ @first_assoc = ThinkingSphinx::Association.stub_instance(
203
+ :join => @first_join, :has_column? => true
204
+ )
205
+ @second_assoc = ThinkingSphinx::Association.stub_instance(
206
+ :join => @second_join, :has_column? => true
207
+ )
208
+ end
209
+
210
+ it "should return the column name if the column is a string" do
211
+ @attribute.columns = [ThinkingSphinx::Index::FauxColumn.new("string")]
212
+ @attribute.send(:column_with_prefix, @attribute.columns.first).should == "string"
213
+ end
214
+
215
+ it "should return the column with model's table prefix if there's no associations for the column" do
216
+ @attribute.send(:column_with_prefix, @attribute.columns.first).should == "`people`.`col_name`"
217
+ end
218
+
219
+ it "should return the column with its join table prefix if an association exists" do
220
+ column = @attribute.columns.first
221
+ @attribute.associations[column] = [@first_assoc]
222
+ @attribute.send(:column_with_prefix, column).should == "`tabular`.`col_name`"
223
+ end
224
+
225
+ it "should return multiple columns concatenated if more than one association exists" do
226
+ column = @attribute.columns.first
227
+ @attribute.associations[column] = [@first_assoc, @second_assoc]
228
+ @attribute.send(:column_with_prefix, column).should == "`tabular`.`col_name`, `data`.`col_name`"
229
+ end
230
+ end
231
+
232
+ describe "is_many? method" do
233
+ before :each do
234
+ @assoc_a = Object.stub_instance(:is_many? => true)
235
+ @assoc_b = Object.stub_instance(:is_many? => true)
236
+ @assoc_c = Object.stub_instance(:is_many? => true)
237
+
238
+ @attribute = ThinkingSphinx::Attribute.new(
239
+ [ThinkingSphinx::Index::FauxColumn.new(:col_name)]
240
+ )
241
+ @attribute.associations = {
242
+ :a => @assoc_a, :b => @assoc_b, :c => @assoc_c
243
+ }
244
+ end
245
+
246
+ it "should return true if all associations return true to is_many?" do
247
+ @attribute.send(:is_many?).should be_true
248
+ end
249
+
250
+ it "should return true if one association returns true to is_many?" do
251
+ @assoc_b.stub_method(:is_many? => false)
252
+ @assoc_c.stub_method(:is_many? => false)
253
+
254
+ @attribute.send(:is_many?).should be_true
255
+ end
256
+
257
+ it "should return false if all associations return false to is_many?" do
258
+ @assoc_a.stub_method(:is_many? => false)
259
+ @assoc_b.stub_method(:is_many? => false)
260
+ @assoc_c.stub_method(:is_many? => false)
261
+
262
+ @attribute.send(:is_many?).should be_false
263
+ end
264
+ end
265
+
266
+ describe "is_string? method" do
267
+ before :each do
268
+ @col_a = ThinkingSphinx::Index::FauxColumn.new("a")
269
+ @col_b = ThinkingSphinx::Index::FauxColumn.new("b")
270
+ @col_c = ThinkingSphinx::Index::FauxColumn.new("c")
271
+
272
+ @attribute = ThinkingSphinx::Attribute.new(
273
+ [@col_a, @col_b, @col_c]
274
+ )
275
+ end
276
+
277
+ it "should return true if all columns return true to is_string?" do
278
+ @attribute.send(:is_string?).should be_true
279
+ end
280
+
281
+ it "should return false if one column returns true to is_string?" do
282
+ @col_a.send(:instance_variable_set, :@name, :a)
283
+ @attribute.send(:is_string?).should be_false
284
+ end
285
+
286
+ it "should return false if all columns return false to is_string?" do
287
+ @col_a.send(:instance_variable_set, :@name, :a)
288
+ @col_b.send(:instance_variable_set, :@name, :b)
289
+ @col_c.send(:instance_variable_set, :@name, :c)
290
+ @attribute.send(:is_string?).should be_false
291
+ end
292
+ end
293
+
294
+ describe "type method" do
295
+ before :each do
296
+ @column = ThinkingSphinx::Index::FauxColumn.new(:col_name)
297
+ @attribute = ThinkingSphinx::Attribute.new([@column])
298
+ @attribute.model = Person
299
+ @attribute.stub_method(:is_many? => false)
300
+ end
301
+
302
+ it "should return :multi if is_many? is true" do
303
+ @attribute.stub_method(:is_many? => true)
304
+ @attribute.send(:type).should == :multi
305
+ end
306
+
307
+ it "should return :string if there's more than one association" do
308
+ @attribute.associations = {:a => :assoc, :b => :assoc}
309
+ @attribute.send(:type).should == :string
310
+ end
311
+
312
+ it "should return the column type from the database if not :multi or more than one association" do
313
+ @column.send(:instance_variable_set, :@name, "birthday")
314
+ @attribute.send(:type).should == :datetime
315
+
316
+ @attribute.send(:instance_variable_set, :@type, nil)
317
+ @column.send(:instance_variable_set, :@name, "first_name")
318
+ @attribute.send(:type).should == :string
319
+
320
+ @attribute.send(:instance_variable_set, :@type, nil)
321
+ @column.send(:instance_variable_set, :@name, "id")
322
+ @attribute.send(:type).should == :integer
323
+ end
324
+ end
325
+
326
+ describe "all_ints? method" do
327
+ it "should return true if all columns are integers" do
328
+ attribute = ThinkingSphinx::Attribute.new(
329
+ [ ThinkingSphinx::Index::FauxColumn.new(:id),
330
+ ThinkingSphinx::Index::FauxColumn.new(:team_id) ]
331
+ )
332
+ attribute.model = Person
333
+ attribute.columns.each { |col| attribute.associations[col] = [] }
334
+
335
+ attribute.send(:all_ints?).should be_true
336
+ end
337
+
338
+ it "should return false if only some columns are integers" do
339
+ attribute = ThinkingSphinx::Attribute.new(
340
+ [ ThinkingSphinx::Index::FauxColumn.new(:id),
341
+ ThinkingSphinx::Index::FauxColumn.new(:first_name) ]
342
+ )
343
+ attribute.model = Person
344
+ attribute.columns.each { |col| attribute.associations[col] = [] }
345
+
346
+ attribute.send(:all_ints?).should be_false
347
+ end
348
+
349
+ it "should return false if no columns are integers" do
350
+ attribute = ThinkingSphinx::Attribute.new(
351
+ [ ThinkingSphinx::Index::FauxColumn.new(:first_name),
352
+ ThinkingSphinx::Index::FauxColumn.new(:last_name) ]
353
+ )
354
+ attribute.model = Person
355
+ attribute.columns.each { |col| attribute.associations[col] = [] }
356
+
357
+ attribute.send(:all_ints?).should be_false
358
+ end
359
+ end
360
+ end