~percona-toolkit-dev/percona-toolkit/fix-log-parser-writer-bug-963225

« back to all changes in this revision

Viewing changes to t/lib/RowDiff-custom.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 -w
 
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
# RowDiff-custom.t tests some of the basic RowDiff functionalities
 
10
# as RowDiff.t but uses a different Perl lib if the MK_PERL_LIB
 
11
# environment var is set. This allows us to test these functionalities
 
12
# against custom versions of DBI, DBD::mysql, etc. If MK_PERL_LIB
 
13
# is not set, then all these tests are skipped.
 
14
 
 
15
package MockSync;
 
16
sub new {
 
17
   return bless [], shift;
 
18
}
 
19
 
 
20
sub same_row {
 
21
   my ( $self, $lr, $rr ) = @_;
 
22
   push @$self, 'same';
 
23
}
 
24
 
 
25
sub not_in_right {
 
26
   my ( $self, $lr ) = @_;
 
27
   push @$self, [ 'not in right', $lr];
 
28
}
 
29
 
 
30
sub not_in_left {
 
31
   my ( $self, $rr ) = @_;
 
32
   push @$self, [ 'not in left', $rr];
 
33
}
 
34
 
 
35
sub done_with_rows {
 
36
   my ( $self ) = @_;
 
37
   push @$self, 'done';
 
38
}
 
39
 
 
40
sub key_cols {
 
41
   return [qw(a)];
 
42
}
 
43
 
 
44
# #############################################################################
 
45
 
 
46
package main;
 
47
 
 
48
BEGIN {
 
49
   if ( defined $ENV{MK_PERL_LIB} ) {
 
50
      die "The MK_PERL_LIB environment variable is not a valid directory: "
 
51
         . $ENV{MK_PERL_LIB} unless -d $ENV{MK_PERL_LIB};
 
52
      print "# Using Perl lib $ENV{MK_PERL_LIB}\n";
 
53
      use lib ($ENV{MK_PERL_LIB} ? "$ENV{MK_PERL_LIB}" : ());
 
54
   }
 
55
};
 
56
 
 
57
use strict;
 
58
use warnings FATAL => 'all';
 
59
 
 
60
use Test::More;
 
61
use English qw(-no_match_vars);
 
62
use DBI;
 
63
use DBD::mysql;  # so we can print $DBD::mysql::VERSION
 
64
use MaatkitTest;
 
65
 
 
66
plan skip_all => "MK_PERL_LIB env var is not set", 4
 
67
   unless defined $ENV{MK_PERL_LIB};
 
68
 
 
69
print "# DBI v$DBI::VERSION\n"
 
70
   . "# DBD::mysql v$DBD::mysql::VERSION\n";
 
71
 
 
72
use RowDiff;
 
73
use Sandbox;
 
74
use DSNParser;
 
75
use TableParser;
 
76
use MySQLDump;
 
77
use Quoter;
 
78
 
 
79
my $d  = new RowDiff(dbh => 1);
 
80
my $s  = new MockSync();
 
81
my $q  = new Quoter();
 
82
my $du = new MySQLDump();
 
83
my $tp = new TableParser(Quoter => $q);
 
84
my $dp = new DSNParser(opts=>$dsn_opts);
 
85
 
 
86
# Connect to sandbox now to make sure it's running.
 
87
my $sb = new Sandbox(basedir => '/tmp', DSNParser => $dp);
 
88
my $master_dbh = $sb->get_dbh_for('master');
 
89
my $slave_dbh  = $sb->get_dbh_for('slave1');
 
90
if ( !$master_dbh ) {
 
91
   plan skip_all => "Cannot connect to sandbox master";
 
92
}
 
93
elsif ( !$slave_dbh ) {
 
94
   plan skip_all => "Cannot connect to sandbox slave";
 
95
}
 
96
else {
 
97
   plan tests => 4;
 
98
}
 
99
 
 
100
 
 
101
$sb->create_dbs($master_dbh, [qw(test)]);
 
102
$sb->load_file('master', 't/lib/samples/issue_11.sql');
 
103
 
 
104
my $tbl = $tp->parse(
 
105
   $du->get_create_table($master_dbh, $q, 'test', 'issue_11'));
 
106
 
 
107
my $left_sth  = $master_dbh->prepare('SELECT * FROM test.issue_11');
 
108
my $right_sth = $slave_dbh->prepare('SELECT * FROM test.issue_11');
 
109
$left_sth->execute();
 
110
$right_sth->execute();
 
111
$s = new MockSync();
 
112
$d->compare_sets(
 
113
   left  => $left_sth,
 
114
   right => $right_sth,
 
115
   syncer => $s,
 
116
   tbl => $tbl,
 
117
);
 
118
is_deeply(
 
119
   $s,
 
120
   ['done',],
 
121
   'no rows (real DBI sth)',
 
122
);
 
123
 
 
124
$slave_dbh->do('INSERT INTO test.issue_11 VALUES (1,2,3)');
 
125
$left_sth  = $master_dbh->prepare('SELECT * FROM test.issue_11');
 
126
$right_sth = $slave_dbh->prepare('SELECT * FROM test.issue_11');
 
127
$left_sth->execute();
 
128
$right_sth->execute();
 
129
$s = new MockSync();
 
130
$d->compare_sets(
 
131
   left   => $left_sth,
 
132
   right  => $right_sth,
 
133
   syncer => $s,
 
134
   tbl    => $tbl,
 
135
);
 
136
is_deeply(
 
137
   $s,
 
138
   [
 
139
      ['not in left', { a => 1, b => 2, c => 3 },],
 
140
      'done',
 
141
   ],
 
142
   'right only (real DBI sth)',
 
143
);
 
144
 
 
145
$slave_dbh->do('TRUNCATE TABLE test.issue_11');
 
146
$master_dbh->do('SET SQL_LOG_BIN=0;');
 
147
$master_dbh->do('INSERT INTO test.issue_11 VALUES (1,2,3)');
 
148
$left_sth  = $master_dbh->prepare('SELECT * FROM test.issue_11');
 
149
$right_sth = $slave_dbh->prepare('SELECT * FROM test.issue_11');
 
150
$left_sth->execute();
 
151
$right_sth->execute();
 
152
$s = new MockSync();
 
153
$d->compare_sets(
 
154
   left   => $left_sth,
 
155
   right  => $right_sth,
 
156
   syncer => $s,
 
157
   tbl    => $tbl,
 
158
);
 
159
is_deeply(
 
160
   $s,
 
161
   [
 
162
      [ 'not in right', { a => 1, b => 2, c => 3 },],
 
163
      'done',
 
164
   ],
 
165
   'left only (real DBI sth)',
 
166
);
 
167
 
 
168
$slave_dbh->do('INSERT INTO test.issue_11 VALUES (1,2,3)');
 
169
$left_sth  = $master_dbh->prepare('SELECT * FROM test.issue_11');
 
170
$right_sth = $slave_dbh->prepare('SELECT * FROM test.issue_11');
 
171
$left_sth->execute();
 
172
$right_sth->execute();
 
173
$s = new MockSync();
 
174
$d->compare_sets(
 
175
   left   => $left_sth,
 
176
   right  => $right_sth,
 
177
   syncer => $s,
 
178
   tbl    => $tbl,
 
179
);
 
180
is_deeply(
 
181
   $s,
 
182
   [
 
183
      'same',
 
184
      'done',
 
185
   ],
 
186
   'one identical row (real DBI sth)',
 
187
);
 
188
 
 
189
$sb->wipe_clean($master_dbh);
 
190
$sb->wipe_clean($slave_dbh);
 
191
 
 
192
exit;