DrMark-thinking-sphinx 0.9.9 → 1.1.6

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 (77) hide show
  1. data/README +64 -2
  2. data/lib/thinking_sphinx.rb +88 -11
  3. data/lib/thinking_sphinx/active_record.rb +136 -21
  4. data/lib/thinking_sphinx/active_record/delta.rb +43 -62
  5. data/lib/thinking_sphinx/active_record/has_many_association.rb +1 -1
  6. data/lib/thinking_sphinx/active_record/search.rb +7 -0
  7. data/lib/thinking_sphinx/adapters/abstract_adapter.rb +42 -0
  8. data/lib/thinking_sphinx/adapters/mysql_adapter.rb +54 -0
  9. data/lib/thinking_sphinx/adapters/postgresql_adapter.rb +130 -0
  10. data/lib/thinking_sphinx/association.rb +17 -0
  11. data/lib/thinking_sphinx/attribute.rb +171 -97
  12. data/lib/thinking_sphinx/collection.rb +126 -2
  13. data/lib/thinking_sphinx/configuration.rb +120 -171
  14. data/lib/thinking_sphinx/core/string.rb +15 -0
  15. data/lib/thinking_sphinx/deltas.rb +27 -0
  16. data/lib/thinking_sphinx/deltas/datetime_delta.rb +50 -0
  17. data/lib/thinking_sphinx/deltas/default_delta.rb +67 -0
  18. data/lib/thinking_sphinx/deltas/delayed_delta.rb +25 -0
  19. data/lib/thinking_sphinx/deltas/delayed_delta/delta_job.rb +24 -0
  20. data/lib/thinking_sphinx/deltas/delayed_delta/flag_as_deleted_job.rb +27 -0
  21. data/lib/thinking_sphinx/deltas/delayed_delta/job.rb +26 -0
  22. data/lib/thinking_sphinx/facet.rb +58 -0
  23. data/lib/thinking_sphinx/facet_collection.rb +60 -0
  24. data/lib/thinking_sphinx/field.rb +18 -52
  25. data/lib/thinking_sphinx/index.rb +246 -199
  26. data/lib/thinking_sphinx/index/builder.rb +85 -16
  27. data/lib/thinking_sphinx/rails_additions.rb +85 -5
  28. data/lib/thinking_sphinx/search.rb +459 -190
  29. data/lib/thinking_sphinx/tasks.rb +128 -0
  30. data/spec/unit/thinking_sphinx/active_record/delta_spec.rb +53 -124
  31. data/spec/unit/thinking_sphinx/active_record/has_many_association_spec.rb +2 -2
  32. data/spec/unit/thinking_sphinx/active_record_spec.rb +110 -30
  33. data/spec/unit/thinking_sphinx/attribute_spec.rb +16 -149
  34. data/spec/unit/thinking_sphinx/collection_spec.rb +14 -0
  35. data/spec/unit/thinking_sphinx/configuration_spec.rb +54 -412
  36. data/spec/unit/thinking_sphinx/core/string_spec.rb +9 -0
  37. data/spec/unit/thinking_sphinx/field_spec.rb +0 -79
  38. data/spec/unit/thinking_sphinx/index/builder_spec.rb +1 -29
  39. data/spec/unit/thinking_sphinx/index/faux_column_spec.rb +1 -39
  40. data/spec/unit/thinking_sphinx/index_spec.rb +78 -226
  41. data/spec/unit/thinking_sphinx/search_spec.rb +29 -228
  42. data/spec/unit/thinking_sphinx_spec.rb +23 -19
  43. data/tasks/distribution.rb +48 -0
  44. data/tasks/rails.rake +1 -0
  45. data/tasks/testing.rb +86 -0
  46. data/vendor/after_commit/LICENSE +20 -0
  47. data/vendor/after_commit/README +16 -0
  48. data/vendor/after_commit/Rakefile +22 -0
  49. data/vendor/after_commit/init.rb +8 -0
  50. data/vendor/after_commit/lib/after_commit.rb +45 -0
  51. data/vendor/after_commit/lib/after_commit/active_record.rb +114 -0
  52. data/vendor/after_commit/lib/after_commit/connection_adapters.rb +103 -0
  53. data/vendor/after_commit/test/after_commit_test.rb +53 -0
  54. data/vendor/delayed_job/lib/delayed/job.rb +251 -0
  55. data/vendor/delayed_job/lib/delayed/message_sending.rb +7 -0
  56. data/vendor/delayed_job/lib/delayed/performable_method.rb +55 -0
  57. data/vendor/delayed_job/lib/delayed/worker.rb +54 -0
  58. data/{lib → vendor/riddle/lib}/riddle.rb +9 -5
  59. data/{lib → vendor/riddle/lib}/riddle/client.rb +6 -26
  60. data/{lib → vendor/riddle/lib}/riddle/client/filter.rb +10 -1
  61. data/{lib → vendor/riddle/lib}/riddle/client/message.rb +0 -0
  62. data/{lib → vendor/riddle/lib}/riddle/client/response.rb +0 -0
  63. data/vendor/riddle/lib/riddle/configuration.rb +33 -0
  64. data/vendor/riddle/lib/riddle/configuration/distributed_index.rb +48 -0
  65. data/vendor/riddle/lib/riddle/configuration/index.rb +142 -0
  66. data/vendor/riddle/lib/riddle/configuration/indexer.rb +19 -0
  67. data/vendor/riddle/lib/riddle/configuration/remote_index.rb +17 -0
  68. data/vendor/riddle/lib/riddle/configuration/searchd.rb +25 -0
  69. data/vendor/riddle/lib/riddle/configuration/section.rb +37 -0
  70. data/vendor/riddle/lib/riddle/configuration/source.rb +23 -0
  71. data/vendor/riddle/lib/riddle/configuration/sql_source.rb +34 -0
  72. data/vendor/riddle/lib/riddle/configuration/xml_source.rb +28 -0
  73. data/vendor/riddle/lib/riddle/controller.rb +44 -0
  74. metadata +63 -10
  75. data/lib/test.rb +0 -46
  76. data/tasks/thinking_sphinx_tasks.rake +0 -1
  77. data/tasks/thinking_sphinx_tasks.rb +0 -86
@@ -15,154 +15,6 @@ describe ThinkingSphinx::Attribute do
15
15
  end
16
16
  end
17
17
 
18
- describe "to_select_sql method with MySQL" do
19
- before :each do
20
- @index = Person.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.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
18
  describe "unique_name method" do
167
19
  before :each do
168
20
  @attribute = ThinkingSphinx::Attribute.new [
@@ -305,7 +157,7 @@ describe ThinkingSphinx::Attribute do
305
157
  end
306
158
 
307
159
  it "should return :string if there's more than one association" do
308
- @attribute.associations = {:a => :assoc, :b => :assoc}
160
+ @attribute.associations = {:a => [:assoc], :b => [:assoc]}
309
161
  @attribute.send(:type).should == :string
310
162
  end
311
163
 
@@ -357,4 +209,19 @@ describe ThinkingSphinx::Attribute do
357
209
  attribute.send(:all_ints?).should be_false
358
210
  end
359
211
  end
212
+
213
+ describe "with custom queries" do
214
+ before :each do
215
+ index = CricketTeam.sphinx_indexes.first
216
+ @statement = index.to_riddle_for_core(0, 0).sql_attr_multi.first
217
+ end
218
+
219
+ it "should track the query type accordingly" do
220
+ @statement.should match(/uint tags from query/)
221
+ end
222
+
223
+ it "should include the SQL statement" do
224
+ @statement.should match(/SELECT cricket_team_id, id FROM tags/)
225
+ end
226
+ end
360
227
  end
@@ -0,0 +1,14 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe ThinkingSphinx::Collection do
4
+ it "should behave like WillPaginate::Collection" do
5
+ ThinkingSphinx::Collection.instance_methods.should include("previous_page")
6
+ ThinkingSphinx::Collection.instance_methods.should include("next_page")
7
+ ThinkingSphinx::Collection.instance_methods.should include("current_page")
8
+ ThinkingSphinx::Collection.instance_methods.should include("total_pages")
9
+ ThinkingSphinx::Collection.instance_methods.should include("total_entries")
10
+ ThinkingSphinx::Collection.instance_methods.should include("offset")
11
+
12
+ ThinkingSphinx::Collection.ancestors.should include(Array)
13
+ end
14
+ end
@@ -5,17 +5,16 @@ describe ThinkingSphinx::Configuration do
5
5
  before :each do
6
6
  ThinkingSphinx::Configuration.send(:class_variable_set, :@@environment, nil)
7
7
 
8
- ENV["RAILS_ENV"] = nil
9
- ENV["MERB_ENV"] = nil
8
+ ENV["RAILS_ENV"] = nil
10
9
  end
11
10
 
12
11
  it "should use the Merb environment value if set" do
13
12
  unless defined?(Merb)
14
13
  module Merb; end
15
14
  end
16
-
15
+
17
16
  ThinkingSphinx::Configuration.stub_method(:defined? => true)
18
- ENV["MERB_ENV"] = "merb_production"
17
+ Merb.stub_method(:environment => "merb_production")
19
18
  ThinkingSphinx::Configuration.environment.should == "merb_production"
20
19
 
21
20
  Object.send(:remove_const, :Merb)
@@ -31,178 +30,6 @@ describe ThinkingSphinx::Configuration do
31
30
  end
32
31
  end
33
32
 
34
- describe "environment instance method" do
35
- it "should return the class method" do
36
- ThinkingSphinx::Configuration.stub_method(:environment => "spec")
37
- ThinkingSphinx::Configuration.new.environment.should == "spec"
38
- ThinkingSphinx::Configuration.should have_received(:environment)
39
- end
40
- end
41
-
42
- describe "build method" do
43
- before :each do
44
- @config = ThinkingSphinx::Configuration.new
45
-
46
- @config.stub_methods(
47
- :load_models => "",
48
- :core_index_for_model => "",
49
- :delta_index_for_model => "",
50
- :distributed_index_for_model => "",
51
- :create_array_accum => true
52
- )
53
-
54
- ThinkingSphinx.stub_method :indexed_models => ["Person", "Friendship"]
55
- YAML.stub_method(:load => {
56
- :development => {
57
- "option" => "value"
58
- }
59
- })
60
-
61
- @person_index_a = ThinkingSphinx::Index.stub_instance(
62
- :to_config => "", :adapter => :mysql, :delta? => false,
63
- :name => "person", :model => Person
64
- )
65
- @person_index_b = ThinkingSphinx::Index.stub_instance(
66
- :to_config => "", :adapter => :mysql, :delta? => false,
67
- :name => "person", :model => Person
68
- )
69
- @friendship_index_a = ThinkingSphinx::Index.stub_instance(
70
- :to_config => "", :adapter => :mysql, :delta? => false,
71
- :name => "friendship", :model => Friendship
72
- )
73
-
74
- Person.stub_method(:indexes => [@person_index_a, @person_index_b])
75
- Friendship.stub_method(:indexes => [@friendship_index_a])
76
-
77
- FileUtils.mkdir_p "#{@config.app_root}/config"
78
- FileUtils.touch "#{@config.app_root}/config/database.yml"
79
- end
80
-
81
- after :each do
82
- ThinkingSphinx.unstub_method :indexed_models
83
- YAML.unstub_method :load
84
-
85
- Person.unstub_method :indexes
86
- Friendship.unstub_method :indexes
87
- #
88
- # FileUtils.rm_rf "#{@config.app_root}/config"
89
- end
90
-
91
- it "should load the models" do
92
- @config.build
93
-
94
- @config.should have_received(:load_models)
95
- end
96
-
97
- it "should load in the database YAML configuration" do
98
- @config.build
99
-
100
- YAML.should have_received(:load)
101
- end
102
-
103
- it "should set the mem limit based on the configuration" do
104
- @config.build
105
-
106
- file = open(@config.config_file) { |f| f.read }
107
- file.should match(/mem_limit\s+= #{@config.mem_limit}/)
108
- end
109
-
110
- it "should use the configuration port" do
111
- @config.build
112
-
113
- file = open(@config.config_file) { |f| f.read }
114
- file.should match(/port\s+= #{@config.port}/)
115
- end
116
-
117
- it "should use the configuration's log file locations" do
118
- @config.build
119
-
120
- file = open(@config.config_file) { |f| f.read }
121
- file.should match(/log\s+= #{@config.searchd_log_file}/)
122
- file.should match(/query_log\s+= #{@config.query_log_file}/)
123
- end
124
-
125
- it "should use the configuration's pid file location" do
126
- @config.build
127
-
128
- file = open(@config.config_file) { |f| f.read }
129
- file.should match(/pid_file\s+= #{@config.pid_file}/)
130
- end
131
-
132
- it "should set max matches from configuration" do
133
- @config.build
134
-
135
- file = open(@config.config_file) { |f| f.read }
136
- file.should match(/max_matches\s+= #{@config.max_matches}/)
137
- end
138
-
139
- it "should request configuration for each index for each model" do
140
- @config.build
141
-
142
- @person_index_a.should have_received(:to_config).with(
143
- Person, 0, {:option => "value"}, @config.charset_type, 0
144
- )
145
- @person_index_b.should have_received(:to_config).with(
146
- Person, 1, {:option => "value"}, @config.charset_type, 0
147
- )
148
- @friendship_index_a.should have_received(:to_config).with(
149
- Friendship, 0, {:option => "value"}, @config.charset_type, 1
150
- )
151
- end
152
-
153
- it "should call create_array_accum if any index uses postgres" do
154
- @person_index_a.stub_method(:adapter => :postgres)
155
-
156
- @config.build
157
-
158
- @config.should have_received(:create_array_accum)
159
- end
160
-
161
- it "should not call create_array_accum if no index uses postgres" do
162
- @config.build
163
-
164
- @config.should_not have_received(:create_array_accum)
165
- end
166
-
167
- it "should call core_index_for_model for each model" do
168
- @config.build
169
-
170
- @config.should have_received(:core_index_for_model).with(
171
- Person, "source = person_0_core\nsource = person_1_core"
172
- )
173
- @config.should have_received(:core_index_for_model).with(
174
- Friendship, "source = friendship_0_core"
175
- )
176
- end
177
-
178
- it "should call delta_index_for_model for each model if any index has a delta" do
179
- @person_index_b.stub_method(:delta? => true)
180
-
181
- @config.build
182
-
183
- @config.should have_received(:delta_index_for_model).with(
184
- Person, "source = person_1_delta"
185
- )
186
- end
187
-
188
- it "should not call delta_index_for_model for each model if no indexes have deltas" do
189
- @config.build
190
-
191
- @config.should_not have_received(:delta_index_for_model)
192
- end
193
-
194
- it "should call distributed_index_for_model for each model" do
195
- @config.build
196
-
197
- @config.should have_received(:distributed_index_for_model).with(Person)
198
- @config.should have_received(:distributed_index_for_model).with(Friendship)
199
- end
200
- end
201
-
202
- describe "load_models method" do
203
- it "should have some specs"
204
- end
205
-
206
33
  describe "parse_config method" do
207
34
  before :each do
208
35
  @settings = {
@@ -214,7 +41,6 @@ describe ThinkingSphinx::Configuration do
214
41
  "searchd_file_path" => "searchd/file/path",
215
42
  "address" => "127.0.0.1",
216
43
  "port" => 3333,
217
- "allow_star" => true,
218
44
  "min_prefix_len" => 2,
219
45
  "min_infix_len" => 3,
220
46
  "mem_limit" => "128M",
@@ -232,9 +58,12 @@ describe ThinkingSphinx::Configuration do
232
58
  end
233
59
 
234
60
  it "should use the accessors to set the configuration values" do
235
- config = ThinkingSphinx::Configuration.new
236
- @settings["development"].each do |key, value|
237
- config.send(key).should == value
61
+ config = ThinkingSphinx::Configuration.instance
62
+ config.send(:parse_config)
63
+
64
+ %w(config_file searchd_log_file query_log_file pid_file searchd_file_path
65
+ address port).each do |key|
66
+ config.send(key).should == @settings["development"][key]
238
67
  end
239
68
  end
240
69
 
@@ -242,253 +71,66 @@ describe ThinkingSphinx::Configuration do
242
71
  FileUtils.rm "#{RAILS_ROOT}/config/sphinx.yml"
243
72
  end
244
73
  end
245
-
246
- describe "core_index_for_model method" do
247
- before :each do
248
- @config = ThinkingSphinx::Configuration.new
249
- @model = Person
250
- end
251
-
252
- it "should take its name from the model, with _core appended" do
253
- @config.send(:core_index_for_model, @model, "my sources").should match(
254
- /index person_core/
255
- )
256
- end
257
-
258
- it "should set the path to follow the name" do
259
- @config.searchd_file_path = "/my/file/path"
260
- @config.send(:core_index_for_model, @model, "my sources").should match(
261
- /path = \/my\/file\/path\/person_core/
262
- )
263
- end
264
-
265
- it "should include the charset type setting" do
266
- @config.charset_type = "specchars"
267
- @config.send(:core_index_for_model, @model, "my sources").should match(
268
- /charset_type = specchars/
269
- )
270
- end
271
-
272
- it "should include the morphology setting if it isn't blank" do
273
- @config.morphology = "morph"
274
- @config.send(:core_index_for_model, @model, "my sources").should match(
275
- /morphology\s+= morph/
276
- )
277
- end
278
-
279
- it "should not include the morphology setting if it is blank" do
280
- @config.morphology = nil
281
- @config.send(:core_index_for_model, @model, "my sources").should_not match(
282
- /morphology\s+=/
283
- )
284
-
285
- @config.morphology = ""
286
- @config.send(:core_index_for_model, @model, "my sources").should_not match(
287
- /morphology\s+=/
288
- )
289
- end
290
-
291
- it "should include the charset_table value if it isn't nil" do
292
- @config.charset_table = "table_chars"
293
- @config.send(:core_index_for_model, @model, "my sources").should match(
294
- /charset_table\s+= table_chars/
295
- )
296
- end
297
-
298
- it "should not set the charset_table value if it is nil" do
299
- @config.charset_table = nil
300
- @config.send(:core_index_for_model, @model, "my sources").should_not match(
301
- /charset_table\s+=/
302
- )
303
- end
304
-
305
- it "should set the ignore_chars value if it isn't nil" do
306
- @config.ignore_chars = "ignorable"
307
- @config.send(:core_index_for_model, @model, "my sources").should match(
308
- /ignore_chars\s+= ignorable/
309
- )
310
- end
311
-
312
- it "should not set the ignore_chars value if it is nil" do
313
- @config.ignore_chars = nil
314
- @config.send(:core_index_for_model, @model, "my sources").should_not match(
315
- /ignore_chars\s+=/
316
- )
317
- end
318
74
 
319
- it "should include the star-related settings when allow_star is true" do
320
- @config.allow_star = true
321
- @config.min_prefix_len = 1
322
- text = @config.send(:core_index_for_model, @model, "my sources")
323
-
324
- text.should match(/enable_star\s+= 1/)
325
- text.should match(/min_prefix_len\s+= 1/)
326
- # text.should match(/min_infix_len\s+= 1/)
327
- end
328
-
329
- it "should use the configuration's infix and prefix length values if set" do
330
- @config.allow_star = true
331
- @config.min_prefix_len = 3
332
- @config.min_infix_len = 2
333
- text = @config.send(:core_index_for_model, @model, "my sources")
334
-
335
- text.should match(/min_prefix_len\s+= 3/)
336
- # text.should match(/min_infix_len\s+= 2/)
75
+ describe "initialisation" do
76
+ it "should have a default bin_path of nothing" do
77
+ ThinkingSphinx::Configuration.instance.bin_path.should == ""
337
78
  end
338
79
 
339
- it "should not include the star-related settings when allow_star is false" do
340
- @config.allow_star = false
341
- text = @config.send(:core_index_for_model, @model, "my sources")
342
-
343
- text.should_not match(/enable_star\s+=/)
344
- text.should_not match(/min_prefix_len\s+=/)
345
- text.should_not match(/min_infix_len\s+=/)
346
- end
347
-
348
- it "should set prefix_fields if any fields are flagged explicitly" do
349
- @model.indexes.first.stub_methods(
350
- :prefix_fields => [
351
- ThinkingSphinx::Field.stub_instance(:unique_name => "a"),
352
- ThinkingSphinx::Field.stub_instance(:unique_name => "b"),
353
- ThinkingSphinx::Field.stub_instance(:unique_name => "c")
354
- ],
355
- :infix_fields => [
356
- ThinkingSphinx::Field.stub_instance(:unique_name => "d"),
357
- ThinkingSphinx::Field.stub_instance(:unique_name => "e"),
358
- ThinkingSphinx::Field.stub_instance(:unique_name => "f")
359
- ]
360
- )
80
+ it "should append a / to bin_path if one is supplied" do
81
+ @settings = {
82
+ "development" => {
83
+ "bin_path" => "path/to/somewhere"
84
+ }
85
+ }
361
86
 
362
- @config.send(:core_index_for_model, @model, "my sources").should match(
363
- /prefix_fields\s+= a, b, c/
364
- )
365
- end
366
-
367
- it "shouldn't set prefix_fields if none are flagged explicitly" do
368
- @config.send(:core_index_for_model, @model, "my sources").should_not match(
369
- /prefix_fields\s+=/
370
- )
371
- end
372
-
373
- it "should set infix_fields if any fields are flagged explicitly" do
374
- @model.indexes.first.stub_methods(
375
- :prefix_fields => [
376
- ThinkingSphinx::Field.stub_instance(:unique_name => "a"),
377
- ThinkingSphinx::Field.stub_instance(:unique_name => "b"),
378
- ThinkingSphinx::Field.stub_instance(:unique_name => "c")
379
- ],
380
- :infix_fields => [
381
- ThinkingSphinx::Field.stub_instance(:unique_name => "d"),
382
- ThinkingSphinx::Field.stub_instance(:unique_name => "e"),
383
- ThinkingSphinx::Field.stub_instance(:unique_name => "f")
384
- ]
385
- )
87
+ open("#{RAILS_ROOT}/config/sphinx.yml", "w") do |f|
88
+ f.write YAML.dump(@settings)
89
+ end
386
90
 
387
- @config.send(:core_index_for_model, @model, "my sources").should match(
388
- /infix_fields\s+= d, e, f/
389
- )
390
- end
391
-
392
- it "shouldn't set infix_fields if none are flagged explicitly" do
393
- @config.send(:core_index_for_model, @model, "my sources").should_not match(
394
- /infix_fields\s+=/
395
- )
396
- end
397
-
398
- it "should include html_strip if value is set" do
399
- @config.html_strip = 1
400
- text = @config.send(:core_index_for_model, @model, "my sources")
401
- text.should match(/html_strip\s+= 1/)
402
- end
403
-
404
- it "shouldn't include html_strip if value is not set" do
405
- text = @config.send(:core_index_for_model, @model, "my sources")
406
- text.should_not match(/html_strip/)
407
- end
408
-
409
- it "should include html_remove_elements if values are set" do
410
- @config.html_remove_elements = 'script'
411
- text = @config.send(:core_index_for_model, @model, "my sources")
412
- text.should match(/html_remove_elements\s+= script/)
413
- end
414
-
415
- it "shouldn't include html_remove_elements if no values are set" do
416
- text = @config.send(:core_index_for_model, @model, "my sources")
417
- text.should_not match(/html_remove_elements/)
91
+ ThinkingSphinx::Configuration.instance.send(:parse_config)
92
+ ThinkingSphinx::Configuration.instance.bin_path.should match(/\/$/)
418
93
  end
419
94
  end
420
95
 
421
- describe "delta_index_for_model method" do
422
- before :each do
423
- @config = ThinkingSphinx::Configuration.new
424
- @model = Person
425
- end
426
-
427
- it "should take its name from the model, with _delta appended" do
428
- @config.send(:delta_index_for_model, @model, "delta_sources").should match(
429
- /index person_delta/
430
- )
431
- end
432
-
433
- it "should inherit from the equivalent core index" do
434
- @config.send(:delta_index_for_model, @model, "delta_sources").should match(
435
- /index person_delta : person_core/
436
- )
437
- end
438
-
439
- it "should set the path to follow the name" do
440
- @config.searchd_file_path = "/my/file/path"
441
- @config.send(:delta_index_for_model, @model, "delta_sources").should match(
442
- /path = \/my\/file\/path\/person_delta/
443
- )
96
+ it "should insert set index options into the configuration file" do
97
+ config = ThinkingSphinx::Configuration.instance
98
+ ThinkingSphinx::Configuration::IndexOptions.each do |option|
99
+ config.index_options[option.to_sym] = "something"
100
+ config.build
101
+
102
+ file = open(config.config_file) { |f| f.read }
103
+ file.should match(/#{option}\s+= something/)
104
+
105
+ config.index_options[option.to_sym] = nil
444
106
  end
445
107
  end
446
108
 
447
- describe "distributed_index_for_model method" do
448
- before :each do
449
- @config = ThinkingSphinx::Configuration.new
450
- @model = Person
451
- end
452
-
453
- it "should take its name from the model" do
454
- @config.send(:distributed_index_for_model, @model).should match(
455
- /index person/
456
- )
457
- end
458
-
459
- it "should have a type of distributed" do
460
- @config.send(:distributed_index_for_model, @model).should match(
461
- /type = distributed/
462
- )
463
- end
464
-
465
- it "should include the core as a local source" do
466
- @config.send(:distributed_index_for_model, @model).should match(
467
- /local = person_core/
468
- )
469
- end
470
-
471
- it "should only include the delta as a local source if an index is flagged to be delta" do
472
- @config.send(:distributed_index_for_model, @model).should_not match(
473
- /local = person_delta/
474
- )
109
+ it "should insert set source options into the configuration file" do
110
+ config = ThinkingSphinx::Configuration.instance
111
+ ThinkingSphinx::Configuration::SourceOptions.each do |option|
112
+ config.source_options[option.to_sym] = "something"
113
+ config.build
475
114
 
476
- @model.indexes.first.stub_method(:delta? => true)
477
- @config.send(:distributed_index_for_model, @model).should match(
478
- /local = person_delta/
479
- )
480
- end
481
-
482
- it "should handle namespaced models correctly" do
483
- Person.stub_method(:name => "Namespaced::Model")
115
+ file = open(config.config_file) { |f| f.read }
116
+ file.should match(/#{option}\s+= something/)
484
117
 
485
- @config.send(:distributed_index_for_model, @model).should match(
486
- /index namespaced_model/
487
- )
118
+ config.source_options[option.to_sym] = nil
488
119
  end
489
120
  end
490
121
 
491
- describe "create_array_accum method" do
492
- it "should create the array_accum method on PostgreSQL"
122
+ it "should set any explicit prefixed or infixed fields" do
123
+ file = open(ThinkingSphinx::Configuration.instance.config_file) { |f|
124
+ f.read
125
+ }
126
+ file.should match(/prefix_fields\s+= city/)
127
+ file.should match(/infix_fields\s+= state/)
128
+ end
129
+
130
+ it "should not have prefix fields in indexes where nothing is set" do
131
+ file = open(ThinkingSphinx::Configuration.instance.config_file) { |f|
132
+ f.read
133
+ }
134
+ file.should_not match(/index alpha_core\s+\{\s+[^\}]*prefix_fields\s+=[^\}]*\}/m)
493
135
  end
494
136
  end