zipme-thinking-sphinx 1.3.14

Sign up to get free protection for your applications and to get access to all the features.
Files changed (158) hide show
  1. data/LICENCE +20 -0
  2. data/README.textile +168 -0
  3. data/VERSION +1 -0
  4. data/features/abstract_inheritance.feature +10 -0
  5. data/features/alternate_primary_key.feature +27 -0
  6. data/features/attribute_transformation.feature +22 -0
  7. data/features/attribute_updates.feature +51 -0
  8. data/features/deleting_instances.feature +67 -0
  9. data/features/direct_attributes.feature +11 -0
  10. data/features/excerpts.feature +13 -0
  11. data/features/extensible_delta_indexing.feature +9 -0
  12. data/features/facets.feature +82 -0
  13. data/features/facets_across_model.feature +29 -0
  14. data/features/handling_edits.feature +92 -0
  15. data/features/retry_stale_indexes.feature +24 -0
  16. data/features/searching_across_models.feature +20 -0
  17. data/features/searching_by_index.feature +40 -0
  18. data/features/searching_by_model.feature +175 -0
  19. data/features/searching_with_find_arguments.feature +56 -0
  20. data/features/sphinx_detection.feature +25 -0
  21. data/features/sphinx_scopes.feature +42 -0
  22. data/features/step_definitions/alpha_steps.rb +16 -0
  23. data/features/step_definitions/beta_steps.rb +7 -0
  24. data/features/step_definitions/common_steps.rb +188 -0
  25. data/features/step_definitions/extensible_delta_indexing_steps.rb +7 -0
  26. data/features/step_definitions/facet_steps.rb +96 -0
  27. data/features/step_definitions/find_arguments_steps.rb +36 -0
  28. data/features/step_definitions/gamma_steps.rb +15 -0
  29. data/features/step_definitions/scope_steps.rb +15 -0
  30. data/features/step_definitions/search_steps.rb +89 -0
  31. data/features/step_definitions/sphinx_steps.rb +35 -0
  32. data/features/sti_searching.feature +19 -0
  33. data/features/support/database.example.yml +3 -0
  34. data/features/support/db/fixtures/alphas.rb +10 -0
  35. data/features/support/db/fixtures/authors.rb +1 -0
  36. data/features/support/db/fixtures/betas.rb +10 -0
  37. data/features/support/db/fixtures/boxes.rb +9 -0
  38. data/features/support/db/fixtures/categories.rb +1 -0
  39. data/features/support/db/fixtures/cats.rb +3 -0
  40. data/features/support/db/fixtures/comments.rb +24 -0
  41. data/features/support/db/fixtures/developers.rb +29 -0
  42. data/features/support/db/fixtures/dogs.rb +3 -0
  43. data/features/support/db/fixtures/extensible_betas.rb +10 -0
  44. data/features/support/db/fixtures/foxes.rb +3 -0
  45. data/features/support/db/fixtures/gammas.rb +10 -0
  46. data/features/support/db/fixtures/music.rb +4 -0
  47. data/features/support/db/fixtures/people.rb +1001 -0
  48. data/features/support/db/fixtures/posts.rb +6 -0
  49. data/features/support/db/fixtures/robots.rb +14 -0
  50. data/features/support/db/fixtures/tags.rb +27 -0
  51. data/features/support/db/migrations/create_alphas.rb +8 -0
  52. data/features/support/db/migrations/create_animals.rb +5 -0
  53. data/features/support/db/migrations/create_authors.rb +3 -0
  54. data/features/support/db/migrations/create_authors_posts.rb +6 -0
  55. data/features/support/db/migrations/create_betas.rb +5 -0
  56. data/features/support/db/migrations/create_boxes.rb +5 -0
  57. data/features/support/db/migrations/create_categories.rb +3 -0
  58. data/features/support/db/migrations/create_comments.rb +10 -0
  59. data/features/support/db/migrations/create_developers.rb +9 -0
  60. data/features/support/db/migrations/create_extensible_betas.rb +5 -0
  61. data/features/support/db/migrations/create_gammas.rb +3 -0
  62. data/features/support/db/migrations/create_genres.rb +3 -0
  63. data/features/support/db/migrations/create_music.rb +6 -0
  64. data/features/support/db/migrations/create_people.rb +13 -0
  65. data/features/support/db/migrations/create_posts.rb +5 -0
  66. data/features/support/db/migrations/create_robots.rb +4 -0
  67. data/features/support/db/migrations/create_taggings.rb +5 -0
  68. data/features/support/db/migrations/create_tags.rb +4 -0
  69. data/features/support/env.rb +21 -0
  70. data/features/support/lib/generic_delta_handler.rb +8 -0
  71. data/features/support/models/alpha.rb +22 -0
  72. data/features/support/models/animal.rb +5 -0
  73. data/features/support/models/author.rb +3 -0
  74. data/features/support/models/beta.rb +8 -0
  75. data/features/support/models/box.rb +8 -0
  76. data/features/support/models/cat.rb +3 -0
  77. data/features/support/models/category.rb +4 -0
  78. data/features/support/models/comment.rb +10 -0
  79. data/features/support/models/developer.rb +16 -0
  80. data/features/support/models/dog.rb +3 -0
  81. data/features/support/models/extensible_beta.rb +9 -0
  82. data/features/support/models/fox.rb +5 -0
  83. data/features/support/models/gamma.rb +5 -0
  84. data/features/support/models/genre.rb +3 -0
  85. data/features/support/models/medium.rb +5 -0
  86. data/features/support/models/music.rb +8 -0
  87. data/features/support/models/person.rb +23 -0
  88. data/features/support/models/post.rb +21 -0
  89. data/features/support/models/robot.rb +12 -0
  90. data/features/support/models/tag.rb +3 -0
  91. data/features/support/models/tagging.rb +4 -0
  92. data/lib/cucumber/thinking_sphinx/external_world.rb +8 -0
  93. data/lib/cucumber/thinking_sphinx/internal_world.rb +126 -0
  94. data/lib/cucumber/thinking_sphinx/sql_logger.rb +20 -0
  95. data/lib/thinking_sphinx.rb +231 -0
  96. data/lib/thinking_sphinx/active_record.rb +373 -0
  97. data/lib/thinking_sphinx/active_record/attribute_updates.rb +46 -0
  98. data/lib/thinking_sphinx/active_record/delta.rb +61 -0
  99. data/lib/thinking_sphinx/active_record/has_many_association.rb +29 -0
  100. data/lib/thinking_sphinx/active_record/scopes.rb +75 -0
  101. data/lib/thinking_sphinx/adapters/abstract_adapter.rb +42 -0
  102. data/lib/thinking_sphinx/adapters/mysql_adapter.rb +54 -0
  103. data/lib/thinking_sphinx/adapters/postgresql_adapter.rb +143 -0
  104. data/lib/thinking_sphinx/association.rb +164 -0
  105. data/lib/thinking_sphinx/attribute.rb +362 -0
  106. data/lib/thinking_sphinx/auto_version.rb +22 -0
  107. data/lib/thinking_sphinx/class_facet.rb +15 -0
  108. data/lib/thinking_sphinx/configuration.rb +283 -0
  109. data/lib/thinking_sphinx/context.rb +68 -0
  110. data/lib/thinking_sphinx/core/array.rb +7 -0
  111. data/lib/thinking_sphinx/core/string.rb +15 -0
  112. data/lib/thinking_sphinx/deltas.rb +28 -0
  113. data/lib/thinking_sphinx/deltas/default_delta.rb +62 -0
  114. data/lib/thinking_sphinx/deploy/capistrano.rb +100 -0
  115. data/lib/thinking_sphinx/excerpter.rb +22 -0
  116. data/lib/thinking_sphinx/facet.rb +125 -0
  117. data/lib/thinking_sphinx/facet_search.rb +136 -0
  118. data/lib/thinking_sphinx/field.rb +82 -0
  119. data/lib/thinking_sphinx/index.rb +157 -0
  120. data/lib/thinking_sphinx/index/builder.rb +296 -0
  121. data/lib/thinking_sphinx/index/faux_column.rb +110 -0
  122. data/lib/thinking_sphinx/property.rb +162 -0
  123. data/lib/thinking_sphinx/rails_additions.rb +150 -0
  124. data/lib/thinking_sphinx/search.rb +775 -0
  125. data/lib/thinking_sphinx/search_methods.rb +439 -0
  126. data/lib/thinking_sphinx/source.rb +153 -0
  127. data/lib/thinking_sphinx/source/internal_properties.rb +46 -0
  128. data/lib/thinking_sphinx/source/sql.rb +130 -0
  129. data/lib/thinking_sphinx/tasks.rb +121 -0
  130. data/lib/thinking_sphinx/test.rb +52 -0
  131. data/rails/init.rb +16 -0
  132. data/spec/thinking_sphinx/active_record/delta_spec.rb +128 -0
  133. data/spec/thinking_sphinx/active_record/has_many_association_spec.rb +55 -0
  134. data/spec/thinking_sphinx/active_record/scopes_spec.rb +177 -0
  135. data/spec/thinking_sphinx/active_record_spec.rb +622 -0
  136. data/spec/thinking_sphinx/association_spec.rb +239 -0
  137. data/spec/thinking_sphinx/attribute_spec.rb +570 -0
  138. data/spec/thinking_sphinx/auto_version_spec.rb +39 -0
  139. data/spec/thinking_sphinx/configuration_spec.rb +234 -0
  140. data/spec/thinking_sphinx/context_spec.rb +119 -0
  141. data/spec/thinking_sphinx/core/array_spec.rb +9 -0
  142. data/spec/thinking_sphinx/core/string_spec.rb +9 -0
  143. data/spec/thinking_sphinx/excerpter_spec.rb +57 -0
  144. data/spec/thinking_sphinx/facet_search_spec.rb +176 -0
  145. data/spec/thinking_sphinx/facet_spec.rb +333 -0
  146. data/spec/thinking_sphinx/field_spec.rb +154 -0
  147. data/spec/thinking_sphinx/index/builder_spec.rb +479 -0
  148. data/spec/thinking_sphinx/index/faux_column_spec.rb +30 -0
  149. data/spec/thinking_sphinx/index_spec.rb +183 -0
  150. data/spec/thinking_sphinx/rails_additions_spec.rb +203 -0
  151. data/spec/thinking_sphinx/search_methods_spec.rb +152 -0
  152. data/spec/thinking_sphinx/search_spec.rb +1181 -0
  153. data/spec/thinking_sphinx/source_spec.rb +235 -0
  154. data/spec/thinking_sphinx_spec.rb +204 -0
  155. data/tasks/distribution.rb +39 -0
  156. data/tasks/rails.rake +1 -0
  157. data/tasks/testing.rb +72 -0
  158. metadata +242 -0
@@ -0,0 +1,154 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe ThinkingSphinx::Field do
4
+ before :each do
5
+ @index = ThinkingSphinx::Index.new(Alpha)
6
+ @source = ThinkingSphinx::Source.new(@index)
7
+ end
8
+
9
+ describe '#initialize' do
10
+ it 'raises if no columns are provided so that configuration errors are easier to track down' do
11
+ lambda {
12
+ ThinkingSphinx::Field.new(@source, [])
13
+ }.should raise_error(RuntimeError)
14
+ end
15
+
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
17
+ lambda {
18
+ ThinkingSphinx::Field.new(@source, [1234])
19
+ }.should raise_error(RuntimeError)
20
+ end
21
+ end
22
+
23
+ describe "unique_name method" do
24
+ before :each do
25
+ @field = ThinkingSphinx::Field.new @source, [
26
+ stub('column', :__stack => [], :__name => "col_name")
27
+ ]
28
+ end
29
+
30
+ it "should use the alias if there is one" do
31
+ @field.alias = "alias"
32
+ @field.unique_name.should == "alias"
33
+ end
34
+
35
+ it "should use the alias if there's multiple columns" do
36
+ @field.columns << stub('column', :__stack => [], :__name => "col_name")
37
+ @field.unique_name.should be_nil
38
+
39
+ @field.alias = "alias"
40
+ @field.unique_name.should == "alias"
41
+ end
42
+
43
+ it "should use the column name if there's no alias and just one column" do
44
+ @field.unique_name.should == "col_name"
45
+ end
46
+ end
47
+
48
+ describe "prefixes method" do
49
+ it "should default to false" do
50
+ @field = ThinkingSphinx::Field.new(
51
+ @source, [stub('column', :__stack => [])]
52
+ )
53
+ @field.prefixes.should be_false
54
+ end
55
+
56
+ it "should be true if the corresponding option is set" do
57
+ @field = ThinkingSphinx::Field.new(
58
+ @source, [stub('column', :__stack => [])], :prefixes => true
59
+ )
60
+ @field.prefixes.should be_true
61
+ end
62
+ end
63
+
64
+ describe "infixes method" do
65
+ it "should default to false" do
66
+ @field = ThinkingSphinx::Field.new(
67
+ @source, [stub('column', :__stack => [])]
68
+ )
69
+ @field.infixes.should be_false
70
+ end
71
+
72
+ it "should be true if the corresponding option is set" do
73
+ @field = ThinkingSphinx::Field.new(
74
+ @source, [stub('column', :__stack => [])], :infixes => true
75
+ )
76
+ @field.infixes.should be_true
77
+ end
78
+ end
79
+
80
+ describe "column_with_prefix method" do
81
+ before :each do
82
+ @field = ThinkingSphinx::Field.new @source, [
83
+ ThinkingSphinx::Index::FauxColumn.new(:col_name)
84
+ ]
85
+ @field.columns.each { |col| @field.associations[col] = [] }
86
+ @field.model = Person
87
+
88
+ @first_join = Object.new
89
+ @first_join.stub!(:aliased_table_name => "tabular")
90
+ @second_join = Object.new
91
+ @second_join.stub!(:aliased_table_name => "data")
92
+
93
+ @first_assoc = ThinkingSphinx::Association.new nil, nil
94
+ @first_assoc.stub!(:join => @first_join, :has_column? => true)
95
+ @second_assoc = ThinkingSphinx::Association.new nil, nil
96
+ @second_assoc.stub!(:join => @second_join, :has_column? => true)
97
+ end
98
+
99
+ it "should return the column name if the column is a string" do
100
+ @field.columns = [ThinkingSphinx::Index::FauxColumn.new("string")]
101
+ @field.send(:column_with_prefix, @field.columns.first).should == "string"
102
+ end
103
+
104
+ it "should return the column with model's table prefix if there's no associations for the column" do
105
+ @field.send(:column_with_prefix, @field.columns.first).should == "`people`.`col_name`"
106
+ end
107
+
108
+ it "should return the column with its join table prefix if an association exists" do
109
+ column = @field.columns.first
110
+ @field.associations[column] = [@first_assoc]
111
+ @field.send(:column_with_prefix, column).should == "`tabular`.`col_name`"
112
+ end
113
+
114
+ it "should return multiple columns concatenated if more than one association exists" do
115
+ column = @field.columns.first
116
+ @field.associations[column] = [@first_assoc, @second_assoc]
117
+ @field.send(:column_with_prefix, column).should == "`tabular`.`col_name`, `data`.`col_name`"
118
+ end
119
+ end
120
+
121
+ describe "is_many? method" do
122
+ before :each do
123
+ @assoc_a = stub('assoc', :is_many? => true)
124
+ @assoc_b = stub('assoc', :is_many? => true)
125
+ @assoc_c = stub('assoc', :is_many? => true)
126
+
127
+ @field = ThinkingSphinx::Field.new(
128
+ @source, [ThinkingSphinx::Index::FauxColumn.new(:col_name)]
129
+ )
130
+ @field.associations = {
131
+ :a => @assoc_a, :b => @assoc_b, :c => @assoc_c
132
+ }
133
+ end
134
+
135
+ it "should return true if all associations return true to is_many?" do
136
+ @field.send(:is_many?).should be_true
137
+ end
138
+
139
+ it "should return true if one association returns true to is_many?" do
140
+ @assoc_b.stub!(:is_many? => false)
141
+ @assoc_c.stub!(:is_many? => false)
142
+
143
+ @field.send(:is_many?).should be_true
144
+ end
145
+
146
+ it "should return false if all associations return false to is_many?" do
147
+ @assoc_a.stub!(:is_many? => false)
148
+ @assoc_b.stub!(:is_many? => false)
149
+ @assoc_c.stub!(:is_many? => false)
150
+
151
+ @field.send(:is_many?).should be_false
152
+ end
153
+ end
154
+ end
@@ -0,0 +1,479 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe ThinkingSphinx::Index::Builder do
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 'aliased field' do
57
+ before :each do
58
+ @index = ThinkingSphinx::Index::Builder.generate(Person) do
59
+ indexes first_name, :as => 'name'
60
+ end
61
+
62
+ @source = @index.sources.first
63
+ end
64
+
65
+ it "should store the alias as a symbol for consistency" do
66
+ @source.fields.last.unique_name.should == :name
67
+ end
68
+ end
69
+
70
+ describe 'aliased attribute' do
71
+ before :each do
72
+ @index = ThinkingSphinx::Index::Builder.generate(Person) do
73
+ indexes first_name
74
+ has :id, :as => 'real_id'
75
+ end
76
+
77
+ @source = @index.sources.first
78
+ end
79
+
80
+ it "should store the alias as a symbol for consistency" do
81
+ @source.attributes.last.unique_name.should == :real_id
82
+ end
83
+ end
84
+
85
+ describe "sortable field" do
86
+ before :each do
87
+ @index = ThinkingSphinx::Index::Builder.generate(Person) do
88
+ indexes first_name, :sortable => true
89
+ end
90
+
91
+ @source = @index.sources.first
92
+ end
93
+
94
+ it "should have one field" do
95
+ @source.fields.length.should == 1
96
+ end
97
+
98
+ it "should have one attribute alongside the four internal ones" do
99
+ @source.attributes.length.should == 5
100
+ end
101
+
102
+ it "should set the attribute name to have the _sort suffix" do
103
+ @source.attributes.last.unique_name.should == :first_name_sort
104
+ end
105
+
106
+ it "should set the attribute column to be the same as the field" do
107
+ @source.attributes.last.columns.length.should == 1
108
+ @source.attributes.last.columns.first.__name.should == :first_name
109
+ end
110
+ end
111
+
112
+ describe "faceted field" do
113
+ before :each do
114
+ @index = ThinkingSphinx::Index::Builder.generate(Person) do
115
+ indexes first_name, :facet => true
116
+ end
117
+
118
+ @source = @index.sources.first
119
+ end
120
+
121
+ after :each do
122
+ Person.sphinx_facets.delete_at(-1)
123
+ end
124
+
125
+ it "should have one field" do
126
+ @source.fields.length.should == 1
127
+ end
128
+
129
+ it "should have one attribute alongside the four internal ones" do
130
+ @source.attributes.length.should == 5
131
+ end
132
+
133
+ it "should set the attribute name to have the _facet suffix" do
134
+ @source.attributes.last.unique_name.should == :first_name_facet
135
+ end
136
+
137
+ it "should set the attribute type to integer" do
138
+ @source.attributes.last.type.should == :integer
139
+ end
140
+
141
+ it "should set the attribute column to be the same as the field" do
142
+ @source.attributes.last.columns.length.should == 1
143
+ @source.attributes.last.columns.first.__name.should == :first_name
144
+ end
145
+ end
146
+
147
+ describe "faceted integer attribute" do
148
+ before :each do
149
+ @index = ThinkingSphinx::Index::Builder.generate(Alpha) do
150
+ indexes :name
151
+ has value, :facet => true
152
+ end
153
+
154
+ @source = @index.sources.first
155
+ end
156
+
157
+ after :each do
158
+ Alpha.sphinx_facets.delete_at(-1)
159
+ end
160
+
161
+ it "should have just one attribute alongside the four internal ones" do
162
+ @source.attributes.length.should == 5
163
+ end
164
+ end
165
+
166
+ describe "faceted timestamp attribute" do
167
+ before :each do
168
+ @index = ThinkingSphinx::Index::Builder.generate(Person) do
169
+ indexes first_name
170
+ has birthday, :facet => true
171
+ end
172
+
173
+ @source = @index.sources.first
174
+ end
175
+
176
+ after :each do
177
+ Person.sphinx_facets.delete_at(-1)
178
+ end
179
+
180
+ it "should have just one attribute alongside the four internal ones" do
181
+ @source.attributes.length.should == 5
182
+ end
183
+ end
184
+
185
+ describe "faceted boolean attribute" do
186
+ before :each do
187
+ @index = ThinkingSphinx::Index::Builder.generate(Beta) do
188
+ indexes :name
189
+ has delta, :facet => true
190
+ end
191
+
192
+ @source = @index.sources.first
193
+ end
194
+
195
+ after :each do
196
+ Beta.sphinx_facets.delete_at(-1)
197
+ end
198
+
199
+ it "should have just one attribute alongside the four internal ones" do
200
+ @source.attributes.length.should == 5
201
+ end
202
+ end
203
+
204
+ describe "faceted float attribute" do
205
+ before :each do
206
+ @index = ThinkingSphinx::Index::Builder.generate(Alpha) do
207
+ indexes :name
208
+ has cost, :facet => true
209
+ end
210
+
211
+ @source = @index.sources.first
212
+ end
213
+
214
+ after :each do
215
+ Alpha.sphinx_facets.delete_at(-1)
216
+ end
217
+
218
+ it "should have just one attribute alongside the four internal ones" do
219
+ @source.attributes.length.should == 5
220
+ end
221
+ end
222
+
223
+ describe "faceted string attribute" do
224
+ before :each do
225
+ @index = ThinkingSphinx::Index::Builder.generate(Person) do
226
+ indexes first_name
227
+ has last_name, :facet => true
228
+ end
229
+
230
+ @source = @index.sources.first
231
+ end
232
+
233
+ after :each do
234
+ Person.sphinx_facets.delete_at(-1)
235
+ end
236
+
237
+ it "should have two attributes alongside the four internal ones" do
238
+ @source.attributes.length.should == 6
239
+ end
240
+
241
+ it "should set the facet attribute name to have the _facet suffix" do
242
+ @source.attributes.last.unique_name.should == :last_name_facet
243
+ end
244
+
245
+ it "should set the attribute type to integer" do
246
+ @source.attributes.last.type.should == :integer
247
+ end
248
+
249
+ it "should set the attribute column to be the same as the field" do
250
+ @source.attributes.last.columns.length.should == 1
251
+ @source.attributes.last.columns.first.__name.should == :last_name
252
+ end
253
+ end
254
+
255
+ describe 'faceted manual MVA' do
256
+ before :each do
257
+ @index = ThinkingSphinx::Index::Builder.generate(Person) do
258
+ indexes first_name
259
+ has 'SQL STATEMENT', :type => :multi, :as => :sql, :facet => true
260
+ end
261
+
262
+ @source = @index.sources.first
263
+ end
264
+
265
+ after :each do
266
+ Person.sphinx_facets.delete_at(-1)
267
+ end
268
+
269
+ it "should have two attributes alongside the four internal ones" do
270
+ @source.attributes.length.should == 6
271
+ end
272
+
273
+ it "should set the facet attribute name to have the _facet suffix" do
274
+ @source.attributes.last.unique_name.should == :sql_facet
275
+ end
276
+
277
+ it "should keep the original attribute's name set as requested" do
278
+ @source.attributes[-2].unique_name.should == :sql
279
+ end
280
+
281
+ it "should set the attribute type to multi" do
282
+ @source.attributes.last.type.should == :multi
283
+ end
284
+
285
+ it "should set the attribute column to be the same as the field" do
286
+ @source.attributes.last.columns.length.should == 1
287
+ @source.attributes.last.columns.first.__name.should == 'SQL STATEMENT'
288
+ end
289
+ end
290
+
291
+ describe 'faceted MVA field' do
292
+ before :each do
293
+ @index = ThinkingSphinx::Index::Builder.generate(Person) do
294
+ indexes tags(:name), :as => :tags, :facet => true
295
+ end
296
+
297
+ @source = @index.sources.first
298
+ end
299
+
300
+ after :each do
301
+ Person.sphinx_facets.delete_at(-1)
302
+ end
303
+
304
+ it "should have one field" do
305
+ @source.fields.length.should == 1
306
+ end
307
+
308
+ it "should have one attribute alongside the four internal ones" do
309
+ @source.attributes.length.should == 5
310
+ end
311
+
312
+ it "should set the attribute name to have the _facet suffix" do
313
+ @source.attributes.last.unique_name.should == :tags_facet
314
+ end
315
+
316
+ it "should set the attribute type to multi" do
317
+ @source.attributes.last.type.should == :multi
318
+ end
319
+
320
+ it "should set the attribute column to be the same as the field" do
321
+ @source.attributes.last.columns.length.should == 1
322
+ @source.attributes.last.columns.first.__name.should == :name
323
+ end
324
+ end
325
+
326
+ describe "no fields" do
327
+ it "should raise an exception" do
328
+ lambda {
329
+ ThinkingSphinx::Index::Builder.generate(Person) do
330
+ #
331
+ end
332
+ }.should raise_error
333
+ end
334
+ end
335
+
336
+ describe "explicit source" do
337
+ before :each do
338
+ @index = ThinkingSphinx::Index::Builder.generate(Person) do
339
+ define_source do
340
+ indexes first_name, last_name
341
+ has birthday
342
+ has id, :as => :internal_id
343
+
344
+ set_property :delta => true
345
+
346
+ where "birthday <= NOW()"
347
+ group_by "first_name"
348
+ end
349
+ end
350
+
351
+ @source = @index.sources.first
352
+ end
353
+
354
+ it "should return an index" do
355
+ @index.should be_a_kind_of(ThinkingSphinx::Index)
356
+ end
357
+
358
+ it "should have one source for the index" do
359
+ @index.sources.length.should == 1
360
+ end
361
+
362
+ it "should have two fields" do
363
+ @source.fields.length.should == 2
364
+ @source.fields[0].unique_name.should == :first_name
365
+ @source.fields[1].unique_name.should == :last_name
366
+ end
367
+
368
+ it "should have two attributes alongside the four internal ones" do
369
+ @source.attributes.length.should == 6
370
+ @source.attributes[4].unique_name.should == :birthday
371
+ @source.attributes[5].unique_name.should == :internal_id
372
+ end
373
+ end
374
+
375
+ describe "multiple sources" do
376
+ before :each do
377
+ @index = ThinkingSphinx::Index::Builder.generate(Person) do
378
+ define_source do
379
+ indexes first_name
380
+ has birthday
381
+ end
382
+
383
+ define_source do
384
+ indexes last_name
385
+ has :id, :as => :internal_id
386
+ end
387
+ end
388
+ end
389
+
390
+ it "should have two sources" do
391
+ @index.sources.length.should == 2
392
+ end
393
+
394
+ it "should have two fields" do
395
+ @index.fields.length.should == 2
396
+ end
397
+
398
+ it "should have one field in each source" do
399
+ @index.sources.each do |source|
400
+ source.fields.length.should == 1
401
+ end
402
+ end
403
+
404
+ it "should have two attributes alongside the eight internal ones" do
405
+ @index.attributes.length.should == 10
406
+ end
407
+
408
+ it "should have one attribute in each source alongside the four internal ones" do
409
+ @index.sources.each do |source|
410
+ source.attributes.length.should == 5
411
+ end
412
+ end
413
+ end
414
+
415
+ describe "index options" do
416
+ before :each do
417
+ @index = ThinkingSphinx::Index::Builder.generate(Person) do
418
+ indexes first_name
419
+
420
+ set_property :charset_type => "utf16"
421
+ set_property :group_concat_max_len => 1024
422
+ end
423
+ end
424
+
425
+ it "should store the index setting for the index" do
426
+ @index.local_options[:charset_type].should == "utf16"
427
+ end
428
+
429
+ it "should store non-Sphinx settings for the index" do
430
+ @index.local_options[:group_concat_max_len].should == 1024
431
+ end
432
+ end
433
+
434
+ describe "delta options" do
435
+ before :each do
436
+ @index = ThinkingSphinx::Index::Builder.generate(Person) do
437
+ indexes first_name
438
+
439
+ set_property :delta => true
440
+ end
441
+ end
442
+
443
+ it "should not keep the delta setting in source options" do
444
+ @index.sources.first.options.should be_empty
445
+ end
446
+
447
+ it "should not keep the delta setting in index options" do
448
+ @index.local_options.should be_empty
449
+ end
450
+
451
+ it "should set the index delta object set" do
452
+ @index.delta_object.should be_a_kind_of(ThinkingSphinx::Deltas::DefaultDelta)
453
+ end
454
+ end
455
+
456
+ context 'index options' do
457
+ before :each do
458
+ @index = ThinkingSphinx::Index::Builder.generate(Person) do
459
+ indexes first_name
460
+
461
+ set_property :index_exact_words => true
462
+ end
463
+ end
464
+
465
+ it "should track the index_exact_words option to the index" do
466
+ @index.local_options[:index_exact_words].should be_true
467
+ end
468
+ end
469
+
470
+ context 'with an explicit name' do
471
+ it "should set the index's name using the provided value" do
472
+ index = ThinkingSphinx::Index::Builder.generate(Person, 'custom') do
473
+ indexes first_name
474
+ end
475
+
476
+ index.name.should == 'custom'
477
+ end
478
+ end
479
+ end