dpickett-thinking-sphinx 1.1.12 → 1.1.23
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +19 -0
- data/lib/thinking_sphinx.rb +36 -2
- data/lib/thinking_sphinx/active_record.rb +18 -3
- data/lib/thinking_sphinx/adapters/postgresql_adapter.rb +9 -3
- data/lib/thinking_sphinx/association.rb +4 -1
- data/lib/thinking_sphinx/attribute.rb +85 -43
- data/lib/thinking_sphinx/configuration.rb +33 -12
- data/lib/thinking_sphinx/deltas.rb +9 -6
- data/lib/thinking_sphinx/deltas/datetime_delta.rb +3 -3
- data/lib/thinking_sphinx/deltas/default_delta.rb +4 -4
- data/lib/thinking_sphinx/deltas/delayed_delta/delta_job.rb +1 -1
- data/lib/thinking_sphinx/deploy/capistrano.rb +82 -64
- data/lib/thinking_sphinx/facet.rb +58 -21
- data/lib/thinking_sphinx/facet_collection.rb +12 -13
- data/lib/thinking_sphinx/field.rb +3 -1
- data/lib/thinking_sphinx/index.rb +28 -353
- data/lib/thinking_sphinx/index/builder.rb +255 -232
- data/lib/thinking_sphinx/property.rb +29 -2
- data/lib/thinking_sphinx/search.rb +32 -96
- data/lib/thinking_sphinx/search/facets.rb +104 -0
- data/lib/thinking_sphinx/source.rb +150 -0
- data/lib/thinking_sphinx/source/internal_properties.rb +46 -0
- data/lib/thinking_sphinx/source/sql.rb +128 -0
- data/lib/thinking_sphinx/tasks.rb +42 -8
- data/rails/init.rb +14 -0
- data/spec/unit/thinking_sphinx/active_record/delta_spec.rb +5 -5
- data/spec/unit/thinking_sphinx/active_record/search_spec.rb +4 -4
- data/spec/unit/thinking_sphinx/active_record_spec.rb +52 -39
- data/spec/unit/thinking_sphinx/association_spec.rb +4 -5
- data/spec/unit/thinking_sphinx/attribute_spec.rb +209 -19
- data/spec/unit/thinking_sphinx/collection_spec.rb +7 -6
- data/spec/unit/thinking_sphinx/configuration_spec.rb +93 -7
- data/spec/unit/thinking_sphinx/facet_spec.rb +256 -0
- data/spec/unit/thinking_sphinx/field_spec.rb +26 -17
- data/spec/unit/thinking_sphinx/index/builder_spec.rb +351 -1
- data/spec/unit/thinking_sphinx/index_spec.rb +3 -102
- data/spec/unit/thinking_sphinx/rails_additions_spec.rb +13 -5
- data/spec/unit/thinking_sphinx/search_spec.rb +154 -29
- data/spec/unit/thinking_sphinx/source_spec.rb +217 -0
- data/spec/unit/thinking_sphinx_spec.rb +22 -4
- data/tasks/distribution.rb +19 -0
- data/vendor/riddle/lib/riddle.rb +1 -1
- data/vendor/riddle/lib/riddle/configuration/section.rb +7 -1
- metadata +26 -3
@@ -1,15 +1,6 @@
|
|
1
1
|
require 'spec/spec_helper'
|
2
2
|
|
3
3
|
describe ThinkingSphinx::Index do
|
4
|
-
describe "generated sql_query" do
|
5
|
-
it "should include explicit groupings if requested" do
|
6
|
-
@index = ThinkingSphinx::Index.new(Person)
|
7
|
-
|
8
|
-
@index.groupings << "custom_sql"
|
9
|
-
@index.to_riddle_for_core(0, 0).sql_query.should match(/GROUP BY.+custom_sql/)
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
4
|
describe "prefix_fields method" do
|
14
5
|
before :each do
|
15
6
|
@index = ThinkingSphinx::Index.new(Person)
|
@@ -18,7 +9,7 @@ describe ThinkingSphinx::Index do
|
|
18
9
|
@field_b = ThinkingSphinx::Field.stub_instance(:prefixes => false)
|
19
10
|
@field_c = ThinkingSphinx::Field.stub_instance(:prefixes => true)
|
20
11
|
|
21
|
-
@index.fields
|
12
|
+
@index.stub_method(:fields => [@field_a, @field_b, @field_c])
|
22
13
|
end
|
23
14
|
|
24
15
|
it "should return fields that are flagged as prefixed" do
|
@@ -39,7 +30,7 @@ describe ThinkingSphinx::Index do
|
|
39
30
|
@field_b = ThinkingSphinx::Field.stub_instance(:infixes => false)
|
40
31
|
@field_c = ThinkingSphinx::Field.stub_instance(:infixes => true)
|
41
32
|
|
42
|
-
@index.fields
|
33
|
+
@index.stub_method(:fields => [@field_a, @field_b, @field_c])
|
43
34
|
end
|
44
35
|
|
45
36
|
it "should return fields that are flagged as infixed" do
|
@@ -51,94 +42,4 @@ describe ThinkingSphinx::Index do
|
|
51
42
|
@index.infix_fields.should_not include(@field_b)
|
52
43
|
end
|
53
44
|
end
|
54
|
-
|
55
|
-
describe "multi-value attribute as ranged-query with has-many association" do
|
56
|
-
before :each do
|
57
|
-
@index = ThinkingSphinx::Index.new(Person) do
|
58
|
-
has tags(:id), :as => :tag_ids, :source => :ranged_query
|
59
|
-
end
|
60
|
-
@index.link!
|
61
|
-
|
62
|
-
@sql = @index.to_riddle_for_core(0, 0).sql_query
|
63
|
-
end
|
64
|
-
|
65
|
-
it "should not include attribute in select-clause sql_query" do
|
66
|
-
@sql.should_not match(/tag_ids/)
|
67
|
-
@sql.should_not match(/GROUP_CONCAT\(`tags`.`id`/)
|
68
|
-
end
|
69
|
-
|
70
|
-
it "should not join with association table" do
|
71
|
-
@sql.should_not match(/LEFT OUTER JOIN `tags`/)
|
72
|
-
end
|
73
|
-
|
74
|
-
it "should include sql_attr_multi as ranged-query" do
|
75
|
-
attribute = @index.send(:attributes).first
|
76
|
-
attribute.send(:type_to_config).to_s.should == "sql_attr_multi"
|
77
|
-
|
78
|
-
declaration, query, range_query = attribute.send(:config_value).split('; ')
|
79
|
-
declaration.should == "uint tag_ids from ranged-query"
|
80
|
-
query.should == "SELECT `tags`.`person_id` #{ThinkingSphinx.unique_id_expression} AS `id`, `tags`.`id` AS `tag_ids` FROM `tags` WHERE `tags`.`person_id` >= $start AND `tags`.`person_id` <= $end"
|
81
|
-
range_query.should == "SELECT MIN(`tags`.`person_id`), MAX(`tags`.`person_id`) FROM `tags`"
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
describe "multi-value attribute as ranged-query with has-many-through association" do
|
86
|
-
before :each do
|
87
|
-
@index = ThinkingSphinx::Index.new(Person) do
|
88
|
-
has football_teams(:id), :as => :football_teams_ids, :source => :ranged_query
|
89
|
-
end
|
90
|
-
@index.link!
|
91
|
-
|
92
|
-
@sql = @index.to_riddle_for_core(0, 0).sql_query
|
93
|
-
end
|
94
|
-
|
95
|
-
it "should not include attribute in select-clause sql_query" do
|
96
|
-
@sql.should_not match(/football_teams_ids/)
|
97
|
-
@sql.should_not match(/GROUP_CONCAT\(`tags`.`football_team_id`/)
|
98
|
-
end
|
99
|
-
|
100
|
-
it "should not join with association table" do
|
101
|
-
@sql.should_not match(/LEFT OUTER JOIN `tags`/)
|
102
|
-
end
|
103
|
-
|
104
|
-
it "should include sql_attr_multi as ranged-query" do
|
105
|
-
attribute = @index.send(:attributes).first
|
106
|
-
attribute.send(:type_to_config).to_s.should == "sql_attr_multi"
|
107
|
-
|
108
|
-
declaration, query, range_query = attribute.send(:config_value).split('; ')
|
109
|
-
declaration.should == "uint football_teams_ids from ranged-query"
|
110
|
-
query.should == "SELECT `tags`.`person_id` #{ThinkingSphinx.unique_id_expression} AS `id`, `tags`.`football_team_id` AS `football_teams_ids` FROM `tags` WHERE `tags`.`person_id` >= $start AND `tags`.`person_id` <= $end"
|
111
|
-
range_query.should == "SELECT MIN(`tags`.`person_id`), MAX(`tags`.`person_id`) FROM `tags`"
|
112
|
-
end
|
113
|
-
end
|
114
|
-
|
115
|
-
describe "multi-value attribute as ranged-query with has-many-through association and foreign_key" do
|
116
|
-
before :each do
|
117
|
-
@index = ThinkingSphinx::Index.new(Person) do
|
118
|
-
has friends(:id), :as => :friend_ids, :source => :ranged_query
|
119
|
-
end
|
120
|
-
@index.link!
|
121
|
-
|
122
|
-
@sql = @index.to_riddle_for_core(0, 0).sql_query
|
123
|
-
end
|
124
|
-
|
125
|
-
it "should not include attribute in select-clause sql_query" do
|
126
|
-
@sql.should_not match(/friend_ids/)
|
127
|
-
@sql.should_not match(/GROUP_CONCAT\(`friendships`.`friend_id`/)
|
128
|
-
end
|
129
|
-
|
130
|
-
it "should not join with association table" do
|
131
|
-
@sql.should_not match(/LEFT OUTER JOIN `friendships`/)
|
132
|
-
end
|
133
|
-
|
134
|
-
it "should include sql_attr_multi as ranged-query" do
|
135
|
-
attribute = @index.send(:attributes).first
|
136
|
-
attribute.send(:type_to_config).to_s.should == "sql_attr_multi"
|
137
|
-
|
138
|
-
declaration, query, range_query = attribute.send(:config_value).split('; ')
|
139
|
-
declaration.should == "uint friend_ids from ranged-query"
|
140
|
-
query.should == "SELECT `friendships`.`person_id` #{ThinkingSphinx.unique_id_expression} AS `id`, `friendships`.`friend_id` AS `friend_ids` FROM `friendships` WHERE `friendships`.`person_id` >= $start AND `friendships`.`person_id` <= $end"
|
141
|
-
range_query.should == "SELECT MIN(`friendships`.`person_id`), MAX(`friendships`.`person_id`) FROM `friendships`"
|
142
|
-
end
|
143
|
-
end
|
144
|
-
end
|
45
|
+
end
|
@@ -20,12 +20,16 @@ describe ThinkingSphinx::HashExcept do
|
|
20
20
|
end
|
21
21
|
|
22
22
|
describe "extends Hash" do
|
23
|
+
before :each do
|
24
|
+
@instance_methods = Hash.instance_methods.collect { |m| m.to_s }
|
25
|
+
end
|
26
|
+
|
23
27
|
it 'with except' do
|
24
|
-
|
28
|
+
@instance_methods.include?('except').should be_true
|
25
29
|
end
|
26
30
|
|
27
31
|
it 'with except!' do
|
28
|
-
|
32
|
+
@instance_methods.include?('except!').should be_true
|
29
33
|
end
|
30
34
|
end
|
31
35
|
end
|
@@ -45,7 +49,7 @@ describe ThinkingSphinx::ArrayExtractOptions do
|
|
45
49
|
|
46
50
|
describe "extends Array" do
|
47
51
|
it 'with extract_options!' do
|
48
|
-
Array.instance_methods.include?('extract_options!').should be_true
|
52
|
+
Array.instance_methods.collect { |m| m.to_s }.include?('extract_options!').should be_true
|
49
53
|
end
|
50
54
|
end
|
51
55
|
end
|
@@ -61,7 +65,9 @@ describe ThinkingSphinx::AbstractQuotedTableName do
|
|
61
65
|
|
62
66
|
describe "extends ActiveRecord::ConnectionAdapters::AbstractAdapter" do
|
63
67
|
it 'with quote_table_name' do
|
64
|
-
ActiveRecord::ConnectionAdapters::AbstractAdapter.instance_methods.
|
68
|
+
ActiveRecord::ConnectionAdapters::AbstractAdapter.instance_methods.collect { |m|
|
69
|
+
m.to_s
|
70
|
+
}.include?('quote_table_name').should be_true
|
65
71
|
end
|
66
72
|
end
|
67
73
|
end
|
@@ -77,7 +83,9 @@ describe ThinkingSphinx::MysqlQuotedTableName do
|
|
77
83
|
describe "extends ActiveRecord::ConnectionAdapters::MysqlAdapter" do
|
78
84
|
it 'with quote_table_name' do
|
79
85
|
adapter = defined?(JRUBY_VERSION) ? :JdbcAdapter : :MysqlAdapter
|
80
|
-
ActiveRecord::ConnectionAdapters.const_get(adapter).instance_methods.
|
86
|
+
ActiveRecord::ConnectionAdapters.const_get(adapter).instance_methods.collect { |m|
|
87
|
+
m.to_s
|
88
|
+
}.include?("quote_table_name").should be_true
|
81
89
|
end
|
82
90
|
end
|
83
91
|
end
|
@@ -3,66 +3,146 @@ require 'will_paginate/collection'
|
|
3
3
|
|
4
4
|
describe ThinkingSphinx::Search do
|
5
5
|
describe "search method" do
|
6
|
-
before :each do
|
7
|
-
@client = Riddle::Client.stub_instance(
|
8
|
-
:filters => [],
|
9
|
-
:filters= => true,
|
10
|
-
:id_range= => true,
|
11
|
-
:sort_mode => :asc,
|
12
|
-
:limit => 5,
|
13
|
-
:offset= => 0,
|
14
|
-
:sort_mode= => true,
|
15
|
-
:query => {
|
16
|
-
:matches => [],
|
17
|
-
:total => 50
|
18
|
-
}
|
19
|
-
)
|
20
|
-
|
21
|
-
ThinkingSphinx::Search.stub_methods(
|
22
|
-
:client_from_options => @client,
|
23
|
-
:search_conditions => ["", []]
|
24
|
-
)
|
25
|
-
end
|
26
|
-
|
27
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
28
|
|
29
29
|
it "should not apply by default" do
|
30
|
+
@client.should_receive(:query).with("foo bar",'*','')
|
31
|
+
|
30
32
|
ThinkingSphinx::Search.search "foo bar"
|
31
|
-
@client.should have_received(:query).with("foo bar")
|
32
33
|
end
|
33
34
|
|
34
35
|
it "should apply when passed, and handle full extended syntax" do
|
35
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)}
|
36
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
|
+
|
37
40
|
ThinkingSphinx::Search.search input, :star => true
|
38
|
-
@client.should have_received(:query).with(expected)
|
39
41
|
end
|
40
42
|
|
41
43
|
it "should default to /\w+/ as token" do
|
44
|
+
@client.should_receive(:query).with("*foo*@*bar*.*com*",'*','')
|
45
|
+
|
42
46
|
ThinkingSphinx::Search.search "foo@bar.com", :star => true
|
43
|
-
@client.should have_received(:query).with("*foo*@*bar*.*com*")
|
44
47
|
end
|
45
48
|
|
46
49
|
it "should honour custom token" do
|
50
|
+
@client.should_receive(:query).with("*foo@bar.com* -*foo-bar*",'*','')
|
51
|
+
|
47
52
|
ThinkingSphinx::Search.search "foo@bar.com -foo-bar", :star => /[\w@.-]+/u
|
48
|
-
|
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)
|
49
99
|
end
|
50
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
|
51
124
|
end
|
52
125
|
end
|
53
126
|
|
54
127
|
describe "facets method" do
|
55
128
|
before :each do
|
56
|
-
@
|
57
|
-
|
58
|
-
|
59
|
-
|
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)
|
60
138
|
|
61
139
|
@config = ThinkingSphinx::Configuration.instance
|
62
140
|
@config.configuration.searchd.max_matches = 10_000
|
63
141
|
end
|
64
142
|
|
65
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
|
+
|
66
146
|
ThinkingSphinx::Search.should_receive(:search) do |options|
|
67
147
|
options[:max_matches].should == 10_000
|
68
148
|
options[:limit].should == 10_000
|
@@ -72,6 +152,8 @@ describe ThinkingSphinx::Search do
|
|
72
152
|
end
|
73
153
|
|
74
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
|
+
|
75
157
|
@config.configuration.searchd.max_matches = nil
|
76
158
|
ThinkingSphinx::Search.should_receive(:search) do |options|
|
77
159
|
options[:max_matches].should == 1000
|
@@ -82,6 +164,8 @@ describe ThinkingSphinx::Search do
|
|
82
164
|
end
|
83
165
|
|
84
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
|
+
|
85
169
|
ThinkingSphinx::Search.should_receive(:search) do |options|
|
86
170
|
options[:max_matches].should == 10_000
|
87
171
|
options[:limit].should == 10_000
|
@@ -93,6 +177,47 @@ describe ThinkingSphinx::Search do
|
|
93
177
|
:limit => 200
|
94
178
|
)
|
95
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
|
96
221
|
end
|
97
222
|
end
|
98
223
|
|
@@ -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
|