-
Notifications
You must be signed in to change notification settings - Fork 25
/
test-database-inspector.rb
740 lines (661 loc) · 19.6 KB
/
test-database-inspector.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
# Copyright (C) 2013 Kouhei Sutou <kou@clear-code.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License version 2.1 as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
class DatabaseInspectorTest < Test::Unit::TestCase
include GroongaTestUtils
setup :setup_database, :before => :append
setup
def setup_options
@options = Groonga::DatabaseInspector::Options.new
end
private
def report
output = StringIO.new
inspector = Groonga::DatabaseInspector.new(@database, @options)
inspector.report(output)
output.string
end
def create_reporter(output)
Groonga::DatabaseInspector::Reporter.new(@database, @options, output)
end
def total_disk_usage
@database.tables.inject(@database.disk_usage) do |previous, table|
previous + total_table_disk_usage(table)
end
end
def total_table_disk_usage(table)
table.columns.inject(table.disk_usage) do |previous, column|
previous + column.disk_usage
end
end
def inspect_disk_usage(disk_usage)
if disk_usage < (2 ** 20)
"%.3fKiB" % (disk_usage / (2 ** 10).to_f)
else
"%.3fMiB" % (disk_usage / (2 ** 20).to_f)
end
end
def inspect_sub_disk_usage(disk_usage)
percent = disk_usage / total_disk_usage.to_f * 100
"%s (%.3f%%)" % [inspect_disk_usage(disk_usage), percent]
end
def inspect_table(table)
<<-INSPECTED
#{table.name}:
ID: #{table.id}
Type: #{inspect_table_type(table)}
Key type: #{inspect_key_type(table)}
Tokenizer: #{inspect_tokenizer(table)}
Normalizer: #{inspect_normalizer(table)}
Path: <#{table.path}>
Total disk usage: #{inspect_sub_disk_usage(total_table_disk_usage(table))}
Disk usage: #{inspect_sub_disk_usage(table.disk_usage)}
N records: #{table.size}
N columns: #{table.columns.size}
#{inspect_columns(table.columns).chomp}
INSPECTED
end
def inspect_table_type(table)
case table
when Groonga::Array
"array"
when Groonga::Hash
"hash"
when Groonga::PatriciaTrie
"patricia trie"
when Groonga::DoubleArrayTrie
"double array trie"
end
end
def inspect_key_type(table)
if table.support_key?
table.domain.name
else
"(no key)"
end
end
def inspect_tokenizer(table)
if table.support_key?
tokenizer = table.default_tokenizer
if tokenizer
tokenizer.name
else
"(no tokenizer)"
end
else
"(no key)"
end
end
def inspect_normalizer(table)
if table.support_key?
normalizer = table.normalizer
if normalizer
normalizer.name
else
"(no normalizer)"
end
else
"(no key)"
end
end
def inspect_columns(columns)
if columns.empty?
<<-INSPECTED
Columns:
None
INSPECTED
else
inspected = " Columns:\n"
columns.each do |column|
inspected << inspect_column(column)
end
inspected
end
end
def inspect_column(column)
<<-INSPECTED
#{column.local_name}:
ID: #{column.id}
Type: #{inspect_column_type(column)}
Value type: #{inspect_value_type(column.range)}
Path: <#{column.path}>
Disk usage: #{inspect_sub_disk_usage(column.disk_usage)}
INSPECTED
end
def inspect_column_type(column)
case column
when Groonga::FixSizeColumn
"scalar"
when Groonga::VariableSizeColumn
if column.vector?
"vector"
else
"scalar"
end
else
"index"
end
end
def inspect_value_type(range)
if range.nil?
"(no value)"
else
range.name
end
end
class DatabaseTest < self
def test_empty
assert_equal(<<-INSPECTED, report)
Database
Path: <#{@database_path}>
Total disk usage: #{inspect_disk_usage(total_disk_usage)}
Disk usage: #{inspect_sub_disk_usage(@database.disk_usage)}
N records: 0
N tables: 0
N columns: 0
Plugins:
None
Tables:
None
INSPECTED
end
def test_no_show_tables
@options.show_tables = false
assert_equal(<<-INSPECTED, report)
Database
Path: <#{@database_path}>
Total disk usage: #{inspect_disk_usage(total_disk_usage)}
Disk usage: #{inspect_sub_disk_usage(@database.disk_usage)}
N records: 0
N tables: 0
N columns: 0
Plugins:
None
INSPECTED
end
class NRecordsTest < self
setup
def setup_tables
Groonga::Schema.define do |schema|
schema.create_table("Users") do |table|
end
schema.create_table("Bookmarks") do |table|
end
end
@users = context["Users"]
@bookmarks = context["Bookmarks"]
end
def test_no_records
assert_equal(inspected(0), report)
end
def test_has_records
@users.add
@users.add
@bookmarks.add
assert_equal(inspected(3), report)
end
private
def inspected(n_records)
<<-INSPECTED
Database
Path: <#{@database_path}>
Total disk usage: #{inspect_disk_usage(total_disk_usage)}
Disk usage: #{inspect_sub_disk_usage(@database.disk_usage)}
N records: #{n_records}
N tables: 2
N columns: 0
Plugins:
None
Tables:
#{inspect_table(@bookmarks).chomp}
#{inspect_table(@users).chomp}
INSPECTED
end
end
class NTablesTest < self
def test_no_tables
assert_equal(inspected(0), report)
end
def test_has_tables
Groonga::Schema.define do |schema|
schema.create_table("Users") do |table|
end
schema.create_table("Bookmarks") do |table|
end
end
assert_equal(inspected(2), report)
end
private
def inspected(n_tables)
inspected_tables = " Tables:\n"
if @database.tables.empty?
inspected_tables << " None\n"
else
@database.tables.each do |table|
inspected_tables << inspect_table(table)
end
end
<<-INSPECTED
Database
Path: <#{@database_path}>
Total disk usage: #{inspect_disk_usage(total_disk_usage)}
Disk usage: #{inspect_sub_disk_usage(@database.disk_usage)}
N records: 0
N tables: #{n_tables}
N columns: 0
Plugins:
None
#{inspected_tables.chomp}
INSPECTED
end
end
class NColumnsTest < self
setup
def setup_tables
Groonga::Schema.define do |schema|
schema.create_table("Users") do |table|
end
schema.create_table("Bookmarks") do |table|
end
end
@users = context["Users"]
@bookmarks = context["Bookmarks"]
end
def test_no_columns
assert_equal(inspected(0), report)
end
def test_has_columns
Groonga::Schema.define do |schema|
schema.create_table("Users") do |table|
table.short_text("name")
table.int8("age")
end
schema.create_table("Bookmarks") do |table|
table.text("description")
end
end
assert_equal(inspected(3), report)
end
private
def inspected(n_columns)
<<-INSPECTED
Database
Path: <#{@database_path}>
Total disk usage: #{inspect_disk_usage(total_disk_usage)}
Disk usage: #{inspect_sub_disk_usage(@database.disk_usage)}
N records: 0
N tables: 2
N columns: #{n_columns}
Plugins:
None
Tables:
#{inspect_table(@bookmarks).chomp}
#{inspect_table(@users).chomp}
INSPECTED
end
end
class PluginsTest < self
def test_no_plugins
assert_equal(inspected(<<-INSPECTED), report)
Plugins:
None
INSPECTED
end
def test_has_plugin
context.register_plugin("query_expanders/tsv")
assert_equal(inspected(<<-INSPECTED), report)
Plugins:
* query_expanders/tsv#{Groonga::Plugin.suffix}
INSPECTED
end
private
def inspected(inspected_plugins)
<<-INSPECTED
Database
Path: <#{@database_path}>
Total disk usage: #{inspect_disk_usage(total_disk_usage)}
Disk usage: #{inspect_sub_disk_usage(@database.disk_usage)}
N records: 0
N tables: 0
N columns: 0
#{inspected_plugins.chomp}
Tables:
None
INSPECTED
end
end
end
class TableTest < self
private
def report
output = StringIO.new
reporter = create_reporter(output)
reporter.send(:report_table, @table)
output.string
end
def inspect_columns(table)
super.gsub(/^ /, "")
end
class NRecordsTest < self
setup
def setup_tables
Groonga::Schema.define do |schema|
schema.create_table("Users") do |table|
end
end
@table = context["Users"]
end
def test_no_record
assert_equal(inspected(0), report)
end
def test_empty
@table.add
@table.add
@table.add
assert_equal(inspected(3), report)
end
private
def inspected(n_records)
<<-INSPECTED
#{@table.name}:
ID: #{@table.id}
Type: #{inspect_table_type(@table)}
Key type: #{inspect_key_type(@table)}
Tokenizer: #{inspect_tokenizer(@table)}
Normalizer: #{inspect_normalizer(@table)}
Path: <#{@table.path}>
Total disk usage: #{inspect_sub_disk_usage(total_table_disk_usage(@table))}
Disk usage: #{inspect_sub_disk_usage(@table.disk_usage)}
N records: #{n_records}
N columns: #{@table.columns.size}
#{inspect_columns(@table.columns).chomp}
INSPECTED
end
end
class TypeTest < self
def test_array
Groonga::Schema.create_table("Users")
@table = Groonga["Users"]
assert_equal(inspected("array"), report)
end
def test_hash
Groonga::Schema.create_table("Users",
:type => :hash,
:key_type => :short_text)
@table = Groonga["Users"]
assert_equal(inspected("hash"), report)
end
def test_patricia_trie
Groonga::Schema.create_table("Users",
:type => :patricia_trie,
:key_type => :short_text)
@table = Groonga["Users"]
assert_equal(inspected("patricia trie"), report)
end
def test_double_array_trie
Groonga::Schema.create_table("Users",
:type => :double_array_trie,
:key_type => :short_text)
@table = Groonga["Users"]
assert_equal(inspected("double array trie"), report)
end
private
def inspected(type)
<<-INSPECTED
#{@table.name}:
ID: #{@table.id}
Type: #{type}
Key type: #{inspect_key_type(@table)}
Tokenizer: #{inspect_tokenizer(@table)}
Normalizer: #{inspect_normalizer(@table)}
Path: <#{@table.path}>
Total disk usage: #{inspect_sub_disk_usage(total_table_disk_usage(@table))}
Disk usage: #{inspect_sub_disk_usage(@table.disk_usage)}
N records: #{@table.size}
N columns: #{@table.columns.size}
#{inspect_columns(@table.columns).chomp}
INSPECTED
end
end
class KeyTypeTest < self
def test_array
Groonga::Schema.create_table("Users")
@table = Groonga["Users"]
assert_equal(inspected("(no key)"), report)
end
def test_key_support_table
Groonga::Schema.create_table("Users",
:type => :hash,
:key_type => "Int32")
@table = Groonga["Users"]
assert_equal(inspected("Int32"), report)
end
private
def inspected(key_type)
<<-INSPECTED
#{@table.name}:
ID: #{@table.id}
Type: #{inspect_table_type(@table)}
Key type: #{key_type}
Tokenizer: #{inspect_tokenizer(@table)}
Normalizer: #{inspect_normalizer(@table)}
Path: <#{@table.path}>
Total disk usage: #{inspect_sub_disk_usage(total_table_disk_usage(@table))}
Disk usage: #{inspect_sub_disk_usage(@table.disk_usage)}
N records: #{@table.size}
N columns: #{@table.columns.size}
#{inspect_columns(@table.columns).chomp}
INSPECTED
end
end
class TokenizerTest < self
def test_array
Groonga::Schema.create_table("Users")
@table = Groonga["Users"]
assert_equal(inspected("(no key)"), report)
end
def test_no_tokenizer
Groonga::Schema.create_table("Users",
:type => :hash,
:key_type => :short_text)
@table = Groonga["Users"]
assert_equal(inspected("(no tokenizer)"), report)
end
def test_have_tokenizer
Groonga::Schema.create_table("Users",
:type => :patricia_trie,
:key_type => :short_text,
:default_tokenizer => "TokenBigram")
@table = Groonga["Users"]
assert_equal(inspected("TokenBigram"), report)
end
private
def inspected(inspected_tokenizer)
<<-INSPECTED
#{@table.name}:
ID: #{@table.id}
Type: #{inspect_table_type(@table)}
Key type: #{inspect_key_type(@table)}
Tokenizer: #{inspected_tokenizer}
Normalizer: #{inspect_normalizer(@table)}
Path: <#{@table.path}>
Total disk usage: #{inspect_sub_disk_usage(total_table_disk_usage(@table))}
Disk usage: #{inspect_sub_disk_usage(@table.disk_usage)}
N records: #{@table.size}
N columns: #{@table.columns.size}
#{inspect_columns(@table.columns).chomp}
INSPECTED
end
end
class NormalizerTest < self
def test_array
Groonga::Schema.create_table("Users")
@table = Groonga["Users"]
assert_equal(inspected("(no key)"), report)
end
def test_no_normalizer
Groonga::Schema.create_table("Users",
:type => :hash,
:key_type => :short_text)
@table = Groonga["Users"]
assert_equal(inspected("(no normalizer)"), report)
end
def test_have_normalizer
Groonga::Schema.create_table("Users",
:type => :patricia_trie,
:key_type => :short_text,
:normalizer => "NormalizerAuto")
@table = Groonga["Users"]
assert_equal(inspected("NormalizerAuto"), report)
end
private
def inspected(inspected_normalizer)
<<-INSPECTED
#{@table.name}:
ID: #{@table.id}
Type: #{inspect_table_type(@table)}
Key type: #{inspect_key_type(@table)}
Tokenizer: #{inspect_tokenizer(@table)}
Normalizer: #{inspected_normalizer}
Path: <#{@table.path}>
Total disk usage: #{inspect_sub_disk_usage(total_table_disk_usage(@table))}
Disk usage: #{inspect_sub_disk_usage(@table.disk_usage)}
N records: #{@table.size}
N columns: #{@table.columns.size}
#{inspect_columns(@table.columns).chomp}
INSPECTED
end
end
class NColumnsTest < self
def test_no_columns
Groonga::Schema.create_table("Users")
@table = Groonga["Users"]
assert_equal(inspected(0), report)
end
def test_have_columns
Groonga::Schema.create_table("Users") do |table|
table.short_text("name")
table.int8("age")
end
@table = Groonga["Users"]
assert_equal(inspected(2), report)
end
private
def inspected(n_columns)
<<-INSPECTED
#{@table.name}:
ID: #{@table.id}
Type: #{inspect_table_type(@table)}
Key type: #{inspect_key_type(@table)}
Tokenizer: #{inspect_tokenizer(@table)}
Normalizer: #{inspect_normalizer(@table)}
Path: <#{@table.path}>
Total disk usage: #{inspect_sub_disk_usage(total_table_disk_usage(@table))}
Disk usage: #{inspect_sub_disk_usage(@table.disk_usage)}
N records: #{@table.size}
N columns: #{n_columns}
#{inspect_columns(@table.columns).chomp}
INSPECTED
end
end
class ColumnsTest < self
setup
def setup_tables
Groonga::Schema.create_table("Users") do |table|
table.short_text("name")
table.int8("age")
end
@table = Groonga["Users"]
end
def test_no_show_columns
@options.show_columns = false
assert_equal(<<-INSPECTED, report)
#{@table.name}:
ID: #{@table.id}
Type: #{inspect_table_type(@table)}
Key type: #{inspect_key_type(@table)}
Tokenizer: #{inspect_tokenizer(@table)}
Normalizer: #{inspect_normalizer(@table)}
Path: <#{@table.path}>
Total disk usage: #{inspect_sub_disk_usage(total_table_disk_usage(@table))}
Disk usage: #{inspect_sub_disk_usage(@table.disk_usage)}
N records: #{@table.size}
N columns: #{@table.columns.size}
INSPECTED
end
end
end
class ColumnTest < self
private
def report
output = StringIO.new
reporter = create_reporter(output)
reporter.send(:report_column, @column)
output.string
end
class TypeTest < self
def test_scalar_fix_size
create_table do |table|
table.int32("age")
end
assert_equal(inspected("scalar"), report)
end
def test_scalar_variable_size
create_table do |table|
table.short_text("name")
end
assert_equal(inspected("scalar"), report)
end
def test_vector
create_table do |table|
table.short_text("tags", :type => :vector)
end
assert_equal(inspected("vector"), report)
end
def test_index
create_table do |table|
end
Groonga::Schema.create_table("Bookmarks") do |table|
table.reference("user", "Users")
end
create_table do |table|
table.index("Bookmarks.user")
end
assert_equal(inspected("index"), report)
end
private
def create_table
Groonga::Schema.create_table("Users") do |table|
yield(table)
end
@table = Groonga["Users"]
@column = @table.columns.first
end
def inspected(type)
if type != "index"
value_type = "\n Value type: #{@column.range.name}"
else
value_type = ""
end
<<-INSPECTED
#{@column.local_name}:
ID: #{@column.id}
Type: #{type}#{value_type}
Path: <#{@column.path}>
Disk usage: #{inspect_sub_disk_usage(@column.disk_usage)}
INSPECTED
end
end
end
end