freelancing-god-thinking-sphinx 0.9.6 → 0.9.7
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.
- data/README +23 -1
- data/lib/thinking_sphinx.rb +4 -1
- data/lib/thinking_sphinx/active_record.rb +19 -0
- data/lib/thinking_sphinx/active_record/delta.rb +4 -0
- data/lib/thinking_sphinx/association.rb +5 -5
- data/lib/thinking_sphinx/attribute.rb +5 -2
- data/lib/thinking_sphinx/configuration.rb +86 -104
- data/lib/thinking_sphinx/field.rb +17 -5
- data/lib/thinking_sphinx/index.rb +71 -2
- data/lib/thinking_sphinx/index/builder.rb +5 -5
- data/lib/thinking_sphinx/search.rb +7 -1
- data/spec/unit/thinking_sphinx/active_record/delta_spec.rb +4 -0
- data/spec/unit/thinking_sphinx/active_record/search_spec.rb +63 -0
- data/spec/unit/thinking_sphinx/active_record_spec.rb +74 -2
- data/spec/unit/thinking_sphinx/association_spec.rb +247 -0
- data/spec/unit/thinking_sphinx/attribute_spec.rb +285 -2
- data/spec/unit/thinking_sphinx/configuration_spec.rb +431 -0
- data/spec/unit/thinking_sphinx/field_spec.rb +169 -5
- data/spec/unit/thinking_sphinx/index_spec.rb +186 -1
- data/spec/unit/thinking_sphinx_spec.rb +25 -0
- metadata +2 -2
@@ -1,7 +1,21 @@
|
|
1
1
|
require 'spec/spec_helper'
|
2
2
|
|
3
3
|
describe ThinkingSphinx::Field do
|
4
|
-
describe
|
4
|
+
describe '#initialize' do
|
5
|
+
it 'raises if no columns are provided so that configuration errors are easier to track down' do
|
6
|
+
lambda {
|
7
|
+
ThinkingSphinx::Field.new([])
|
8
|
+
}.should raise_error(RuntimeError)
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'raises if an element of the columns param is an integer - as happens when you use id instead of :id - so that configuration errors are easier to track down' do
|
12
|
+
lambda {
|
13
|
+
ThinkingSphinx::Field.new([1234])
|
14
|
+
}.should raise_error(RuntimeError)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "to_select_sql method with MySQL" do
|
5
19
|
before :each do
|
6
20
|
@index = Person.indexes.first
|
7
21
|
@index.link!
|
@@ -20,9 +34,31 @@ describe ThinkingSphinx::Field do
|
|
20
34
|
end
|
21
35
|
end
|
22
36
|
|
37
|
+
describe "to_select_sql method with PostgreSQL" do
|
38
|
+
before :each do
|
39
|
+
@index = Person.indexes.first
|
40
|
+
Person.connection.class.stub_method(
|
41
|
+
:name => "ActiveRecord::ConnectionAdapters::PostgreSQLAdapter"
|
42
|
+
)
|
43
|
+
@index.link!
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should concat with spaces if there are multiple columns" do
|
47
|
+
@index.fields.first.to_select_sql.should match(/|| ' ' ||/)
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should concat with spaces if a column has more than one association" do
|
51
|
+
@index.fields[1].to_select_sql.should match(/|| ' ' ||/)
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should group if any association for any column is a has_many or has_and_belongs_to_many" do
|
55
|
+
@index.fields[2].to_select_sql.should match(/array_to_string\(array_accum\(/)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
23
59
|
describe "to_group_sql method" do
|
24
60
|
before :each do
|
25
|
-
@field = ThinkingSphinx::Field.new([])
|
61
|
+
@field = ThinkingSphinx::Field.new([Object.stub_instance(:__stack => [])])
|
26
62
|
@field.stub_methods(:is_many? => false)
|
27
63
|
|
28
64
|
ThinkingSphinx.stub_method(:use_group_by_shortcut? => false)
|
@@ -41,11 +77,139 @@ describe ThinkingSphinx::Field do
|
|
41
77
|
end
|
42
78
|
|
43
79
|
it "should return an array if neither is_many? or shortcut allowed" do
|
80
|
+
@field.stub_method(:column_with_prefix => 'hello')
|
44
81
|
@field.to_group_sql.should be_a_kind_of(Array)
|
45
82
|
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe "unique_name method" do
|
86
|
+
before :each do
|
87
|
+
@field = ThinkingSphinx::Field.new [
|
88
|
+
Object.stub_instance(:__stack => [], :__name => "col_name")
|
89
|
+
]
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should use the alias if there is one" do
|
93
|
+
@field.alias = "alias"
|
94
|
+
@field.unique_name.should == "alias"
|
95
|
+
end
|
96
|
+
|
97
|
+
it "should use the alias if there's multiple columns" do
|
98
|
+
@field.columns << Object.stub_instance(:__stack => [], :__name => "col_name")
|
99
|
+
@field.unique_name.should be_nil
|
100
|
+
|
101
|
+
@field.alias = "alias"
|
102
|
+
@field.unique_name.should == "alias"
|
103
|
+
end
|
104
|
+
|
105
|
+
it "should use the column name if there's no alias and just one column" do
|
106
|
+
@field.unique_name.should == "col_name"
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
describe "prefixes method" do
|
111
|
+
it "should default to false" do
|
112
|
+
@field = ThinkingSphinx::Field.new([Object.stub_instance(:__stack => [])])
|
113
|
+
@field.prefixes.should be_false
|
114
|
+
end
|
46
115
|
|
47
|
-
|
48
|
-
ThinkingSphinx.
|
116
|
+
it "should be true if the corresponding option is set" do
|
117
|
+
@field = ThinkingSphinx::Field.new(
|
118
|
+
[Object.stub_instance(:__stack => [])], :prefixes => true
|
119
|
+
)
|
120
|
+
@field.prefixes.should be_true
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
describe "infixes method" do
|
125
|
+
it "should default to false" do
|
126
|
+
@field = ThinkingSphinx::Field.new([Object.stub_instance(:__stack => [])])
|
127
|
+
@field.infixes.should be_false
|
128
|
+
end
|
129
|
+
|
130
|
+
it "should be true if the corresponding option is set" do
|
131
|
+
@field = ThinkingSphinx::Field.new(
|
132
|
+
[Object.stub_instance(:__stack => [])], :infixes => true
|
133
|
+
)
|
134
|
+
@field.infixes.should be_true
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
describe "quote_column_name method" do
|
139
|
+
it "should delegate the call to the model's connection" do
|
140
|
+
@field = ThinkingSphinx::Field.new [
|
141
|
+
ThinkingSphinx::Index::FauxColumn.new(:col_name)
|
142
|
+
]
|
143
|
+
@field.model = Person
|
144
|
+
Person.connection.stub_method(:quote_column_name => "quoted!")
|
145
|
+
|
146
|
+
@field.send(:quote_column, "blah").should == "quoted!"
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
describe "column_with_prefix method" do
|
151
|
+
before :each do
|
152
|
+
@field = ThinkingSphinx::Field.new [
|
153
|
+
ThinkingSphinx::Index::FauxColumn.new(:col_name)
|
154
|
+
]
|
155
|
+
@field.columns.each { |col| @field.associations[col] = [] }
|
156
|
+
@field.model = Person
|
157
|
+
|
158
|
+
@first_join = Object.stub_instance(:aliased_table_name => "tabular")
|
159
|
+
@second_join = Object.stub_instance(:aliased_table_name => "data")
|
160
|
+
|
161
|
+
@first_assoc = ThinkingSphinx::Association.stub_instance(:join => @first_join)
|
162
|
+
@second_assoc = ThinkingSphinx::Association.stub_instance(:join => @second_join)
|
163
|
+
end
|
164
|
+
|
165
|
+
it "should return the column with model's table prefix if there's no associations for the column" do
|
166
|
+
@field.send(:column_with_prefix, @field.columns.first).should == "`people`.`col_name`"
|
167
|
+
end
|
168
|
+
|
169
|
+
it "should return the column with its join table prefix if an association exists" do
|
170
|
+
column = @field.columns.first
|
171
|
+
@field.associations[column] = [@first_assoc]
|
172
|
+
@field.send(:column_with_prefix, column).should == "`tabular`.`col_name`"
|
173
|
+
end
|
174
|
+
|
175
|
+
it "should return multiple columns concatenated if more than one association exists" do
|
176
|
+
column = @field.columns.first
|
177
|
+
@field.associations[column] = [@first_assoc, @second_assoc]
|
178
|
+
@field.send(:column_with_prefix, column).should == "`tabular`.`col_name`, `data`.`col_name`"
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
describe "is_many? method" do
|
183
|
+
before :each do
|
184
|
+
@assoc_a = Object.stub_instance(:is_many? => true)
|
185
|
+
@assoc_b = Object.stub_instance(:is_many? => true)
|
186
|
+
@assoc_c = Object.stub_instance(:is_many? => true)
|
187
|
+
|
188
|
+
@field = ThinkingSphinx::Field.new(
|
189
|
+
[ThinkingSphinx::Index::FauxColumn.new(:col_name)]
|
190
|
+
)
|
191
|
+
@field.associations = {
|
192
|
+
:a => @assoc_a, :b => @assoc_b, :c => @assoc_c
|
193
|
+
}
|
194
|
+
end
|
195
|
+
|
196
|
+
it "should return true if all associations return true to is_many?" do
|
197
|
+
@field.send(:is_many?).should be_true
|
198
|
+
end
|
199
|
+
|
200
|
+
it "should return true if one association returns true to is_many?" do
|
201
|
+
@assoc_b.stub_method(:is_many? => false)
|
202
|
+
@assoc_c.stub_method(:is_many? => false)
|
203
|
+
|
204
|
+
@field.send(:is_many?).should be_true
|
205
|
+
end
|
206
|
+
|
207
|
+
it "should return false if all associations return false to is_many?" do
|
208
|
+
@assoc_a.stub_method(:is_many? => false)
|
209
|
+
@assoc_b.stub_method(:is_many? => false)
|
210
|
+
@assoc_c.stub_method(:is_many? => false)
|
211
|
+
|
212
|
+
@field.send(:is_many?).should be_false
|
49
213
|
end
|
50
214
|
end
|
51
|
-
end
|
215
|
+
end
|
@@ -1,5 +1,190 @@
|
|
1
1
|
require 'spec/spec_helper'
|
2
2
|
|
3
3
|
describe ThinkingSphinx::Index do
|
4
|
-
|
4
|
+
describe "to_config method" do
|
5
|
+
before :each do
|
6
|
+
@index = ThinkingSphinx::Index.new(Person)
|
7
|
+
|
8
|
+
@index.stub_methods(
|
9
|
+
:attributes => [
|
10
|
+
ThinkingSphinx::Attribute.stub_instance(:to_sphinx_clause => "attr a"),
|
11
|
+
ThinkingSphinx::Attribute.stub_instance(:to_sphinx_clause => "attr b")
|
12
|
+
],
|
13
|
+
:link! => true,
|
14
|
+
:adapter => :mysql,
|
15
|
+
:to_sql_query_pre => "sql_query_pre",
|
16
|
+
:to_sql => "SQL",
|
17
|
+
:to_sql_query_range => "sql_query_range",
|
18
|
+
:to_sql_query_info => "sql_query_info",
|
19
|
+
:delta? => false
|
20
|
+
)
|
21
|
+
|
22
|
+
@database = {
|
23
|
+
:host => "localhost",
|
24
|
+
:username => "username",
|
25
|
+
:password => "blank",
|
26
|
+
:database => "db"
|
27
|
+
}
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should call link!" do
|
31
|
+
@index.to_config(0, @database, "utf-8")
|
32
|
+
|
33
|
+
@index.should have_received(:link!)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should raise an exception if the adapter isn't mysql or postgres" do
|
37
|
+
@index.stub_method(:adapter => :sqlite)
|
38
|
+
|
39
|
+
lambda { @index.to_config(0, @database, "utf-8") }.should raise_error
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should set the core source name to {model}_{index}_core" do
|
43
|
+
@index.to_config(0, @database, "utf-8").should match(
|
44
|
+
/source person_0_core/
|
45
|
+
)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should include the database config supplied" do
|
49
|
+
conf = @index.to_config(0, @database, "utf-8")
|
50
|
+
conf.should match(/type\s+= mysql/)
|
51
|
+
conf.should match(/sql_host\s+= localhost/)
|
52
|
+
conf.should match(/sql_user\s+= username/)
|
53
|
+
conf.should match(/sql_pass\s+= blank/)
|
54
|
+
conf.should match(/sql_db\s+= db/)
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should have a pre query 'SET NAMES utf8' if using mysql and utf8 charset" do
|
58
|
+
@index.to_config(0, @database, "utf-8").should match(
|
59
|
+
/sql_query_pre\s+= SET NAMES utf8/
|
60
|
+
)
|
61
|
+
|
62
|
+
@index.to_config(0, @database, "non-utf-8").should_not match(
|
63
|
+
/SET NAMES utf8/
|
64
|
+
)
|
65
|
+
|
66
|
+
@index.stub_method(:adapter => :postgres)
|
67
|
+
@index.to_config(0, @database, "utf-8").should_not match(
|
68
|
+
/SET NAMES utf8/
|
69
|
+
)
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should use the pre query from the index" do
|
73
|
+
@index.to_config(0, @database, "utf-8").should match(
|
74
|
+
/sql_query_pre\s+= sql_query_pre/
|
75
|
+
)
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should use the main query from the index" do
|
79
|
+
@index.to_config(0, @database, "utf-8").should match(
|
80
|
+
/sql_query\s+= SQL/
|
81
|
+
)
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should use the range query from the index" do
|
85
|
+
@index.to_config(0, @database, "utf-8").should match(
|
86
|
+
/sql_query_range\s+= sql_query_range/
|
87
|
+
)
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should use the info query from the index" do
|
91
|
+
@index.to_config(0, @database, "utf-8").should match(
|
92
|
+
/sql_query_info\s+= sql_query_info/
|
93
|
+
)
|
94
|
+
end
|
95
|
+
|
96
|
+
it "should include the attribute sources" do
|
97
|
+
@index.to_config(0, @database, "utf-8").should match(
|
98
|
+
/attr a\n\s+attr b/
|
99
|
+
)
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should add a delta index with name {model}_{index}_delta if requested" do
|
103
|
+
@index.stub_method(:delta? => true)
|
104
|
+
|
105
|
+
@index.to_config(0, @database, "utf-8").should match(
|
106
|
+
/source person_0_delta/
|
107
|
+
)
|
108
|
+
end
|
109
|
+
|
110
|
+
it "should not add a delta index unless requested" do
|
111
|
+
@index.to_config(0, @database, "utf-8").should_not match(
|
112
|
+
/source person_0_delta/
|
113
|
+
)
|
114
|
+
end
|
115
|
+
|
116
|
+
it "should have the delta index inherit from the core index" do
|
117
|
+
@index.stub_method(:delta? => true)
|
118
|
+
|
119
|
+
@index.to_config(0, @database, "utf-8").should match(
|
120
|
+
/source person_0_delta : person_0_core/
|
121
|
+
)
|
122
|
+
end
|
123
|
+
|
124
|
+
it "should redefine the main query for the delta index" do
|
125
|
+
@index.stub_method(:delta? => true)
|
126
|
+
|
127
|
+
@index.to_config(0, @database, "utf-8").should match(
|
128
|
+
/source person_0_delta.+sql_query\s+= SQL/m
|
129
|
+
)
|
130
|
+
end
|
131
|
+
|
132
|
+
it "should redefine the range query for the delta index" do
|
133
|
+
@index.stub_method(:delta? => true)
|
134
|
+
|
135
|
+
@index.to_config(0, @database, "utf-8").should match(
|
136
|
+
/source person_0_delta.+sql_query_range\s+= sql_query_range/m
|
137
|
+
)
|
138
|
+
end
|
139
|
+
|
140
|
+
it "should redefine the pre query for the delta index" do
|
141
|
+
@index.stub_method(:delta? => true)
|
142
|
+
|
143
|
+
@index.to_config(0, @database, "utf-8").should match(
|
144
|
+
/source person_0_delta.+sql_query_pre\s+=\s*\n/m
|
145
|
+
)
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
describe "prefix_fields method" do
|
150
|
+
before :each do
|
151
|
+
@index = ThinkingSphinx::Index.new(Person)
|
152
|
+
|
153
|
+
@field_a = ThinkingSphinx::Field.stub_instance(:prefixes => true)
|
154
|
+
@field_b = ThinkingSphinx::Field.stub_instance(:prefixes => false)
|
155
|
+
@field_c = ThinkingSphinx::Field.stub_instance(:prefixes => true)
|
156
|
+
|
157
|
+
@index.fields = [@field_a, @field_b, @field_c]
|
158
|
+
end
|
159
|
+
|
160
|
+
it "should return fields that are flagged as prefixed" do
|
161
|
+
@index.prefix_fields.should include(@field_a)
|
162
|
+
@index.prefix_fields.should include(@field_c)
|
163
|
+
end
|
164
|
+
|
165
|
+
it "should not return fields that aren't flagged as prefixed" do
|
166
|
+
@index.prefix_fields.should_not include(@field_b)
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
describe "infix_fields" do
|
171
|
+
before :each do
|
172
|
+
@index = ThinkingSphinx::Index.new(Person)
|
173
|
+
|
174
|
+
@field_a = ThinkingSphinx::Field.stub_instance(:infixes => true)
|
175
|
+
@field_b = ThinkingSphinx::Field.stub_instance(:infixes => false)
|
176
|
+
@field_c = ThinkingSphinx::Field.stub_instance(:infixes => true)
|
177
|
+
|
178
|
+
@index.fields = [@field_a, @field_b, @field_c]
|
179
|
+
end
|
180
|
+
|
181
|
+
it "should return fields that are flagged as infixed" do
|
182
|
+
@index.infix_fields.should include(@field_a)
|
183
|
+
@index.infix_fields.should include(@field_c)
|
184
|
+
end
|
185
|
+
|
186
|
+
it "should not return fields that aren't flagged as infixed" do
|
187
|
+
@index.infix_fields.should_not include(@field_b)
|
188
|
+
end
|
189
|
+
end
|
5
190
|
end
|
@@ -78,5 +78,30 @@ describe ThinkingSphinx do
|
|
78
78
|
|
79
79
|
ThinkingSphinx.use_group_by_shortcut?.should be_false
|
80
80
|
end
|
81
|
+
|
82
|
+
describe "if not using MySQL" do
|
83
|
+
before :each do
|
84
|
+
@connection = ::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.stub_instance(
|
85
|
+
:select_all => true
|
86
|
+
)
|
87
|
+
::ActiveRecord::Base.stub_method(
|
88
|
+
:connection => @connection
|
89
|
+
)
|
90
|
+
end
|
91
|
+
|
92
|
+
after :each do
|
93
|
+
::ActiveRecord::Base.unstub_method(:connection)
|
94
|
+
end
|
95
|
+
|
96
|
+
it "should return false" do
|
97
|
+
ThinkingSphinx.use_group_by_shortcut?.should be_false
|
98
|
+
end
|
99
|
+
|
100
|
+
it "should not call select_all" do
|
101
|
+
ThinkingSphinx.use_group_by_shortcut?
|
102
|
+
|
103
|
+
@connection.should_not have_received(:select_all)
|
104
|
+
end
|
105
|
+
end
|
81
106
|
end
|
82
107
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: freelancing-god-thinking-sphinx
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pat Allan
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-05
|
12
|
+
date: 2008-06-05 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|