groonga-schema 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/test/test-diff.rb ADDED
@@ -0,0 +1,549 @@
1
+ # Copyright (C) 2016 Kouhei Sutou <kou@clear-code.com>
2
+ #
3
+ # This library is free software; you can redistribute it and/or
4
+ # modify it under the terms of the GNU Lesser General Public
5
+ # License as published by the Free Software Foundation; either
6
+ # version 2.1 of the License, or (at your option) any later version.
7
+ #
8
+ # This library is distributed in the hope that it will be useful,
9
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
+ # Lesser General Public License for more details.
12
+ #
13
+ # You should have received a copy of the GNU Lesser General Public
14
+ # License along with this library; if not, write to the Free Software
15
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
+
17
+ class DiffTest < Test::Unit::TestCase
18
+ def setup
19
+ @diff = GroongaSchema::Diff.new
20
+ end
21
+
22
+ def plugin(name)
23
+ GroongaSchema::Plugin.new(name)
24
+ end
25
+
26
+ def table(name, options)
27
+ table = GroongaSchema::Table.new(name)
28
+ options.each do |key, value|
29
+ table.__send__("#{key}=", value)
30
+ end
31
+ table
32
+ end
33
+
34
+ def column(table_name, name, options)
35
+ column = GroongaSchema::Column.new(table_name, name)
36
+ options.each do |key, value|
37
+ column.__send__("#{key}=", value)
38
+ end
39
+ column
40
+ end
41
+
42
+ sub_test_case "#same?" do
43
+ test "same" do
44
+ assert do
45
+ @diff.same?
46
+ end
47
+ end
48
+
49
+ test "different" do
50
+ @diff.added_plugins << plugin("token_filters/stem")
51
+ assert do
52
+ not @diff.same?
53
+ end
54
+ end
55
+ end
56
+
57
+ sub_test_case "#to_groonga_command_list" do
58
+ test "plugins" do
59
+ @diff.added_plugins << plugin("token_filters/stem")
60
+ @diff.removed_plugins << plugin("token_filters/stop_word")
61
+ assert_equal(<<-LIST, @diff.to_groonga_command_list)
62
+ plugin_register --name "token_filters/stem"
63
+
64
+ plugin_unregister --name "token_filters/stop_word"
65
+ LIST
66
+ end
67
+
68
+ test "added tables - without column" do
69
+ token_filters = [
70
+ "TokenFilterStopWord",
71
+ "TokenFilterStem",
72
+ ]
73
+ @diff.added_tables["Words"] = table("Words",
74
+ :type => :pat_key,
75
+ :key_type => "ShortText",
76
+ :default_tokenizer => "TokenBigram",
77
+ :normalizer => "NormalizerAuto",
78
+ :token_filters => token_filters)
79
+ @diff.added_tables["Names"] = table("Names",
80
+ :type => :hash_key,
81
+ :flags => "KEY_LARGE",
82
+ :key_type => "ShortText",
83
+ :normalizer => "NormalizerAuto")
84
+ @diff.added_tables["Commands"] = table("Commands",
85
+ :type => :hash_key,
86
+ :key_type => "Names",
87
+ :reference_key_type => true)
88
+
89
+ assert_equal(<<-LIST.gsub(/\\\n\s+/, ""), @diff.to_groonga_command_list)
90
+ table_create \\
91
+ --flags "TABLE_HASH_KEY|KEY_LARGE" \\
92
+ --key_type "ShortText" \\
93
+ --name "Names" \\
94
+ --normalizer "NormalizerAuto"
95
+
96
+ table_create \\
97
+ --default_tokenizer "TokenBigram" \\
98
+ --flags "TABLE_PAT_KEY" \\
99
+ --key_type "ShortText" \\
100
+ --name "Words" \\
101
+ --normalizer "NormalizerAuto" \\
102
+ --token_filters "TokenFilterStopWord|TokenFilterStem"
103
+
104
+ table_create \\
105
+ --flags "TABLE_HASH_KEY" \\
106
+ --key_type "Names" \\
107
+ --name "Commands"
108
+ LIST
109
+ end
110
+
111
+ test "added tables - with column" do
112
+ @diff.added_tables["Names"] = table("Names",
113
+ :type => :hash_key,
114
+ :flags => "KEY_LARGE",
115
+ :key_type => "ShortText",
116
+ :normalizer => "NormalizerAuto")
117
+ @diff.added_tables["Commands"] = table("Commands",
118
+ :type => :hash_key,
119
+ :key_type => "Names",
120
+ :reference_key_type => true)
121
+ @diff.added_columns["Commands"] = {
122
+ "description" => column("Commands", "description",
123
+ :value_type => "Text"),
124
+ }
125
+ token_filters = [
126
+ "TokenFilterStopWord",
127
+ "TokenFilterStem",
128
+ ]
129
+ @diff.added_tables["Words"] = table("Words",
130
+ :type => :pat_key,
131
+ :key_type => "ShortText",
132
+ :default_tokenizer => "TokenBigram",
133
+ :normalizer => "NormalizerAuto",
134
+ :token_filters => token_filters)
135
+ @diff.added_columns["Words"] = {
136
+ "commands_description" => column("Words", "commands_description",
137
+ :type => :index,
138
+ :flags => ["WITH_POSITION"],
139
+ :value_type => "Commands",
140
+ :sources => ["description"],
141
+ :reference_value_type => true),
142
+ }
143
+
144
+ assert_equal(<<-LIST.gsub(/\\\n\s+/, ""), @diff.to_groonga_command_list)
145
+ table_create \\
146
+ --flags "TABLE_HASH_KEY|KEY_LARGE" \\
147
+ --key_type "ShortText" \\
148
+ --name "Names" \\
149
+ --normalizer "NormalizerAuto"
150
+
151
+ table_create \\
152
+ --default_tokenizer "TokenBigram" \\
153
+ --flags "TABLE_PAT_KEY" \\
154
+ --key_type "ShortText" \\
155
+ --name "Words" \\
156
+ --normalizer "NormalizerAuto" \\
157
+ --token_filters "TokenFilterStopWord|TokenFilterStem"
158
+
159
+ table_create \\
160
+ --flags "TABLE_HASH_KEY" \\
161
+ --key_type "Names" \\
162
+ --name "Commands"
163
+ column_create \\
164
+ --flags "COLUMN_SCALAR" \\
165
+ --name "description" \\
166
+ --table "Commands" \\
167
+ --type "Text"
168
+
169
+ column_create \\
170
+ --flags "COLUMN_INDEX|WITH_POSITION" \\
171
+ --name "commands_description" \\
172
+ --source "description" \\
173
+ --table "Words" \\
174
+ --type "Commands"
175
+ LIST
176
+ end
177
+
178
+ test "added columns" do
179
+ @diff.added_columns["Commands"] = {
180
+ "description" => column("Commands", "description",
181
+ :value_type => "Text"),
182
+ }
183
+ @diff.added_columns["Words"] = {
184
+ "commands_description" => column("Words", "commands_description",
185
+ :type => :index,
186
+ :flags => ["WITH_POSITION"],
187
+ :value_type => "Commands",
188
+ :sources => ["description"],
189
+ :reference_value_type => true),
190
+ }
191
+
192
+ assert_equal(<<-LIST.gsub(/\\\n\s+/, ""), @diff.to_groonga_command_list)
193
+ column_create \\
194
+ --flags "COLUMN_SCALAR" \\
195
+ --name "description" \\
196
+ --table "Commands" \\
197
+ --type "Text"
198
+
199
+ column_create \\
200
+ --flags "COLUMN_INDEX|WITH_POSITION" \\
201
+ --name "commands_description" \\
202
+ --source "description" \\
203
+ --table "Words" \\
204
+ --type "Commands"
205
+ LIST
206
+ end
207
+
208
+ test "removed columns" do
209
+ @diff.removed_columns["Words"] = {
210
+ "weight" => column("Words", "weight",
211
+ :value_type => "Float"),
212
+ "commands_description" => column("Words", "commands_description",
213
+ :type => :index,
214
+ :flags => ["WITH_POSITION"],
215
+ :value_type => "Commands",
216
+ :sources => ["description"],
217
+ :reference_value_type => true),
218
+ }
219
+ @diff.removed_columns["Commands"] = {
220
+ "description" => column("Commands", "description",
221
+ :value_type => "Text"),
222
+ "comment" => column("Commands", "comment",
223
+ :value_type => "ShortText"),
224
+ }
225
+
226
+ assert_equal(<<-LIST.gsub(/\\\n\s+/, ""), @diff.to_groonga_command_list)
227
+ column_remove \\
228
+ --name "commands_description" \\
229
+ --table "Words"
230
+
231
+ column_remove \\
232
+ --name "comment" \\
233
+ --table "Commands"
234
+ column_remove \\
235
+ --name "description" \\
236
+ --table "Commands"
237
+
238
+ column_remove \\
239
+ --name "weight" \\
240
+ --table "Words"
241
+ LIST
242
+ end
243
+
244
+ test "removed tables" do
245
+ @diff.removed_tables["Names"] = table("Names",
246
+ :type => :hash_key,
247
+ :flags => "KEY_LARGE",
248
+ :key_type => "ShortText",
249
+ :normalizer => "NormalizerAuto")
250
+ @diff.removed_tables["Commands"] = table("Commands",
251
+ :type => :hash_key,
252
+ :key_type => "Names",
253
+ :reference_key_type => true)
254
+
255
+ assert_equal(<<-LIST.gsub(/\\\n\s+/, ""), @diff.to_groonga_command_list)
256
+ table_remove \\
257
+ --name "Commands"
258
+
259
+ table_remove \\
260
+ --name "Names"
261
+ LIST
262
+ end
263
+
264
+ test "changed tables - without columns" do
265
+ @diff.changed_tables["Names"] = table("Names",
266
+ :type => :hash_key,
267
+ :flags => "KEY_LARGE",
268
+ :key_type => "ShortText",
269
+ :normalizer => "NormalizerAuto")
270
+ @diff.changed_tables["Commands"] = table("Commands",
271
+ :type => :hash_key,
272
+ :key_type => "Names",
273
+ :reference_key_type => true)
274
+
275
+ assert_equal(<<-LIST.gsub(/\\\n\s+/, ""), @diff.to_groonga_command_list)
276
+ table_create \\
277
+ --flags "TABLE_HASH_KEY|KEY_LARGE" \\
278
+ --key_type "ShortText" \\
279
+ --name "Names_new" \\
280
+ --normalizer "NormalizerAuto"
281
+ table_copy \\
282
+ --from_name "Names" \\
283
+ --to_name "Names_new"
284
+ table_rename \\
285
+ --name "Names" \\
286
+ --new_name "Names_old"
287
+ table_rename \\
288
+ --name "Names_new" \\
289
+ --new_name "Names"
290
+
291
+ table_create \\
292
+ --flags "TABLE_HASH_KEY" \\
293
+ --key_type "Names" \\
294
+ --name "Commands_new"
295
+ table_copy \\
296
+ --from_name "Commands" \\
297
+ --to_name "Commands_new"
298
+ table_rename \\
299
+ --name "Commands" \\
300
+ --new_name "Commands_old"
301
+ table_rename \\
302
+ --name "Commands_new" \\
303
+ --new_name "Commands"
304
+
305
+ table_remove \\
306
+ --name "Names_old"
307
+
308
+ table_remove \\
309
+ --name "Commands_old"
310
+ LIST
311
+ end
312
+
313
+ test "changed tables - with columns" do
314
+ token_filters = [
315
+ "TokenFilterStopWord",
316
+ "TokenFilterStem",
317
+ ]
318
+ words_columns = [
319
+ column("Words", "weight",
320
+ :value_type => "Float"),
321
+ column("Words", "commands_description",
322
+ :type => :index,
323
+ :flags => ["WITH_POSITION"],
324
+ :value_type => "Commands",
325
+ :sources => ["description"],
326
+ :reference_value_type => true),
327
+ ]
328
+ @diff.changed_tables["Words"] = table("Words",
329
+ :type => :pat_key,
330
+ :key_type => "ShortText",
331
+ :default_tokenizer => "TokenBigram",
332
+ :normalizer => "NormalizerAuto",
333
+ :token_filters => token_filters,
334
+ :columns => words_columns)
335
+ @diff.changed_tables["Names"] = table("Names",
336
+ :type => :hash_key,
337
+ :flags => "KEY_LARGE",
338
+ :key_type => "ShortText",
339
+ :normalizer => "NormalizerAuto")
340
+ commands_columns = [
341
+ column("Commands", "description",
342
+ :value_type => "Text"),
343
+ column("Commands", "comment",
344
+ :value_type => "ShortText"),
345
+ ]
346
+ @diff.changed_tables["Commands"] = table("Commands",
347
+ :type => :hash_key,
348
+ :key_type => "Names",
349
+ :reference_key_type => true,
350
+ :columns => commands_columns)
351
+
352
+ assert_equal(<<-LIST.gsub(/\\\n\s+/, ""), @diff.to_groonga_command_list)
353
+ table_create \\
354
+ --flags "TABLE_HASH_KEY|KEY_LARGE" \\
355
+ --key_type "ShortText" \\
356
+ --name "Names_new" \\
357
+ --normalizer "NormalizerAuto"
358
+ table_copy \\
359
+ --from_name "Names" \\
360
+ --to_name "Names_new"
361
+ table_rename \\
362
+ --name "Names" \\
363
+ --new_name "Names_old"
364
+ table_rename \\
365
+ --name "Names_new" \\
366
+ --new_name "Names"
367
+
368
+ table_create \\
369
+ --default_tokenizer "TokenBigram" \\
370
+ --flags "TABLE_PAT_KEY" \\
371
+ --key_type "ShortText" \\
372
+ --name "Words_new" \\
373
+ --normalizer "NormalizerAuto" \\
374
+ --token_filters "TokenFilterStopWord|TokenFilterStem"
375
+ column_create \\
376
+ --flags "COLUMN_SCALAR" \\
377
+ --name "weight" \\
378
+ --table "Words_new" \\
379
+ --type "Float"
380
+ column_copy \\
381
+ --from_name "weight" \\
382
+ --from_table "Words" \\
383
+ --to_name "weight" \\
384
+ --to_table "Words_new"
385
+ table_rename \\
386
+ --name "Words" \\
387
+ --new_name "Words_old"
388
+ table_rename \\
389
+ --name "Words_new" \\
390
+ --new_name "Words"
391
+
392
+ table_create \\
393
+ --flags "TABLE_HASH_KEY" \\
394
+ --key_type "Names" \\
395
+ --name "Commands_new"
396
+ column_create \\
397
+ --flags "COLUMN_SCALAR" \\
398
+ --name "comment" \\
399
+ --table "Commands_new" \\
400
+ --type "ShortText"
401
+ column_copy \\
402
+ --from_name "comment" \\
403
+ --from_table "Commands" \\
404
+ --to_name "comment" \\
405
+ --to_table "Commands_new"
406
+ column_create \\
407
+ --flags "COLUMN_SCALAR" \\
408
+ --name "description" \\
409
+ --table "Commands_new" \\
410
+ --type "Text"
411
+ column_copy \\
412
+ --from_name "description" \\
413
+ --from_table "Commands" \\
414
+ --to_name "description" \\
415
+ --to_table "Commands_new"
416
+ table_rename \\
417
+ --name "Commands" \\
418
+ --new_name "Commands_old"
419
+ table_rename \\
420
+ --name "Commands_new" \\
421
+ --new_name "Commands"
422
+
423
+ table_remove \\
424
+ --name "Names_old"
425
+
426
+ column_create \\
427
+ --flags "COLUMN_INDEX|WITH_POSITION" \\
428
+ --name "commands_description" \\
429
+ --source "description" \\
430
+ --table "Words" \\
431
+ --type "Commands"
432
+ table_remove \\
433
+ --name "Words_old"
434
+
435
+ table_remove \\
436
+ --name "Commands_old"
437
+ LIST
438
+ end
439
+
440
+ test "changed columns" do
441
+ @diff.changed_columns["Words"] = {
442
+ "weight" => column("Words", "weight",
443
+ :value_type => "Float"),
444
+ "commands_description" => column("Words", "commands_description",
445
+ :type => :index,
446
+ :flags => ["WITH_POSITION"],
447
+ :value_type => "Commands",
448
+ :sources => ["description"],
449
+ :reference_value_type => true),
450
+ }
451
+ @diff.changed_columns["Commands"] = {
452
+ "description" => column("Commands", "description",
453
+ :value_type => "Text"),
454
+ "comment" => column("Commands", "comment",
455
+ :value_type => "ShortText"),
456
+ }
457
+
458
+ assert_equal(<<-LIST.gsub(/\\\n\s+/, ""), @diff.to_groonga_command_list)
459
+ column_create \\
460
+ --flags "COLUMN_SCALAR" \\
461
+ --name "comment_new" \\
462
+ --table "Commands" \\
463
+ --type "ShortText"
464
+ column_copy \\
465
+ --from_name "comment" \\
466
+ --from_table "Commands" \\
467
+ --to_name "comment_new" \\
468
+ --to_table "Commands"
469
+ column_rename \\
470
+ --name "comment" \\
471
+ --new_name "comment_old" \\
472
+ --table "Commands"
473
+ column_rename \\
474
+ --name "comment_new" \\
475
+ --new_name "comment" \\
476
+ --table "Commands"
477
+
478
+ column_create \\
479
+ --flags "COLUMN_SCALAR" \\
480
+ --name "description_new" \\
481
+ --table "Commands" \\
482
+ --type "Text"
483
+ column_copy \\
484
+ --from_name "description" \\
485
+ --from_table "Commands" \\
486
+ --to_name "description_new" \\
487
+ --to_table "Commands"
488
+ column_rename \\
489
+ --name "description" \\
490
+ --new_name "description_old" \\
491
+ --table "Commands"
492
+ column_rename \\
493
+ --name "description_new" \\
494
+ --new_name "description" \\
495
+ --table "Commands"
496
+
497
+ column_create \\
498
+ --flags "COLUMN_SCALAR" \\
499
+ --name "weight_new" \\
500
+ --table "Words" \\
501
+ --type "Float"
502
+ column_copy \\
503
+ --from_name "weight" \\
504
+ --from_table "Words" \\
505
+ --to_name "weight_new" \\
506
+ --to_table "Words"
507
+ column_rename \\
508
+ --name "weight" \\
509
+ --new_name "weight_old" \\
510
+ --table "Words"
511
+ column_rename \\
512
+ --name "weight_new" \\
513
+ --new_name "weight" \\
514
+ --table "Words"
515
+
516
+ column_create \\
517
+ --flags "COLUMN_INDEX|WITH_POSITION" \\
518
+ --name "commands_description_new" \\
519
+ --source "description" \\
520
+ --table "Words" \\
521
+ --type "Commands"
522
+ column_rename \\
523
+ --name "commands_description" \\
524
+ --new_name "commands_description_old" \\
525
+ --table "Words"
526
+ column_rename \\
527
+ --name "commands_description_new" \\
528
+ --new_name "commands_description" \\
529
+ --table "Words"
530
+
531
+ column_remove \\
532
+ --name "commands_description_old" \\
533
+ --table "Words"
534
+
535
+ column_remove \\
536
+ --name "comment_old" \\
537
+ --table "Commands"
538
+
539
+ column_remove \\
540
+ --name "description_old" \\
541
+ --table "Commands"
542
+
543
+ column_remove \\
544
+ --name "weight_old" \\
545
+ --table "Words"
546
+ LIST
547
+ end
548
+ end
549
+ end