sherpa99-thinking-sphinx 1.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (145) hide show
  1. data/LICENCE +20 -0
  2. data/README +107 -0
  3. data/README.textile +107 -0
  4. data/Rakefile +4 -0
  5. data/contribute.rb +328 -0
  6. data/cucumber.yml +1 -0
  7. data/features/a.rb +17 -0
  8. data/features/attribute_transformation.feature +22 -0
  9. data/features/datetime_deltas.feature +55 -0
  10. data/features/delayed_delta_indexing.feature +37 -0
  11. data/features/deleting_instances.feature +52 -0
  12. data/features/facets.feature +26 -0
  13. data/features/handling_edits.feature +67 -0
  14. data/features/retry_stale_indexes.feature +24 -0
  15. data/features/searching_across_models.feature +20 -0
  16. data/features/searching_by_model.feature +118 -0
  17. data/features/searching_with_find_arguments.feature +56 -0
  18. data/features/sphinx_detection.feature +16 -0
  19. data/features/step_definitions/alpha_steps.rb +3 -0
  20. data/features/step_definitions/beta_steps.rb +11 -0
  21. data/features/step_definitions/cat_steps.rb +3 -0
  22. data/features/step_definitions/common_steps.rb +154 -0
  23. data/features/step_definitions/datetime_delta_steps.rb +11 -0
  24. data/features/step_definitions/delayed_delta_indexing_steps.rb +7 -0
  25. data/features/step_definitions/facet_steps.rb +30 -0
  26. data/features/step_definitions/find_arguments_steps.rb +36 -0
  27. data/features/step_definitions/gamma_steps.rb +15 -0
  28. data/features/step_definitions/search_steps.rb +66 -0
  29. data/features/step_definitions/sphinx_steps.rb +23 -0
  30. data/features/support/db/active_record.rb +40 -0
  31. data/features/support/db/database.example.yml +4 -0
  32. data/features/support/db/migrations/create_alphas.rb +18 -0
  33. data/features/support/db/migrations/create_animals.rb +9 -0
  34. data/features/support/db/migrations/create_betas.rb +15 -0
  35. data/features/support/db/migrations/create_boxes.rb +13 -0
  36. data/features/support/db/migrations/create_comments.rb +13 -0
  37. data/features/support/db/migrations/create_delayed_betas.rb +28 -0
  38. data/features/support/db/migrations/create_developers.rb +39 -0
  39. data/features/support/db/migrations/create_gammas.rb +14 -0
  40. data/features/support/db/migrations/create_people.rb +1014 -0
  41. data/features/support/db/migrations/create_posts.rb +6 -0
  42. data/features/support/db/migrations/create_thetas.rb +16 -0
  43. data/features/support/db/mysql.rb +4 -0
  44. data/features/support/db/postgresql.rb +4 -0
  45. data/features/support/env.rb +6 -0
  46. data/features/support/models/alpha.rb +9 -0
  47. data/features/support/models/animal.rb +5 -0
  48. data/features/support/models/beta.rb +7 -0
  49. data/features/support/models/box.rb +8 -0
  50. data/features/support/models/cat.rb +3 -0
  51. data/features/support/models/comment.rb +3 -0
  52. data/features/support/models/delayed_beta.rb +7 -0
  53. data/features/support/models/developer.rb +8 -0
  54. data/features/support/models/gamma.rb +5 -0
  55. data/features/support/models/person.rb +8 -0
  56. data/features/support/models/post.rb +8 -0
  57. data/features/support/models/theta.rb +7 -0
  58. data/features/support/post_database.rb +37 -0
  59. data/features/support/z.rb +19 -0
  60. data/ginger_scenarios.rb +24 -0
  61. data/init.rb +12 -0
  62. data/lib/thinking_sphinx.rb +144 -0
  63. data/lib/thinking_sphinx/active_record.rb +245 -0
  64. data/lib/thinking_sphinx/active_record/delta.rb +74 -0
  65. data/lib/thinking_sphinx/active_record/has_many_association.rb +29 -0
  66. data/lib/thinking_sphinx/active_record/search.rb +57 -0
  67. data/lib/thinking_sphinx/adapters/abstract_adapter.rb +34 -0
  68. data/lib/thinking_sphinx/adapters/mysql_adapter.rb +53 -0
  69. data/lib/thinking_sphinx/adapters/postgresql_adapter.rb +129 -0
  70. data/lib/thinking_sphinx/association.rb +144 -0
  71. data/lib/thinking_sphinx/attribute.rb +258 -0
  72. data/lib/thinking_sphinx/collection.rb +142 -0
  73. data/lib/thinking_sphinx/configuration.rb +236 -0
  74. data/lib/thinking_sphinx/core/string.rb +22 -0
  75. data/lib/thinking_sphinx/deltas.rb +22 -0
  76. data/lib/thinking_sphinx/deltas/datetime_delta.rb +50 -0
  77. data/lib/thinking_sphinx/deltas/default_delta.rb +65 -0
  78. data/lib/thinking_sphinx/deltas/delayed_delta.rb +25 -0
  79. data/lib/thinking_sphinx/deltas/delayed_delta/delta_job.rb +24 -0
  80. data/lib/thinking_sphinx/deltas/delayed_delta/flag_as_deleted_job.rb +27 -0
  81. data/lib/thinking_sphinx/deltas/delayed_delta/job.rb +26 -0
  82. data/lib/thinking_sphinx/facet.rb +58 -0
  83. data/lib/thinking_sphinx/facet_collection.rb +44 -0
  84. data/lib/thinking_sphinx/field.rb +172 -0
  85. data/lib/thinking_sphinx/index.rb +414 -0
  86. data/lib/thinking_sphinx/index/builder.rb +233 -0
  87. data/lib/thinking_sphinx/index/faux_column.rb +110 -0
  88. data/lib/thinking_sphinx/rails_additions.rb +133 -0
  89. data/lib/thinking_sphinx/search.rb +638 -0
  90. data/lib/thinking_sphinx/tasks.rb +128 -0
  91. data/rails/init.rb +6 -0
  92. data/spec/fixtures/data.sql +32 -0
  93. data/spec/fixtures/database.yml.default +3 -0
  94. data/spec/fixtures/models.rb +81 -0
  95. data/spec/fixtures/structure.sql +84 -0
  96. data/spec/spec_helper.rb +54 -0
  97. data/spec/sphinx_helper.rb +109 -0
  98. data/spec/unit/thinking_sphinx/active_record/delta_spec.rb +136 -0
  99. data/spec/unit/thinking_sphinx/active_record/has_many_association_spec.rb +53 -0
  100. data/spec/unit/thinking_sphinx/active_record/search_spec.rb +107 -0
  101. data/spec/unit/thinking_sphinx/active_record_spec.rb +256 -0
  102. data/spec/unit/thinking_sphinx/association_spec.rb +247 -0
  103. data/spec/unit/thinking_sphinx/attribute_spec.rb +212 -0
  104. data/spec/unit/thinking_sphinx/collection_spec.rb +14 -0
  105. data/spec/unit/thinking_sphinx/configuration_spec.rb +136 -0
  106. data/spec/unit/thinking_sphinx/core/string_spec.rb +9 -0
  107. data/spec/unit/thinking_sphinx/field_spec.rb +145 -0
  108. data/spec/unit/thinking_sphinx/index/builder_spec.rb +5 -0
  109. data/spec/unit/thinking_sphinx/index/faux_column_spec.rb +30 -0
  110. data/spec/unit/thinking_sphinx/index_spec.rb +54 -0
  111. data/spec/unit/thinking_sphinx/search_spec.rb +59 -0
  112. data/spec/unit/thinking_sphinx_spec.rb +129 -0
  113. data/tasks/distribution.rb +48 -0
  114. data/tasks/rails.rake +1 -0
  115. data/tasks/testing.rb +86 -0
  116. data/thinking-sphinx.gemspec +232 -0
  117. data/vendor/after_commit/LICENSE +20 -0
  118. data/vendor/after_commit/README +16 -0
  119. data/vendor/after_commit/Rakefile +22 -0
  120. data/vendor/after_commit/init.rb +5 -0
  121. data/vendor/after_commit/lib/after_commit.rb +42 -0
  122. data/vendor/after_commit/lib/after_commit/active_record.rb +91 -0
  123. data/vendor/after_commit/lib/after_commit/connection_adapters.rb +103 -0
  124. data/vendor/after_commit/test/after_commit_test.rb +53 -0
  125. data/vendor/delayed_job/lib/delayed/job.rb +251 -0
  126. data/vendor/delayed_job/lib/delayed/message_sending.rb +7 -0
  127. data/vendor/delayed_job/lib/delayed/performable_method.rb +55 -0
  128. data/vendor/delayed_job/lib/delayed/worker.rb +54 -0
  129. data/vendor/riddle/lib/riddle.rb +30 -0
  130. data/vendor/riddle/lib/riddle/client.rb +619 -0
  131. data/vendor/riddle/lib/riddle/client/filter.rb +53 -0
  132. data/vendor/riddle/lib/riddle/client/message.rb +65 -0
  133. data/vendor/riddle/lib/riddle/client/response.rb +84 -0
  134. data/vendor/riddle/lib/riddle/configuration.rb +33 -0
  135. data/vendor/riddle/lib/riddle/configuration/distributed_index.rb +48 -0
  136. data/vendor/riddle/lib/riddle/configuration/index.rb +142 -0
  137. data/vendor/riddle/lib/riddle/configuration/indexer.rb +19 -0
  138. data/vendor/riddle/lib/riddle/configuration/remote_index.rb +17 -0
  139. data/vendor/riddle/lib/riddle/configuration/searchd.rb +25 -0
  140. data/vendor/riddle/lib/riddle/configuration/section.rb +37 -0
  141. data/vendor/riddle/lib/riddle/configuration/source.rb +23 -0
  142. data/vendor/riddle/lib/riddle/configuration/sql_source.rb +34 -0
  143. data/vendor/riddle/lib/riddle/configuration/xml_source.rb +28 -0
  144. data/vendor/riddle/lib/riddle/controller.rb +44 -0
  145. metadata +248 -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,212 @@
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 "unique_name method" do
19
+ before :each do
20
+ @attribute = ThinkingSphinx::Attribute.new [
21
+ Object.stub_instance(:__stack => [], :__name => "col_name")
22
+ ]
23
+ end
24
+
25
+ it "should use the alias if there is one" do
26
+ @attribute.alias = "alias"
27
+ @attribute.unique_name.should == "alias"
28
+ end
29
+
30
+ it "should use the alias if there's multiple columns" do
31
+ @attribute.columns << Object.stub_instance(:__stack => [], :__name => "col_name")
32
+ @attribute.unique_name.should be_nil
33
+
34
+ @attribute.alias = "alias"
35
+ @attribute.unique_name.should == "alias"
36
+ end
37
+
38
+ it "should use the column name if there's no alias and just one column" do
39
+ @attribute.unique_name.should == "col_name"
40
+ end
41
+ end
42
+
43
+ describe "column_with_prefix method" do
44
+ before :each do
45
+ @attribute = ThinkingSphinx::Attribute.new [
46
+ ThinkingSphinx::Index::FauxColumn.new(:col_name)
47
+ ]
48
+ @attribute.columns.each { |col| @attribute.associations[col] = [] }
49
+ @attribute.model = Person
50
+
51
+ @first_join = Object.stub_instance(:aliased_table_name => "tabular")
52
+ @second_join = Object.stub_instance(:aliased_table_name => "data")
53
+
54
+ @first_assoc = ThinkingSphinx::Association.stub_instance(
55
+ :join => @first_join, :has_column? => true
56
+ )
57
+ @second_assoc = ThinkingSphinx::Association.stub_instance(
58
+ :join => @second_join, :has_column? => true
59
+ )
60
+ end
61
+
62
+ it "should return the column name if the column is a string" do
63
+ @attribute.columns = [ThinkingSphinx::Index::FauxColumn.new("string")]
64
+ @attribute.send(:column_with_prefix, @attribute.columns.first).should == "string"
65
+ end
66
+
67
+ it "should return the column with model's table prefix if there's no associations for the column" do
68
+ @attribute.send(:column_with_prefix, @attribute.columns.first).should == "`people`.`col_name`"
69
+ end
70
+
71
+ it "should return the column with its join table prefix if an association exists" do
72
+ column = @attribute.columns.first
73
+ @attribute.associations[column] = [@first_assoc]
74
+ @attribute.send(:column_with_prefix, column).should == "`tabular`.`col_name`"
75
+ end
76
+
77
+ it "should return multiple columns concatenated if more than one association exists" do
78
+ column = @attribute.columns.first
79
+ @attribute.associations[column] = [@first_assoc, @second_assoc]
80
+ @attribute.send(:column_with_prefix, column).should == "`tabular`.`col_name`, `data`.`col_name`"
81
+ end
82
+ end
83
+
84
+ describe "is_many? method" do
85
+ before :each do
86
+ @assoc_a = Object.stub_instance(:is_many? => true)
87
+ @assoc_b = Object.stub_instance(:is_many? => true)
88
+ @assoc_c = Object.stub_instance(:is_many? => true)
89
+
90
+ @attribute = ThinkingSphinx::Attribute.new(
91
+ [ThinkingSphinx::Index::FauxColumn.new(:col_name)]
92
+ )
93
+ @attribute.associations = {
94
+ :a => @assoc_a, :b => @assoc_b, :c => @assoc_c
95
+ }
96
+ end
97
+
98
+ it "should return true if all associations return true to is_many?" do
99
+ @attribute.send(:is_many?).should be_true
100
+ end
101
+
102
+ it "should return true if one association returns true to is_many?" do
103
+ @assoc_b.stub_method(:is_many? => false)
104
+ @assoc_c.stub_method(:is_many? => false)
105
+
106
+ @attribute.send(:is_many?).should be_true
107
+ end
108
+
109
+ it "should return false if all associations return false to is_many?" do
110
+ @assoc_a.stub_method(:is_many? => false)
111
+ @assoc_b.stub_method(:is_many? => false)
112
+ @assoc_c.stub_method(:is_many? => false)
113
+
114
+ @attribute.send(:is_many?).should be_false
115
+ end
116
+ end
117
+
118
+ describe "is_string? method" do
119
+ before :each do
120
+ @col_a = ThinkingSphinx::Index::FauxColumn.new("a")
121
+ @col_b = ThinkingSphinx::Index::FauxColumn.new("b")
122
+ @col_c = ThinkingSphinx::Index::FauxColumn.new("c")
123
+
124
+ @attribute = ThinkingSphinx::Attribute.new(
125
+ [@col_a, @col_b, @col_c]
126
+ )
127
+ end
128
+
129
+ it "should return true if all columns return true to is_string?" do
130
+ @attribute.send(:is_string?).should be_true
131
+ end
132
+
133
+ it "should return false if one column returns true to is_string?" do
134
+ @col_a.send(:instance_variable_set, :@name, :a)
135
+ @attribute.send(:is_string?).should be_false
136
+ end
137
+
138
+ it "should return false if all columns return false to is_string?" do
139
+ @col_a.send(:instance_variable_set, :@name, :a)
140
+ @col_b.send(:instance_variable_set, :@name, :b)
141
+ @col_c.send(:instance_variable_set, :@name, :c)
142
+ @attribute.send(:is_string?).should be_false
143
+ end
144
+ end
145
+
146
+ describe "type method" do
147
+ before :each do
148
+ @column = ThinkingSphinx::Index::FauxColumn.new(:col_name)
149
+ @attribute = ThinkingSphinx::Attribute.new([@column])
150
+ @attribute.model = Person
151
+ @attribute.stub_method(:is_many? => false)
152
+ end
153
+
154
+ it "should return :multi if is_many? is true" do
155
+ @attribute.stub_method(:is_many? => true)
156
+ @attribute.send(:type).should == :multi
157
+ end
158
+
159
+ it "should return :string if there's more than one association" do
160
+ @attribute.associations = {:a => [:assoc], :b => [:assoc]}
161
+ @attribute.send(:type).should == :string
162
+ end
163
+
164
+ it "should return the column type from the database if not :multi or more than one association" do
165
+ @column.send(:instance_variable_set, :@name, "birthday")
166
+ @attribute.send(:type).should == :datetime
167
+
168
+ @attribute.send(:instance_variable_set, :@type, nil)
169
+ @column.send(:instance_variable_set, :@name, "first_name")
170
+ @attribute.send(:type).should == :string
171
+
172
+ @attribute.send(:instance_variable_set, :@type, nil)
173
+ @column.send(:instance_variable_set, :@name, "id")
174
+ @attribute.send(:type).should == :integer
175
+ end
176
+ end
177
+
178
+ describe "all_ints? method" do
179
+ it "should return true if all columns are integers" do
180
+ attribute = ThinkingSphinx::Attribute.new(
181
+ [ ThinkingSphinx::Index::FauxColumn.new(:id),
182
+ ThinkingSphinx::Index::FauxColumn.new(:team_id) ]
183
+ )
184
+ attribute.model = Person
185
+ attribute.columns.each { |col| attribute.associations[col] = [] }
186
+
187
+ attribute.send(:all_ints?).should be_true
188
+ end
189
+
190
+ it "should return false if only some columns are integers" do
191
+ attribute = ThinkingSphinx::Attribute.new(
192
+ [ ThinkingSphinx::Index::FauxColumn.new(:id),
193
+ ThinkingSphinx::Index::FauxColumn.new(:first_name) ]
194
+ )
195
+ attribute.model = Person
196
+ attribute.columns.each { |col| attribute.associations[col] = [] }
197
+
198
+ attribute.send(:all_ints?).should be_false
199
+ end
200
+
201
+ it "should return false if no columns are integers" do
202
+ attribute = ThinkingSphinx::Attribute.new(
203
+ [ ThinkingSphinx::Index::FauxColumn.new(:first_name),
204
+ ThinkingSphinx::Index::FauxColumn.new(:last_name) ]
205
+ )
206
+ attribute.model = Person
207
+ attribute.columns.each { |col| attribute.associations[col] = [] }
208
+
209
+ attribute.send(:all_ints?).should be_false
210
+ end
211
+ end
212
+ end