File: ReportFormatter.t

package info (click to toggle)
percona-toolkit 3.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 68,916 kB
  • sloc: perl: 241,287; sql: 22,868; sh: 19,746; javascript: 6,799; makefile: 353; awk: 38; python: 30; sed: 1
file content (501 lines) | stat: -rw-r--r-- 13,405 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/perl

BEGIN {
   die "The PERCONA_TOOLKIT_BRANCH environment variable is not set.\n"
      unless $ENV{PERCONA_TOOLKIT_BRANCH} && -d $ENV{PERCONA_TOOLKIT_BRANCH};
   unshift @INC, "$ENV{PERCONA_TOOLKIT_BRANCH}/lib";
};

use strict;
use warnings FATAL => 'all';
use English qw(-no_match_vars);
use Test::More tests => 22;

use Transformers;
use ReportFormatter;
use PerconaTest;

my $rf = new ReportFormatter();
isa_ok($rf, 'ReportFormatter');

# #############################################################################
# truncate_value()
# #############################################################################
is(
   $rf->truncate_value(
      {truncate_mark=>'...', truncate_side=>'right'},
      "hello world",
      7,
   ),
   "hell...",
   "truncate_value(), right side"
);

is(
   $rf->truncate_value(
      {truncate_mark=>'...', truncate_side=>'left'},
      "hello world",
      7,
   ),
   "...orld",
   "truncate_value(), left side"
);

is(
   $rf->truncate_value(
      {truncate_mark=>'...', truncate_side=>'left'},
      "hello world",
      11,
   ),
   "hello world",
   "truncate_value(), max width == val width"
);

is(
   $rf->truncate_value(
      {truncate_mark=>'...', truncate_side=>'left'},
      "hello world",
      100,
   ),
   "hello world",
   "truncate_value(), max width > val width"
);


# #############################################################################
# truncate_callback
# #############################################################################
$rf->set_columns(
   { name => 'foo', width => 5, },
   { name => 'bar', width => 5, },
);

$rf->add_line('1234567890', '1234567890');
my $callback = sub {
   my ($col, $val, $print_width) = @_;
   return 'foo';
};
is(
   $rf->get_report(truncate_callback=>$callback),
"# foo   bar
# ===== =====
# foo   foo
",
   "Truncate callback"
);

# #############################################################################
# Basic report.
# #############################################################################
$rf = new ReportFormatter();
$rf->title('Checksum differences');
$rf->set_columns(
   {
      name        => 'Query ID',
      width_fixed => length '0x234DDDAC43820481-3',
   },
   {
      name => 'db-1.foo.com',
   },
   {
      name => '123.123.123.123',
   },
);

$rf->add_line(qw(0x3A99CC42AEDCCFCD-1  ABC12345  ADD12345));
$rf->add_line(qw(0x234DDDAC43820481-3  0007C99B  BB008171));

is(
   $rf->get_report(),
"# Checksum differences
# Query ID             db-1.foo.com 123.123.123.123
# ==================== ============ ===============
# 0x3A99CC42AEDCCFCD-1 ABC12345     ADD12345
# 0x234DDDAC43820481-3 0007C99B     BB008171
",
   'Basic report'
);

# #############################################################################
# Header that's too wide.
# #############################################################################
$rf = new ReportFormatter();
$rf->set_columns(
   { name => 'We are very long header columns that are going to cause', },
   { name => 'this sub to die because together we cannot fit on one line' },
);
is(
   $rf->get_report(),
"# ...ader columns that are going to cause ...e together we cannot fit on one l
# ======================================= ====================================
",
   "Full auto-fit columns to line"
);

$rf = new ReportFormatter();
$rf->set_columns(
   {
      name      => 'We are very long header columns that are going to cause',
      width_pct => 40,
   },
   {
      name      => 'this sub to die because together we cannot fit on one line',
      width_pct => 60,
   },
);

is(
   $rf->get_report(),
"# ...umns that are going to cause ... because together we cannot fit on one li
# =============================== ============================================
",
   "Two fixed percentage-width columsn"
);

$rf = new ReportFormatter();
$rf->set_columns(
   {
      name  => 'header1',
      width => 7,
   },
   { name => 'this long line should take up the rest of the line.......!', },
);

is(
   $rf->get_report(),
"# header1 this long line should take up the rest of the line.......!
# ======= ====================================================================
",
   "One fixed char-width column and one auto-width column"
);

# #############################################################################
# Test that header underline respects line width.
# #############################################################################
$rf = new ReportFormatter();
$rf->set_columns(
   { name => 'col1' },
   { name => 'col2' },
);
$rf->add_line('short', 'long long long long long long long long long long long long long long long long long long');

is(
   $rf->get_report(),
"# col1  col2
# ===== ======================================================================
# short long long long long long long long long long long long long long lo...
",
   'Truncate header underlining to line width'
);

# #############################################################################
# Test taht header labels are always left justified.
# #############################################################################
$rf = new ReportFormatter();
$rf->set_columns(
   { name => 'Rank',          right_justify => 1, },
   { name => 'Query ID',                          },
   { name => 'Response time', right_justify => 1, },
   { name => 'Calls',         right_justify => 1, },
   { name => 'R/Call',        right_justify => 1, },
   { name => 'Item',                              },
);
$rf->add_line(
   '123456789', '0x31DA25F95494CA95', '0.1494 99.9%', '1', '0.1494', 'SHOW');

is(
   $rf->get_report(),
"# Rank      Query ID           Response time Calls R/Call Item
# ========= ================== ============= ===== ====== ====
# 123456789 0x31DA25F95494CA95  0.1494 99.9%     1 0.1494 SHOW
",
   'Header labels are always left justified'
);

# #############################################################################
# Respect line width.
# #############################################################################
$rf = new ReportFormatter();
$rf->title('Respect line width');
$rf->set_columns(
   { name => 'col1' },
   { name => 'col2' },
   { name => 'col3' },
);
$rf->add_line(
   'short',
   'longer',
   'long long long long long long long long long long long long long long long long long long'
);
$rf->add_line(
   'a',
   'b',
   'c',
);

is(
   $rf->get_report(),
"# Respect line width
# col1  col2   col3
# ===== ====== ===============================================================
# short longer long long long long long long long long long long long long ...
# a     b      c
",
   'Respects line length'
);

# #############################################################################
# extend_right
# #############################################################################
$rf = new ReportFormatter(extend_right=>1);
$rf->title('extend_right');
$rf->set_columns(
   { name => 'col1' },
   { name => 'col2' },
   { name => 'col3' },
);
$rf->add_line(
   'short',
   'longer',
   'long long long long long long long long long long long long long long long long long long'
);
$rf->add_line(
   'a',
   'b',
   'c',
);

is(
   $rf->get_report(),
"# extend_right
# col1  col2   col3
# ===== ====== ===============================================================
# short longer long long long long long long long long long long long long long long long long long long
# a     b      c
",
   "Allow right-most column to extend beyond line width"
);

# #############################################################################
# Relvative column widths.
# #############################################################################
$rf = new ReportFormatter();
$rf->title('Relative col widths');
$rf->set_columns(
   { name => 'col1', width_pct=>'20', },
   { name => 'col2', width_pct=>'40', },
   { name => 'col3', width_pct=>'40',  },
);
$rf->add_line(
   'shortest',
   'a b c d e f g h i j k l m n o p',
   'seoncd longest line',
);
$rf->add_line(
   'x',
   'y',
   'z',
);

is(
   $rf->get_report(),
"# Relative col widths
# col1            col2                            col3
# =============== =============================== ============================
# shortest        a b c d e f g h i j k l m n o p seoncd longest line
# x               y                               z
",
   "Relative col widths that fit"
);

$rf = new ReportFormatter();
$rf->title('Relative col widths');
$rf->set_columns(
   { name => 'col1', width_pct=>'20', },
   { name => 'col2', width_pct=>'40', },
   { name => 'col3', width_pct=>'40',  },
);
$rf->add_line(
   'shortest',
   'a b c d e f g h i j k l m n o p',
   'seoncd longest line',
);
$rf->add_line(
   'x',
   'y',
   'z',
);
$rf->add_line(
   'this line is going to have to be truncated because it is too long',
   'this line is ok',
   'and this line will have to be truncated, too',
);

is(
   $rf->get_report(),
"# Relative col widths
# col1            col2                            col3
# =============== =============================== ============================
# shortest        a b c d e f g h i j k l m n o p seoncd longest line
# x               y                               z
# this line is... this line is ok                 and this line will have t...
",
   "Relative columns made smaller to fit"
);

$rf = new ReportFormatter();
$rf->title('Relative col widths');
$rf->set_columns(
   { name => 'col1', width    =>'25', },
   { name => 'col2', width_pct=>'33', },
   { name => 'col3', width_pct=>'33', },
);
$rf->add_line(
   'shortest',
   'a b c d e f g h i j k l m n o p',
   'seoncd longest line',
);
$rf->add_line(
   'x',
   'y',
   'z',
);
$rf->add_line(
   '1234567890123456789012345xxxxxx',
   'this line is ok',
   'and this line will have to be truncated, too',
);

is(
   $rf->get_report(),
"# Relative col widths
# col1                      col2                      col3
# ========================= ========================= ========================
# shortest                  a b c d e f g h i j k ... seoncd longest line
# x                         y                         z
# 1234567890123456789012... this line is ok           and this line will ha...
",
   "Fixed and relative columns"
);


$rf = new ReportFormatter();
$rf->title('Short cols');
$rf->set_columns(
   { name => 'I am column1', },
   { name => 'I am column2', },
   { name => "I don't know who I am", },
);
$rf->add_line(
   '',
   '',
   '',
);

is(
   $rf->get_report(),
"# Short cols
# I am column1              I am column2              I don't know who I am
# ========================= ========================= ========================
#                                                     
",
   "Short columsn, blank data"
);

$rf = new ReportFormatter();
$rf->title('Short cols');
$rf->set_columns(
   { name => 'I am column1', },
   { name => 'I am column2', },
   { name => "I don't know who I am", },
);
$rf->add_line(undef,undef,undef);

is(
   $rf->get_report(),
"# Short cols
# I am column1              I am column2              I don't know who I am
# ========================= ========================= ========================
#                                                     
",
   "Short columsn, undef data"
);

$rf = new ReportFormatter();
$rf->title('Short cols');
$rf->set_columns(
   { name => 'I am column1', },
   { name => 'I am column2', },
   { name => "I don't know who I am", },
);
$rf->add_line('','','');
$rf->add_line(qw(a b c));

is(
   $rf->get_report(),
"# Short cols
# I am column1 I am column2 I don't know who I am
# ============ ============ =====================
#                           
# a            b            c
",
   "Short columsn, blank and short data"
);


# #############################################################################
# Issue 1196: mk-query-digest --explain is broken
# #############################################################################
$rf = new ReportFormatter(
   line_width       => 82,
   long_last_column => 1,
   extend_right     => 1,
);
my @cols = (
   { name => 'Rank',          right_justify => 1,             },
   { name => 'Query ID',                                      },
   { name => 'Response time', right_justify => 1,             },
   { name => 'Calls',         right_justify => 1,             },
   { name => 'R/Call',        right_justify => 1,             },
   { name => 'Apdx',          right_justify => 1, width => 4, },
   { name => 'V/M',           right_justify => 1, width => 5, },
   { name => 'EXPLAIN'                                        },
   { name => 'Item',                                          },
);
$rf->set_columns(@cols);
$rf->add_line(
   '1',
   '0x0F52986F18B3A4D0',
   '    0.0000 100.0%',  # leading whitespace
   '1',
   '0.0000',
   '1.00',
   '0.00',
   "",  # EXPLAIN column
   'SELECT trees',
);
my $output = $rf->get_report();
ok(
   no_diff(
      $output,  
      "t/lib/samples/ReportFormatter/report001.txt",
      cmd_output => 1,
   ),
   'Whitespace stripped (issue 1196)',
);

# #############################################################################
# Done.
# #############################################################################
$output = '';
{
   local *STDERR;
   open STDERR, '>', \$output;
   $rf->_d('Complete test coverage');
}
like(
   $output,
   qr/Complete test coverage/,
   '_d() works'
);
exit;