ebeigarts-thinking-sphinx 1.1.21

Sign up to get free protection for your applications and to get access to all the features.
Files changed (91) hide show
  1. data/LICENCE +20 -0
  2. data/README.textile +143 -0
  3. data/lib/thinking_sphinx.rb +217 -0
  4. data/lib/thinking_sphinx/active_record.rb +278 -0
  5. data/lib/thinking_sphinx/active_record/attribute_updates.rb +48 -0
  6. data/lib/thinking_sphinx/active_record/delta.rb +87 -0
  7. data/lib/thinking_sphinx/active_record/has_many_association.rb +29 -0
  8. data/lib/thinking_sphinx/active_record/search.rb +57 -0
  9. data/lib/thinking_sphinx/adapters/abstract_adapter.rb +53 -0
  10. data/lib/thinking_sphinx/adapters/mysql_adapter.rb +54 -0
  11. data/lib/thinking_sphinx/adapters/postgresql_adapter.rb +135 -0
  12. data/lib/thinking_sphinx/association.rb +164 -0
  13. data/lib/thinking_sphinx/attribute.rb +269 -0
  14. data/lib/thinking_sphinx/class_facet.rb +15 -0
  15. data/lib/thinking_sphinx/collection.rb +148 -0
  16. data/lib/thinking_sphinx/configuration.rb +275 -0
  17. data/lib/thinking_sphinx/core/string.rb +15 -0
  18. data/lib/thinking_sphinx/deltas.rb +30 -0
  19. data/lib/thinking_sphinx/deltas/datetime_delta.rb +50 -0
  20. data/lib/thinking_sphinx/deltas/default_delta.rb +68 -0
  21. data/lib/thinking_sphinx/deltas/delayed_delta.rb +27 -0
  22. data/lib/thinking_sphinx/deltas/delayed_delta/delta_job.rb +24 -0
  23. data/lib/thinking_sphinx/deltas/delayed_delta/flag_as_deleted_job.rb +27 -0
  24. data/lib/thinking_sphinx/deltas/delayed_delta/job.rb +26 -0
  25. data/lib/thinking_sphinx/deploy/capistrano.rb +82 -0
  26. data/lib/thinking_sphinx/facet.rb +108 -0
  27. data/lib/thinking_sphinx/facet_collection.rb +59 -0
  28. data/lib/thinking_sphinx/field.rb +82 -0
  29. data/lib/thinking_sphinx/index.rb +99 -0
  30. data/lib/thinking_sphinx/index/builder.rb +287 -0
  31. data/lib/thinking_sphinx/index/faux_column.rb +110 -0
  32. data/lib/thinking_sphinx/property.rb +160 -0
  33. data/lib/thinking_sphinx/rails_additions.rb +136 -0
  34. data/lib/thinking_sphinx/search.rb +727 -0
  35. data/lib/thinking_sphinx/search/facets.rb +104 -0
  36. data/lib/thinking_sphinx/source.rb +175 -0
  37. data/lib/thinking_sphinx/source/internal_properties.rb +46 -0
  38. data/lib/thinking_sphinx/source/sql.rb +126 -0
  39. data/lib/thinking_sphinx/tasks.rb +245 -0
  40. data/rails/init.rb +14 -0
  41. data/spec/unit/thinking_sphinx/active_record/delta_spec.rb +136 -0
  42. data/spec/unit/thinking_sphinx/active_record/has_many_association_spec.rb +53 -0
  43. data/spec/unit/thinking_sphinx/active_record/search_spec.rb +107 -0
  44. data/spec/unit/thinking_sphinx/active_record_spec.rb +329 -0
  45. data/spec/unit/thinking_sphinx/association_spec.rb +246 -0
  46. data/spec/unit/thinking_sphinx/attribute_spec.rb +338 -0
  47. data/spec/unit/thinking_sphinx/collection_spec.rb +15 -0
  48. data/spec/unit/thinking_sphinx/configuration_spec.rb +222 -0
  49. data/spec/unit/thinking_sphinx/core/string_spec.rb +9 -0
  50. data/spec/unit/thinking_sphinx/facet_collection_spec.rb +64 -0
  51. data/spec/unit/thinking_sphinx/facet_spec.rb +302 -0
  52. data/spec/unit/thinking_sphinx/field_spec.rb +154 -0
  53. data/spec/unit/thinking_sphinx/index/builder_spec.rb +355 -0
  54. data/spec/unit/thinking_sphinx/index/faux_column_spec.rb +30 -0
  55. data/spec/unit/thinking_sphinx/index_spec.rb +45 -0
  56. data/spec/unit/thinking_sphinx/rails_additions_spec.rb +191 -0
  57. data/spec/unit/thinking_sphinx/search_spec.rb +228 -0
  58. data/spec/unit/thinking_sphinx/source_spec.rb +217 -0
  59. data/spec/unit/thinking_sphinx_spec.rb +151 -0
  60. data/tasks/distribution.rb +67 -0
  61. data/tasks/rails.rake +1 -0
  62. data/tasks/testing.rb +100 -0
  63. data/vendor/after_commit/LICENSE +20 -0
  64. data/vendor/after_commit/README +16 -0
  65. data/vendor/after_commit/Rakefile +22 -0
  66. data/vendor/after_commit/init.rb +8 -0
  67. data/vendor/after_commit/lib/after_commit.rb +45 -0
  68. data/vendor/after_commit/lib/after_commit/active_record.rb +114 -0
  69. data/vendor/after_commit/lib/after_commit/connection_adapters.rb +103 -0
  70. data/vendor/after_commit/test/after_commit_test.rb +53 -0
  71. data/vendor/delayed_job/lib/delayed/job.rb +251 -0
  72. data/vendor/delayed_job/lib/delayed/message_sending.rb +7 -0
  73. data/vendor/delayed_job/lib/delayed/performable_method.rb +55 -0
  74. data/vendor/delayed_job/lib/delayed/worker.rb +54 -0
  75. data/vendor/riddle/lib/riddle.rb +30 -0
  76. data/vendor/riddle/lib/riddle/client.rb +619 -0
  77. data/vendor/riddle/lib/riddle/client/filter.rb +53 -0
  78. data/vendor/riddle/lib/riddle/client/message.rb +65 -0
  79. data/vendor/riddle/lib/riddle/client/response.rb +84 -0
  80. data/vendor/riddle/lib/riddle/configuration.rb +33 -0
  81. data/vendor/riddle/lib/riddle/configuration/distributed_index.rb +48 -0
  82. data/vendor/riddle/lib/riddle/configuration/index.rb +142 -0
  83. data/vendor/riddle/lib/riddle/configuration/indexer.rb +19 -0
  84. data/vendor/riddle/lib/riddle/configuration/remote_index.rb +17 -0
  85. data/vendor/riddle/lib/riddle/configuration/searchd.rb +25 -0
  86. data/vendor/riddle/lib/riddle/configuration/section.rb +43 -0
  87. data/vendor/riddle/lib/riddle/configuration/source.rb +23 -0
  88. data/vendor/riddle/lib/riddle/configuration/sql_source.rb +34 -0
  89. data/vendor/riddle/lib/riddle/configuration/xml_source.rb +28 -0
  90. data/vendor/riddle/lib/riddle/controller.rb +44 -0
  91. metadata +191 -0
@@ -0,0 +1,228 @@
1
+ require 'spec/spec_helper'
2
+ require 'will_paginate/collection'
3
+
4
+ describe ThinkingSphinx::Search do
5
+ describe "search method" do
6
+ describe ":star option" do
7
+ before :each do
8
+ @client = Riddle::Client.new
9
+ @client.stub!(
10
+ :filters => [],
11
+ :filters= => true,
12
+ :id_range= => true,
13
+ :sort_mode => :asc,
14
+ :limit => 5,
15
+ :offset= => 0,
16
+ :sort_mode= => true,
17
+ :query => {
18
+ :matches => [],
19
+ :total => 50
20
+ }
21
+ )
22
+
23
+ ThinkingSphinx::Search.stub_methods(
24
+ :client_from_options => @client,
25
+ :search_conditions => ["", []]
26
+ )
27
+ end
28
+
29
+ it "should not apply by default" do
30
+ @client.should_receive(:query).with("foo bar",'*','')
31
+
32
+ ThinkingSphinx::Search.search "foo bar"
33
+ end
34
+
35
+ it "should apply when passed, and handle full extended syntax" do
36
+ input = %{a b* c (d | e) 123 5&6 (f_f g) !h "i j" "k l"~10 "m n"/3 @o p -(q|r)}
37
+ expected = %{*a* b* *c* (*d* | *e*) *123* *5*&*6* (*f_f* *g*) !*h* "i j" "k l"~10 "m n"/3 @o *p* -(*q*|*r*)}
38
+ @client.should_receive(:query).with(expected,'*','')
39
+
40
+ ThinkingSphinx::Search.search input, :star => true
41
+ end
42
+
43
+ it "should default to /\w+/ as token" do
44
+ @client.should_receive(:query).with("*foo*@*bar*.*com*",'*','')
45
+
46
+ ThinkingSphinx::Search.search "foo@bar.com", :star => true
47
+ end
48
+
49
+ it "should honour custom token" do
50
+ @client.should_receive(:query).with("*foo@bar.com* -*foo-bar*",'*','')
51
+
52
+ ThinkingSphinx::Search.search "foo@bar.com -foo-bar", :star => /[\w@.-]+/u
53
+ end
54
+ end
55
+
56
+ describe "sort modes" do
57
+ before :each do
58
+ @client = Riddle::Client.new
59
+ @client.stub_method(:query => {:matches => []})
60
+ Riddle::Client.stub_method(:new => @client)
61
+ end
62
+
63
+ it "should use :relevance as a default" do
64
+ ThinkingSphinx::Search.search "foo"
65
+ @client.sort_mode.should == :relevance
66
+ end
67
+
68
+ it "should use :attr_asc if a symbol is supplied to :order" do
69
+ ThinkingSphinx::Search.search "foo", :order => :created_at
70
+ @client.sort_mode.should == :attr_asc
71
+ end
72
+
73
+ it "should use :attr_desc if a symbol is supplied and :desc is the mode" do
74
+ ThinkingSphinx::Search.search "foo", :order => :created_at, :sort_mode => :desc
75
+ @client.sort_mode.should == :attr_desc
76
+ end
77
+
78
+ it "should use :extended if a string is supplied to :order" do
79
+ ThinkingSphinx::Search.search "foo", :order => "created_at ASC"
80
+ @client.sort_mode.should == :extended
81
+ end
82
+
83
+ it "should use :expr if explicitly requested with a string supplied to :order" do
84
+ ThinkingSphinx::Search.search "foo", :order => "created_at ASC", :sort_mode => :expr
85
+ @client.sort_mode.should == :expr
86
+ end
87
+
88
+ it "should use :attr_desc if explicitly requested with a string supplied to :order" do
89
+ ThinkingSphinx::Search.search "foo", :order => "created_at", :sort_mode => :desc
90
+ @client.sort_mode.should == :attr_desc
91
+ end
92
+ end
93
+
94
+ describe "grouping" do
95
+ before :each do
96
+ @client = Riddle::Client.new
97
+ @client.stub_method(:query => {:matches => []})
98
+ Riddle::Client.stub_method(:new => @client)
99
+ end
100
+
101
+ it "should convert group into group_by and group_function" do
102
+ ThinkingSphinx::Search.search "foo", :group => :edition
103
+ @client.group_function.should == :attr
104
+ @client.group_by.should == "edition"
105
+ end
106
+ end
107
+
108
+ describe ":comment option" do
109
+ before :each do
110
+ @client = Riddle::Client.new
111
+ @client.stub_method(:query => {:matches => []})
112
+ Riddle::Client.stub_method(:new => @client)
113
+ end
114
+
115
+ it "should add comment to log" do
116
+ ThinkingSphinx::Search.search 'foo bar', :comment => 'custom log'
117
+ @client.should have_received(:query).with('foo bar', '*', 'custom log')
118
+ end
119
+
120
+ it "should use a blank string when no comment is specified" do
121
+ ThinkingSphinx::Search.search 'foo bar'
122
+ @client.should have_received(:query).with('foo bar', '*', '')
123
+ end
124
+ end
125
+ end
126
+
127
+ describe "facets method" do
128
+ before :each do
129
+ @person = Person.find(:first)
130
+
131
+ @city_results = [@person]
132
+ @city_results.stub!(:each_with_groupby_and_count).
133
+ and_yield(@person, @person.city.to_crc32, 1)
134
+
135
+ @birthday_results = [@person]
136
+ @birthday_results.stub!(:each_with_groupby_and_count).
137
+ and_yield(@person, @person.birthday.to_i, 1)
138
+
139
+ @config = ThinkingSphinx::Configuration.instance
140
+ @config.configuration.searchd.max_matches = 10_000
141
+ end
142
+
143
+ it "should use the system-set max_matches for limit on facet calls" do
144
+ ThinkingSphinx::Search.stub!(:search).and_return(@city_results, @birthday_results)
145
+
146
+ ThinkingSphinx::Search.should_receive(:search) do |options|
147
+ options[:max_matches].should == 10_000
148
+ options[:limit].should == 10_000
149
+ end
150
+
151
+ ThinkingSphinx::Search.facets :all_attributes => true
152
+ end
153
+
154
+ it "should use the default max-matches if there is no explicit setting" do
155
+ ThinkingSphinx::Search.stub!(:search).and_return(@city_results, @birthday_results)
156
+
157
+ @config.configuration.searchd.max_matches = nil
158
+ ThinkingSphinx::Search.should_receive(:search) do |options|
159
+ options[:max_matches].should == 1000
160
+ options[:limit].should == 1000
161
+ end
162
+
163
+ ThinkingSphinx::Search.facets :all_attributes => true
164
+ end
165
+
166
+ it "should ignore user-provided max_matches and limit on facet calls" do
167
+ ThinkingSphinx::Search.stub!(:search).and_return(@city_results, @birthday_results)
168
+
169
+ ThinkingSphinx::Search.should_receive(:search) do |options|
170
+ options[:max_matches].should == 10_000
171
+ options[:limit].should == 10_000
172
+ end
173
+
174
+ ThinkingSphinx::Search.facets(
175
+ :all_attributes => true,
176
+ :max_matches => 500,
177
+ :limit => 200
178
+ )
179
+ end
180
+
181
+ it "should use explicit facet list if one is provided" do
182
+ ThinkingSphinx::Search.should_receive(:search).once.and_return(@city_results)
183
+
184
+ ThinkingSphinx::Search.facets(
185
+ :facets => ['city'],
186
+ :class => Person
187
+ )
188
+ end
189
+
190
+ it "should not use an explicit :page" do
191
+ ThinkingSphinx::Search.stub!(:search).and_return(@city_results, @birthday_results)
192
+ ThinkingSphinx::Search.should_receive(:search) do |options|
193
+ options[:page].should == 1
194
+ end
195
+
196
+ ThinkingSphinx::Search.facets(
197
+ :all_attributes => true,
198
+ :page => 3
199
+ )
200
+ end
201
+
202
+ describe "conflicting facets" do
203
+ before :each do
204
+ @index = ThinkingSphinx::Index::Builder.generate(Alpha) do
205
+ indexes :name
206
+ has :value, :as => :city, :facet => true
207
+ end
208
+ end
209
+
210
+ after :each do
211
+ Alpha.sphinx_facets.delete_at(-1)
212
+ Alpha.sphinx_indexes.delete_at(-1)
213
+ end
214
+
215
+ it "should raise an error if searching with facets of same name but different type" do
216
+ lambda {
217
+ ThinkingSphinx::Search.facets :all_attributes => true
218
+ }.should raise_error
219
+ end
220
+ end
221
+ end
222
+ end
223
+
224
+ describe ThinkingSphinx::Search, "playing nice with Search model" do
225
+ it "should not conflict with models called Search" do
226
+ lambda { Search.find(:all) }.should_not raise_error
227
+ end
228
+ end
@@ -0,0 +1,217 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe ThinkingSphinx::Source do
4
+ before :each do
5
+ @index = ThinkingSphinx::Index.new(Person)
6
+ @source = ThinkingSphinx::Source.new(@index, :sql_range_step => 1000)
7
+ end
8
+
9
+ it "should generate the name from the model" do
10
+ @source.name.should == "person"
11
+ end
12
+
13
+ it "should handle namespaced models for name generation" do
14
+ index = ThinkingSphinx::Index.new(Admin::Person)
15
+ source = ThinkingSphinx::Source.new(index)
16
+ source.name.should == "admin_person"
17
+ end
18
+
19
+ describe "#to_riddle_for_core" do
20
+ before :each do
21
+ config = ThinkingSphinx::Configuration.instance
22
+ config.source_options[:sql_ranged_throttle] = 100
23
+
24
+ ThinkingSphinx::Field.new(
25
+ @source, ThinkingSphinx::Index::FauxColumn.new(:first_name)
26
+ )
27
+ ThinkingSphinx::Field.new(
28
+ @source, ThinkingSphinx::Index::FauxColumn.new(:last_name)
29
+ )
30
+
31
+ ThinkingSphinx::Attribute.new(
32
+ @source, ThinkingSphinx::Index::FauxColumn.new(:id), :as => :internal_id
33
+ )
34
+ ThinkingSphinx::Attribute.new(
35
+ @source, ThinkingSphinx::Index::FauxColumn.new(:birthday)
36
+ )
37
+ ThinkingSphinx::Attribute.new(
38
+ @source, ThinkingSphinx::Index::FauxColumn.new(:tags, :id), :as => :tag_ids
39
+ )
40
+ ThinkingSphinx::Attribute.new(
41
+ @source, ThinkingSphinx::Index::FauxColumn.new(:contacts, :id),
42
+ :as => :contact_ids, :source => :query
43
+ )
44
+
45
+ @source.conditions << "`birthday` <= NOW()"
46
+ @source.groupings << "`first_name`"
47
+
48
+ @index.local_options[:group_concat_max_len] = 1024
49
+
50
+ @riddle = @source.to_riddle_for_core(1, 0)
51
+ end
52
+
53
+ it "should generate a Riddle Source object" do
54
+ @riddle.should be_a_kind_of(Riddle::Configuration::SQLSource)
55
+ end
56
+
57
+ it "should use the index and name its own name" do
58
+ @riddle.name.should == "person_core_0"
59
+ end
60
+
61
+ it "should use the model's database connection to determine type" do
62
+ @riddle.type.should == "mysql"
63
+ end
64
+
65
+ it "should match the model's database settings" do
66
+ config = Person.connection.instance_variable_get(:@config)
67
+ @riddle.sql_db.should == config[:database]
68
+ @riddle.sql_user.should == config[:username]
69
+ @riddle.sql_pass.should == config[:password].to_s
70
+ @riddle.sql_host.should == config[:host]
71
+ @riddle.sql_port.should == config[:port]
72
+ @riddle.sql_sock.should == config[:socket]
73
+ end
74
+
75
+ it "should assign attributes" do
76
+ # 3 internal attributes plus the one requested
77
+ @riddle.sql_attr_uint.length.should == 4
78
+ @riddle.sql_attr_uint.last.should == :internal_id
79
+
80
+ @riddle.sql_attr_timestamp.length.should == 1
81
+ @riddle.sql_attr_timestamp.first.should == :birthday
82
+ end
83
+
84
+ it "should set Sphinx Source options" do
85
+ @riddle.sql_range_step.should == 1000
86
+ @riddle.sql_ranged_throttle.should == 100
87
+ end
88
+
89
+ describe "#sql_query" do
90
+ before :each do
91
+ @query = @riddle.sql_query
92
+ end
93
+
94
+ it "should select data from the model table" do
95
+ @query.should match(/FROM `people`/)
96
+ end
97
+
98
+ it "should select each of the fields" do
99
+ @query.should match(/`first_name`.+FROM/)
100
+ @query.should match(/`last_name`.+FROM/)
101
+ end
102
+
103
+ it "should select each of the attributes" do
104
+ @query.should match(/`id` AS `internal_id`.+FROM/)
105
+ @query.should match(/`birthday`.+FROM/)
106
+ @query.should match(/`tags`.`id`.+ AS `tag_ids`.+FROM/)
107
+ end
108
+
109
+ it "should not match the sourced MVA attribute" do
110
+ @query.should_not match(/contact_ids/)
111
+ end
112
+
113
+ it "should include joins for required associations" do
114
+ @query.should match(/LEFT OUTER JOIN `tags`/)
115
+ end
116
+
117
+ it "should not include joins for the sourced MVA attribute" do
118
+ @query.should_not match(/LEFT OUTER JOIN `contacts`/)
119
+ end
120
+
121
+ it "should include any defined conditions" do
122
+ @query.should match(/WHERE.+`birthday` <= NOW()/)
123
+ end
124
+
125
+ it "should include any defined groupings" do
126
+ @query.should match(/GROUP BY.+`first_name`/)
127
+ end
128
+ end
129
+
130
+ describe "#sql_query_range" do
131
+ before :each do
132
+ @query = @riddle.sql_query_range
133
+ end
134
+
135
+ it "should select data from the model table" do
136
+ @query.should match(/FROM `people`/)
137
+ end
138
+
139
+ it "should select the minimum and the maximum ids" do
140
+ @query.should match(/SELECT.+MIN.+MAX.+FROM/)
141
+ end
142
+ end
143
+
144
+ describe "#sql_query_info" do
145
+ before :each do
146
+ @query = @riddle.sql_query_info
147
+ end
148
+
149
+ it "should select all fields from the model table" do
150
+ @query.should match(/SELECT \* FROM `people`/)
151
+ end
152
+
153
+ it "should filter the primary key with the offset" do
154
+ model_count = ThinkingSphinx.indexed_models.size
155
+ @query.should match(/WHERE `id` = \(\(\$id - 1\) \/ #{model_count}\)/)
156
+ end
157
+ end
158
+
159
+ describe "#sql_query_pre" do
160
+ before :each do
161
+ @queries = @riddle.sql_query_pre
162
+ end
163
+
164
+ it "should default to just the UTF8 statement" do
165
+ @queries.detect { |query|
166
+ query == "SET NAMES utf8"
167
+ }.should_not be_nil
168
+ end
169
+
170
+ it "should set the group_concat_max_len session value for MySQL if requested" do
171
+ @queries.detect { |query|
172
+ query == "SET SESSION group_concat_max_len = 1024"
173
+ }.should_not be_nil
174
+ end
175
+ end
176
+ end
177
+
178
+ describe "#to_riddle_for_core with range disabled" do
179
+ before :each do
180
+ ThinkingSphinx::Field.new(
181
+ @source, ThinkingSphinx::Index::FauxColumn.new(:first_name)
182
+ )
183
+ end
184
+
185
+ describe "set per-index" do
186
+ before :each do
187
+ @index.local_options[:disable_range] = true
188
+ @riddle = @source.to_riddle_for_core(1, 0)
189
+ end
190
+
191
+ it "should not have the range in the sql_query" do
192
+ @riddle.sql_query.should_not match(/`people`.`id` >= \$start/)
193
+ @riddle.sql_query.should_not match(/`people`.`id` <= \$end/)
194
+ end
195
+
196
+ it "should not have a sql_query_range" do
197
+ @riddle.sql_query_range.should be_nil
198
+ end
199
+ end
200
+
201
+ describe "set globally" do
202
+ before :each do
203
+ ThinkingSphinx::Configuration.instance.index_options[:disable_range] = true
204
+ @riddle = @source.to_riddle_for_core(1, 0)
205
+ end
206
+
207
+ it "should not have the range in the sql_query" do
208
+ @riddle.sql_query.should_not match(/`people`.`id` >= \$start/)
209
+ @riddle.sql_query.should_not match(/`people`.`id` <= \$end/)
210
+ end
211
+
212
+ it "should not have a sql_query_range" do
213
+ @riddle.sql_query_range.should be_nil
214
+ end
215
+ end
216
+ end
217
+ end
@@ -0,0 +1,151 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe ThinkingSphinx do
4
+ it "should define indexes by default" do
5
+ ThinkingSphinx.define_indexes?.should be_true
6
+ end
7
+
8
+ it "should disable index definition" do
9
+ ThinkingSphinx.define_indexes = false
10
+ ThinkingSphinx.define_indexes?.should be_false
11
+ end
12
+
13
+ it "should enable index definition" do
14
+ ThinkingSphinx.define_indexes = false
15
+ ThinkingSphinx.define_indexes?.should be_false
16
+ ThinkingSphinx.define_indexes = true
17
+ ThinkingSphinx.define_indexes?.should be_true
18
+ end
19
+
20
+ it "should index deltas by default" do
21
+ ThinkingSphinx.deltas_enabled = nil
22
+ ThinkingSphinx.deltas_enabled?.should be_true
23
+ end
24
+
25
+ it "should disable delta indexing" do
26
+ ThinkingSphinx.deltas_enabled = false
27
+ ThinkingSphinx.deltas_enabled?.should be_false
28
+ end
29
+
30
+ it "should enable delta indexing" do
31
+ ThinkingSphinx.deltas_enabled = false
32
+ ThinkingSphinx.deltas_enabled?.should be_false
33
+ ThinkingSphinx.deltas_enabled = true
34
+ ThinkingSphinx.deltas_enabled?.should be_true
35
+ end
36
+
37
+ it "should update indexes by default" do
38
+ ThinkingSphinx.updates_enabled = nil
39
+ ThinkingSphinx.updates_enabled?.should be_true
40
+ end
41
+
42
+ it "should disable index updating" do
43
+ ThinkingSphinx.updates_enabled = false
44
+ ThinkingSphinx.updates_enabled?.should be_false
45
+ end
46
+
47
+ it "should enable index updating" do
48
+ ThinkingSphinx.updates_enabled = false
49
+ ThinkingSphinx.updates_enabled?.should be_false
50
+ ThinkingSphinx.updates_enabled = true
51
+ ThinkingSphinx.updates_enabled?.should be_true
52
+ end
53
+
54
+ it "should always say Sphinx is running if flagged as being on a remote machine" do
55
+ ThinkingSphinx.remote_sphinx = true
56
+ ThinkingSphinx.stub_method(:sphinx_running_by_pid? => false)
57
+
58
+ ThinkingSphinx.sphinx_running?.should be_true
59
+ end
60
+
61
+ it "should actually pay attention to Sphinx if not on a remote machine" do
62
+ ThinkingSphinx.remote_sphinx = false
63
+ ThinkingSphinx.stub_method(:sphinx_running_by_pid? => false)
64
+ ThinkingSphinx.sphinx_running?.should be_false
65
+
66
+ ThinkingSphinx.stub_method(:sphinx_running_by_pid? => true)
67
+ ThinkingSphinx.sphinx_running?.should be_true
68
+ end
69
+
70
+ describe "use_group_by_shortcut? method" do
71
+ before :each do
72
+ adapter = defined?(JRUBY_VERSION) ? :JdbcAdapter : :MysqlAdapter
73
+ unless ::ActiveRecord::ConnectionAdapters.const_defined?(adapter)
74
+ pending "No MySQL"
75
+ return
76
+ end
77
+
78
+ @connection = ::ActiveRecord::ConnectionAdapters.const_get(adapter).stub_instance(
79
+ :select_all => true,
80
+ :config => {:adapter => defined?(JRUBY_VERSION) ? 'jdbcmysql' : 'mysql'}
81
+ )
82
+ ::ActiveRecord::Base.stub_method(
83
+ :connection => @connection
84
+ )
85
+ end
86
+
87
+ it "should return true if no ONLY_FULL_GROUP_BY" do
88
+ @connection.stub_method(
89
+ :select_all => {:a => "OTHER SETTINGS"}
90
+ )
91
+
92
+ ThinkingSphinx.use_group_by_shortcut?.should be_true
93
+ end
94
+
95
+ it "should return true if NULL value" do
96
+ @connection.stub_method(
97
+ :select_all => {:a => nil}
98
+ )
99
+
100
+ ThinkingSphinx.use_group_by_shortcut?.should be_true
101
+ end
102
+
103
+ it "should return false if ONLY_FULL_GROUP_BY is set" do
104
+ @connection.stub_method(
105
+ :select_all => {:a => "OTHER SETTINGS,ONLY_FULL_GROUP_BY,blah"}
106
+ )
107
+
108
+ ThinkingSphinx.use_group_by_shortcut?.should be_false
109
+ end
110
+
111
+ it "should return false if ONLY_FULL_GROUP_BY is set in any of the values" do
112
+ @connection.stub_method(
113
+ :select_all => {
114
+ :a => "OTHER SETTINGS",
115
+ :b => "ONLY_FULL_GROUP_BY"
116
+ }
117
+ )
118
+
119
+ ThinkingSphinx.use_group_by_shortcut?.should be_false
120
+ end
121
+
122
+ describe "if not using MySQL" do
123
+ before :each do
124
+ adapter = defined?(JRUBY_VERSION) ? 'JdbcAdapter' : 'PostgreSQLAdapter'
125
+ unless ::ActiveRecord::ConnectionAdapters.const_defined?(adapter)
126
+ pending "No PostgreSQL"
127
+ return
128
+ end
129
+
130
+ @connection = stub(adapter).as_null_object
131
+ @connection.stub!(
132
+ :select_all => true,
133
+ :config => {:adapter => defined?(JRUBY_VERSION) ? 'jdbcpostgresql' : 'postgresql'}
134
+ )
135
+ ::ActiveRecord::Base.stub_method(
136
+ :connection => @connection
137
+ )
138
+ end
139
+
140
+ it "should return false" do
141
+ ThinkingSphinx.use_group_by_shortcut?.should be_false
142
+ end
143
+
144
+ it "should not call select_all" do
145
+ @connection.should_not_receive(:select_all)
146
+
147
+ ThinkingSphinx.use_group_by_shortcut?
148
+ end
149
+ end
150
+ end
151
+ end