File: TextResultSetParser.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 (204 lines) | stat: -rw-r--r-- 4,997 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
#!/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 => 13;

use TextResultSetParser;
use PerconaTest;

use Data::Dumper;
$Data::Dumper::Indent    = 1;
$Data::Dumper::Sortkeys  = 1;
$Data::Dumper::Quotekeys = 0;

my $r = new TextResultSetParser();
isa_ok($r, 'TextResultSetParser');

throws_ok(
   sub { $r->parse(load_file('t/lib/samples/slowlogs/slow002.txt')) },
   qr/Cannot determine if text is/,
   "Dies if output type cannot be determined"
);

is_deeply(
   $r->parse( load_file('t/lib/samples/pl/recset001.txt') ),
   [
      {
         Time     => '0',
         Command  => 'Query',
         db       => '',
         Id       => '9',
         Info     => 'show processlist',
         User     => 'msandbox',
         State    => '',
         Host     => 'localhost'
      },
   ],
   'Basic tablular processlist'
);

is_deeply(
   $r->parse( load_file('t/lib/samples/pl/recset002.txt') ),
   [
      {
         Time     => '4',
         Command  => 'Query',
         db       => 'foo',
         Id       => '1',
         Info     => 'select * from foo1;',
         User     => 'user1',
         State    => 'Locked',
         Host     => '1.2.3.4:3333'
      },
      {
         Time     => '5',
         Command  => 'Query',
         db       => 'foo',
         Id       => '2',
         Info     => 'select * from foo2;',
         User     => 'user1',
         State    => 'Locked',
         Host     => '1.2.3.4:5455'
      },
   ],
   '2 row vertical processlist'
);

my $recset = $r->parse ( load_file('t/lib/samples/pl/recset003.txt') );
cmp_ok(
   scalar @$recset,
   '==',
   113,
   '113 row vertical processlist'
);

$recset = $r->parse( load_file('t/lib/samples/pl/recset004.txt') );
cmp_ok(
   scalar @$recset,
   '==',
   51,
   '51 row vertical processlist'
);

is_deeply(
   $r->parse( load_file('t/lib/samples/pl/recset005.txt') ),
   [
      {
         Id    => '29392005',
         User  => 'remote',
         Host  => '1.2.3.148:49718',
         db    => 'happy',
         Command => 'Sleep',
         Time  => '17',
         State => undef,
         Info  => undef,
      }
   ],
   '1 vertical row, No State value'
);

is_deeply(
   $r->parse( load_file('t/lib/samples/pl/recset009.txt') ),
   [
      {
         Id      => '21',
         User    => 'msandbox',
         Host    => 'localhost:54732',
         db      => undef,
         Command => 'Binlog Dump',
         Time    => '3081',
         State   => 'Has sent all binlog to slave; waiting for binlog to be updated',
         Info    => undef,
      },
      {
         Id      => '41',
         User    => 'msandbox',
         Host    => 'localhost',
         db      => undef,
         Command => 'Query',
         Time    => '0',
         State   => undef,
         Info    => 'show full processlist',
      }
   ],
   'Horizontal, tab-separated'
);

$recset = $r->parse(load_file('t/lib/samples/show-variables/vars001.txt'));
# Should only get the var once.
my $got_var = grep { $_->{Variable_name} eq 'warning_count' } @$recset;
is(
   $got_var,
   1,
   "vars001.txt"
);

$recset = $r->parse(load_file('t/lib/samples/show-variables/vars002.txt'));
$got_var = grep { $_->{Variable_name} eq 'warning_count' } @$recset;
is(
   $got_var,
   1,
   "vars002.txt"
);


# #############################################################################
# Parse with NAME_lc for lowercase key/col names.
# #############################################################################
$r = new TextResultSetParser(NAME_lc => 1);

$recset = $r->parse(load_file('t/lib/samples/show-variables/vars001.txt'));
$got_var = grep { $_->{variable_name} eq 'warning_count' } @$recset;
is(
   $got_var,
   1,
   "NAME_lc tabular"
);

$recset = $r->parse(load_file('t/lib/samples/show-variables/vars002.txt'));
$got_var = grep { $_->{variable_name} eq 'warning_count' } @$recset;
is(
   $got_var,
   1,
   "NAME_lc tab-separated"
);

is_deeply(
   $r->parse( load_file('t/lib/samples/pl/recset002.txt') ),
   [
      {
         time     => '4',
         command  => 'Query',
         db       => 'foo',
         id       => '1',
         info     => 'select * from foo1;',
         user     => 'user1',
         state    => 'Locked',
         host     => '1.2.3.4:3333'
      },
      {
         time     => '5',
         command  => 'Query',
         db       => 'foo',
         id       => '2',
         info     => 'select * from foo2;',
         user     => 'user1',
         state    => 'Locked',
         host     => '1.2.3.4:5455'
      },
   ],
   "NAME_lc vertical"
);

# #############################################################################
# Done.
# #############################################################################
exit;