airblade-Sphincter 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +15 -0
- data/LICENSE.txt +27 -0
- data/Manifest.txt +16 -0
- data/README.txt +153 -0
- data/Rakefile +21 -0
- data/lib/sphincter.rb +107 -0
- data/lib/sphincter/association_searcher.rb +22 -0
- data/lib/sphincter/configure.rb +499 -0
- data/lib/sphincter/search.rb +199 -0
- data/lib/sphincter/search_stub.rb +60 -0
- data/lib/sphincter/tasks.rb +114 -0
- data/test/sphincter_test_case.rb +220 -0
- data/test/test_sphincter_association_searcher.rb +39 -0
- data/test/test_sphincter_configure.rb +386 -0
- data/test/test_sphincter_search.rb +100 -0
- data/test/test_sphincter_search_stub.rb +50 -0
- metadata +101 -0
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'test/sphincter_test_case'
|
2
|
+
require 'sphincter/association_searcher'
|
3
|
+
|
4
|
+
class TestSphincterAssociationSearcher < SphincterTestCase
|
5
|
+
|
6
|
+
class Proxy
|
7
|
+
include Sphincter::AssociationSearcher
|
8
|
+
|
9
|
+
attr_accessor :reflection
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
@reflection = SphincterTestCase::BelongsTo.reflections.last
|
13
|
+
klass = Object.new
|
14
|
+
def klass.search_args() @search_args end
|
15
|
+
def klass.search(*args) @search_args = args; :searched end
|
16
|
+
@reflection.klass = klass
|
17
|
+
end
|
18
|
+
|
19
|
+
def proxy_reflection()
|
20
|
+
@reflection
|
21
|
+
end
|
22
|
+
|
23
|
+
def proxy_owner()
|
24
|
+
SphincterTestCase::BelongsTo.new
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_search
|
29
|
+
proxy = Proxy.new
|
30
|
+
|
31
|
+
results = proxy.search 'words'
|
32
|
+
|
33
|
+
assert_equal :searched, results
|
34
|
+
assert_equal ['words', { :conditions => { 'models_id' => 42 } } ],
|
35
|
+
proxy.proxy_reflection.klass.search_args
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
@@ -0,0 +1,386 @@
|
|
1
|
+
require 'test/sphincter_test_case'
|
2
|
+
require 'sphincter/configure'
|
3
|
+
|
4
|
+
class TestSphincterConfigure < SphincterTestCase
|
5
|
+
|
6
|
+
DEFAULT_GET_CONF_EXPECTED = {
|
7
|
+
"mysql" => {
|
8
|
+
"sql_query_pre" => [
|
9
|
+
"SET NAMES utf8",
|
10
|
+
]
|
11
|
+
},
|
12
|
+
"sphincter" => {
|
13
|
+
"address" => "127.0.0.1",
|
14
|
+
"path" => "sphinx/RAILS_ENV",
|
15
|
+
"per_page" => 10,
|
16
|
+
"port" => 3312,
|
17
|
+
},
|
18
|
+
"index" => {
|
19
|
+
"charset_type" => "utf-8",
|
20
|
+
"docinfo" => "extern",
|
21
|
+
"min_word_len" => 1,
|
22
|
+
"morphology" => "stem_en",
|
23
|
+
"stopwords" => "",
|
24
|
+
},
|
25
|
+
"source" => {
|
26
|
+
"index_html_attrs" => "",
|
27
|
+
"sql_query_post" => "",
|
28
|
+
"sql_range_step" => 20000,
|
29
|
+
"strip_html" => 0,
|
30
|
+
},
|
31
|
+
"indexer" => {
|
32
|
+
"mem_limit" => "32M",
|
33
|
+
},
|
34
|
+
"searchd" => {
|
35
|
+
"address" => "127.0.0.1",
|
36
|
+
"log" => "log/sphinx/searchd.RAILS_ENV.log",
|
37
|
+
"max_children" => 30,
|
38
|
+
"max_matches" => 1000,
|
39
|
+
"pid_file" => "sphinx/RAILS_ENV/searchd.pid",
|
40
|
+
"port" => 3312,
|
41
|
+
"query_log" => "log/sphinx/query.RAILS_ENV.log",
|
42
|
+
"read_timeout" => 5,
|
43
|
+
}
|
44
|
+
}
|
45
|
+
|
46
|
+
def test_self_configure
|
47
|
+
expected = <<-EOF
|
48
|
+
indexer
|
49
|
+
{
|
50
|
+
mem_limit = 32M
|
51
|
+
}
|
52
|
+
|
53
|
+
searchd
|
54
|
+
{
|
55
|
+
address = 127.0.0.1
|
56
|
+
log = log/sphinx/searchd.RAILS_ENV.log
|
57
|
+
max_children = 30
|
58
|
+
max_matches = 1000
|
59
|
+
pid_file = sphinx/RAILS_ENV/searchd.pid
|
60
|
+
port = 3312
|
61
|
+
query_log = log/sphinx/query.RAILS_ENV.log
|
62
|
+
read_timeout = 5
|
63
|
+
}
|
64
|
+
EOF
|
65
|
+
|
66
|
+
Sphincter::Configure.configure
|
67
|
+
|
68
|
+
assert_equal expected, File.read(Sphincter::Configure.sphinx_conf)
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_self_deep_merge
|
72
|
+
h1 = { 'x' => { 'm' => 0 },
|
73
|
+
'y' => { 'a' => 1, 'b' => 2 } }
|
74
|
+
h2 = { 'y' => { 'b' => -2, 'c' => -3 },
|
75
|
+
'z' => { 'm' => 0 } }
|
76
|
+
result = Sphincter::Configure.deep_merge h1, h2
|
77
|
+
|
78
|
+
expected = {
|
79
|
+
'x' => { 'm' => 0 },
|
80
|
+
'y' => { 'a' => 1, 'b' => -2, 'c' => -3 },
|
81
|
+
'z' => { 'm' => 0 },
|
82
|
+
}
|
83
|
+
|
84
|
+
assert_equal expected, result
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_self_get_conf
|
88
|
+
expected = DEFAULT_GET_CONF_EXPECTED
|
89
|
+
|
90
|
+
assert_equal expected, Sphincter::Configure.get_conf
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_self_get_conf_app_conf
|
94
|
+
FileUtils.mkdir_p 'config'
|
95
|
+
File.open 'config/sphincter.yml', 'w' do |fp|
|
96
|
+
fp.puts "sphincter:\n port: 3313"
|
97
|
+
end
|
98
|
+
|
99
|
+
expected = util_deep_clone DEFAULT_GET_CONF_EXPECTED
|
100
|
+
expected['sphincter']['port'] = 3313
|
101
|
+
expected['searchd']['port'] = 3313
|
102
|
+
|
103
|
+
assert_equal expected, Sphincter::Configure.get_conf
|
104
|
+
end
|
105
|
+
|
106
|
+
def test_self_get_conf_env_conf
|
107
|
+
FileUtils.mkdir_p 'config/environments'
|
108
|
+
File.open 'config/sphincter.yml', 'w' do |fp|
|
109
|
+
fp.puts "sphincter:\n port: 3313"
|
110
|
+
end
|
111
|
+
File.open "config/environments/sphincter.#{RAILS_ENV}.yml", 'w' do |fp|
|
112
|
+
fp.puts "sphincter:\n port: 3314"
|
113
|
+
end
|
114
|
+
|
115
|
+
expected = util_deep_clone DEFAULT_GET_CONF_EXPECTED
|
116
|
+
expected['sphincter']['port'] = 3314
|
117
|
+
expected['searchd']['port'] = 3314
|
118
|
+
|
119
|
+
assert_equal expected, Sphincter::Configure.get_conf
|
120
|
+
end
|
121
|
+
|
122
|
+
def test_self_get_conf_from
|
123
|
+
assert_equal Hash.new, Sphincter::Configure.get_conf_from('/nonexistent')
|
124
|
+
|
125
|
+
File.open 'foo.yml', 'w' do |fp| fp.puts "foo:\n bar" end
|
126
|
+
|
127
|
+
assert_equal({'foo' => 'bar'}, Sphincter::Configure.get_conf_from('foo.yml'))
|
128
|
+
end
|
129
|
+
|
130
|
+
def test_self_get_sources
|
131
|
+
Sphincter::Search.indexes[Model] << {
|
132
|
+
:fields => %w[text belongs_to.string]
|
133
|
+
}
|
134
|
+
|
135
|
+
expected = {
|
136
|
+
"models" => {
|
137
|
+
"strip_html" => 0,
|
138
|
+
"sql_group_column" => ["sphincter_index_id"],
|
139
|
+
"sql_query_range" => "SELECT MIN(`id`), MAX(`id`) FROM models",
|
140
|
+
"sql_query_info" =>
|
141
|
+
"SELECT * FROM models WHERE models.`id` = (($id - 0) / 1)",
|
142
|
+
"sql_date_column" => [],
|
143
|
+
"sql_query" =>
|
144
|
+
"SELECT (models.`id` * 1 + 0) AS `id`, " \
|
145
|
+
"0 AS sphincter_index_id, " \
|
146
|
+
"'Model' AS sphincter_klass, "\
|
147
|
+
"models.`text` AS `text`, " \
|
148
|
+
"belongs_tos.`string` AS `belongs_tos_string` "\
|
149
|
+
"FROM models LEFT JOIN belongs_tos ON " \
|
150
|
+
"models.`belongs_to_id` = belongs_tos.`id` " \
|
151
|
+
"WHERE models.`id` >= $start AND models.`id` <= $end"
|
152
|
+
}
|
153
|
+
}
|
154
|
+
|
155
|
+
assert_equal expected, Sphincter::Configure.get_sources
|
156
|
+
end
|
157
|
+
|
158
|
+
def test_self_get_db_conf
|
159
|
+
expected = {
|
160
|
+
'type' => 'mysql',
|
161
|
+
'sql_host' => 'host',
|
162
|
+
'sql_pass' => 'password',
|
163
|
+
'sql_db' => 'database',
|
164
|
+
'sql_user' => 'username',
|
165
|
+
'sql_sock' => 'socket',
|
166
|
+
}
|
167
|
+
|
168
|
+
assert_equal expected, Sphincter::Configure.get_db_conf
|
169
|
+
end
|
170
|
+
|
171
|
+
def test_self_index_count
|
172
|
+
Sphincter::Search.indexes[Object] << { :fields => %w[title body] }
|
173
|
+
Sphincter::Search.indexes[Object] << {
|
174
|
+
:fields => %w[title body], :name => 'foo'
|
175
|
+
}
|
176
|
+
|
177
|
+
assert_equal 2, Sphincter::Configure.index_count
|
178
|
+
|
179
|
+
expected = {
|
180
|
+
Object => [
|
181
|
+
{ :index_id => 0, :fields => %w[title body] },
|
182
|
+
{ :index_id => 1, :fields => %w[title body], :name => 'foo' },
|
183
|
+
],
|
184
|
+
}
|
185
|
+
|
186
|
+
assert_equal expected, Sphincter::Search.indexes
|
187
|
+
end
|
188
|
+
|
189
|
+
def test_self_section
|
190
|
+
heading = 'searchd'
|
191
|
+
data = {
|
192
|
+
'array' => %w[value1 value2],
|
193
|
+
'empty' => '',
|
194
|
+
'nil' => nil,
|
195
|
+
'string' => 'value',
|
196
|
+
}
|
197
|
+
|
198
|
+
expected = <<-EOF.strip
|
199
|
+
searchd
|
200
|
+
{
|
201
|
+
array = value1
|
202
|
+
array = value2
|
203
|
+
empty =
|
204
|
+
nil =
|
205
|
+
string = value
|
206
|
+
}
|
207
|
+
EOF
|
208
|
+
|
209
|
+
assert_equal expected, Sphincter::Configure.section(heading, data)
|
210
|
+
end
|
211
|
+
|
212
|
+
def test_self_sphinx_conf
|
213
|
+
assert_equal File.join(RAILS_ROOT, 'sphinx/RAILS_ENV/sphinx.conf'),
|
214
|
+
Sphincter::Configure.sphinx_conf
|
215
|
+
end
|
216
|
+
|
217
|
+
def test_self_sphinx_dir
|
218
|
+
assert_equal File.join(RAILS_ROOT, 'sphinx/RAILS_ENV'),
|
219
|
+
Sphincter::Configure.sphinx_dir
|
220
|
+
end
|
221
|
+
|
222
|
+
def test_self_write_configuration
|
223
|
+
conf = Hash.new { |h,k| h[k] = {} }
|
224
|
+
sources = Hash.new { |h,k| h[k] = {} }
|
225
|
+
|
226
|
+
conf['sphincter']['path'] = 'sphinx/development'
|
227
|
+
conf['source']['key1'] = 'value1'
|
228
|
+
conf['index']['key1'] = 'value1'
|
229
|
+
|
230
|
+
sources['source_1']['key2'] = 'value2'
|
231
|
+
|
232
|
+
sources['source_2']['key1'] = 'value3'
|
233
|
+
sources['source_2']['key2'] = 'value4'
|
234
|
+
|
235
|
+
expected = <<-EOF
|
236
|
+
indexer
|
237
|
+
{
|
238
|
+
}
|
239
|
+
|
240
|
+
searchd
|
241
|
+
{
|
242
|
+
}
|
243
|
+
|
244
|
+
source source_1
|
245
|
+
{
|
246
|
+
key1 = value1
|
247
|
+
key2 = value2
|
248
|
+
}
|
249
|
+
|
250
|
+
index source_1
|
251
|
+
{
|
252
|
+
key1 = value1
|
253
|
+
path = #{Sphincter::Configure.sphinx_dir}/source_1
|
254
|
+
source = source_1
|
255
|
+
}
|
256
|
+
|
257
|
+
source source_2
|
258
|
+
{
|
259
|
+
key1 = value3
|
260
|
+
key2 = value4
|
261
|
+
}
|
262
|
+
|
263
|
+
index source_2
|
264
|
+
{
|
265
|
+
key1 = value1
|
266
|
+
path = #{Sphincter::Configure.sphinx_dir}/source_2
|
267
|
+
source = source_2
|
268
|
+
}
|
269
|
+
EOF
|
270
|
+
|
271
|
+
Sphincter::Configure.write_configuration conf, sources
|
272
|
+
|
273
|
+
assert_equal expected, File.read(Sphincter::Configure.sphinx_conf)
|
274
|
+
end
|
275
|
+
|
276
|
+
def util_deep_clone(obj)
|
277
|
+
Marshal.load Marshal.dump(obj)
|
278
|
+
end
|
279
|
+
|
280
|
+
end
|
281
|
+
|
282
|
+
class Sphincter::Configure::Index
|
283
|
+
attr_reader :fields, :where, :tables, :group
|
284
|
+
end
|
285
|
+
|
286
|
+
class TestSphincterConfigureIndex < SphincterTestCase
|
287
|
+
|
288
|
+
def setup
|
289
|
+
super
|
290
|
+
|
291
|
+
@index = Sphincter::Configure::Index.new Model, {}
|
292
|
+
end
|
293
|
+
|
294
|
+
def test_self_add_field
|
295
|
+
fields = []
|
296
|
+
fields << @index.add_field('date')
|
297
|
+
fields << @index.add_field('datetime')
|
298
|
+
fields << @index.add_field('boolean')
|
299
|
+
fields << @index.add_field('integer')
|
300
|
+
fields << @index.add_field('string')
|
301
|
+
fields << @index.add_field('time')
|
302
|
+
fields << @index.add_field('timestamp')
|
303
|
+
fields << @index.add_field('text')
|
304
|
+
|
305
|
+
expected_fields = [
|
306
|
+
"UNIX_TIMESTAMP(models.`date`) AS `date`",
|
307
|
+
"UNIX_TIMESTAMP(models.`datetime`) AS `datetime`",
|
308
|
+
"models.`boolean` AS `boolean`",
|
309
|
+
"models.`integer` AS `integer`",
|
310
|
+
"models.`string` AS `string`",
|
311
|
+
"UNIX_TIMESTAMP(models.`time`) AS `time`",
|
312
|
+
"UNIX_TIMESTAMP(models.`timestamp`) AS `timestamp`",
|
313
|
+
"models.`text` AS `text`"
|
314
|
+
]
|
315
|
+
|
316
|
+
assert_equal expected_fields, fields
|
317
|
+
|
318
|
+
assert_equal %w[sphincter_index_id boolean integer],
|
319
|
+
@index.source_conf['sql_group_column']
|
320
|
+
assert_equal %w[date datetime time timestamp],
|
321
|
+
@index.source_conf['sql_date_column']
|
322
|
+
end
|
323
|
+
|
324
|
+
def test_self_add_field_unknown
|
325
|
+
e = assert_raise Sphincter::Error do
|
326
|
+
@index.add_field 'float'
|
327
|
+
end
|
328
|
+
|
329
|
+
assert_equal 'unknown column type float', e.message
|
330
|
+
end
|
331
|
+
|
332
|
+
def test_add_include_belongs_to
|
333
|
+
@index.add_include 'belongs_to', 'string'
|
334
|
+
|
335
|
+
assert_equal ["belongs_tos.`string` AS `belongs_tos_string`"], @index.fields
|
336
|
+
|
337
|
+
tables = "models LEFT JOIN belongs_tos ON " \
|
338
|
+
"models.`belongs_to_id` = belongs_tos.`id`"
|
339
|
+
assert_equal tables, @index.tables
|
340
|
+
|
341
|
+
assert_equal [], @index.where
|
342
|
+
|
343
|
+
assert_equal false, @index.group
|
344
|
+
end
|
345
|
+
|
346
|
+
def test_add_include_has_many
|
347
|
+
@index.add_include 'manys', 'string'
|
348
|
+
|
349
|
+
fields = [
|
350
|
+
"GROUP_CONCAT(has_manys.`string` SEPARATOR ' ') AS `has_manys_string`"
|
351
|
+
]
|
352
|
+
assert_equal fields, @index.fields
|
353
|
+
|
354
|
+
tables = "models LEFT JOIN has_manys ON models.`id` = has_manys.`manys_id`"
|
355
|
+
assert_equal tables, @index.tables
|
356
|
+
|
357
|
+
assert_equal [], @index.where
|
358
|
+
assert_equal true, @index.group
|
359
|
+
end
|
360
|
+
|
361
|
+
def test_add_include_has_many_polymorphic
|
362
|
+
@index.add_include 'polys', 'string'
|
363
|
+
|
364
|
+
fields = ["GROUP_CONCAT(polys.`string` SEPARATOR ' ') AS `polys_string`"]
|
365
|
+
assert_equal fields, @index.fields
|
366
|
+
|
367
|
+
tables = "models LEFT JOIN polys ON " \
|
368
|
+
"models.`id` = polys.`polyable_id` AND " \
|
369
|
+
"'Model' = polys.`polyable_type`"
|
370
|
+
assert_equal tables, @index.tables
|
371
|
+
|
372
|
+
assert_equal [], @index.where
|
373
|
+
assert_equal true, @index.group
|
374
|
+
end
|
375
|
+
|
376
|
+
def test_add_include_nonexistent_association
|
377
|
+
e = assert_raise Sphincter::Error do
|
378
|
+
@index.add_include 'nonexistent', 'string'
|
379
|
+
end
|
380
|
+
|
381
|
+
assert_equal "could not find association \"nonexistent\" in Model",
|
382
|
+
e.message
|
383
|
+
end
|
384
|
+
|
385
|
+
end
|
386
|
+
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'test/sphincter_test_case'
|
2
|
+
require 'sphincter/search'
|
3
|
+
|
4
|
+
SphincterTestCase::Model.extend Sphincter::Search
|
5
|
+
|
6
|
+
class TestSphincterSearch < SphincterTestCase
|
7
|
+
|
8
|
+
def test_self_indexes
|
9
|
+
assert_equal [], Sphincter::Search.indexes[Model]
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_add_index
|
13
|
+
Model.add_index :fields => %w[text]
|
14
|
+
|
15
|
+
assert_equal [{ :fields => %w[text belongs_to_id] }],
|
16
|
+
Sphincter::Search.indexes[Model]
|
17
|
+
|
18
|
+
belongs_to_belongs_to = BelongsTo.reflections.first
|
19
|
+
belongs_to_has_many = BelongsTo.reflections.last
|
20
|
+
|
21
|
+
assert_equal({}, belongs_to_belongs_to.options, 'BelongsTo belongs_to')
|
22
|
+
assert_equal({ :extend => [Sphincter::AssociationSearcher] },
|
23
|
+
belongs_to_has_many.options, 'BelongsTo has_many')
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_sphincter_convert_values
|
27
|
+
assert_equal [0, 1], Model.sphincter_convert_values([false, true])
|
28
|
+
|
29
|
+
now = Time.at 999_932_400
|
30
|
+
expected = [999_932_400]
|
31
|
+
|
32
|
+
assert_equal expected,
|
33
|
+
Model.sphincter_convert_values([999_932_400])
|
34
|
+
assert_equal expected,
|
35
|
+
Model.sphincter_convert_values([now])
|
36
|
+
assert_equal expected,
|
37
|
+
Model.sphincter_convert_values([Date.parse(now.to_s)])
|
38
|
+
assert_equal expected,
|
39
|
+
Model.sphincter_convert_values([DateTime.parse(now.to_s)])
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_search
|
43
|
+
Model.add_index :fields => %w[text]
|
44
|
+
Sphincter::Configure.get_conf['sphincter']['host'] = 'localhost'
|
45
|
+
Sphincter::Configure.get_conf['sphincter']['port'] = 3312
|
46
|
+
|
47
|
+
results = Model.search 'words'
|
48
|
+
|
49
|
+
assert_equal [11, 13, 12], results.records
|
50
|
+
assert_equal 3, results.total
|
51
|
+
assert_equal 10, results.per_page
|
52
|
+
|
53
|
+
assert_equal 'localhost', Sphinx::Client.last_client.host
|
54
|
+
assert_equal 3312, Sphinx::Client.last_client.port
|
55
|
+
assert_equal 'words', Sphinx::Client.last_client.query
|
56
|
+
assert_equal 'models', Sphinx::Client.last_client.index
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_search_between
|
60
|
+
Model.add_index :fields => %w[text]
|
61
|
+
|
62
|
+
now = Time.now
|
63
|
+
ago = now - 3600
|
64
|
+
between = {
|
65
|
+
:created_at => [ago, now]
|
66
|
+
}
|
67
|
+
|
68
|
+
Model.search 'words', :between => between
|
69
|
+
|
70
|
+
expected = { 'created_at' => [:range, ago.to_i, now.to_i] }
|
71
|
+
|
72
|
+
assert_equal expected, Sphinx::Client.last_client.filters
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_search_conditions
|
76
|
+
Model.add_index :fields => %w[text]
|
77
|
+
|
78
|
+
now = Time.now
|
79
|
+
conditions = {
|
80
|
+
:some_id => 1,
|
81
|
+
:other_id => [1, 2],
|
82
|
+
:boolean => [true, false],
|
83
|
+
}
|
84
|
+
|
85
|
+
Model.search 'words', :conditions => conditions
|
86
|
+
|
87
|
+
expected = { 'some_id' => [1], 'other_id' => [1, 2], 'boolean' => [1, 0] }
|
88
|
+
|
89
|
+
assert_equal expected, Sphinx::Client.last_client.filters
|
90
|
+
end
|
91
|
+
|
92
|
+
def test_search_index
|
93
|
+
Model.add_index :fields => %w[text]
|
94
|
+
|
95
|
+
Model.search 'words', :index => 'other'
|
96
|
+
|
97
|
+
assert_equal 'other', Sphinx::Client.last_client.index
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|