/
test_sphincter_configure.rb
347 lines (290 loc) · 9.07 KB
/
test_sphincter_configure.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
require 'test/sphincter_test_case'
require 'sphincter/configure'
class TestSphincterConfigure < SphincterTestCase
DEFAULT_GET_CONF_EXPECTED = {
"mysql" => {
"sql_query_pre" => [
"SET NAMES utf8",
]
},
"sphincter" => {
"address" => "127.0.0.1",
"path" => "sphinx/RAILS_ENV",
"per_page" => 10,
"port" => 3312,
},
"index" => {
"charset_type" => "utf-8",
"docinfo" => "extern",
"min_word_len" => 1,
"morphology" => "stem_en",
"stopwords" => "",
},
"source" => {
"index_html_attrs" => "",
"sql_query_post" => "",
"sql_range_step" => 20000,
"strip_html" => 0,
},
"indexer" => {
"mem_limit" => "32M",
},
"searchd" => {
"address" => "127.0.0.1",
"log" => "log/sphinx/searchd.RAILS_ENV.log",
"max_children" => 30,
"max_matches" => 1000,
"pid_file" => "sphinx/RAILS_ENV/searchd.pid",
"port" => 3312,
"query_log" => "log/sphinx/query.RAILS_ENV.log",
"read_timeout" => 5,
}
}
def test_self_configure
expected = <<-EOF
indexer
{
mem_limit = 32M
}
searchd
{
address = 127.0.0.1
log = log/sphinx/searchd.RAILS_ENV.log
max_children = 30
max_matches = 1000
pid_file = sphinx/RAILS_ENV/searchd.pid
port = 3312
query_log = log/sphinx/query.RAILS_ENV.log
read_timeout = 5
}
EOF
Sphincter::Configure.configure
assert_equal expected, File.read(Sphincter::Configure.sphinx_conf)
end
def test_self_deep_merge
h1 = { 'x' => { 'm' => 0 },
'y' => { 'a' => 1, 'b' => 2 } }
h2 = { 'y' => { 'b' => -2, 'c' => -3 },
'z' => { 'm' => 0 } }
result = Sphincter::Configure.deep_merge h1, h2
expected = {
'x' => { 'm' => 0 },
'y' => { 'a' => 1, 'b' => -2, 'c' => -3 },
'z' => { 'm' => 0 },
}
assert_equal expected, result
end
def test_self_get_conf
expected = DEFAULT_GET_CONF_EXPECTED
assert_equal expected, Sphincter::Configure.get_conf
end
def test_self_get_conf_app_conf
FileUtils.mkdir_p 'config'
File.open 'config/sphincter.yml', 'w' do |fp|
fp.puts "sphincter:\n port: 3313"
end
expected = util_deep_clone DEFAULT_GET_CONF_EXPECTED
expected['sphincter']['port'] = 3313
expected['searchd']['port'] = 3313
assert_equal expected, Sphincter::Configure.get_conf
end
def test_self_get_conf_env_conf
FileUtils.mkdir_p 'config/environments'
File.open 'config/sphincter.yml', 'w' do |fp|
fp.puts "sphincter:\n port: 3313"
end
File.open "config/environments/sphincter.#{RAILS_ENV}.yml", 'w' do |fp|
fp.puts "sphincter:\n port: 3314"
end
expected = util_deep_clone DEFAULT_GET_CONF_EXPECTED
expected['sphincter']['port'] = 3314
expected['searchd']['port'] = 3314
assert_equal expected, Sphincter::Configure.get_conf
end
def test_self_get_conf_from
assert_equal Hash.new, Sphincter::Configure.get_conf_from('/nonexistent')
File.open 'foo.yml', 'w' do |fp| fp.puts "foo:\n bar" end
assert_equal({'foo' => 'bar'}, Sphincter::Configure.get_conf_from('foo.yml'))
end
def test_self_get_sources
Sphincter::Search.indexes[Model] << { :fields => %w[text] }
expected = {
"models" => {
"strip_html" => 0,
"sql_group_column" => ["sphincter_index_id"],
"sql_query_range" => "SELECT MIN(`id`), MAX(`id`) FROM models",
"sql_query_info" =>
"SELECT * FROM models WHERE models.`id` = (($id - 0) / 1)",
"sql_date_column" => [],
"sql_query" =>
"SELECT (models.`id` * 1 + 0) AS `id`, " \
"0 AS sphincter_index_id, " \
"'Model' AS sphincter_klass, "\
"models.`text` AS `text` " \
"FROM models WHERE models.`id` >= $start AND " \
"models.`id` <= $end"
}
}
assert_equal expected, Sphincter::Configure.get_sources
end
def test_self_get_sources_include
Sphincter::Search.indexes[Model] << {
:fields => %w[text],
:include => %w[other.string]
}
expected = {
"models" => {
"strip_html" => 0,
"sql_group_column" => ["sphincter_index_id"],
"sql_query_range" => "SELECT MIN(`id`), MAX(`id`) FROM models",
"sql_query_info" =>
"SELECT * FROM models WHERE models.`id` = (($id - 0) / 1)",
"sql_date_column" => [],
"sql_query" =>
"SELECT (models.`id` * 1 + 0) AS `id`, " \
"0 AS sphincter_index_id, " \
"'Model' AS sphincter_klass, " \
"models.`text` AS `text`, " \
"others.`string` AS `others_string` " \
"FROM models, others " \
"WHERE models.`other_id` = others.`id` AND "\
"models.`id` >= $start AND " \
"models.`id` <= $end"
}
}
assert_equal expected, Sphincter::Configure.get_sources
end
def test_self_get_sources_field
source_conf = { 'sql_group_column' => [], 'sql_date_column' => [] }
klass = Model
fields = []
fields << Sphincter::Configure.get_sources_field(source_conf, klass,
'date')
fields << Sphincter::Configure.get_sources_field(source_conf, klass,
'datetime')
fields << Sphincter::Configure.get_sources_field(source_conf, klass,
'boolean')
fields << Sphincter::Configure.get_sources_field(source_conf, klass,
'integer')
fields << Sphincter::Configure.get_sources_field(source_conf, klass,
'string')
fields << Sphincter::Configure.get_sources_field(source_conf, klass,
'time')
fields << Sphincter::Configure.get_sources_field(source_conf, klass,
'timestamp')
fields << Sphincter::Configure.get_sources_field(source_conf, klass,
'text')
expected_fields = [
"UNIX_TIMESTAMP(models.`date`) AS `date`",
"UNIX_TIMESTAMP(models.`datetime`) AS `datetime`",
"models.`boolean` AS `boolean`",
"models.`integer` AS `integer`",
"models.`string` AS `string`",
"UNIX_TIMESTAMP(models.`time`) AS `time`",
"UNIX_TIMESTAMP(models.`timestamp`) AS `timestamp`",
"models.`text` AS `text`"
]
assert_equal expected_fields, fields
assert_equal %w[boolean integer], source_conf['sql_group_column']
assert_equal %w[date datetime time timestamp],
source_conf['sql_date_column']
end
def test_self_get_db_conf
expected = {
'type' => 'mysql',
'sql_host' => 'host',
'sql_pass' => 'password',
'sql_db' => 'database',
'sql_user' => 'username',
'sql_sock' => 'socket',
}
assert_equal expected, Sphincter::Configure.get_db_conf
end
def test_self_index_count
Sphincter::Search.indexes[Object] << { :fields => %w[title body] }
Sphincter::Search.indexes[Object] << {
:fields => %w[title body], :name => 'foo'
}
assert_equal 2, Sphincter::Configure.index_count
expected = {
Object => [
{ :index_id => 0, :fields => %w[title body] },
{ :index_id => 1, :fields => %w[title body], :name => 'foo' },
],
}
assert_equal expected, Sphincter::Search.indexes
end
def test_self_section
heading = 'searchd'
data = {
'array' => %w[value1 value2],
'empty' => '',
'nil' => nil,
'string' => 'value',
}
expected = <<-EOF.strip
searchd
{
array = value1
array = value2
empty =
nil =
string = value
}
EOF
assert_equal expected, Sphincter::Configure.section(heading, data)
end
def test_self_sphinx_conf
assert_equal File.join(RAILS_ROOT, 'sphinx/RAILS_ENV/sphinx.conf'),
Sphincter::Configure.sphinx_conf
end
def test_self_sphinx_dir
assert_equal File.join(RAILS_ROOT, 'sphinx/RAILS_ENV'),
Sphincter::Configure.sphinx_dir
end
def test_self_write_configuration
conf = Hash.new { |h,k| h[k] = {} }
sources = Hash.new { |h,k| h[k] = {} }
conf['sphincter']['path'] = 'sphinx/development'
conf['source']['key1'] = 'value1'
conf['index']['key1'] = 'value1'
sources['source_1']['key2'] = 'value2'
sources['source_2']['key1'] = 'value3'
sources['source_2']['key2'] = 'value4'
expected = <<-EOF
indexer
{
}
searchd
{
}
source source_1
{
key1 = value1
key2 = value2
}
index source_1
{
key1 = value1
path = #{Sphincter::Configure.sphinx_dir}/source_1
source = source_1
}
source source_2
{
key1 = value3
key2 = value4
}
index source_2
{
key1 = value1
path = #{Sphincter::Configure.sphinx_dir}/source_2
source = source_2
}
EOF
Sphincter::Configure.write_configuration conf, sources
assert_equal expected, File.read(Sphincter::Configure.sphinx_conf)
end
def util_deep_clone(obj)
Marshal.load Marshal.dump(obj)
end
end