~percona-toolkit-dev/percona-toolkit/pt-agent

« back to all changes in this revision

Viewing changes to t/lib/MockSyncStream.t

  • Committer: Daniel Nichter
  • Date: 2011-06-24 17:22:06 UTC
  • Revision ID: daniel@percona.com-20110624172206-c7q4s4ad6r260zz6
Add lib/, t/lib/, and sandbox/.  All modules are updated and passing on MySQL 5.1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl
 
2
 
 
3
BEGIN {
 
4
   die "The PERCONA_TOOLKIT_BRANCH environment variable is not set.\n"
 
5
      unless $ENV{PERCONA_TOOLKIT_BRANCH} && -d $ENV{PERCONA_TOOLKIT_BRANCH};
 
6
   unshift @INC, "$ENV{PERCONA_TOOLKIT_BRANCH}/lib";
 
7
};
 
8
 
 
9
use strict;
 
10
use warnings FATAL => 'all';
 
11
use English qw(-no_match_vars);
 
12
use Test::More tests => 5;
 
13
 
 
14
use MockSyncStream;
 
15
use Quoter;
 
16
use MockSth;
 
17
use RowDiff;
 
18
use MaatkitTest;
 
19
 
 
20
my $rd = new RowDiff( dbh => 1 );
 
21
my @rows;
 
22
 
 
23
sub same_row {
 
24
   push @rows, 'same';
 
25
}
 
26
sub not_in_left {
 
27
   push @rows, 'not in left';
 
28
}
 
29
sub not_in_right {
 
30
   push @rows, 'not in right';
 
31
}
 
32
 
 
33
my $mss = new MockSyncStream(
 
34
   query        => 'SELECT a, b, c FROM foo WHERE id = 1',
 
35
   cols         => [qw(a b c)],
 
36
   same_row     => \&same_row,
 
37
   not_in_left  => \&not_in_left,
 
38
   not_in_right => \&not_in_right,
 
39
);
 
40
 
 
41
is(
 
42
   $mss->get_sql(),
 
43
   'SELECT a, b, c FROM foo WHERE id = 1',
 
44
   'get_sql()',
 
45
);
 
46
 
 
47
is( $mss->done(), undef, 'Not done yet' );
 
48
 
 
49
@rows = ();
 
50
$rd->compare_sets(
 
51
   left_sth => new MockSth(
 
52
      { a => 1, b => 2, c => 3 },
 
53
      { a => 2, b => 2, c => 3 },
 
54
      { a => 3, b => 2, c => 3 },
 
55
      # { a => 4, b => 2, c => 3 },
 
56
   ),
 
57
   right_sth => new MockSth(
 
58
      # { a => 1, b => 2, c => 3 },
 
59
      { a => 2, b => 2, c => 3 },
 
60
      { a => 3, b => 2, c => 3 },
 
61
      { a => 4, b => 2, c => 3 },
 
62
   ),
 
63
   syncer     => $mss,
 
64
   tbl_struct => {},
 
65
);
 
66
is_deeply(
 
67
   \@rows,
 
68
   [
 
69
      'not in right',
 
70
      'same',
 
71
      'same',
 
72
      'not in left',
 
73
   ],
 
74
   'rows from handler',
 
75
);
 
76
 
 
77
# #############################################################################
 
78
# Test online stuff, e.g. get_cols_and_struct().
 
79
# #############################################################################
 
80
use DSNParser;
 
81
use Sandbox;
 
82
my $dp  = new DSNParser(opts=>$dsn_opts);
 
83
my $sb  = new Sandbox(basedir => '/tmp', DSNParser => $dp);
 
84
my $dbh = $sb->get_dbh_for('master');
 
85
 
 
86
SKIP: {
 
87
   skip 'Cannot connect to sandbox mater', 2 unless $dbh;
 
88
 
 
89
   diag(`/tmp/12345/use -e 'CREATE DATABASE test' 2>/dev/null`);
 
90
   diag(`/tmp/12345/use < $trunk/t/lib/samples/col_types.sql`);
 
91
 
 
92
   my $sth = $dbh->prepare('SELECT * FROM test.col_types_1');
 
93
   $sth->execute();
 
94
   is_deeply(
 
95
      MockSyncStream::get_result_set_struct($dbh, $sth),
 
96
      {
 
97
         cols => [
 
98
            'id',
 
99
            'i',
 
100
            'f',
 
101
            'd',
 
102
            'dt',
 
103
            'ts',
 
104
            'c',
 
105
            'c2',
 
106
            'v',
 
107
            't',
 
108
         ],
 
109
         type_for => {
 
110
            id => 'integer',
 
111
            i  => 'integer',
 
112
            f  => 'float',
 
113
            d  => 'decimal',
 
114
            dt => 'timestamp',
 
115
            ts => 'timestamp',
 
116
            c  => 'char',
 
117
            c2 => 'char',
 
118
            v  => 'varchar',
 
119
            t  => 'blob',
 
120
         },
 
121
         is_numeric => {
 
122
            id => 1,
 
123
            i  => 1,
 
124
            f  => 1,
 
125
            d  => 1,
 
126
            dt => 0,
 
127
            ts => 0,
 
128
            c  => 0,
 
129
            c2 => 0,
 
130
            v  => 0,
 
131
            t  => 0,
 
132
         },
 
133
         is_col => {
 
134
            id => 1,
 
135
            i  => 1,
 
136
            f  => 1,
 
137
            d  => 1,
 
138
            dt => 1,
 
139
            ts => 1,
 
140
            c  => 1,
 
141
            c2 => 1,
 
142
            v  => 1,
 
143
            t  => 1,
 
144
         },
 
145
         col_posn => {
 
146
            id => 0,
 
147
            i  => 1,
 
148
            f  => 2,
 
149
            d  => 3,
 
150
            dt => 4,
 
151
            ts => 5,
 
152
            c  => 6,
 
153
            c2 => 7,
 
154
            v  => 8,
 
155
            t  => 9,
 
156
         },
 
157
         is_nullable => {
 
158
            id => 1,
 
159
            i  => 1,
 
160
            f  => 1,
 
161
            d  => 1,
 
162
            dt => 0,
 
163
            ts => 0,
 
164
            c  => 1,
 
165
            c2 => 1,  # it's really not but this is a sth limitation
 
166
            v  => 1,
 
167
            t  => 1,
 
168
         },
 
169
         size => {
 
170
            id => undef,
 
171
            i  => undef,
 
172
            f  => '(31,12)',
 
173
            d  => '(7,2)',
 
174
            dt => undef,
 
175
            ts => undef,
 
176
            c  => '(1)',
 
177
            c2 => '(15)',
 
178
            v  => '(32)',
 
179
            t  => undef,
 
180
         },
 
181
      },
 
182
      'Gets result set struct from sth attribs'
 
183
   );
 
184
 
 
185
   $sth = $dbh->prepare('SELECT v, c, t, id, i, f, d FROM test.col_types_1');
 
186
   $sth->execute();
 
187
   my $row = $sth->fetchrow_hashref();
 
188
   is_deeply(
 
189
      MockSyncStream::as_arrayref($sth, $row),
 
190
      ['hello world','c','this is text',1,1,3.14,5.08,],
 
191
      'as_arrayref()'
 
192
   );
 
193
 
 
194
   $sth->finish();
 
195
   $sb->wipe_clean($dbh);
 
196
   $dbh->disconnect();
 
197
};
 
198
 
 
199
# #############################################################################
 
200
# Done.
 
201
# #############################################################################
 
202
exit;