sayso-thinking-sphinx 2.0.3.001

Sign up to get free protection for your applications and to get access to all the features.
Files changed (164) hide show
  1. data/LICENCE +20 -0
  2. data/README.textile +251 -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 +77 -0
  8. data/features/deleting_instances.feature +67 -0
  9. data/features/direct_attributes.feature +11 -0
  10. data/features/excerpts.feature +21 -0
  11. data/features/extensible_delta_indexing.feature +9 -0
  12. data/features/facets.feature +88 -0
  13. data/features/facets_across_model.feature +29 -0
  14. data/features/field_sorting.feature +18 -0
  15. data/features/handling_edits.feature +94 -0
  16. data/features/retry_stale_indexes.feature +24 -0
  17. data/features/searching_across_models.feature +20 -0
  18. data/features/searching_by_index.feature +40 -0
  19. data/features/searching_by_model.feature +168 -0
  20. data/features/searching_with_find_arguments.feature +56 -0
  21. data/features/sphinx_detection.feature +25 -0
  22. data/features/sphinx_scopes.feature +68 -0
  23. data/features/step_definitions/alpha_steps.rb +16 -0
  24. data/features/step_definitions/beta_steps.rb +7 -0
  25. data/features/step_definitions/common_steps.rb +197 -0
  26. data/features/step_definitions/extensible_delta_indexing_steps.rb +7 -0
  27. data/features/step_definitions/facet_steps.rb +96 -0
  28. data/features/step_definitions/find_arguments_steps.rb +36 -0
  29. data/features/step_definitions/gamma_steps.rb +15 -0
  30. data/features/step_definitions/scope_steps.rb +19 -0
  31. data/features/step_definitions/search_steps.rb +94 -0
  32. data/features/step_definitions/sphinx_steps.rb +35 -0
  33. data/features/sti_searching.feature +19 -0
  34. data/features/support/env.rb +27 -0
  35. data/features/support/lib/generic_delta_handler.rb +8 -0
  36. data/features/thinking_sphinx/database.example.yml +3 -0
  37. data/features/thinking_sphinx/db/fixtures/alphas.rb +10 -0
  38. data/features/thinking_sphinx/db/fixtures/authors.rb +1 -0
  39. data/features/thinking_sphinx/db/fixtures/betas.rb +11 -0
  40. data/features/thinking_sphinx/db/fixtures/boxes.rb +9 -0
  41. data/features/thinking_sphinx/db/fixtures/categories.rb +1 -0
  42. data/features/thinking_sphinx/db/fixtures/cats.rb +3 -0
  43. data/features/thinking_sphinx/db/fixtures/comments.rb +24 -0
  44. data/features/thinking_sphinx/db/fixtures/developers.rb +31 -0
  45. data/features/thinking_sphinx/db/fixtures/dogs.rb +3 -0
  46. data/features/thinking_sphinx/db/fixtures/extensible_betas.rb +10 -0
  47. data/features/thinking_sphinx/db/fixtures/foxes.rb +3 -0
  48. data/features/thinking_sphinx/db/fixtures/gammas.rb +10 -0
  49. data/features/thinking_sphinx/db/fixtures/music.rb +4 -0
  50. data/features/thinking_sphinx/db/fixtures/people.rb +1001 -0
  51. data/features/thinking_sphinx/db/fixtures/posts.rb +6 -0
  52. data/features/thinking_sphinx/db/fixtures/robots.rb +14 -0
  53. data/features/thinking_sphinx/db/fixtures/tags.rb +27 -0
  54. data/features/thinking_sphinx/db/migrations/create_alphas.rb +8 -0
  55. data/features/thinking_sphinx/db/migrations/create_animals.rb +5 -0
  56. data/features/thinking_sphinx/db/migrations/create_authors.rb +3 -0
  57. data/features/thinking_sphinx/db/migrations/create_authors_posts.rb +6 -0
  58. data/features/thinking_sphinx/db/migrations/create_betas.rb +5 -0
  59. data/features/thinking_sphinx/db/migrations/create_boxes.rb +5 -0
  60. data/features/thinking_sphinx/db/migrations/create_categories.rb +3 -0
  61. data/features/thinking_sphinx/db/migrations/create_comments.rb +10 -0
  62. data/features/thinking_sphinx/db/migrations/create_developers.rb +7 -0
  63. data/features/thinking_sphinx/db/migrations/create_extensible_betas.rb +5 -0
  64. data/features/thinking_sphinx/db/migrations/create_gammas.rb +3 -0
  65. data/features/thinking_sphinx/db/migrations/create_genres.rb +3 -0
  66. data/features/thinking_sphinx/db/migrations/create_music.rb +6 -0
  67. data/features/thinking_sphinx/db/migrations/create_people.rb +13 -0
  68. data/features/thinking_sphinx/db/migrations/create_posts.rb +5 -0
  69. data/features/thinking_sphinx/db/migrations/create_robots.rb +4 -0
  70. data/features/thinking_sphinx/db/migrations/create_taggings.rb +5 -0
  71. data/features/thinking_sphinx/db/migrations/create_tags.rb +4 -0
  72. data/features/thinking_sphinx/models/alpha.rb +23 -0
  73. data/features/thinking_sphinx/models/andrew.rb +17 -0
  74. data/features/thinking_sphinx/models/animal.rb +5 -0
  75. data/features/thinking_sphinx/models/author.rb +3 -0
  76. data/features/thinking_sphinx/models/beta.rb +13 -0
  77. data/features/thinking_sphinx/models/box.rb +8 -0
  78. data/features/thinking_sphinx/models/cat.rb +3 -0
  79. data/features/thinking_sphinx/models/category.rb +4 -0
  80. data/features/thinking_sphinx/models/comment.rb +10 -0
  81. data/features/thinking_sphinx/models/developer.rb +20 -0
  82. data/features/thinking_sphinx/models/dog.rb +3 -0
  83. data/features/thinking_sphinx/models/extensible_beta.rb +9 -0
  84. data/features/thinking_sphinx/models/fox.rb +5 -0
  85. data/features/thinking_sphinx/models/gamma.rb +5 -0
  86. data/features/thinking_sphinx/models/genre.rb +3 -0
  87. data/features/thinking_sphinx/models/medium.rb +5 -0
  88. data/features/thinking_sphinx/models/music.rb +8 -0
  89. data/features/thinking_sphinx/models/person.rb +24 -0
  90. data/features/thinking_sphinx/models/post.rb +21 -0
  91. data/features/thinking_sphinx/models/robot.rb +12 -0
  92. data/features/thinking_sphinx/models/tag.rb +3 -0
  93. data/features/thinking_sphinx/models/tagging.rb +4 -0
  94. data/lib/cucumber/thinking_sphinx/external_world.rb +12 -0
  95. data/lib/cucumber/thinking_sphinx/internal_world.rb +127 -0
  96. data/lib/cucumber/thinking_sphinx/sql_logger.rb +20 -0
  97. data/lib/thinking-sphinx.rb +1 -0
  98. data/lib/thinking_sphinx.rb +301 -0
  99. data/lib/thinking_sphinx/action_controller.rb +31 -0
  100. data/lib/thinking_sphinx/active_record.rb +352 -0
  101. data/lib/thinking_sphinx/active_record/attribute_updates.rb +52 -0
  102. data/lib/thinking_sphinx/active_record/delta.rb +92 -0
  103. data/lib/thinking_sphinx/active_record/has_many_association.rb +36 -0
  104. data/lib/thinking_sphinx/active_record/has_many_association_with_scopes.rb +21 -0
  105. data/lib/thinking_sphinx/active_record/log_subscriber.rb +61 -0
  106. data/lib/thinking_sphinx/active_record/scopes.rb +93 -0
  107. data/lib/thinking_sphinx/adapters/abstract_adapter.rb +87 -0
  108. data/lib/thinking_sphinx/adapters/mysql_adapter.rb +58 -0
  109. data/lib/thinking_sphinx/adapters/postgresql_adapter.rb +153 -0
  110. data/lib/thinking_sphinx/association.rb +169 -0
  111. data/lib/thinking_sphinx/attribute.rb +389 -0
  112. data/lib/thinking_sphinx/auto_version.rb +38 -0
  113. data/lib/thinking_sphinx/bundled_search.rb +44 -0
  114. data/lib/thinking_sphinx/class_facet.rb +16 -0
  115. data/lib/thinking_sphinx/configuration.rb +355 -0
  116. data/lib/thinking_sphinx/context.rb +76 -0
  117. data/lib/thinking_sphinx/core/string.rb +15 -0
  118. data/lib/thinking_sphinx/deltas.rb +28 -0
  119. data/lib/thinking_sphinx/deltas/default_delta.rb +62 -0
  120. data/lib/thinking_sphinx/deploy/capistrano.rb +101 -0
  121. data/lib/thinking_sphinx/excerpter.rb +23 -0
  122. data/lib/thinking_sphinx/facet.rb +127 -0
  123. data/lib/thinking_sphinx/facet_search.rb +166 -0
  124. data/lib/thinking_sphinx/field.rb +82 -0
  125. data/lib/thinking_sphinx/index.rb +157 -0
  126. data/lib/thinking_sphinx/index/builder.rb +312 -0
  127. data/lib/thinking_sphinx/index/faux_column.rb +118 -0
  128. data/lib/thinking_sphinx/join.rb +37 -0
  129. data/lib/thinking_sphinx/property.rb +185 -0
  130. data/lib/thinking_sphinx/railtie.rb +46 -0
  131. data/lib/thinking_sphinx/search.rb +950 -0
  132. data/lib/thinking_sphinx/search_methods.rb +439 -0
  133. data/lib/thinking_sphinx/source.rb +163 -0
  134. data/lib/thinking_sphinx/source/internal_properties.rb +46 -0
  135. data/lib/thinking_sphinx/source/sql.rb +148 -0
  136. data/lib/thinking_sphinx/tasks.rb +139 -0
  137. data/lib/thinking_sphinx/test.rb +55 -0
  138. data/spec/thinking_sphinx/active_record/delta_spec.rb +128 -0
  139. data/spec/thinking_sphinx/active_record/has_many_association_spec.rb +72 -0
  140. data/spec/thinking_sphinx/active_record/scopes_spec.rb +176 -0
  141. data/spec/thinking_sphinx/active_record_spec.rb +576 -0
  142. data/spec/thinking_sphinx/adapters/abstract_adapter_spec.rb +145 -0
  143. data/spec/thinking_sphinx/association_spec.rb +216 -0
  144. data/spec/thinking_sphinx/attribute_spec.rb +560 -0
  145. data/spec/thinking_sphinx/auto_version_spec.rb +63 -0
  146. data/spec/thinking_sphinx/configuration_spec.rb +288 -0
  147. data/spec/thinking_sphinx/context_spec.rb +128 -0
  148. data/spec/thinking_sphinx/core/array_spec.rb +9 -0
  149. data/spec/thinking_sphinx/core/string_spec.rb +9 -0
  150. data/spec/thinking_sphinx/excerpter_spec.rb +49 -0
  151. data/spec/thinking_sphinx/facet_search_spec.rb +170 -0
  152. data/spec/thinking_sphinx/facet_spec.rb +359 -0
  153. data/spec/thinking_sphinx/field_spec.rb +127 -0
  154. data/spec/thinking_sphinx/index/builder_spec.rb +508 -0
  155. data/spec/thinking_sphinx/index/faux_column_spec.rb +36 -0
  156. data/spec/thinking_sphinx/index_spec.rb +183 -0
  157. data/spec/thinking_sphinx/search_methods_spec.rb +156 -0
  158. data/spec/thinking_sphinx/search_spec.rb +1387 -0
  159. data/spec/thinking_sphinx/source_spec.rb +253 -0
  160. data/spec/thinking_sphinx/test_spec.rb +20 -0
  161. data/spec/thinking_sphinx_spec.rb +203 -0
  162. data/tasks/distribution.rb +33 -0
  163. data/tasks/testing.rb +80 -0
  164. metadata +509 -0
@@ -0,0 +1,63 @@
1
+ require 'spec_helper'
2
+
3
+ describe ThinkingSphinx::AutoVersion do
4
+ describe '.detect' do
5
+ before :each do
6
+ @config = ThinkingSphinx::Configuration.instance
7
+ end
8
+
9
+ it "should require 0.9.8 if that is the detected version" do
10
+ ThinkingSphinx::AutoVersion.should_receive(:require).
11
+ with('riddle/0.9.8')
12
+
13
+ @config.stub!(:version => '0.9.8')
14
+ ThinkingSphinx::AutoVersion.detect
15
+ end
16
+
17
+ it "should require 0.9.9 if that is the detected version" do
18
+ ThinkingSphinx::AutoVersion.should_receive(:require).
19
+ with('riddle/0.9.9')
20
+
21
+ @config.stub!(:version => '0.9.9')
22
+ ThinkingSphinx::AutoVersion.detect
23
+ end
24
+
25
+ it "should require 1.10-beta if that is the detected version" do
26
+ ThinkingSphinx::AutoVersion.should_receive(:require).
27
+ with('riddle/1.10')
28
+
29
+ @config.stub!(:version => '1.10-beta')
30
+ ThinkingSphinx::AutoVersion.detect
31
+ end
32
+
33
+ it "should require 1.10-beta if using 1.10-beta compiled with id64 support" do
34
+ ThinkingSphinx::AutoVersion.should_receive(:require).
35
+ with('riddle/1.10')
36
+
37
+ @config.stub!(:version => '1.10-id64-beta')
38
+ ThinkingSphinx::AutoVersion.detect
39
+ end
40
+
41
+ it "should require 2.0.1 if using Sphinx 2.0.1 beta" do
42
+ ThinkingSphinx::AutoVersion.should_receive(:require).
43
+ with('riddle/2.0.1')
44
+
45
+ @config.stub!(:version => '2.0.1-beta')
46
+ ThinkingSphinx::AutoVersion.detect
47
+ end
48
+
49
+ it "should output a warning if the detected version is unsupported" do
50
+ STDERR.should_receive(:puts).with(/unsupported/i)
51
+
52
+ @config.stub!(:version => '0.9.7')
53
+ ThinkingSphinx::AutoVersion.detect
54
+ end
55
+
56
+ it "should output a warning if the version cannot be determined" do
57
+ STDERR.should_receive(:puts).at_least(:once)
58
+
59
+ @config.stub!(:version => nil)
60
+ ThinkingSphinx::AutoVersion.detect
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,288 @@
1
+ require 'spec_helper'
2
+
3
+ describe ThinkingSphinx::Configuration do
4
+ describe "environment class method" do
5
+ before :each do
6
+ ThinkingSphinx::Configuration.reset_environment
7
+
8
+ ENV["RAILS_ENV"] = nil
9
+ end
10
+
11
+ it "should use Rails.env if set" do
12
+ was = Rails.env
13
+ Rails.env = 'global_rails'
14
+
15
+ ThinkingSphinx::Configuration.environment.should == 'global_rails'
16
+
17
+ Rails.env = was
18
+ end
19
+
20
+ it "should use the Rails environment value if set" do
21
+ Rails.stub!(:env => "rails_production")
22
+ ThinkingSphinx::Configuration.environment.should == "rails_production"
23
+ end
24
+
25
+ it "should default to development" do
26
+ ThinkingSphinx::Configuration.environment.should == "development"
27
+ end
28
+ end
29
+
30
+ describe '#version' do
31
+ before :each do
32
+ @config = ThinkingSphinx::Configuration.instance
33
+ @config.reset
34
+ end
35
+
36
+ it "should use the given version from sphinx.yml if there is one" do
37
+ open("#{Rails.root}/config/sphinx.yml", "w") do |f|
38
+ f.write YAML.dump({'development' => {'version' => '0.9.7'}})
39
+ end
40
+ @config.reset
41
+
42
+ @config.version.should == '0.9.7'
43
+
44
+ FileUtils.rm "#{Rails.root}/config/sphinx.yml"
45
+ end
46
+
47
+ it "should detect the version from Riddle otherwise" do
48
+ controller = @config.controller
49
+ controller.stub!(:sphinx_version => '0.9.6')
50
+
51
+ Riddle::Controller.stub!(:new => controller)
52
+ @config.reset
53
+
54
+ @config.version.should == '0.9.6'
55
+ end
56
+ end
57
+
58
+ describe "parse_config method" do
59
+ before :each do
60
+ @settings = {
61
+ "development" => {
62
+ "config_file" => "tmp/config/development.sphinx.conf",
63
+ "searchd_log_file" => "searchd_log_file.log",
64
+ "query_log_file" => "query_log_file.log",
65
+ "pid_file" => "pid_file.pid",
66
+ "searchd_file_path" => "searchd/file/path",
67
+ "address" => "127.0.0.1",
68
+ "port" => 3333,
69
+ "min_prefix_len" => 2,
70
+ "min_infix_len" => 3,
71
+ "mem_limit" => "128M",
72
+ "max_matches" => 1001,
73
+ "morphology" => "stem_ru",
74
+ "charset_type" => "latin1",
75
+ "charset_table" => "table",
76
+ "ignore_chars" => "e",
77
+ "searchd_binary_name" => "sphinx-searchd",
78
+ "indexer_binary_name" => "sphinx-indexer",
79
+ "index_exact_words" => true,
80
+ "indexed_models" => ['Alpha', 'Beta']
81
+ }
82
+ }
83
+
84
+ open("#{Rails.root}/config/sphinx.yml", "w") do |f|
85
+ f.write YAML.dump(@settings)
86
+ end
87
+ end
88
+
89
+ it "should use the accessors to set the configuration values" do
90
+ config = ThinkingSphinx::Configuration.instance
91
+ config.send(:parse_config)
92
+
93
+ %w(config_file searchd_log_file query_log_file pid_file searchd_file_path
94
+ address port searchd_binary_name indexer_binary_name).each do |key|
95
+ config.send(key).should == @settings["development"][key]
96
+ end
97
+ end
98
+
99
+ after :each do
100
+ FileUtils.rm "#{Rails.root}/config/sphinx.yml"
101
+ end
102
+ end
103
+
104
+ describe "block configuration" do
105
+ it "should let the user set-up a custom app_root" do
106
+ ThinkingSphinx::Configuration.configure do |config|
107
+ config.app_root = "/here/somewhere"
108
+ end
109
+ ThinkingSphinx::Configuration.instance.app_root.should == "/here/somewhere"
110
+
111
+ ThinkingSphinx::Configuration.instance.reset
112
+ end
113
+ end
114
+
115
+ describe "initialisation" do
116
+ it "should have a default bin_path of nothing" do
117
+ ThinkingSphinx::Configuration.instance.bin_path.should == ""
118
+ end
119
+
120
+ it "should append a / to bin_path if one is supplied" do
121
+ @settings = {
122
+ "development" => {
123
+ "bin_path" => "path/to/somewhere"
124
+ }
125
+ }
126
+
127
+ open("#{Rails.root}/config/sphinx.yml", "w") do |f|
128
+ f.write YAML.dump(@settings)
129
+ end
130
+
131
+ ThinkingSphinx::Configuration.instance.send(:parse_config)
132
+ ThinkingSphinx::Configuration.instance.bin_path.should match(/\/$/)
133
+
134
+ FileUtils.rm "#{Rails.root}/config/sphinx.yml"
135
+ end
136
+ end
137
+
138
+ describe "index options" do
139
+ before :each do
140
+ @settings = {
141
+ "development" => {"disable_range" => true}
142
+ }
143
+
144
+ open("#{Rails.root}/config/sphinx.yml", "w") do |f|
145
+ f.write YAML.dump(@settings)
146
+ end
147
+
148
+ @config = ThinkingSphinx::Configuration.instance
149
+ @config.send(:parse_config)
150
+ end
151
+
152
+ it "should collect disable_range" do
153
+ @config.index_options[:disable_range].should be_true
154
+ end
155
+
156
+ after :each do
157
+ FileUtils.rm "#{Rails.root}/config/sphinx.yml"
158
+ end
159
+ end
160
+
161
+ it "should insert set index options into the configuration file" do
162
+ config = ThinkingSphinx::Configuration.instance
163
+
164
+ ThinkingSphinx::Configuration::IndexOptions.each do |option|
165
+ config.reset
166
+ config.index_options[option.to_sym] = "something"
167
+ config.build
168
+
169
+ file = open(config.config_file) { |f| f.read }
170
+ file.should match(/#{option}\s+= something/)
171
+
172
+ config.index_options[option.to_sym] = nil
173
+ end
174
+ end
175
+
176
+ it "should insert set source options into the configuration file" do
177
+ config = ThinkingSphinx::Configuration.instance
178
+ config.reset
179
+
180
+ config.source_options[:sql_query_pre] = ["something"]
181
+ ThinkingSphinx::Configuration::SourceOptions.each do |option|
182
+ config.source_options[option.to_sym] ||= "something"
183
+ config.build
184
+
185
+ file = open(config.config_file) { |f| f.read }
186
+ file.should match(/#{option}\s+= something/)
187
+
188
+ config.source_options.delete option.to_sym
189
+ end
190
+
191
+ config.source_options[:sql_query_pre] = []
192
+ end
193
+
194
+ it "should not blow away delta or utf options if sql pre is specified in config" do
195
+ config = ThinkingSphinx::Configuration.instance
196
+ config.reset
197
+
198
+ config.source_options[:sql_query_pre] = ["a pre query"]
199
+ config.build
200
+ file = open(config.config_file) { |f| f.read }
201
+
202
+ file.should match(/sql_query_pre = a pre query\n\s*sql_query_pre = UPDATE `\w+` SET `delta` = 0 WHERE `delta` = 1/im)
203
+ file.should match(/sql_query_pre = a pre query\n\s*sql_query_pre = \n/im)
204
+
205
+ config.source_options[:sql_query_pre] = []
206
+ end
207
+
208
+ it "should set any explicit prefixed or infixed fields" do
209
+ ThinkingSphinx::Configuration.instance.build
210
+
211
+ file = open(ThinkingSphinx::Configuration.instance.config_file) { |f|
212
+ f.read
213
+ }
214
+ file.should match(/prefix_fields\s+= city/)
215
+ file.should match(/infix_fields\s+= state/)
216
+ end
217
+
218
+ it "should not have prefix fields in indexes where nothing is set" do
219
+ ThinkingSphinx::Configuration.instance.build
220
+
221
+ file = open(ThinkingSphinx::Configuration.instance.config_file) { |f|
222
+ f.read
223
+ }
224
+ file.should_not match(/index alpha_core\s+\{\s+[^\}]*prefix_fields\s+=[^\}]*\}/m)
225
+ end
226
+
227
+ describe '#generate' do
228
+ let(:config) { ThinkingSphinx::Configuration.instance }
229
+
230
+ it "should set all sphinx_internal_id attributes to bigints if one is" do
231
+ config.reset
232
+ config.generate
233
+
234
+ config.configuration.indexes.each do |index|
235
+ next if index.is_a? Riddle::Configuration::DistributedIndex
236
+
237
+ index.sources.each do |source|
238
+ source.sql_attr_bigint.should include(:sphinx_internal_id)
239
+ end
240
+ end
241
+ end
242
+ end
243
+
244
+ describe '#client' do
245
+ before :each do
246
+ @config = ThinkingSphinx::Configuration.instance
247
+ @config.address = 'domain.url'
248
+ @config.port = 3333
249
+ @config.configuration.searchd.max_matches = 100
250
+ @config.timeout = 1
251
+ end
252
+
253
+ it "should return an instance of Riddle::Client" do
254
+ @config.client.should be_a(Riddle::Client)
255
+ end
256
+
257
+ it "should use the configuration address" do
258
+ @config.client.server.should == 'domain.url'
259
+ end
260
+
261
+ it "should use the configuration port" do
262
+ @config.client.port.should == 3333
263
+ end
264
+
265
+ it "should use the configuration max matches" do
266
+ @config.client.max_matches.should == 100
267
+ end
268
+
269
+ it "should use the configuration timeout" do
270
+ @config.client.timeout.should == 1
271
+ end
272
+ end
273
+
274
+ describe '#models_by_crc' do
275
+ before :each do
276
+ @config = ThinkingSphinx::Configuration.instance
277
+ end
278
+
279
+ it "should return a hash" do
280
+ @config.models_by_crc.should be_a(Hash)
281
+ end
282
+
283
+ it "should pair class names to their crc codes" do
284
+ @config.models_by_crc[Person.to_crc32].should == 'Person'
285
+ @config.models_by_crc[Alpha.to_crc32].should == 'Alpha'
286
+ end
287
+ end
288
+ end
@@ -0,0 +1,128 @@
1
+ require 'spec_helper'
2
+
3
+ describe ThinkingSphinx::Context do
4
+ before :each do
5
+ @context = ThinkingSphinx::Context.new
6
+ end
7
+
8
+ describe '#prepare' do
9
+ before :each do
10
+ @config = ThinkingSphinx::Configuration.instance
11
+ @config.model_directories = ['']
12
+
13
+ @file_name = 'a.rb'
14
+ @model_name_lower = 'a'
15
+ @class_name = 'A'
16
+
17
+ @file_name.stub!(:gsub).and_return(@model_name_lower)
18
+ @model_name_lower.stub!(:camelize).and_return(@class_name)
19
+ Dir.stub(:[]).and_return([@file_name])
20
+ end
21
+
22
+ it "should load the files by guessing the file name" do
23
+ @class_name.should_receive(:constantize).and_return(true)
24
+
25
+ @context.prepare
26
+ end
27
+
28
+ it "should not raise errors if the model name is nil" do
29
+ @file_name.stub!(:gsub).and_return(nil)
30
+
31
+ lambda {
32
+ @context.prepare
33
+ }.should_not raise_error
34
+ end
35
+
36
+ it "should not raise errors if the file name does not represent a class name" do
37
+ @class_name.should_receive(:constantize).and_raise(NameError)
38
+
39
+ lambda {
40
+ @context.prepare
41
+ }.should_not raise_error
42
+ end
43
+
44
+ # Fails in Ruby 1.9 (or maybe it's an RSpec update). Not sure why.
45
+ it "should retry if the first pass fails and contains a directory" do
46
+ @model_name_lower.stub!(:gsub!).and_return(true, nil)
47
+ @class_name.stub(:constantize).and_raise(LoadError)
48
+ @model_name_lower.should_receive(:camelize).twice
49
+
50
+ lambda {
51
+ @context.prepare
52
+ }.should_not raise_error
53
+ end
54
+
55
+ it "should catch database errors with a warning" do
56
+ @class_name.should_receive(:constantize).and_raise(Mysql::Error)
57
+ STDERR.stub!(:puts => '')
58
+ STDERR.should_receive(:puts).with('Warning: Error loading a.rb:')
59
+
60
+ lambda {
61
+ @context.prepare
62
+ }.should_not raise_error
63
+ end
64
+
65
+ it "should not load models if they're explicitly set in the configuration" do
66
+ @config.indexed_models = ['Alpha', 'Beta']
67
+ @context.prepare
68
+
69
+ @context.indexed_models.should == ['Alpha', 'Beta']
70
+ end
71
+ end
72
+
73
+ describe '#define_indexes' do
74
+ it "should call define_indexes on all known indexed models" do
75
+ @context.stub!(:indexed_models => ['Alpha', 'Beta'])
76
+ Alpha.should_receive(:define_indexes)
77
+ Beta.should_receive(:define_indexes)
78
+
79
+ @context.define_indexes
80
+ end
81
+ end
82
+
83
+ describe '#add_indexed_model' do
84
+ before :each do
85
+ @context.indexed_models.clear
86
+ end
87
+
88
+ it "should add the model to the collection" do
89
+ @context.add_indexed_model 'Alpha'
90
+
91
+ @context.indexed_models.should == ['Alpha']
92
+ end
93
+
94
+ it "should not duplicate models in the collection" do
95
+ @context.add_indexed_model 'Alpha'
96
+ @context.add_indexed_model 'Alpha'
97
+
98
+ @context.indexed_models.should == ['Alpha']
99
+ end
100
+
101
+ it "should keep the collection in alphabetical order" do
102
+ @context.add_indexed_model 'Beta'
103
+ @context.add_indexed_model 'Alpha'
104
+
105
+ @context.indexed_models.should == ['Alpha', 'Beta']
106
+ end
107
+
108
+ it "should translate classes to their names" do
109
+ @context.add_indexed_model Alpha
110
+
111
+ @context.indexed_models.should == ['Alpha']
112
+ end
113
+ end
114
+
115
+ describe '#superclass_indexed_models' do
116
+ it "should return indexed model names" do
117
+ @context.stub!(:indexed_models => ['Alpha', 'Beta'])
118
+
119
+ @context.superclass_indexed_models.should == ['Alpha', 'Beta']
120
+ end
121
+
122
+ it "should not include classes which have indexed superclasses" do
123
+ @context.stub!(:indexed_models => ['Parent', 'Person'])
124
+
125
+ @context.superclass_indexed_models.should == ['Person']
126
+ end
127
+ end
128
+ end